File: test_tool_config.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 (197 lines) | stat: -rw-r--r-- 10,347 bytes parent folder | download | duplicates (3)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
from lti import ToolConfig, InvalidLTIConfigError
from lxml import etree
import unittest

CC_LTI_XML = b'''<?xml version="1.0" encoding="UTF-8"?>
<cartridge_basiclti_link xmlns:blti="http://www.imsglobal.org/xsd/imsbasiclti_v1p0" xmlns:lticm="http://www.imsglobal.org/xsd/imslticm_v1p0" xmlns:lticp="http://www.imsglobal.org/xsd/imslticp_v1p0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.imsglobal.org/xsd/imslticc_v1p0" xsi:schemaLocation="http://www.imsglobal.org/xsd/imslticc_v1p0 http://www.imsglobal.org/xsd/lti/ltiv1p0/imslticc_v1p0.xsd http://www.imsglobal.org/xsd/imsbasiclti_v1p0 http://www.imsglobal.org/xsd/lti/ltiv1p0/imsbasiclti_v1p0p1.xsd http://www.imsglobal.org/xsd/imslticm_v1p0 http://www.imsglobal.org/xsd/lti/ltiv1p0/imslticm_v1p0.xsd http://www.imsglobal.org/xsd/imslticp_v1p0 http://www.imsglobal.org/xsd/lti/ltiv1p0/imslticp_v1p0.xsd">
    <blti:title>Test Config</blti:title>
    <blti:description>Description of boringness</blti:description>
    <blti:launch_url>http://www.example.com/lti</blti:launch_url>
    <blti:secure_launch_url>https://www.example.com/lti</blti:secure_launch_url>
    <blti:vendor>
        <lticp:name>test.tool</lticp:name>
        <lticp:code>test</lticp:code>
        <lticp:description>We test things</lticp:description>
        <lticp:url>http://www.example.com/about</lticp:url>
        <lticp:contact>
            <lticp:name>Joe Support</lticp:name>
            <lticp:email>support@example.com</lticp:email>
        </lticp:contact>
    </blti:vendor>
    <blti:custom>
        <lticm:property name="custom1">customval1</lticm:property>
        <lticm:property name="custom2">customval2</lticm:property>
    </blti:custom>
    <blti:extensions platform="example.com">
        <lticm:property name="extkey1">extval1</lticm:property>
        <lticm:property name="extkey2">extval2</lticm:property>
        <lticm:options name="extopt1">
            <lticm:property name="optkey1">optval1</lticm:property>
            <lticm:property name="optkey2">optval2</lticm:property>
        </lticm:options>
    </blti:extensions>
    <blti:extensions platform="two.example.com">
        <lticm:property name="ext1key">ext1val</lticm:property>
    </blti:extensions>
    <cartridge_bundle identifierref="BLTI001_Bundle"/>
</cartridge_basiclti_link>
'''

