def test_auth_serializer(self): eth_address, eth_key = get_eth_address_with_key() data = get_auth_mock_data(eth_key) ethereum_signed_message = EthereumSignedMessage( data['push_token'], data['signature']['v'], data['signature']['r'], data['signature']['s']) push_token_hash = ethereum_signed_message.message_hash auth_serializer = AuthSerializer(data=data) self.assertTrue(auth_serializer.is_valid()) self.assertEqual(auth_serializer.validated_data['message_hash'], push_token_hash) self.assertEqual(auth_serializer.validated_data['signing_address'], eth_address) bad_auth_data = { 'push_token': data['push_token'], 'signature': get_bad_signature(data['push_token'], eth_key) } auth_serializer = AuthSerializer(data=bad_auth_data) self.assertRaises(ValidationError, auth_serializer.is_valid, raise_exception=True)
def test_pairing_deletion_serializer(self): device_address, device_key = get_eth_address_with_key() deletion_data = { 'device': device_address, 'signature': get_signature_json(device_address, device_key) } remove_pairing = PairingDeletionSerializer(data=deletion_data) self.assertTrue(remove_pairing.is_valid())
def test_notification_serializer_sending_itself(self): device_address, device_key = get_eth_address_with_key() message = faker.name() data = { 'devices': [device_address], 'message': message, 'signature': get_signature_json(message, device_key) } notification_serializer = NotificationSerializer(data=data) self.assertFalse(notification_serializer.is_valid())
def test_auth_creation(self): eth_account, eth_key = get_eth_address_with_key() auth_data = get_auth_mock_data(key=eth_key) response = self.client.post(reverse('v1:auth-creation'), data=auth_data, format='json') self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual( Device.objects.get(owner=eth_account).push_token, auth_data['push_token']) response_json = response.json() self.assertEqual(response_json['owner'], eth_account) self.assertEqual(response_json['pushToken'], auth_data['push_token']) # Try repeating the request with same push token and different address eth_account, eth_key = get_eth_address_with_key() auth_data = get_auth_mock_data(key=eth_key, token=auth_data['push_token']) response = self.client.post(reverse('v1:auth-creation'), data=auth_data, format='json') self.assertEqual(response.status_code, status.HTTP_201_CREATED) # Try repeating the request with different push token but same address auth_data = get_auth_mock_data(key=eth_key) response = self.client.post(reverse('v1:auth-creation'), data=auth_data, format='json') self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual( Device.objects.get(owner=eth_account).push_token, auth_data['push_token']) response_json = response.json() self.assertEqual(response_json['owner'], eth_account) self.assertEqual(response_json['pushToken'], auth_data['push_token'])
def test_pairing_with_same_address(self): eth_address, eth_key = get_eth_address_with_key() expiration_date = isoformat_without_ms(timezone.now() + timedelta(days=2)) data = { "temporary_authorization": { "expiration_date": expiration_date, "signature": get_signature_json(expiration_date, eth_key), }, "signature": get_signature_json(eth_address, eth_key) } pairing_serializer = PairingSerializer(data=data) self.assertFalse(pairing_serializer.is_valid()) self.assertTrue('non_field_errors' in pairing_serializer.errors)
def test_notification_serializer(self): notification_data = get_notification_mock_data() serializer = NotificationSerializer(data=notification_data) self.assertTrue(serializer.is_valid()) # Message longer than 4Kb notification_data = get_notification_mock_data( message=json.dumps({'my_message': 'A' * 4096})) serializer = NotificationSerializer(data=notification_data) serializer.is_valid() self.assertFalse(serializer.is_valid()) self.assertIn('4096', serializer.errors['message'][0]) # Message no json compliant notification_data = get_notification_mock_data() notification_data['message'] = 'NoValid{Json}' serializer = NotificationSerializer(data=notification_data) self.assertFalse(serializer.is_valid()) self.assertTrue('message' in serializer.errors) # Bad format invalid_notification_data = get_notification_mock_data( devices=['0x0', '0x1']) serializer = NotificationSerializer(data=invalid_notification_data) self.assertFalse(serializer.is_valid()) # Bad checksum invalid_notification_data = get_notification_mock_data(devices=[ '0xBBc1069ba4806e632fd11fAc9336b1f9dbf074a90EEA861d0b79cf256127abF3', '0xAAc1066ba4806e632fd11fBc9336b1f9dbf074a90ecc861d0b79cf256127ABF3' ]) serializer = NotificationSerializer(data=invalid_notification_data) self.assertFalse(serializer.is_valid()) # Duplicated addresses address, _ = get_eth_address_with_key() invalid_notification_data = get_notification_mock_data( devices=[address, address]) serializer = NotificationSerializer(data=invalid_notification_data) self.assertFalse(serializer.is_valid()) # No addresses invalid_notification_data['devices'] = [] serializer = NotificationSerializer(data=invalid_notification_data) self.assertFalse(serializer.is_valid())
def test_simple_notification_creation(self): random_address, _ = get_eth_address_with_key() message = '{}' data = { 'devices': [random_address], 'message': message, } response = self.client.post(reverse('v1:simple-notifications'), data=data, format='json') self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) device_pair = DevicePairFactory() d1 = device_pair.authorizing_device d2 = device_pair.authorized_device data = { 'devices': [d1.owner, d2.owner], 'message': message, } response = self.client.post(reverse('v1:simple-notifications'), data=data, format='json') self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) with self.settings(NOTIFICATION_SERVICE_PASS='******'): response = self.client.post(reverse('v1:simple-notifications'), data=data, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) data['password'] = '******' response = self.client.post(reverse('v1:simple-notifications'), data=data, format='json') self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)