|
1
|
|
|
import pytest |
|
2
|
|
|
from django.urls import reverse |
|
3
|
|
|
from rest_framework import status |
|
4
|
|
|
|
|
5
|
|
|
from goals.models import BoardParticipant, Category, Goal |
|
6
|
|
|
from tests.utils import BaseTestCase |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
@pytest.fixture() |
|
10
|
|
|
def another_user(user_factory): |
|
11
|
|
|
return user_factory.create() |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
View Code Duplication |
@pytest.mark.django_db() |
|
|
|
|
|
|
15
|
|
|
class BoardTestCase(BaseTestCase): |
|
16
|
|
|
|
|
17
|
|
|
@pytest.fixture(autouse=True) |
|
18
|
|
|
def set_url(self, board_factory, user): # noqa: PT004 |
|
19
|
|
|
self.board = board_factory.create(with_owner=user) |
|
20
|
|
|
self.url = reverse('goals:board-detail', args=[self.board.id]) |
|
21
|
|
|
|
|
22
|
|
|
def test_auth_required(self, client): |
|
23
|
|
|
"""Тест на эндпоинт [GET, PATCH, DELETE]: /goals/board/<id> |
|
24
|
|
|
|
|
25
|
|
|
Производит проверку требований аутентификации. |
|
26
|
|
|
""" |
|
27
|
|
|
response = getattr(client, self.method)(self.url, {}) |
|
28
|
|
|
assert response.status_code == status.HTTP_403_FORBIDDEN |
|
29
|
|
|
|
|
30
|
|
|
def test_user_not_board_participant(self, client, another_user): |
|
31
|
|
|
"""Тест на эндпоинт [GET, PATCH, DELETE]: /goals/board/<id> |
|
32
|
|
|
|
|
33
|
|
|
Производит проверку отображения только той доски, |
|
34
|
|
|
в которой пользователь является участником |
|
35
|
|
|
""" |
|
36
|
|
|
assert not self.board.participants.filter(user=another_user).count() |
|
37
|
|
|
|
|
38
|
|
|
client.force_login(another_user) |
|
39
|
|
|
response = getattr(client, self.method)(self.url) |
|
40
|
|
|
assert response.status_code == status.HTTP_404_NOT_FOUND |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
class TestBoardRetrieve(BoardTestCase): |
|
44
|
|
|
method = 'get' |
|
45
|
|
|
|
|
46
|
|
|
def test_success(self, auth_client, user): |
|
47
|
|
|
"""Тест на эндпоинт GET: /goals/board/<id> |
|
48
|
|
|
|
|
49
|
|
|
Производит проверку корректности структуры ответа при успешном запросе доски. |
|
50
|
|
|
""" |
|
51
|
|
|
response = auth_client.get(self.url) |
|
52
|
|
|
assert response.status_code == status.HTTP_200_OK |
|
53
|
|
|
|
|
54
|
|
|
assert self.board.participants.count() == 1 |
|
55
|
|
|
participant: BoardParticipant = self.board.participants.last() |
|
56
|
|
|
assert participant.user == user |
|
57
|
|
|
|
|
58
|
|
|
assert response.json() == { # noqa: ECE001 |
|
59
|
|
|
'id': self.board.id, |
|
60
|
|
|
'participants': [ |
|
61
|
|
|
{ |
|
62
|
|
|
'id': participant.id, |
|
63
|
|
|
'role': BoardParticipant.Role.owner.value, |
|
64
|
|
|
'user': user.username, |
|
65
|
|
|
'created': self.datetime_to_str(participant.created), |
|
66
|
|
|
'updated': self.datetime_to_str(participant.created), |
|
67
|
|
|
'board': participant.board_id |
|
68
|
|
|
} |
|
69
|
|
|
], |
|
70
|
|
|
'created': self.datetime_to_str(self.board.created), |
|
71
|
|
|
'updated': self.datetime_to_str(self.board.updated), |
|
72
|
|
|
'title': self.board.title, |
|
73
|
|
|
'is_deleted': False |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
class TestBoardUpdate(BoardTestCase): |
|
78
|
|
|
method = 'patch' |
|
79
|
|
|
|
|
80
|
|
|
@pytest.fixture(autouse=True) |
|
81
|
|
|
def setup(self): # noqa: PT004 |
|
82
|
|
|
self.participant: BoardParticipant = self.board.participants.last() |
|
83
|
|
|
|
|
84
|
|
|
@pytest.mark.parametrize( |
|
85
|
|
|
'user_role', |
|
86
|
|
|
[BoardParticipant.Role.writer, BoardParticipant.Role.reader], |
|
87
|
|
|
ids=['writer', 'reader'] |
|
88
|
|
|
) |
|
89
|
|
|
def test_failed_update_board_by_reader_or_writer(self, faker, user_role, auth_client): |
|
90
|
|
|
"""Тест на эндпоинт PATCH: /goals/board/<id> |
|
91
|
|
|
|
|
92
|
|
|
Производит проверку отсутствия у пользователя возможности редактировать доску, |
|
93
|
|
|
в которой он является редактором или читателем. |
|
94
|
|
|
""" |
|
95
|
|
|
self.participant.role = user_role |
|
96
|
|
|
self.participant.save(update_fields=('role',)) |
|
97
|
|
|
assert self.participant.role == user_role |
|
98
|
|
|
|
|
99
|
|
|
response = auth_client.patch(self.url, faker.pydict(1)) |
|
100
|
|
|
assert response.status_code == status.HTTP_403_FORBIDDEN |
|
101
|
|
|
|
|
102
|
|
|
def test_success_update_board_by_owner(self, auth_client, faker): |
|
103
|
|
|
"""Тест на эндпоинт PATCH: /goals/board/<id> |
|
104
|
|
|
|
|
105
|
|
|
Производит проверку наличия у пользователя возможности редактировать доску, |
|
106
|
|
|
в которой он является автором. |
|
107
|
|
|
""" |
|
108
|
|
|
assert self.participant.role == BoardParticipant.Role.owner |
|
109
|
|
|
new_title = faker.sentence() |
|
110
|
|
|
|
|
111
|
|
|
response = auth_client.patch(self.url, {'title': new_title}) |
|
112
|
|
|
assert response.status_code == status.HTTP_200_OK |
|
113
|
|
|
|
|
114
|
|
|
self.board.refresh_from_db(fields=('title',)) |
|
115
|
|
|
assert self.board.title == new_title |
|
116
|
|
|
|
|
117
|
|
|
|
|
118
|
|
View Code Duplication |
class TestBoardDestroy(BoardTestCase): |
|
|
|
|
|
|
119
|
|
|
method = 'delete' |
|
120
|
|
|
|
|
121
|
|
|
@pytest.fixture(autouse=True) |
|
122
|
|
|
def setup(self, category_factory, goal_factory, user): # noqa: PT004 |
|
123
|
|
|
self.cat: Category = category_factory.create(board=self.board, user=user) |
|
124
|
|
|
self.goal: Goal = goal_factory.create(category=self.cat, user=user) |
|
125
|
|
|
self.participant: BoardParticipant = self.board.participants.last() |
|
126
|
|
|
|
|
127
|
|
|
@pytest.mark.parametrize( |
|
128
|
|
|
'user_role', |
|
129
|
|
|
[BoardParticipant.Role.writer, BoardParticipant.Role.reader], |
|
130
|
|
|
ids=['writer', 'reader'] |
|
131
|
|
|
) |
|
132
|
|
|
def test_failed_delete_board_by_reader_or_writer(self, auth_client, user_role): |
|
133
|
|
|
"""Тест на эндпоинт DELETE: /goals/board/<id> |
|
134
|
|
|
|
|
135
|
|
|
Производит проверку отсутствия у пользователя возможности удалить доску, |
|
136
|
|
|
в которой он является редактором или читателем. |
|
137
|
|
|
""" |
|
138
|
|
|
self.participant.role = user_role |
|
139
|
|
|
self.participant.save(update_fields=('role',)) |
|
140
|
|
|
|
|
141
|
|
|
response = auth_client.delete(self.url) |
|
142
|
|
|
assert response.status_code == status.HTTP_403_FORBIDDEN |
|
143
|
|
|
|
|
144
|
|
|
def test_success_delete_board_by_owner(self, auth_client): |
|
145
|
|
|
"""Тест на эндпоинт DELETE: /goals/board/<id> |
|
146
|
|
|
|
|
147
|
|
|
Производит проверку наличия у пользователя возможности удалить доску, |
|
148
|
|
|
в которой он является автором. А так же выполнение следующих условий: |
|
149
|
|
|
- доска: is_deleted == True |
|
150
|
|
|
- категория: is_deleted == True |
|
151
|
|
|
- цель: Status == archived |
|
152
|
|
|
""" |
|
153
|
|
|
assert self.participant.role == BoardParticipant.Role.owner |
|
154
|
|
|
|
|
155
|
|
|
response = auth_client.delete(self.url) |
|
156
|
|
|
assert response.status_code == status.HTTP_204_NO_CONTENT |
|
157
|
|
|
|
|
158
|
|
|
self.board.refresh_from_db(fields=('is_deleted',)) |
|
159
|
|
|
self.cat.refresh_from_db(fields=('is_deleted',)) |
|
160
|
|
|
self.goal.refresh_from_db(fields=('status',)) |
|
161
|
|
|
assert self.board.is_deleted |
|
162
|
|
|
assert self.cat.is_deleted |
|
163
|
|
|
assert self.goal.status == Goal.Status.archived |
|
164
|
|
|
|