Skip to content

Commit b7a085b

Browse files
authored
Merge pull request #349 from Mosquito-Alert/fix_device_api_fcm_token
Fix fcm_token duplicates
2 parents 213b3c0 + 54ff046 commit b7a085b

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

api/serializers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1639,7 +1639,7 @@ def create(self, validated_data):
16391639
# That is for the users that are migrating from the legacy API to this.
16401640
device = Device.objects.filter(
16411641
models.Q(model=model) |
1642-
models.Q(registration_id=validated_data.get('fcm_token'))
1642+
models.Q(registration_id=validated_data.get('registration_id'))
16431643
).filter(user=user, device_id=None).first()
16441644
if device:
16451645
Device.objects.filter(user=user, model=model, device_id=device_id).delete()

api/tests/test_views.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
from django.utils import timezone
1111
from django.utils.module_loading import import_string
1212

13+
from fcm_django.models import DeviceType
14+
1315
from rest_framework import status
1416
from rest_framework.authtoken.models import Token
1517
from rest_framework.test import APIClient
@@ -731,6 +733,39 @@ def test_device_without_device_id_is_updated_on_create(self, app_user):
731733
assert device.pk == device_pk
732734
assert Device.objects.filter(user=app_user).count() == 1
733735

736+
def test_device_with_same_fcm_token_is_updated_on_create(self, app_user, app_api_client):
737+
device = Device.objects.create(
738+
user=app_user,
739+
device_id=None,
740+
registration_id='fcm_unique_token',
741+
type=DeviceType.ANDROID,
742+
model=None,
743+
)
744+
response = app_api_client.post(
745+
self.endpoint,
746+
data={
747+
'device_id': 'unique_id',
748+
'fcm_token': 'fcm_unique_token',
749+
'type': device.type,
750+
'manufacturer': device.manufacturer,
751+
'model': 'new_model',
752+
'os': {
753+
'name': 'test_os_name',
754+
'version': 'test_os_version'
755+
}
756+
},
757+
format='json'
758+
)
759+
assert response.status_code == status.HTTP_201_CREATED
760+
assert Device.objects.filter(user=app_user).count() == 1
761+
device.refresh_from_db()
762+
assert device.device_id == 'unique_id'
763+
assert device.model == 'new_model'
764+
assert device.registration_id == 'fcm_unique_token'
765+
assert device.os_name == 'test_os_name'
766+
assert device.os_version == 'test_os_version'
767+
768+
734769
@pytest.mark.django_db
735770
@pytest.mark.usefixtures("taxa")
736771
class TestIdentificationTaskAnnotationsApi:

0 commit comments

Comments
 (0)