Completed
Push — master ( 257c83...664ad9 )
by Camille
01:02
created

UserTests.test_edit_email_nonvalid_email()   A

Complexity

Conditions 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 7
rs 9.4286
1
import json
2
3
from rest_framework import status
4
from rest_framework.test import APITestCase, force_authenticate
5
6
from sigma_core.tests.factories import UserFactory, AdminUserFactory
7
from sigma_core.serializers.user import UserSerializer
8
9
10
class UserTests(APITestCase):
11
    @classmethod
12
    def setUpTestData(self):
13
        super(UserTests, self).setUpTestData()
14
15
        self.user = UserFactory()
16
        self.user2 = UserFactory()
17
        self.admin_user = AdminUserFactory()
18
19
        serializer = UserSerializer(self.user)
20
        self.user_data = serializer.data
21
        self.user_url = '/user/%d/' % self.user.id
22
23
        self.users_list = [self.user, self.user2, self.admin_user]
24
25
        self.new_user_data = {'lastname': 'Doe', 'firstname': 'John', 'email': '[email protected]', 'password': 'password'}
26
27
#### List requests
28
    def test_get_users_list_unauthed(self):
29
        # Client not authenticated
30
        response = self.client.get('/user/')
31
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
32
33
    # def test_get_users_list_forbidden(self):
34
    #     # Client authenticated but has no permission
35
    #     self.client.force_authenticate(user=self.user)
36
    #     response = self.client.get('/user/')
37
    #     self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
38
39
    def test_get_users_list_ok(self):
40
        # Client has permissions
41
        self.client.force_authenticate(user=self.user)
42
        response = self.client.get('/user/')
43
        self.assertEqual(response.status_code, status.HTTP_200_OK)
44
        self.assertEqual(len(response.data), len(self.users_list))
45
46
#### Get requests
47
    def test_get_user_unauthed(self):
48
        # Client is not authenticated
49
        response = self.client.get(self.user_url)
50
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
51
52
    # def test_get_user_forbidden(self):
53
    #     response = self.client.get(self.user_url)
54
    #     self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
55
56
    def test_get_user_ok(self):
57
        # Client has permissions
58
        self.client.force_authenticate(user=self.user)
59
        response = self.client.get(self.user_url)
60
        self.assertEqual(response.status_code, status.HTTP_200_OK)
61
        self.assertEqual(response.data, self.user_data)
62
63
#### "Get my data" requests
64
    def test_get_my_data_unauthed(self):
65
        # Client is not authenticated
66
        response = self.client.get('/user/me/')
67
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
68
69
    def test_get_my_data_ok(self):
70
        # Client is authenticated
71
        self.client.force_authenticate(user=self.user)
72
        response = self.client.get('/user/me/')
73
        self.assertEqual(response.status_code, status.HTTP_200_OK)
74
        self.assertEqual(response.data['id'], self.user.id)
75
76
#### Create requests
77
    def test_create_user_unauthed(self):
78
        # Client is not authenticated
79
        response = self.client.post('/user/', self.new_user_data)
80
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
81
82
    # def test_create_user_forbidden(self):
83
    #     # Client has no permission
84
    #     self.client.force_authenticate(user=self.user)
85
    #     response = self.client.post('/user/', self.new_user_data)
86
    #     self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
87
88
    def test_create_user_ok(self):
89
        # Client has permissions
90
        self.client.force_authenticate(user=self.admin_user)
91
        response = self.client.post('/user/', self.new_user_data)
92
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
93
        self.assertEqual(response.data['lastname'], self.new_user_data['lastname'])
94
95
#### Modification requests
96
    # def test_edit_email_wrong_permission(self):
97
    #     # Client wants to change another user's email
98
    #     self.client.force_authenticate(user=self.user)
99
    #     user_data = UserSerializer(self.user2).data
100
    #     user_data['email'] = "[email protected]"
101
    #     response = self.client.put("/user/%d/" % self.user2.id, user_data)
102
    #     self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
103
104
    def test_edit_email_nonvalid_email(self):
105
        # Client wants to change his email with a non valid value
106
        self.client.force_authenticate(user=self.user)
107
        user_data = self.user_data.copy()
108
        user_data['email'] = "ThisIsNotAnEmail"
109
        response = self.client.put("/user/%d/" % self.user.id, user_data)
110
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
111
112
    def test_edit_email_ok(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
        # Client wants to change his email and succeed in
114
        self.client.force_authenticate(user=self.user)
115
        user_data = self.user_data.copy()
116
        user_data['email'] = "[email protected]"
117
        response = self.client.put("/user/%d/" % self.user.id, user_data)
118
        self.assertEqual(response.status_code, status.HTTP_200_OK)
119
        self.assertEqual(response.data['email'], user_data['email'])
120
        # Guarantee that tests are independant
121
        self.user.email = self.user_data['email']
122
        self.user.save()
123
124
    # def test_edit_profile_wrong_permission(self):
125
    #     # Client wants to change another user's phone number
126
    #     self.client.force_authenticate(user=self.user)
127
    #     user_data = UserSerializer(self.user2).data
128
    #     user_data['phone'] = "0123456789"
129
    #     response = self.client.put("/user/%d/" % self.user2.id, user_data)
130
    #     self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
131
132
    def test_edit_profile_ok(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
133
        # Client wants to change his phone number
134
        self.client.force_authenticate(user=self.user)
135
        user_data = self.user_data.copy()
136
        user_data['phone'] = "0123456789"
137
        response = self.client.put("/user/%d/" % self.user.id, user_data)
138
        self.assertEqual(response.status_code, status.HTTP_200_OK)
139
        self.assertEqual(response.data['phone'], user_data['phone'])
140
        # Guarantee that tests are independant
141
        self.user.phone = self.user_data['phone']
142
        self.user.save()
143
144
    def test_edit_lastname_wrong_permission(self):
145
        # Client wants to change his lastname
146
        pass
147
148
    def test_edit_lastname_ok(self):
149
        # Admin wants to change an user's lastname
150
        pass
151
152
153
154
#### "Change password" requests
155
156
#### Deletion requests
157