This commit is contained in:
dormantman 2019-12-30 18:13:06 +03:00 committed by Kuroshini
parent 13022ed476
commit 25e118cf0b
2 changed files with 25 additions and 13 deletions

View File

@ -1,4 +1,5 @@
"""Notification app models."""
from django.conf import settings
from django.db import models
from django.utils.timezone import now
@ -116,11 +117,8 @@ class Subscriber(ProjectBaseMixin):
new_ids = set(existing_answer.answer.id for existing_answer in subscribes)
old_subscribes_types = [sub for sub in old_subscribes if sub.id not in new_ids]
old_subscribes = Subscribe.objects.filter(subscriber=self, subscription_types__in=[old_subscribes_types])
for sub in old_subscribes:
sub.unsubscribe_date = now()
sub.save()
old_subscribes = Subscribe.objects.filter(subscriber=self, subscription_types__in=old_subscribes_types)
old_subscribes.update(unsubscribe_date=now())
self.save()

View File

@ -4,7 +4,7 @@ from http.cookies import SimpleCookie
from rest_framework import status
from rest_framework.test import APITestCase
from account.models import User, Role, UserRole
from account.models import Role, User, UserRole
from location.models import Country
from main.models import SiteSettings
from news.models import News, NewsType
@ -22,9 +22,9 @@ class BaseTestCase(APITestCase):
# get tokens
tokens = User.create_jwt_tokens(self.user)
self.client.cookies = SimpleCookie({
'access_token': tokens.get('access_token'),
'refresh_token': tokens.get('refresh_token')
})
'access_token': tokens.get('access_token'),
'refresh_token': tokens.get('refresh_token')
})
class NotificationAnonSubscribeTestCase(APITestCase):
@ -113,7 +113,7 @@ class NotificationUnsubscribeTestCase(APITestCase):
class NotificationManySubscribeTestCase(APITestCase):
def test_unsubscribe(self):
def test_subscribe(self):
self.username = 'sedragurda'
self.password = 'sedragurdaredips19'
self.email = 'sedragurda@desoz.com'
@ -123,8 +123,10 @@ class NotificationManySubscribeTestCase(APITestCase):
# get tokens
tokens = User.create_jwt_tokens(self.user)
self.client.cookies = SimpleCookie(
{'access_token': tokens.get('access_token'),
'refresh_token': tokens.get('refresh_token')})
{
'access_token': tokens.get('access_token'),
'refresh_token': tokens.get('refresh_token')
})
self.test_news_type = NewsType.objects.create(name="Test news type")
self.lang, created = Language.objects.get_or_create(
@ -178,4 +180,16 @@ class NotificationManySubscribeTestCase(APITestCase):
response = self.client.post("/api/web/notifications/subscribe/", data=test_data, format="json")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.json()["email"], test_data["email"])
self.assertEqual(response.json()["email"], test_data["email"])
def test_unsubscribe(self):
test_data = {
"email": self.email
}
test_subscriber = Subscriber.objects.create(user=self.user, email=self.email)
response = self.client.patch(f"/api/web/notifications/unsubscribe/{test_subscriber.update_code}/",
data=test_data, format="json")
self.assertEqual(response.status_code, status.HTTP_200_OK)