-
Notifications
You must be signed in to change notification settings - Fork 20
/
test_article_vote_view.py
153 lines (129 loc) · 4.74 KB
/
test_article_vote_view.py
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
from http import HTTPStatus
from uuid import UUID
from rest_framework.response import Response
from rest_framework.test import APIRequestFactory
from myapp.application.adapter.api.http.article_vote_view import ArticleVoteView
from myapp.application.domain.model.identifier.article_id import ArticleId
from myapp.application.domain.model.identifier.user_id import UserId
from myapp.application.domain.model.vote import Vote
from myapp.application.domain.model.vote_for_article_result import (
AlreadyVotedResult,
InsufficientKarmaResult,
SuccessfullyVotedResult,
VoteForArticleResult
)
from myapp.application.port.api.command.vote_for_article_command import (
VoteForArticleCommand
)
from myapp.application.port.api.vote_for_article_use_case import (
VoteForArticleUseCase
)
def test_successfully_vote_for_an_article(
arf: APIRequestFactory
):
article_vote_view = ArticleVoteView.as_view(
vote_for_article_use_case=VoteForArticleUseCaseSuccessfullyVotedStub()
)
response: Response = article_vote_view(
arf.post(
'/article_vote',
{
'article_id': ArticleId(UUID('3f577757-0000-0000-0000-000000000000')),
'user_id': UserId(UUID('9af8961e-0000-0000-0000-000000000000')),
'vote': Vote.DOWN.value
},
format='json'
)
)
assert response.status_code == HTTPStatus.CREATED
assert response.data == {
'article_id': '3f577757-0000-0000-0000-000000000000',
'user_id': '9af8961e-0000-0000-0000-000000000000',
'vote': 'DOWN'
}
def test_post_article_vote_with_malformed_data_returns_bad_request(
arf: APIRequestFactory,
a_user_id: UserId,
an_article_id: ArticleId
):
article_vote_view = ArticleVoteView.as_view(
vote_for_article_use_case=VoteForArticleUseCaseSuccessfullyVotedStub()
)
response: Response = article_vote_view(
arf.post(
'/article_vote',
{
'user_id': str(a_user_id),
'article_id': str(an_article_id)
},
format='json'
)
)
assert response.status_code == HTTPStatus.BAD_REQUEST
def test_user_with_insufficient_karma_votes_for_article_returns_bad_request(
arf: APIRequestFactory,
an_article_id: ArticleId
):
article_vote_view = ArticleVoteView.as_view(
vote_for_article_use_case=VoteForArticleUseCaseInsufficientKarmaStub()
)
response: Response = article_vote_view(
arf.post(
'/article_vote',
{
'user_id': UserId(UUID('2e8a5b4e-0000-0000-0000-000000000000')),
'article_id': an_article_id,
'vote': Vote.UP.value
},
format='json'
)
)
assert response.status_code == HTTPStatus.BAD_REQUEST
assert response.data == {
'status': 400,
'detail': "User 2e8a5b4e-0000-0000-0000-000000000000 does not have enough "
"karma to vote for an article",
'title': "Cannot vote for an article"
}
def test_user_votes_for_the_same_article_returns_conflict(
arf: APIRequestFactory
):
article_vote_view = ArticleVoteView.as_view(
vote_for_article_use_case=VoteForArticleUseCaseAlreadyVotedStub()
)
response: Response = article_vote_view(
arf.post(
'/article_vote',
{
'user_id': UserId(UUID('a3854820-0000-0000-0000-000000000000')),
'article_id': ArticleId(UUID('dd494bd6-0000-0000-0000-000000000000')),
'vote': Vote.UP.value
},
format='json'
)
)
assert response.status_code == HTTPStatus.CONFLICT
assert response.data == {
'status': 409,
'detail': "User \"a3854820-0000-0000-0000-000000000000\" has already voted"
" for article \"dd494bd6-0000-0000-0000-000000000000\"",
'title': "Cannot vote for an article"
}
class VoteForArticleUseCaseInsufficientKarmaStub(VoteForArticleUseCase):
def vote_for_article(self, command: VoteForArticleCommand) -> VoteForArticleResult:
return InsufficientKarmaResult(
user_id=command.user_id
)
class VoteForArticleUseCaseSuccessfullyVotedStub(VoteForArticleUseCase):
def vote_for_article(self, command: VoteForArticleCommand) -> VoteForArticleResult:
return SuccessfullyVotedResult(
command.article_id,
command.user_id,
command.vote
)
class VoteForArticleUseCaseAlreadyVotedStub(VoteForArticleUseCase):
def vote_for_article(self, command: VoteForArticleCommand) -> VoteForArticleResult:
return AlreadyVotedResult(
user_id=command.user_id,
article_id=command.article_id
)