2017-11-09 12:24:32 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2020-05-17 10:11:08 +00:00
|
|
|
import time
|
|
|
|
|
2017-11-09 12:24:32 +00:00
|
|
|
from django.test import TestCase
|
|
|
|
from django.test import Client as HttpClient
|
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.core.management import call_command
|
|
|
|
|
|
|
|
from faker import Factory
|
|
|
|
|
|
|
|
|
|
|
|
class ViewsTestCase(TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
super(ViewsTestCase, cls).setUpClass()
|
|
|
|
fake = Factory.create()
|
|
|
|
call_command('migrate', verbosity=0)
|
|
|
|
|
|
|
|
cls.c = HttpClient()
|
|
|
|
|
|
|
|
fake_user = fake.simple_profile()
|
|
|
|
cls.credentials = {
|
|
|
|
'username': fake_user['username'],
|
|
|
|
'password': fake.password()
|
|
|
|
}
|
|
|
|
cls.user = User.objects.create_user(
|
|
|
|
is_superuser=True, **cls.credentials)
|
|
|
|
|
|
|
|
cls.c.login(**cls.credentials)
|
|
|
|
|
|
|
|
def test_root_router(self):
|
|
|
|
page = self.c.get('/')
|
|
|
|
self.assertEqual(page.url, '/dashboard/')
|
2017-11-13 00:43:40 +00:00
|
|
|
|
2020-05-17 10:11:08 +00:00
|
|
|
def test_rolling_sessions(self):
|
|
|
|
self.c.get('/')
|
|
|
|
session1 = str(self.c.cookies['sessionid'])
|
|
|
|
# Sleep longer than ROLLING_SESSION_REFRESH in our
|
|
|
|
# settings module, to test we get a new session.
|
|
|
|
time.sleep(2)
|
|
|
|
self.c.get('/')
|
|
|
|
session2 = str(self.c.cookies['sessionid'])
|
|
|
|
self.c.get('/')
|
|
|
|
session3 = str(self.c.cookies['sessionid'])
|
|
|
|
self.assertNotEqual(session1, session2)
|
|
|
|
self.assertEqual(session2, session3)
|
|
|
|
|
2017-12-05 15:53:28 +00:00
|
|
|
def test_user_reset_api_key(self):
|
|
|
|
api_key_before = User.objects.get(pk=self.user.id).settings.api_key()
|
|
|
|
page = self.c.get('/user/reset-api-key/')
|
|
|
|
self.assertEqual(page.status_code, 302)
|
|
|
|
self.assertNotEqual(
|
|
|
|
api_key_before,
|
|
|
|
User.objects.get(pk=self.user.id).settings.api_key()
|
|
|
|
)
|
|
|
|
|
2017-11-13 00:43:40 +00:00
|
|
|
def test_user_settings(self):
|
|
|
|
page = self.c.get('/user/settings/')
|
|
|
|
self.assertEqual(page.status_code, 200)
|
|
|
|
|
2017-12-11 22:18:22 +00:00
|
|
|
def test_user_views(self):
|
2017-12-13 18:49:10 +00:00
|
|
|
# Staff setting is required to access user management.
|
|
|
|
page = self.c.get('/users/')
|
2018-08-04 20:00:51 +00:00
|
|
|
self.assertEqual(page.status_code, 403)
|
2017-12-13 18:49:10 +00:00
|
|
|
self.user.is_staff = True
|
|
|
|
self.user.save()
|
|
|
|
|
2017-12-11 22:25:25 +00:00
|
|
|
page = self.c.get('/users/')
|
2017-12-11 22:18:22 +00:00
|
|
|
self.assertEqual(page.status_code, 200)
|
2017-12-11 22:25:25 +00:00
|
|
|
page = self.c.get('/users/add/')
|
2017-12-11 22:18:22 +00:00
|
|
|
self.assertEqual(page.status_code, 200)
|
|
|
|
|
|
|
|
entry = User.objects.first()
|
2017-12-13 23:12:32 +00:00
|
|
|
page = self.c.get('/users/{}/edit/'.format(entry.id))
|
2017-12-11 22:18:22 +00:00
|
|
|
self.assertEqual(page.status_code, 200)
|
2017-12-11 22:25:25 +00:00
|
|
|
page = self.c.get('/users/{}/delete/'.format(entry.id))
|
2017-12-11 22:18:22 +00:00
|
|
|
self.assertEqual(page.status_code, 200)
|
|
|
|
|
2017-11-13 00:43:40 +00:00
|
|
|
def test_welcome(self):
|
|
|
|
page = self.c.get('/welcome/')
|
|
|
|
self.assertEqual(page.status_code, 200)
|