Skip to content

Commit

Permalink
Fix bug in admin action and add test (#6)
Browse files Browse the repository at this point in the history
* Fix bug in admin action and add test

* Bump version for 1.0.2
  • Loading branch information
lukeburden authored Jan 30, 2021
1 parent adbb602 commit 349ddde
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 14 deletions.
2 changes: 1 addition & 1 deletion fcm_devices/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def send_notification(self, request, queryset):
error_counts = defaultdict(int)
for device in queryset.iterator():
response = service.get_fcm_backend().send_notification(
device, title="Testing 123", body="A test notification"
device, message_title="Testing 123", message_body="A test notification"
)
if int(response["success"]):
success += 1
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
description=description,
long_description=long_description,
long_description_content_type="text/markdown",
version="1.0.1",
version="1.0.2",
license="MIT",
url=url,
packages=find_packages(exclude=["tests", "testproj"]),
Expand Down
13 changes: 12 additions & 1 deletion tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,24 @@
USE_TZ = True
TIME_ZONE = "UTC"

MIDDLEWARE = [] # from 2.0 onwards, only MIDDLEWARE is used
MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]

ROOT_URLCONF = "tests.urls"
INSTALLED_APPS = [
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sites",
"django.contrib.admin",
"django.contrib.sessions",
"django.contrib.messages",
"fcm_devices",
"tests",
]
Expand Down
49 changes: 49 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json

from django.core.exceptions import ImproperlyConfigured
from django.test import override_settings
from django.urls import reverse
Expand Down Expand Up @@ -288,3 +290,50 @@ def test_create_device_duplicate(api_client):
assert response.status_code == 201
new_device = device.user.devices.last()
assert new_device == device


# test admin send action


@pytest.mark.django_db
@responses.activate
@override_settings(FCM_DEVICES_BACKEND_CLASS="fcm_devices.fcm.FCMBackend")
def test_send_test_notification_action(client, mocker):
responses.add(
responses.Response(
method="POST",
url=FCMNotification.FCM_END_POINT,
match_querystring=False,
json=success_response,
status=200,
)
)

device = baker.make(
"fcm_devices.Device", user__is_staff=True, user__is_superuser=True
)
responses.add(
responses.Response(
method="POST",
url=FCMNotification.FCM_END_POINT,
match_querystring=False,
json=success_response,
status=200,
)
)

client.force_login(device.user)

send_url = reverse("admin:fcm_devices_device_changelist")
data = {
"action": "send_notification",
"select_across": "0",
"index": "0",
"_selected_action": device.id,
}

response = client.post(send_url, data=data)

assert response.status_code == 302
assert len(responses.calls) == 1
assert json.loads(responses.calls[0].response.text) == success_response
17 changes: 6 additions & 11 deletions tests/urls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.urls import include
from django.contrib import admin
from django.urls import include, path

from rest_framework.routers import SimpleRouter

Expand All @@ -8,13 +9,7 @@
router = SimpleRouter()
router.register("devices", DeviceViewSet, basename="devices")


try:
from django.urls import path

urlpatterns = [path("", include(router.urls))]

except ImportError:
from django.conf.urls import url

urlpatterns = [url("", include(router.urls))]
urlpatterns = [
path("admin/", admin.site.urls),
path("api/", include(router.urls)),
]

0 comments on commit 349ddde

Please sign in to comment.