Skip to content

Commit

Permalink
feat(notifications): test for unsubscribing to in-app notifications
Browse files Browse the repository at this point in the history
- add tests for unsubscribing to in-app notifications
- add save method when notification status is changed

[Delivers #162948848]
  • Loading branch information
andrewhingah committed Feb 1, 2019
1 parent d60697b commit 23dcdfd
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 29 deletions.
5 changes: 3 additions & 2 deletions authors/apps/usernotifications/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ def article_handler(sender, instance, created, **kwargs):
followers = Friend.objects.select_related(
'user_from', 'user_to').filter(user_to=article_author.id).all()
recipients = [get_user_model().objects.get(id=u.user_from_id) for u in list(followers)]
for recipient in recipients:
if not recipient.email_notification_subscription:
for user in recipients:
recipient = User.objects.get(email=user)
if recipient.app_notification_subscription is False:
return

url = "api/articles/"
Expand Down
6 changes: 1 addition & 5 deletions authors/apps/usernotifications/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from notifications.models import Notification

from authors.apps.articles.models import Article, Comment
from authors.apps.articles.models import Article
from authors.apps.authentication.models import User


Expand All @@ -25,10 +25,6 @@ def to_representation(self, value):
actor_type = "article"
data = value.slug

elif isinstance(value, Comment):
actor_type = "comment"
data = value.body

elif isinstance(value, User):
actor_type = "user"
data = value.username
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ <h1 style="line-height: 35px;text-align: center;">
<p><a href={{resource_url}}><button>See Here</button></a></p>
<p>To opt out of these notifications, click below</p>
<p></p><a href={{opt_out_link}}>Unsubscribe</a></p>
<!-- <div style="margin-top: 7%;">
<a id="confirm-link" href="{{link}}" style="background-color:
#23bebe; color:#fff;padding: 0.4em;font-size: 2em;text-decoration: none;margin-left: 19%;">Verify Account</a>
</div> -->
<!-- <br> -->
<p style="text-align: center;">Thank you for being an active user.</p>

</div>
Expand Down
64 changes: 63 additions & 1 deletion authors/apps/usernotifications/tests/test_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def setUp(self):
self.token1 = login_res1.data["token"]


class NotificationsTestCase(BaseTestCase):
class InAppNotificationsTestCase(BaseTestCase):
def test_get_notification_on_article_creation(self):
# follow user
res = self.client.post(
Expand Down Expand Up @@ -78,3 +78,65 @@ def test_get_notification_on_article_creation(self):
HTTP_AUTHORIZATION='Token ' + self.token1)
self.assertEqual(response2.status_code, status.HTTP_200_OK)
self.assertEqual(response2.data["count"], 1)

def test_get_notification_when_followed(self):
# assert no notifcations
response1 = self.client.get(
reverse("notifications:all-notifications"),
HTTP_AUTHORIZATION='Token ' + self.token)
self.assertEqual(response1.status_code, status.HTTP_200_OK)
self.assertEqual(response1.data["count"], 0)

# send follow request
response2 = self.client.post(
'http://127.0.0.1:8000/api/profiles/testuser/follow',
HTTP_AUTHORIZATION='Token ' + self.token1)
self.assertEqual(response2.status_code, status.HTTP_200_OK)

# assert notifcation is available
response3 = self.client.get(
reverse("notifications:all-notifications"),
HTTP_AUTHORIZATION='Token ' + self.token)
self.assertEqual(response3.status_code, status.HTTP_200_OK)
self.assertEqual(response3.data["count"], 1)

def test_unsubscribe_from_in_app(self):
# test unsubscribe
response1 = self.client.put(
'http://127.0.0.1:8000/api/notifications/unsubscribe/',
HTTP_AUTHORIZATION='Token ' + self.token1)
self.assertEqual(response1.status_code, status.HTTP_200_OK)
self.assertEqual(
response1.data['message'],
"You have successfully unsubscribed from app notifications")
self.assertEqual(response1.data["email"], True)
self.assertEqual(response1.data["app"], False)

# test unsubscribed user won't receive notifications
# follow user
response2 = self.client.post(
'http://127.0.0.1:8000/api/profiles/testuser/follow',
HTTP_AUTHORIZATION='Token ' + self.token1)
self.assertEqual(response2.status_code, status.HTTP_200_OK)

# check notification is empty
response3 = self.client.get(
reverse("notifications:all-notifications"),
HTTP_AUTHORIZATION='Token ' + self.token1)
self.assertEqual(response3.status_code, status.HTTP_200_OK)
self.assertEqual(response3.data["count"], 0)

# followed user post article
response4 = self.client.post(
reverse("articles:articles-listcreate"),
data=json.dumps(self.article_details),
content_type="application/json",
HTTP_AUTHORIZATION='Token ' + self.token)
self.assertEqual(response4.status_code, status.HTTP_201_CREATED)

# check notification is still empty
response5 = self.client.get(
reverse("notifications:all-notifications"),
HTTP_AUTHORIZATION='Token ' + self.token1)
self.assertEqual(response5.status_code, status.HTTP_200_OK)
self.assertEqual(response5.data["count"], 0)
3 changes: 2 additions & 1 deletion authors/apps/usernotifications/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,11 @@ def put(self, request):
user = get_object_or_404(User, email=request.user.email)
user.app_notification_subscription = False
message = {
"message": "You have successfully unsubscribed from notifications",
"message": "You have successfully unsubscribed from app notifications",
"email": user.email_notification_subscription,
"app": user.app_notification_subscription
}
user.save()
return Response(message, status=status.HTTP_200_OK)


Expand Down
2 changes: 1 addition & 1 deletion authors/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True

DOMAIN = os.getenv("DOMAIN")
DOMAIN = os.getenv("APP_BASE_URL")

cloudinary.config(
cloud_name=os.getenv("CLOUD_NAME"),
Expand Down
54 changes: 40 additions & 14 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,29 +1,55 @@
astroid==2.1.0
autopep8==1.4.3
certifi==2018.11.29
chardet==3.0.4
cloudinary==1.15.0
coreapi==2.3.3
coreschema==0.0.4
coverage==4.5.2
defusedxml==0.5.0
dj-database-url==0.5.0
Django==2.1.5
django-braces==1.13.0
django-cors-middleware==1.3.1
django-extensions==2.1.4
django-heroku==0.3.1
django-model-utils==3.1.2
django-notifications-hq==1.5.0
django-oauth-toolkit==1.2.0
django-rest-framework-social-oauth2==1.1.0
django-rest-swagger==2.1.0
django-taggit==0.23.0
django-taggit-serializer==0.1.7
djangorestframework==3.9.0
gunicorn==19.9.0
idna==2.8
isort==4.3.4
itypes==1.1.0
Jinja2==2.10
jsonfield==2.0.2
lazy-object-proxy==1.3.1
MarkupSafe==1.1.0
mccabe==0.6.1
mock==2.0.0
oauthlib==3.0.1
openapi-codec==1.3.2
pbr==5.1.1
pep8==1.7.1
Pillow==5.4.1
psycopg2==2.7.6.1
psycopg2-binary==2.7.6.1
pycodestyle==2.4.0
dj-database-url==0.5.0
whitenoise==4.1.2
gunicorn==19.9.0
django-heroku==0.3.1
PyJWT==1.7.1
pylint==2.2.2
python3-openid==3.1.0
pytz==2018.9
cloudinary==1.15.0
Pillow==5.4.1
requests==2.21.0
requests-oauthlib==1.2.0
simplejson==3.16.0
six==1.12.0
django-taggit==0.23.0
django-taggit-serializer==0.1.7
pytz==2018.9
cloudinary==1.15.0
Pillow==5.4.1
social-auth-app-django==3.1.0
social-auth-core==3.0.0
django-rest-framework-social-oauth2==1.1.0
django-oauth-toolkit==1.2.0
coverage==4.5.2
uritemplate==3.0.0
urllib3==1.24.1
whitenoise==4.1.2
wrapt==1.11.0

0 comments on commit 23dcdfd

Please sign in to comment.