test_user_data.py 1.82 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
import json

from backend_app.models.userData import UserData, UserDataViewSet
from backend_app.tests.utils import WithUserTestCase


class UserDataTestCase(WithUserTestCase):
    """
    Some basic tests on the userData viewset.
    """

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

    def test_only_one_is_returned(self):
        # make sure there is data in the db
        self.assertNotEqual((UserData.objects.all()), 0)
        # get the data from the apo
        response = self.staff_client.get(self.api_user_data)
        content = json.loads(response.content)
        # make sure only one is returned ant that it is the one corresponding to the requester
        self.assertEqual(len(content), 1)
        self.assertEqual(content[0]["id"], self.staff_user.pk)

    def test_get_working(self):
        """
        Will also indirectly check that backend_app_permissions_request works
        :return:
        """

        def test_for_user(user_to_get, client, user_client):
            response = client.get(self.api_user_data + "{}/".format(user_to_get.pk))
            if user_to_get.pk != user_client.pk:
                self.assertEqual(response.status_code, 404)
            else:
                content = json.loads(response.content)
                self.assertEqual(content["id"], user_client.pk)

        clients = [
            self.authenticated_client,
            self.moderator_client,
            self.dri_client,
            self.staff_client,
        ]
        users = [
            self.authenticated_user,
            self.moderator_user,
            self.dri_user,
            self.staff_user,
        ]
        for (client, client_user) in zip(clients, users):
            for user in users:
                test_for_user(user, client, client_user)