Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions km_api/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ def api_rf():
return UserAPIRequestFactory(user=AnonymousUser())


@pytest.fixture
def apns_device_factory(db):
"""
Fixture to get the factory used to create APNS devices.
"""
return factories.APNSDeviceFactory


@pytest.fixture
def email_factory(db):
"""
Expand Down
11 changes: 11 additions & 0 deletions km_api/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@
import factory


class APNSDeviceFactory(factory.django.DjangoModelFactory):
"""
Factory for generating APNS devices.
"""
user = factory.SubFactory('factories.UserFactory')
registration_id = factory.Sequence(lambda n: str(n))

class Meta:
model = 'push_notifications.APNSDevice'


class EmailFactory(factory.django.DjangoModelFactory):
"""
Factory for generating ``EmailAddress`` instances.
Expand Down
2 changes: 2 additions & 0 deletions km_api/km_api/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
'corsheaders',
'django_filters',
'dry_rest_permissions',
'push_notifications',
'raven.contrib.django.raven_compat',
'rest_email_auth',
'rest_framework',
Expand All @@ -59,6 +60,7 @@
'know_me.journal',
'know_me.profile',
'mailing_list',
'notifications',
]

MIDDLEWARE = [
Expand Down
1 change: 1 addition & 0 deletions km_api/km_api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@
url(r'^auth/', include('km_auth.urls')),
url(r'^docs/', include_docs_urls(title='Know Me API')),
url(r'^know-me/', include('know_me.urls')),
url(r'^notifications/', include('notifications.urls')),
]
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import pytest

from push_notifications.api.rest_framework import APNSDeviceSerializer

from rest_framework import status
from rest_framework.reverse import reverse


url = reverse('notifications:apns-device-list')


@pytest.mark.integration
def test_get_apns_device_list(
api_client,
api_rf,
apns_device_factory,
user_factory):
"""
Sending a GET request to the view should return a list of the user's
registered APNS devices.
"""
user = user_factory()
api_client.force_authenticate(user=user)
api_rf.user = user

apns_device_factory(user=user)
apns_device_factory(user=user)

request = api_rf.get(url)
response = api_client.get(url)

assert response.status_code == status.HTTP_200_OK

serializer = APNSDeviceSerializer(
user.apnsdevice_set.all(),
context={'request': request},
many=True,
)

assert response.data == serializer.data


@pytest.mark.integration
def test_post_new_apns_device(api_client, api_rf, user_factory):
"""
Sending a POST request to the view should create a new APNS device
attached to the requesting user.
"""
user = user_factory()
api_client.force_authenticate(user=user)
api_rf.user = user

data = {
# The registration ID must be 64 or 200 characters
'registration_id': '1' * 64,
}

request = api_rf.post(url, data)
response = api_client.post(url, data)

assert response.status_code == status.HTTP_201_CREATED

serializer = APNSDeviceSerializer(
user.apnsdevice_set.get(),
context={'request': request},
)

assert response.data == serializer.data
Empty file.
17 changes: 17 additions & 0 deletions km_api/notifications/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from django.conf.urls import include, url

from push_notifications.api.rest_framework import APNSDeviceAuthorizedViewSet

from rest_framework.routers import DefaultRouter


app_name = 'notifications'


router = DefaultRouter()
router.register('apns', APNSDeviceAuthorizedViewSet, 'apns-device')


urlpatterns = [
url(r'^', include(router.urls)),
]
1 change: 1 addition & 0 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ boto3 == 1.6.8
coreapi == 2.3.3
django-cors-headers == 2.2.0
django-filter == 1.1.0
django-push-notifications == 1.6.0
django-rest-email-auth == 1.0.0
django-ses == 0.8.5
django-solo == 1.1.3
Expand Down