File: test_contentitem_response.py

package info (click to toggle)
python-lti 0.9.5-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 404 kB
  • sloc: python: 2,400; sh: 6; makefile: 3
file content (130 lines) | stat: -rw-r--r-- 5,234 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
from lti import ContentItemResponse, LaunchParams
from lti.utils import parse_qs, InvalidLTIConfigError
import unittest

from oauthlib.common import generate_client_id, generate_token, unquote
from requests import PreparedRequest

class TestContentItemResponse(unittest.TestCase):

    def setUp(self):
        pass

    def test_constructor(self):
        client_id = generate_client_id()
        client_secret = generate_token()
        tc = ContentItemResponse(client_id, client_secret,
                          launch_url='http://example.edu')
        self.assertIsInstance(tc.launch_params, LaunchParams)

        lp = LaunchParams()
        tc = ContentItemResponse(client_id, client_secret,
                          launch_url='http://example.edu', params=lp)
        self.assertEqual(tc.launch_params, lp)

        lp_dict = {'resource_link_id': 1}
        tc = ContentItemResponse(client_id, client_secret,
                          launch_url='http://example.edu',
                          params=lp_dict)
        self.assertIsInstance(tc.launch_params, LaunchParams)
        self.assertEqual(tc.launch_params._params.get('resource_link_id'), 1)

        # no launch_url should raise exception
        self.assertRaises(InvalidLTIConfigError, ContentItemResponse,
                              client_id, client_secret,
                              params=lp_dict)

        # but confirm that 'launch_url' can still be passed in params
        # (backwards compatibility)
        lp_dict['launch_url'] = 'http://example.edu'
        tc = ContentItemResponse(client_id, client_secret, params=lp_dict)
        self.assertEqual(tc.launch_url, 'http://example.edu')

    def test_has_required_params(self):

        client_id = generate_client_id()
        client_secret = generate_token()
        tc = ContentItemResponse(client_id, client_secret,
                          launch_url='http://example.edu')

        #Can't assert false for has_required_params as the only required params are lti_version and lti_message_type
        #However should consider checking the message type in the future

        tc.launch_params['lti_version'] = 'LTI-1p0'
        tc.launch_params['lti_message_type'] = 'ContentItemSelection'
        self.assertTrue(tc.has_required_params())

    def test_generate_launch_request(self):
        launch_params = {
            'lti_version': 'foo',
            'lti_message_type': 'bar',
            'resource_link_id': 'baz'
        }
        tc = ContentItemResponse('client_key', 'client_secret',
                          launch_url='http://example.edu/',
                          params=launch_params)
        launch_req = tc.generate_launch_request(nonce='abcd1234',
                                                timestamp='1234567890')

        self.assertIsInstance(launch_req, PreparedRequest)

        got = parse_qs(unquote(launch_req.body.decode('utf-8')))
        correct = launch_params.copy()
        correct.update({
            'oauth_nonce': 'abcd1234',
            'oauth_timestamp': '1234567890',
            'oauth_version': '1.0',
            'oauth_signature_method': 'HMAC-SHA1',
            'oauth_consumer_key': 'client_key',
            'oauth_signature': 'u2xlj 1gF4y 6gKHNeiL9cN3tOI=',
        })

        self.assertEqual(got, correct)

    def test_launch_request_with_qs(self):
        """
        test that qs params in launch url are ok
        """
        launch_params = {
            'lti_version': 'abc',
            'lti_message_type': 'def',
            'resource_link_id': '123'
        }
        tc = ContentItemResponse('client_key', 'client_secret',
                          launch_url='http://example.edu/foo?bar=1',
                          params=launch_params)
        launch_req = tc.generate_launch_request(nonce='wxyz7890',
                                                timestamp='2345678901')
        got = parse_qs(unquote(launch_req.body.decode('utf-8')))
        correct = launch_params.copy()
        correct.update({
            'oauth_nonce': 'wxyz7890',
            'oauth_timestamp': '2345678901',
            'oauth_version': '1.0',
            'oauth_signature_method': 'HMAC-SHA1',
            'oauth_consumer_key': 'client_key',
            'oauth_signature': 'UH2l86Wq/g5Mu64GpCRcec6tEYY=',
            })
        self.assertEqual(got, correct)

    def test_generate_launch_data(self):
        launch_params = {
            'lti_version': 'abc',
            'lti_message_type': 'def',
            'resource_link_id': '123'
        }
        tc = ContentItemResponse('client_key', 'client_secret',
                          launch_url='http://example.edu/',
                          params=launch_params)
        got = tc.generate_launch_data(nonce='wxyz7890',
                                      timestamp='2345678901')
        correct = launch_params.copy()
        correct.update({
            'oauth_nonce': 'wxyz7890',
            'oauth_timestamp': '2345678901',
            'oauth_version': '1.0',
            'oauth_signature_method': 'HMAC-SHA1',
            'oauth_consumer_key': 'client_key',
            'oauth_signature': 'gXIAk60dLsrh6YQGT5ZGK6tHDGY=',
            })
        self.assertEqual(got, correct)