2020-06-16 09:17:21 +00:00
|
|
|
from django.contrib.auth.models import Permission, User
|
2020-05-29 09:47:13 +00:00
|
|
|
from django.shortcuts import reverse
|
|
|
|
from django.test import Client, TestCase
|
2015-02-27 08:53:51 +00:00
|
|
|
|
2020-05-29 09:47:13 +00:00
|
|
|
|
|
|
|
class AccountsTestCase(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.client.login(username='admin', password='admin')
|
2020-06-16 09:17:21 +00:00
|
|
|
user = User.objects.create_user(username='test', password='test')
|
|
|
|
permission = Permission.objects.get(codename='change_password')
|
|
|
|
user.user_permissions.add(permission)
|
2020-05-29 09:47:13 +00:00
|
|
|
|
|
|
|
def test_profile(self):
|
|
|
|
response = self.client.get(reverse('profile'))
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
response = self.client.get(reverse('account', args=[2]))
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
def test_login_logout(self):
|
|
|
|
user = User.objects.get(username='test')
|
|
|
|
self.assertEqual(user.id, 2)
|
|
|
|
|
|
|
|
client = Client()
|
|
|
|
|
2020-09-25 12:57:17 +00:00
|
|
|
response = client.post(reverse("login"), {"username": "test", "password": "test"})
|
2020-05-29 09:47:13 +00:00
|
|
|
self.assertRedirects(response, reverse('profile'))
|
|
|
|
|
|
|
|
response = client.get(reverse('logout'))
|
|
|
|
self.assertRedirects(response, reverse('login'))
|
2020-06-16 09:17:21 +00:00
|
|
|
|
|
|
|
def test_password_change(self):
|
|
|
|
client = Client()
|
|
|
|
|
|
|
|
logged_in = client.login(username='test', password='test')
|
|
|
|
self.assertTrue(logged_in)
|
|
|
|
|
|
|
|
response = client.get(reverse('change_password'))
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
response = client.post(
|
|
|
|
reverse('change_password'),
|
|
|
|
{
|
|
|
|
'old_password': 'wrongpass',
|
|
|
|
'new_password1': 'newpw',
|
|
|
|
'new_password2': 'newpw',
|
|
|
|
},
|
|
|
|
)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
response = client.post(
|
|
|
|
reverse('change_password'),
|
|
|
|
{
|
|
|
|
'old_password': 'test',
|
|
|
|
'new_password1': 'newpw',
|
|
|
|
'new_password2': 'newpw',
|
|
|
|
},
|
|
|
|
)
|
|
|
|
self.assertRedirects(response, reverse('profile'))
|
|
|
|
|
|
|
|
client.logout()
|
|
|
|
|
|
|
|
logged_in = client.login(username='test', password='newpw')
|
|
|
|
self.assertTrue(logged_in)
|