def test_validation_response_content_type(self): """ A ``ValidationResponse`` should be set to the provided content type. """ resp = ValidationResponse(context={'ticket': self.st, 'error': None}, content_type='text/xml') self.assertEqual(resp.get('Content-Type'), 'text/xml')
def test_validation_response_attributes(self): """ When given custom user attributes, a ``ValidationResponse`` should include the attributes in the response. """ attrs = { 'givenName': 'Ellen', 'sn': 'Cohen', 'email': '*****@*****.**' } resp = ValidationResponse(context={ 'ticket': self.st, 'error': None, 'attributes': attrs }, content_type='text/xml') attributes = parse( resp.content).find('./authenticationSuccess/attributes') self.assertIsNotNone(attributes) self.assertEqual(len(attributes), len(attrs)) for child in attributes: self.assertTrue(child.tag in attrs) self.assertEqual(child.text, attrs[child.tag]) # Ordering is not guaranteed, so remove attributes from # the dict as they are validated. When done, check if the # dict is empty to see if all attributes were matched. del attrs[child.tag] self.assertEqual(len(attrs), 0)
def test_validation_response_list_type_attributes(self): """ When given a custom user attribute of type list, a ``ValidationResponse`` should include all the list items as values in the resposne. """ attrs = { 'givenName': 'Ellen', 'sn': 'Cohen', 'email': '*****@*****.**', 'groups': ['group1', 'group2', 'group3'] } resp = ValidationResponse(context={ 'ticket': self.st, 'error': None, 'attributes': attrs }, content_type='text/xml') attributes = parse( resp.content).find('./authenticationSuccess/attributes') self.assertIsNotNone(attributes) total_attributes = 0 for attr_key in attrs.keys(): attr_values = attributes.findall(attr_key) if (len(attr_values) > 1): self.assertEqual(len(attr_values), len(attrs[attr_key])) for attr_value in attr_values: self.assertTrue(attr_value.text in attrs[attr_key]) total_attributes += len(attrs[attr_key]) else: attr_value = attr_values[0] self.assertTrue(attr_value.tag in attrs) self.assertEqual(attr_value.text, attrs[attr_value.tag]) total_attributes += 1 self.assertEqual(len(attributes), total_attributes)
def test_validation_response_ticket(self): """ When given a ticket, a ``ValidationResponse`` should return an authentication success with the authenticated user. """ resp = ValidationResponse(context={'ticket': self.st, 'error': None}, content_type='text/xml') user = parse(resp.content).find('./authenticationSuccess/user') self.assertIsNotNone(user) self.assertEqual(user.text, 'ellen')
def test_validation_response_pgt(self): """ When given a ``ProxyGrantingTicket``, a ``ValidationResponse`` should include a proxy-granting ticket. """ resp = ValidationResponse(context={'ticket': self.st, 'error': None, 'pgt': self.pgt}, content_type='text/xml') pgt = parse(resp.content).find('./authenticationSuccess/proxyGrantingTicket') self.assertIsNotNone(pgt) self.assertEqual(pgt.text, self.pgt.iou)
def test_validation_response_error(self): """ When given an error, a ``ValidationResponse`` should return an authentication failure with the error code and text. """ error = InvalidTicket('Testing Error') resp = ValidationResponse(context={'ticket': None, 'error': error}, content_type='text/xml') failure = parse(resp.content).find('./authenticationFailure') self.assertIsNotNone(failure) self.assertEqual(failure.get('code'), 'INVALID_TICKET') self.assertEqual(failure.text, 'Testing Error')
def test_validation_response_unicode_attributes(self): """ When given Unicode attributes, the values should be handled correctly in the response. """ attrs = {'unicode': u'тнє мαмαѕ & тнє ραραѕ'} resp = ValidationResponse(context={'ticket': self.st, 'error': None, 'attributes': attrs}, content_type='text/xml') attributes = parse(resp.content).find('./authenticationSuccess/attributes') self.assertIsNotNone(attributes) self.assertEqual(attributes[0].tag, 'unicode') self.assertEqual(attributes[0].text, 'тнє мαмαѕ & тнє ραραѕ')
def test_validation_response_nonstring_attributes(self): """ When given non-string attributes, the values should be converted to strings in the response. """ attrs = {'boolean': True} resp = ValidationResponse(context={'ticket': self.st, 'error': None, 'attributes': attrs}, content_type='text/xml') attributes = parse(resp.content).find('./authenticationSuccess/attributes') self.assertIsNotNone(attributes) self.assertEqual(attributes[0].tag, 'boolean') self.assertEqual(attributes[0].text, 'True')
def test_validation_response_proxies(self): """ When given a list of proxies, a ``ValidationResponse`` should include the list with ordering retained. """ proxy_list = ['https://proxy2/pgtUrl', 'https://proxy1/pgtUrl'] resp = ValidationResponse(context={'ticket': self.st, 'error': None, 'proxies': proxy_list}, content_type='text/xml') proxies = parse(resp.content).find('./authenticationSuccess/proxies') self.assertIsNotNone(proxies) self.assertEqual(len(proxies.findall('proxy')), len(proxy_list)) self.assertEqual(proxies[0].text, proxy_list[0]) self.assertEqual(proxies[1].text, proxy_list[1])