tests.py 1.85 KB
Newer Older
luxcem's avatar
luxcem committed
1
from django.test import TestCase
luxcem's avatar
luxcem committed
2
3
from django.test import Client
from django_factory_boy import auth as auth_factories
luxcem's avatar
luxcem committed
4

luxcem's avatar
luxcem committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
AUTHENTICATION_BACKENDS = [
    # Default
    "django.contrib.auth.backends.ModelBackend",
    # Email or Username for login
    "core.auth_backends.EmailOrUsernameModelBackend"
]


class ApiTest(TestCase):
    def setUp(self):
        self.client = Client()
        self.user = auth_factories.UserFactory(password="dummypassword")

    def test_authenticate(self):
        with self.settings(AUTHENTICATION_BACKENDS=AUTHENTICATION_BACKENDS):
            # username, wrong password
            login_status = self.client.login(
                username=self.user.username,
                password="notthepassword"
            )
            assert not login_status

            # username, correct password
            login_status = self.client.login(
                username=self.user.username,
                password="dummypassword"
            )
            assert login_status

            # email, wrong password
            login_status = self.client.login(
                username=self.user.email,
                password="notthepassowrd"
            )
            assert not login_status

            # email, correct password
            login_status = self.client.login(
                username=self.user.email,
                password="dummypassword"
            )
            assert login_status

            # username, wrong user
            login_status = self.client.login(
                username="{}-2".format(self.user.username),
                password="dummypassword"
            )
            assert not login_status

            # email, wrong user
            login_status = self.client.login(
                username="{}-2".format(self.user.email),
                password="dummypassword"
            )
            assert not login_status