test_user.py 2.01 KB
Newer Older
1
2
3
4
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
import json

import pytest
from django.core.exceptions import ValidationError

from backend_app.tests.utils import WithUserTestCase
from base_app.models import UserViewset, User


class UserTestCase(WithUserTestCase):
    """
    Tests for the custom user class
    """

    @classmethod
    def setUpMoreTestData(cls):
        cls.api_users = "/api/{}/".format(UserViewset.end_point_route)

    def test_authentificated_can_be_anonymous(self):
        self.authenticated_user.allow_sharing_personal_info = True
        self.authenticated_user.save()
        self.authenticated_user.allow_sharing_personal_info = False
        self.authenticated_user.save()

    def test_moderators_cant_be_anonymous(self):
        self.moderator_user.allow_sharing_personal_info = True
        self.moderator_user.save()

        with pytest.raises(ValidationError):
            self.moderator_user.allow_sharing_personal_info = False
            self.moderator_user.save()

    def test_moderators_cant_be_anonymous_request(self):
        response = self.moderator_client.put(
            self.api_users + "{}/".format(self.moderator_user.pk),
            dict(allow_sharing_personal_info=False),
        )
        self.assertEqual(response.status_code, 400)

    def test_get_all_returns_empty_list(self):
        # check that we have indeed users in the db
        self.assertNotEqual(len(User.objects.all()), 0)

        # Check what is the response of the endpoint
        response = self.staff_client.get(self.api_users)
        content = json.loads(response.content)
        self.assertEqual(len(content), 0)

    def test_data_cleaned(self):
        self.authenticated_user.allow_sharing_personal_info = False
        self.authenticated_user.save()

        response = self.authenticated_client_2.get(
            self.api_users + "{}/".format(self.authenticated_user.pk)
        )
        content = json.loads(response.content)
        for key in ["username", "first_name", "last_name", "email", "secondary_email"]:
            self.assertIsNone(content[key])