2013-03-03 10:09:33 -08:00
|
|
|
from django.core.urlresolvers import reverse
|
2013-03-03 09:11:49 -08:00
|
|
|
from django.test import TestCase
|
2013-03-03 10:09:33 -08:00
|
|
|
from django.test.utils import override_settings
|
2013-03-03 09:11:49 -08:00
|
|
|
|
|
|
|
|
import mock
|
|
|
|
|
|
|
|
|
|
from .auth.backends import CombinedAuthBackend
|
|
|
|
|
from .models import User
|
|
|
|
|
|
|
|
|
|
|
2018-08-21 08:05:46 -07:00
|
|
|
def mock_requests_get(url, headers=None):
|
2019-12-08 21:02:49 +00:00
|
|
|
response = mock.Mock(content=open('docs/src/imgs/logo-dark.png', 'rb').read())
|
2013-03-19 15:41:55 +01:00
|
|
|
return response
|
2013-03-03 09:11:49 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class CombinedAuthBackendTest(TestCase):
|
|
|
|
|
def setUp(self):
|
|
|
|
|
self.backend = CombinedAuthBackend()
|
|
|
|
|
self.username = 'jdoe'
|
|
|
|
|
self.email = 'jdoe@example.com'
|
|
|
|
|
self.password = 'password'
|
|
|
|
|
User.objects.create_user(username=self.username, email=self.email, password=self.password)
|
|
|
|
|
|
|
|
|
|
def test_authenticate_username(self):
|
|
|
|
|
self.assertTrue(self.backend.authenticate(username=self.username, password=self.password))
|
|
|
|
|
|
|
|
|
|
def test_authenticate_email(self):
|
|
|
|
|
self.assertTrue(self.backend.authenticate(username=self.email, password=self.password))
|
|
|
|
|
|
|
|
|
|
def test_authenticate_wrong_password(self):
|
|
|
|
|
self.assertIsNone(self.backend.authenticate(username=self.username, password='wrong-password'))
|
|
|
|
|
|
|
|
|
|
def test_authenticate_unknown_user(self):
|
|
|
|
|
self.assertIsNone(self.backend.authenticate(username='wrong-username', password='wrong-password'))
|
|
|
|
|
|
2013-03-03 10:09:33 -08:00
|
|
|
|
|
|
|
|
class CreateUserTest(TestCase):
|
|
|
|
|
def test_create_post(self):
|
|
|
|
|
data = {
|
|
|
|
|
'username': 'jdoe',
|
|
|
|
|
'email': 'jdoe@example.com',
|
2019-12-04 00:02:53 +08:00
|
|
|
'password': 'password',
|
|
|
|
|
'password_repeat': 'password',
|
2013-03-03 10:09:33 -08:00
|
|
|
}
|
2019-12-04 00:02:53 +08:00
|
|
|
response = self.client.post(
|
|
|
|
|
reverse('users:user-list'),
|
|
|
|
|
data=data,
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(response.status_code, 201)
|
2013-03-03 10:09:33 -08:00
|
|
|
|
|
|
|
|
@override_settings(ALLOW_NEW_REGISTRATIONS=False)
|
|
|
|
|
def test_create_post_not_allowed(self):
|
2019-12-04 00:02:53 +08:00
|
|
|
data = {
|
|
|
|
|
'username': 'jdoe',
|
|
|
|
|
'email': 'jdoe@example.com',
|
|
|
|
|
'password': 'password',
|
|
|
|
|
'password_repeat': 'password',
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
response = self.client.post(
|
|
|
|
|
reverse('users:user-list'),
|
|
|
|
|
data=data,
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(response.status_code, 403)
|
2013-03-03 10:09:33 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class LogoutViewTest(TestCase):
|
|
|
|
|
def setUp(self):
|
|
|
|
|
User.objects.create_user(username='jdoe', password='password')
|
|
|
|
|
self.client.login(username='jdoe', password='password')
|
|
|
|
|
|
|
|
|
|
def test_logout_view(self):
|
|
|
|
|
response = self.client.get(reverse('users:logout'))
|
2019-12-04 00:02:53 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|