CC_LTI_WITH_SUBOPTIONS_XML = b'''<?xml version="1.0" encoding="UTF-8"?>
<cartridge_basiclti_link xmlns:blti="http://www.imsglobal.org/xsd/imsbasiclti_v1p0" xmlns:lticm="http://www.imsglobal.org/xsd/imslticm_v1p0" xmlns:lticp="http://www.imsglobal.org/xsd/imslticp_v1p0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.imsglobal.org/xsd/imslticc_v1p0" xsi:schemaLocation="http://www.imsglobal.org/xsd/imslticc_v1p0 http://www.imsglobal.org/xsd/lti/ltiv1p0/imslticc_v1p0.xsd http://www.imsglobal.org/xsd/imsbasiclti_v1p0 http://www.imsglobal.org/xsd/lti/ltiv1p0/imsbasiclti_v1p0p1.xsd http://www.imsglobal.org/xsd/imslticm_v1p0 http://www.imsglobal.org/xsd/lti/ltiv1p0/imslticm_v1p0.xsd http://www.imsglobal.org/xsd/imslticp_v1p0 http://www.imsglobal.org/xsd/lti/ltiv1p0/imslticp_v1p0.xsd">
    <blti:title>Test Config</blti:title>
    <blti:description>Description of boringness</blti:description>
    <blti:launch_url>http://www.example.com/lti</blti:launch_url>
    <blti:secure_launch_url>https://www.example.com/lti</blti:secure_launch_url>
    <blti:vendor>
        <lticp:name>test.tool</lticp:name>
        <lticp:code>test</lticp:code>
        <lticp:description>We test things</lticp:description>
        <lticp:url>http://www.example.com/about</lticp:url>
        <lticp:contact>
            <lticp:name>Joe Support</lticp:name>
            <lticp:email>support@example.com</lticp:email>
        </lticp:contact>
    </blti:vendor>
    <blti:custom>
        <lticm:property name="custom1">customval1</lticm:property>
        <lticm:property name="custom2">customval2</lticm:property>
    </blti:custom>
    <blti:extensions platform="example.com">
        <lticm:property name="extkey1">extval1</lticm:property>
        <lticm:property name="extkey2">extval2</lticm:property>
        <lticm:options name="extopt1">
            <lticm:options name="labels">
                <lticm:property name="en">Image Library</lticm:property>
                <lticm:property name="es">Biblioteca de Imagenes</lticm:property>
            </lticm:options>
        </lticm:options>
    </blti:extensions>
    <blti:extensions platform="two.example.com">
        <lticm:property name="ext1key">ext1val</lticm:property>
    </blti:extensions>
    <cartridge_bundle identifierref="BLTI001_Bundle"/>
</cartridge_basiclti_link>
'''

CC_LTI_OPTIONAL_PARAMS_XML = b'''<?xml version="1.0" encoding="UTF-8"?>
<cartridge_basiclti_link xmlns:blti="http://www.imsglobal.org/xsd/imsbasiclti_v1p0" xmlns:lticm="http://www.imsglobal.org/xsd/imslticm_v1p0" xmlns:lticp="http://www.imsglobal.org/xsd/imslticp_v1p0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.imsglobal.org/xsd/imslticc_v1p0" xsi:schemaLocation="http://www.imsglobal.org/xsd/imslticc_v1p0 http://www.imsglobal.org/xsd/lti/ltiv1p0/imslticc_v1p0.xsd http://www.imsglobal.org/xsd/imsbasiclti_v1p0 http://www.imsglobal.org/xsd/lti/ltiv1p0/imsbasiclti_v1p0p1.xsd http://www.imsglobal.org/xsd/imslticm_v1p0 http://www.imsglobal.org/xsd/lti/ltiv1p0/imslticm_v1p0.xsd http://www.imsglobal.org/xsd/imslticp_v1p0 http://www.imsglobal.org/xsd/lti/ltiv1p0/imslticp_v1p0.xsd">
  <blti:title>Test config</blti:title>
  <blti:description/>
  <blti:launch_url>http://www.example.com</blti:launch_url>
  <blti:secure_launch_url>http://www.example.com</blti:secure_launch_url>
  <blti:icon>http://wil.to/_/beardslap.gif</blti:icon>
  <blti:secure_icon>https://www.example.com/secure_icon.png</blti:secure_icon>
  <blti:vendor/>
  <cartridge_icon identifierref="BLTI001_Icon"/>
</cartridge_basiclti_link>
'''

def normalize_xml(xml_str):
    parser = etree.XMLParser(remove_blank_text=True)
    root = etree.XML(xml_str, parser)
    return etree.tostring(root, with_tail=False, method="c14n")

class TestToolConfig(unittest.TestCase):

    def test_generate_xml(self):
        '''
        Should generate the expected config xml.
        '''
        config = ToolConfig(title = "Test Config",
                secure_launch_url = "https://www.example.com/lti",
                custom_params = {"custom1": "customval1"})
        config.description ='Description of boringness'
        config.launch_url = 'http://www.example.com/lti'
        config.vendor_code = 'test'
        config.vendor_name = 'test.tool'
        config.vendor_description = 'We test things'
        config.vendor_url = 'http://www.example.com/about'
        config.vendor_contact_email = 'support@example.com'
        config.vendor_contact_name = 'Joe Support'

        config.set_custom_param('custom2', 'customval2')

        config.set_ext_params('example.com', { 'extkey1': 'extval1' })
        config.set_ext_param('example.com', 'extkey2', 'extval2')
        config.set_ext_param('example.com', 'extopt1',
                { 'optkey1': 'optval1', 'optkey2': 'optval2' })
        config.set_ext_param('two.example.com', 'ext1key', 'ext1val')

        config.cartridge_bundle = 'BLTI001_Bundle'

        correct = normalize_xml(CC_LTI_XML)
        got = normalize_xml(config.to_xml())
        self.assertEqual(got, correct)

    def test_allow_suboptions(self):

        config = ToolConfig(title = "Test Config",
                secure_launch_url = "https://www.example.com/lti",
                custom_params = {"custom1": "customval1"})
        config.description ='Description of boringness'
        config.launch_url = 'http://www.example.com/lti'
        config.vendor_code = 'test'
        config.vendor_name = 'test.tool'
        config.vendor_description = 'We test things'
        config.vendor_url = 'http://www.example.com/about'
        config.vendor_contact_email = 'support@example.com'
        config.vendor_contact_name = 'Joe Support'

        config.set_custom_param('custom2', 'customval2')

        config.set_ext_params('example.com', { 'extkey1': 'extval1' })
        config.set_ext_param('example.com', 'extkey2', 'extval2')
        config.set_ext_param('example.com', 'extopt1',
                { 'optkey1': 'optval1', 'optkey2': 'optval2' })
        config.set_ext_param('example.com', 'extopt1',
                { 'labels':{
                    'en':'Image Library',
                    'es':'Biblioteca de Imagenes'
                    }
                })
        config.set_ext_param('two.example.com', 'ext1key', 'ext1val')

        config.cartridge_bundle = 'BLTI001_Bundle'

        correct = normalize_xml(CC_LTI_WITH_SUBOPTIONS_XML)
        got = normalize_xml(config.to_xml())
        self.assertEqual(got, correct)

    def test_optional_config_parameters(self):
        '''
        Should contain cartridge_icon, and blti:icon.
        '''
        config = ToolConfig(title = "Test config",
                launch_url = "http://www.example.com",
                secure_launch_url = "http://www.example.com")
        config.icon = 'http://wil.to/_/beardslap.gif'
        config.secure_icon = 'https://www.example.com/secure_icon.png'
        config.cartridge_icon = 'BLTI001_Icon'

        correct = normalize_xml(CC_LTI_OPTIONAL_PARAMS_XML)
        got = normalize_xml(config.to_xml())
        self.assertEqual(got, correct)

    def test_can_parse_optional_config_parameters(self):
        '''
        Config should have cartridge_icon and blti:icon set
        '''
        config = ToolConfig.create_from_xml(CC_LTI_OPTIONAL_PARAMS_XML)
        self.assertEqual(config.cartridge_icon, 'BLTI001_Icon')
        self.assertEqual(config.icon, 'http://wil.to/_/beardslap.gif')
        self.assertEqual(config.secure_icon, 'https://www.example.com/secure_icon.png')

    def test_read_xml_config(self):
        '''
        Should read an XML config.
        '''
        config = ToolConfig.create_from_xml(CC_LTI_XML)
        self.assertEqual(normalize_xml(config.to_xml()), normalize_xml(CC_LTI_XML))

    def test_invalid_config_xml(self):
        '''
        Should not allow creating invalid config xml.
        '''
        config = ToolConfig(title = 'Test Config')
        self.assertRaises(InvalidLTIConfigError, config.to_xml)