Commit c3e609fe by Kevin Chugh

fix merge conflict

parents 5c91edd4 908fd46f
import json
from django.contrib.auth.models import AnonymousUser
from django.core.exceptions import PermissionDenied
from django.http import Http404
......@@ -7,7 +9,7 @@ from django.test.utils import override_settings
from mock import Mock, patch
from notification_prefs import NOTIFICATION_PREF_KEY
from notification_prefs.views import ajax_enable, ajax_disable, unsubscribe
from notification_prefs.views import ajax_enable, ajax_disable, ajax_status, unsubscribe
from student.tests.factories import UserFactory
from user_api.models import UserPreference
......@@ -57,6 +59,34 @@ class NotificationPrefViewTest(TestCase):
UserPreference.objects.filter(user=user, key=NOTIFICATION_PREF_KEY).exists()
)
# AJAX status view
def test_ajax_status_get_0(self):
request = self.request_factory.get("dummy")
request.user = self.user
response = ajax_status(request)
self.assertEqual(response.status_code, 200)
self.assertEqual(json.loads(response.content), {"status":0})
def test_ajax_status_get_1(self):
self.create_prefs()
request = self.request_factory.get("dummy")
request.user = self.user
response = ajax_status(request)
self.assertEqual(response.status_code, 200)
self.assertEqual(json.loads(response.content), {"status":1})
def test_ajax_status_post(self):
request = self.request_factory.post("dummy")
request.user = self.user
response = ajax_status(request)
self.assertEqual(response.status_code, 405)
def test_ajax_status_anon_user(self):
request = self.request_factory.get("dummy")
request.user = AnonymousUser()
self.assertRaises(PermissionDenied, ajax_status, request)
# AJAX enable view
def test_ajax_enable_get(self):
......
from base64 import urlsafe_b64encode, urlsafe_b64decode
from hashlib import sha256
import json
from Crypto.Cipher import AES
from Crypto import Random
......@@ -131,7 +132,7 @@ def ajax_disable(request):
return HttpResponse(status=204)
@require_POST
@require_GET
def ajax_status(request):
"""
A view that sends notifications status for the authenticated user
......@@ -142,22 +143,13 @@ def ajax_status(request):
if not request.user.is_authenticated():
raise PermissionDenied
prefs = UserPreference.objects.get(
qs = UserPreference.objects.filter(
user=request.user,
key=NOTIFICATION_PREF_KEY,
defaults={
"value": UsernameCipher.encrypt(request.user.username)
}
key=NOTIFICATION_PREF_KEY
)
if prefs
answer = true
else
answer = false
return utils.JsonResponse({
'status': answer
})
return HttpResponse(json.dumps({"status":len(qs)}), content_type="application/json")
@require_GET
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment