def test_write(self): """ Test that an Authentication struct can be written to a data stream. """ # Test with a single UsernamePasswordCredential. authentication = contents.Authentication(credentials=[ objects.Credential( credential_type=enums.CredentialType.USERNAME_AND_PASSWORD, credential_value=objects.UsernamePasswordCredential( username="******", password="******")) ]) stream = utils.BytearrayStream() authentication.write(stream) self.assertEqual(len(self.username_password_encoding), len(stream)) self.assertEqual(str(self.username_password_encoding), str(stream)) # Test with a single DeviceCredential. authentication = contents.Authentication(credentials=[ objects.Credential(credential_type=enums.CredentialType.DEVICE, credential_value=objects.DeviceCredential( device_serial_number="serNum123456", password="******", device_identifier="devID2233", network_identifier="netID9000", machine_identifier="machineID1", media_identifier="mediaID313")) ]) stream = utils.BytearrayStream() authentication.write(stream) self.assertEqual(len(self.device_encoding), len(stream)) self.assertEqual(str(self.device_encoding), str(stream)) # Test with multiple Credentials. authentication = contents.Authentication(credentials=[ objects.Credential( credential_type=enums.CredentialType.USERNAME_AND_PASSWORD, credential_value=objects.UsernamePasswordCredential( username="******", password="******")), objects.Credential(credential_type=enums.CredentialType.DEVICE, credential_value=objects.DeviceCredential( device_serial_number="serNum123456", password="******", device_identifier="devID2233", network_identifier="netID9000", machine_identifier="machineID1", media_identifier="mediaID313")) ]) stream = utils.BytearrayStream() authentication.write(stream) self.assertEqual(len(self.multiple_credentials_encoding), len(stream)) self.assertEqual(str(self.multiple_credentials_encoding), str(stream))
def test_not_equal_on_not_equal_credentials(self): """ Test that the inequality operator returns True when comparing two Authentication structs with different credentials. """ a = contents.Authentication( credentials=[ objects.Credential( credential_type=enums.CredentialType.USERNAME_AND_PASSWORD, credential_value=objects.UsernamePasswordCredential( username="******", password="******" ) ) ] ) b = contents.Authentication( credentials=[ objects.Credential( credential_type=enums.CredentialType.DEVICE, credential_value=objects.DeviceCredential( device_serial_number="serNum123456", password="******", device_identifier="devID2233", network_identifier="netID9000", machine_identifier="machineID1", media_identifier="mediaID313" ) ) ] ) self.assertTrue(a != b) self.assertTrue(b != a)
def create_device_credential(value): dsn = value.get('Device Serial Number') password = value.get('Password') dev_id = value.get('Device Identifier') net_id = value.get('Network Identifier') mach_id = value.get('Machine Identifier') med_id = value.get('Media Identifier') return objects.DeviceCredential( device_serial_number=dsn, password=password, device_identifier=dev_id, network_identifier=net_id, machine_identifier=mach_id, media_identifier=med_id )
def test_str(self): """ Test that str can be applied to an Authentication struct. """ # Test with a UsernamePasswordCredential. authentication = contents.Authentication(credentials=[ objects.Credential( credential_type=enums.CredentialType.USERNAME_AND_PASSWORD, credential_value=objects.UsernamePasswordCredential( username="******", password="******")) ]) expected = str({ "credentials": [{ "credential_type": enums.CredentialType.USERNAME_AND_PASSWORD, "credential_value": str({ "username": "******", "password": "******" }) }] }) observed = str(authentication) self.assertEqual(expected, observed) # Test with a DeviceCredential. authentication = contents.Authentication(credentials=[ objects.Credential(credential_type=enums.CredentialType.DEVICE, credential_value=objects.DeviceCredential( device_serial_number="serNum123456", password="******", device_identifier="devID2233", network_identifier="netID9000", machine_identifier="machineID1", media_identifier="mediaID313")) ]) expected = str({ "credentials": [{ "credential_type": enums.CredentialType.DEVICE, "credential_value": str({ "device_serial_number": "serNum123456", "password": "******", "device_identifier": "devID2233", "network_identifier": "netID9000", "machine_identifier": "machineID1", "media_identifier": "mediaID313" }) }] }) observed = str(authentication) self.assertEqual(expected, observed) # Test with multiple Credentials. authentication = contents.Authentication(credentials=[ objects.Credential( credential_type=enums.CredentialType.USERNAME_AND_PASSWORD, credential_value=objects.UsernamePasswordCredential( username="******", password="******")), objects.Credential(credential_type=enums.CredentialType.DEVICE, credential_value=objects.DeviceCredential( device_serial_number="serNum123456", password="******", device_identifier="devID2233", network_identifier="netID9000", machine_identifier="machineID1", media_identifier="mediaID313")) ]) expected = str({ "credentials": [{ "credential_type": enums.CredentialType.USERNAME_AND_PASSWORD, "credential_value": str({ "username": "******", "password": "******" }) }, { "credential_type": enums.CredentialType.DEVICE, "credential_value": str({ "device_serial_number": "serNum123456", "password": "******", "device_identifier": "devID2233", "network_identifier": "netID9000", "machine_identifier": "machineID1", "media_identifier": "mediaID313" }) }] }) observed = str(authentication) self.assertEqual(expected, observed)
def test_repr(self): """ Test that repr can be applied to an Authentication struct. """ # Test with a UsernamePasswordCredential. authentication = contents.Authentication(credentials=[ objects.Credential( credential_type=enums.CredentialType.USERNAME_AND_PASSWORD, credential_value=objects.UsernamePasswordCredential( username="******", password="******")) ]) expected = ("Authentication(" "credentials=[" "Credential(" "credential_type=CredentialType.USERNAME_AND_PASSWORD, " "credential_value=UsernamePasswordCredential(" "username='******', " "password='******'))])") observed = repr(authentication) self.assertEqual(expected, observed) # Test with a DeviceCredential. authentication = contents.Authentication(credentials=[ objects.Credential(credential_type=enums.CredentialType.DEVICE, credential_value=objects.DeviceCredential( device_serial_number="serNum123456", password="******", device_identifier="devID2233", network_identifier="netID9000", machine_identifier="machineID1", media_identifier="mediaID313")) ]) expected = ("Authentication(" "credentials=[" "Credential(" "credential_type=CredentialType.DEVICE, " "credential_value=DeviceCredential(" "device_serial_number='serNum123456', " "password='******', " "device_identifier='devID2233', " "network_identifier='netID9000', " "machine_identifier='machineID1', " "media_identifier='mediaID313'))])") observed = repr(authentication) self.assertEqual(expected, observed) # Test with multiple Credentials. authentication = contents.Authentication(credentials=[ objects.Credential( credential_type=enums.CredentialType.USERNAME_AND_PASSWORD, credential_value=objects.UsernamePasswordCredential( username="******", password="******")), objects.Credential(credential_type=enums.CredentialType.DEVICE, credential_value=objects.DeviceCredential( device_serial_number="serNum123456", password="******", device_identifier="devID2233", network_identifier="netID9000", machine_identifier="machineID1", media_identifier="mediaID313")) ]) expected = ("Authentication(" "credentials=[" "Credential(" "credential_type=CredentialType.USERNAME_AND_PASSWORD, " "credential_value=UsernamePasswordCredential(" "username='******', " "password='******')), " "Credential(" "credential_type=CredentialType.DEVICE, " "credential_value=DeviceCredential(" "device_serial_number='serNum123456', " "password='******', " "device_identifier='devID2233', " "network_identifier='netID9000', " "machine_identifier='machineID1', " "media_identifier='mediaID313'))])") observed = repr(authentication) self.assertEqual(expected, observed)
def test_read(self): """ Test that an Authentication struct can be read from a data stream. """ # Test with a single UsernamePasswordCredential. authentication = contents.Authentication() self.assertEqual([], authentication.credentials) authentication.read(self.username_password_encoding) self.assertEqual(1, len(authentication.credentials)) self.assertEqual( objects.Credential( credential_type=enums.CredentialType.USERNAME_AND_PASSWORD, credential_value=objects.UsernamePasswordCredential( username="******", password="******")), authentication.credentials[0]) # Test with a single DeviceCredential. authentication = contents.Authentication() self.assertEqual([], authentication.credentials) authentication.read(self.device_encoding) self.assertEqual(1, len(authentication.credentials)) self.assertEqual( objects.Credential(credential_type=enums.CredentialType.DEVICE, credential_value=objects.DeviceCredential( device_serial_number="serNum123456", password="******", device_identifier="devID2233", network_identifier="netID9000", machine_identifier="machineID1", media_identifier="mediaID313")), authentication.credentials[0]) # Test with multiple Credentials. authentication = contents.Authentication() self.assertEqual([], authentication.credentials) authentication.read(self.multiple_credentials_encoding) self.assertEqual(2, len(authentication.credentials)) self.assertEqual( objects.Credential( credential_type=enums.CredentialType.USERNAME_AND_PASSWORD, credential_value=objects.UsernamePasswordCredential( username="******", password="******")), authentication.credentials[0]) self.assertEqual( objects.Credential(credential_type=enums.CredentialType.DEVICE, credential_value=objects.DeviceCredential( device_serial_number="serNum123456", password="******", device_identifier="devID2233", network_identifier="netID9000", machine_identifier="machineID1", media_identifier="mediaID313")), authentication.credentials[1])