File: test_tool_consumer.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 (142 lines) | stat: -rw-r--r-- 5,542 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
131
132
133
134
135
136
137
138
139
140
141
142
from lti import ToolConsumer, 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 TestToolConsumer(unittest.TestCase):

    def setUp(self):
        pass

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

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

        lp_dict = {'resource_link_id': 1}
        tc = ToolConsumer(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, ToolConsumer,
                              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 = ToolConsumer(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 = ToolConsumer(client_id, client_secret,
                          launch_url='http://example.edu')

        self.assertFalse(tc.has_required_params())

        tc.launch_params['resource_link_id'] = generate_token()
        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 = ToolConsumer('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 = ToolConsumer('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 = ToolConsumer('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)

    def test_generate_launch_data_with_empty_value(self):
        launch_params = {
            'lti_version': 'abc',
            'lti_message_type': 'def',
            'resource_link_id': '123',
            'custom_test_value': '',
        }
        tc = ToolConsumer('client_key', 'client_secret',
                          launch_url='http://example.edu/',
                          params=launch_params)
        got = tc.generate_launch_data(nonce='wxyz7890',
                                      timestamp='2345678901')
        self.assertEqual(got['custom_test_value'], '')