-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtests.py
More file actions
103 lines (81 loc) · 3.88 KB
/
tests.py
File metadata and controls
103 lines (81 loc) · 3.88 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from django.test import TestCase
from rest_framework.test import APIRequestFactory, force_authenticate
from tests.constants import USER_CREATE_DATA
from users.models import CustomUser
from users.views import UserList
from industries.models import Industry
from industries.views import IndustryDetail, IndustryList
class IndustryTestCase(TestCase):
"""Tests for industries+"""
def setUp(self):
self.factory = APIRequestFactory()
self.user_list_view = UserList.as_view()
self.industry_list_view = IndustryList.as_view()
self.industry_detail_view = IndustryDetail.as_view()
self.INDUSTRY_NAME = "Test Industry"
self.CREATE_DATA = {
"name": self.INDUSTRY_NAME,
}
def test_industry_creation(self):
user = self._user_create()
request = self.factory.post("industries/", self.CREATE_DATA)
force_authenticate(request, user=user)
response = self.industry_list_view(request)
self.assertEqual(response.status_code, 201)
self.assertEqual(response.data["name"], self.INDUSTRY_NAME)
def test_industry_creation_with_too_long_name(self):
user = self._user_create()
request = self.factory.post("industries/", {"name": "too_long_string_" * 257})
force_authenticate(request, user=user)
response = self.industry_list_view(request)
self.assertEqual(response.status_code, 400)
def test_industry_creation_with_empty_name(self):
user = self._user_create()
request = self.factory.post("industries/", {"name": ""})
force_authenticate(request, user=user)
response = self.industry_list_view(request)
self.assertEqual(response.status_code, 400)
def test_industry_creation_with_wrong_data(self):
user = self._user_create()
request = self.factory.post("industries/", {"wrong_name": "Wrong value"})
force_authenticate(request, user=user)
response = self.industry_list_view(request)
self.assertEqual(response.status_code, 400)
def test_industry_creation_with_empty_data(self):
user = self._user_create()
request = self.factory.post("industries/", {})
force_authenticate(request, user=user)
response = self.industry_list_view(request)
self.assertEqual(response.status_code, 400)
def test_industry_update(self):
user = self._user_create()
request = self.factory.post("industries/", self.CREATE_DATA)
force_authenticate(request, user=user)
response = self.industry_list_view(request)
industry_id = response.data["id"]
industry = Industry.objects.get(id=industry_id)
request = self.factory.patch(f"industries/{industry.pk}/", {"name": "Test2"})
force_authenticate(request, user=user)
response = self.industry_detail_view(request, pk=industry.pk)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data["name"], "Test2")
def test_industry_update_with_wrong_data(self):
user = self._user_create()
request = self.factory.post("industries/", self.CREATE_DATA)
force_authenticate(request, user=user)
response = self.industry_list_view(request)
industry_id = response.data["id"]
industry = Industry.objects.get(id=industry_id)
request = self.factory.patch(f"industries/{industry.pk}/", {"name": ""})
force_authenticate(request, user=user)
response = self.industry_detail_view(request, pk=industry.pk)
self.assertEqual(response.status_code, 400)
def _user_create(self) -> CustomUser:
request = self.factory.post("auth/users/", USER_CREATE_DATA)
response = self.user_list_view(request)
user_id = response.data["id"]
user = CustomUser.objects.get(id=user_id)
user.is_active = True
user.is_staff = True
user.save()
return user