Skip to content

Commit

Permalink
Validate token registration semantic version
Browse files Browse the repository at this point in the history
  • Loading branch information
Uxio0 committed Aug 26, 2020
1 parent 6536e52 commit 16e0db4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ firebase-admin==4.3.0
gnosis-py==2.5.6
gunicorn[gevent]==20.0.4
hexbytes==0.2.1
packaging>=19.2
psycopg2-binary==2.8.5
redis==3.5.3
requests==2.24.0
Expand Down
10 changes: 9 additions & 1 deletion safe_transaction_service/notifications/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from django.db import IntegrityError

from packaging import version as semantic_version
from rest_framework import serializers

from gnosis.eth.django.serializers import EthereumAddressField
Expand All @@ -23,9 +24,16 @@ class FirebaseDeviceSerializer(serializers.Serializer):

def validate_safes(self, safes: Sequence[str]):
if SafeContract.objects.filter(address__in=safes).count() != len(safes):
raise serializers.ValidationError("At least one Safe provided was not found")
raise serializers.ValidationError('At least one Safe provided was not found')
return safes

def validate_version(self, value: str):
try:
semantic_version.Version(value)
except semantic_version.InvalidVersion:
raise serializers.ValidationError('Semantic version was expected')
return value

def save(self, **kwargs):
try:
firebase_device, _ = FirebaseDevice.objects.update_or_create(
Expand Down
9 changes: 9 additions & 0 deletions safe_transaction_service/notifications/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,20 @@ def test_notifications_devices_create_view(self):
self.assertEqual(FirebaseDevice.objects.first().safes.count(), 2)

# Use not valid deviceType
previous_device_type = data['deviceType']
data['deviceType'] = 'RANGER-MORPHER'
response = self.client.post(reverse('v1:notifications-devices'), format='json', data=data)
self.assertIn('is not a valid choice', response.content.decode())
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(safe_contract.firebase_devices.count(), 1)
data['deviceType'] = previous_device_type

# Use not valid version
data['version'] = 'Megazord'
response = self.client.post(reverse('v1:notifications-devices'), format='json', data=data)
self.assertIn('Semantic version was expected', response.content.decode())
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(safe_contract.firebase_devices.count(), 1)

def test_notifications_devices_delete_view(self):
safe_contract = SafeContractFactory()
Expand Down

0 comments on commit 16e0db4

Please sign in to comment.