annotate wiki/tests/test_middleware.py @ 1188:cb712b2c34af

Add JSON option to dlcatreport management command.
author Brian Neal <bgneal@gmail.com>
date Mon, 27 Dec 2021 17:30:26 -0600
parents ba3230aba90c
children
rev   line source
bgneal@1174 1 # coding=utf-8
bgneal@626 2 """
bgneal@626 3 Tests for the wiki integration application.
bgneal@626 4
bgneal@626 5 """
bgneal@626 6 import hashlib
bgneal@626 7 import datetime
bgneal@626 8
bgneal@626 9 from django.contrib.auth.models import User
bgneal@626 10 from django.test import TestCase
bgneal@626 11 from django.test.client import RequestFactory
bgneal@626 12 from django.http import HttpResponse
bgneal@626 13 from django.conf import settings
bgneal@626 14
bgneal@626 15 from core.services import get_redis_connection
bgneal@627 16 from wiki.middleware import WikiMiddleware
bgneal@629 17 from wiki.constants import SESSION_SET_MEMBER
bgneal@626 18
bgneal@626 19
bgneal@626 20 class MiddleWareTestCase(TestCase):
bgneal@626 21
bgneal@626 22 def setUp(self):
bgneal@626 23 self.factory = RequestFactory()
bgneal@1174 24 self.user = User.objects.create_user(u'Sérgio', u'test@example.com',
bgneal@1174 25 u'password')
bgneal@626 26 self.conn = get_redis_connection()
bgneal@626 27 self.mw = WikiMiddleware()
bgneal@626 28
bgneal@626 29 def tearDown(self):
bgneal@626 30 self.conn.delete(settings.WIKI_REDIS_SET)
bgneal@626 31
bgneal@629 32 def create_request(self):
bgneal@626 33 request = self.factory.get('/contact/')
bgneal@626 34 request.session = {}
bgneal@626 35 request.user = self.user
bgneal@629 36 return request
bgneal@629 37
bgneal@629 38 def test_middleware(self):
bgneal@629 39
bgneal@629 40 request = self.create_request()
bgneal@626 41 response = HttpResponse()
bgneal@626 42
bgneal@629 43 request.wiki_set_cookie = True
bgneal@626 44 response = self.mw.process_response(request, response)
bgneal@626 45
bgneal@626 46 cookie = response.cookies.get(settings.WIKI_COOKIE_NAME)
bgneal@626 47 cookie_val = ''
bgneal@626 48 self.assertIsNotNone(cookie)
bgneal@626 49 if cookie:
bgneal@626 50 self.assertEqual(cookie['domain'], settings.WIKI_COOKIE_DOMAIN)
bgneal@626 51 self.assertEqual(cookie['path'], '/')
bgneal@626 52 self.assertEqual(cookie['max-age'], settings.WIKI_COOKIE_AGE)
bgneal@626 53
bgneal@626 54 cookie_val = cookie.value
bgneal@626 55 try:
bgneal@1174 56 user, email, key = cookie_val.decode('utf-8').split('#')
bgneal@627 57 except ValueError:
bgneal@626 58 self.fail('invalid cookie value')
bgneal@626 59 else:
bgneal@626 60 self.assertEqual(user, self.user.username)
bgneal@626 61 self.assertEqual(email, self.user.email)
bgneal@626 62 self.assertEqual(len(key), 64)
bgneal@626 63
bgneal@626 64 self.assertEqual(self.conn.zcard(settings.WIKI_REDIS_SET), 1)
bgneal@626 65
bgneal@626 66 h = hashlib.sha256()
bgneal@626 67 h.update(cookie_val)
bgneal@626 68 member = h.hexdigest()
bgneal@626 69
bgneal@626 70 score = self.conn.zscore(settings.WIKI_REDIS_SET, member)
bgneal@626 71 now = datetime.datetime.utcnow()
bgneal@626 72 session_start = datetime.datetime.fromtimestamp(score)
bgneal@626 73 self.assertLess(now - session_start, datetime.timedelta(seconds=2))
bgneal@626 74
bgneal@627 75 session_member = request.session.get(SESSION_SET_MEMBER)
bgneal@627 76 self.assertTrue(session_member and session_member == member)
bgneal@626 77
bgneal@626 78 # test the destroy session logic
bgneal@626 79
bgneal@629 80 request = self.create_request()
bgneal@627 81 request.wiki_delete_cookie = member
bgneal@626 82 response = self.mw.process_response(request, response)
bgneal@626 83
bgneal@626 84 cookie = response.cookies.get(settings.WIKI_COOKIE_NAME)
bgneal@626 85 self.assertIsNotNone(cookie)
bgneal@626 86 if cookie:
bgneal@626 87 self.assertEqual(cookie.value, '')
bgneal@626 88 self.assertEqual(cookie['domain'], settings.WIKI_COOKIE_DOMAIN)
bgneal@626 89 self.assertEqual(cookie['path'], '/')
bgneal@626 90 self.assertEqual(cookie['max-age'], 0)
bgneal@626 91 self.assertEqual(cookie['expires'], 'Thu, 01-Jan-1970 00:00:00 GMT')
bgneal@626 92
bgneal@626 93 self.assertEqual(self.conn.zcard(settings.WIKI_REDIS_SET), 0)