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 generate_launch_data(self, **kwargs): """ Provided for backwards compatibility """ r = self.generate_launch_request(**kwargs) return parse_qs(unquote(r.body.decode('utf-8')))
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 generate_launch_data(self, **kwargs): """ Provided for backwards compatibility """ r = self.generate_launch_request(**kwargs) return parse_qs(unquote(r.body))
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)) 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_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)) 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_post_outcome_request(self): request_headers = {"User-Agent": "unit-test"} request = OutcomeRequest(headers=request_headers) self.assertRaises(InvalidLTIConfigError, request.post_outcome_request) request.consumer_key = 'consumer' request.consumer_secret = 'secret' request.lis_outcome_service_url = 'http://example.edu/' request.lis_result_sourcedid = 'foo' request.operation = REPLACE_REQUEST with HTTMock(response_content): resp = request.post_outcome_request(nonce='my_nonce', timestamp='1234567890') self.assertIsInstance(resp, OutcomeResponse) request = resp.post_response.request self.assertTrue('authorization' in request.headers) self.assertEqual(request.headers.get('user-agent'), b"unit-test") self.assertEqual(request.headers.get('content-type'), b"application/xml") auth_header = unquote(request.headers['authorization'].decode('utf-8')) correct = ('OAuth ' 'oauth_nonce="my_nonce", oauth_timestamp="1234567890", ' 'oauth_version="1.0", oauth_signature_method="HMAC-SHA1", ' 'oauth_consumer_key="consumer", ' 'oauth_body_hash="glWvnsZZ8lMif1ATz8Tx64CTTaY=", ' 'oauth_signature="XR6A1CmUauXZdJZXa1pJpTQi6OQ="') self.assertEqual(auth_header, correct)
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)) 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)) 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_post_outcome_request(self): request = OutcomeRequest() self.assertRaises(InvalidLTIConfigError, request.post_outcome_request) request.consumer_key = 'consumer' request.consumer_secret = 'secret' request.lis_outcome_service_url = 'http://example.edu/' request.lis_result_sourcedid = 'foo' request.operation = REPLACE_REQUEST with HTTMock(response_content): resp = request.post_outcome_request( nonce='my_nonce', timestamp='1234567890' ) self.assertIsInstance(resp, OutcomeResponse) request = resp.post_response.request self.assertTrue('authorization' in request.headers) auth_header = unquote(request.headers['authorization']) correct = ('OAuth ' 'oauth_nonce="my_nonce", oauth_timestamp="1234567890", ' 'oauth_version="1.0", oauth_signature_method="HMAC-SHA1", ' 'oauth_consumer_key="consumer", ' 'oauth_signature="rbvjzAHwXPs/e41Runtu6w9Gv+w="') self.assertEqual(auth_header, correct)
def test_post_outcome_request(self): request = OutcomeRequest() self.assertRaises(InvalidLTIConfigError, request.post_outcome_request) request.consumer_key = 'consumer' request.consumer_secret = 'secret' request.lis_outcome_service_url = 'http://example.edu/' request.lis_result_sourcedid = 'foo' request.operation = REPLACE_REQUEST with HTTMock(response_content): resp = request.post_outcome_request( nonce='my_nonce', timestamp='1234567890' ) self.assertIsInstance(resp, OutcomeResponse) request = resp.post_response.request self.assertTrue('authorization' in request.headers) auth_header = unquote(request.headers['authorization'].decode('utf-8')) correct = ('OAuth ' 'oauth_nonce="my_nonce", oauth_timestamp="1234567890", ' 'oauth_version="1.0", oauth_signature_method="HMAC-SHA1", ' 'oauth_consumer_key="consumer", ' 'oauth_body_hash="glWvnsZZ8lMif1ATz8Tx64CTTaY=", ' 'oauth_signature="XR6A1CmUauXZdJZXa1pJpTQi6OQ="') self.assertEqual(auth_header, correct)
def unescape(u): if not isinstance(u, unicode_type): raise ValueError('Only unicode objects are unescapable.') return unquote(u)