-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathtest_send_grant_reply.py
More file actions
150 lines (105 loc) · 4.9 KB
/
test_send_grant_reply.py
File metadata and controls
150 lines (105 loc) · 4.9 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
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
from unittest.mock import ANY
import pytest
from django.contrib.admin.models import LogEntry
from grants.models import Grant
from grants.tests.factories import GrantFactory
from users.tests.factories import UserFactory
pytestmark = pytest.mark.django_db
def _send_grant_reply(graphql_client, grant, *, status, message=""):
document = """
mutation sendGrantReply ($input: SendGrantReplyInput!) {
sendGrantReply(input: $input) {
__typename
...on Grant {
id
}
...on SendGrantReplyError {
message
}
}
}
"""
variables = {
"status": status,
"instance": grant.id,
}
return graphql_client.query(document, variables={"input": variables})
def test_user_is_not_the_owner(graphql_client, user):
graphql_client.force_login(user)
other_user = UserFactory()
grant = GrantFactory(user_id=other_user.id)
response = _send_grant_reply(graphql_client, grant, status="refused")
assert response["data"]["sendGrantReply"]["__typename"] == "SendGrantReplyError"
assert (
response["data"]["sendGrantReply"]["message"]
== "You cannot reply to this grant"
)
def test_user_cannot_reply_if_status_is_pending(graphql_client, user):
graphql_client.force_login(user)
grant = GrantFactory(user_id=user.id, status=Grant.Status.pending)
response = _send_grant_reply(graphql_client, grant, status="refused")
assert response["data"]["sendGrantReply"]["__typename"] == "SendGrantReplyError"
assert (
response["data"]["sendGrantReply"]["message"]
== "You cannot reply to this grant"
)
def test_user_cannot_reply_if_status_is_rejected(graphql_client, user):
graphql_client.force_login(user)
grant = GrantFactory(user_id=user.id, status=Grant.Status.rejected)
response = _send_grant_reply(graphql_client, grant, status="refused")
assert response["data"]["sendGrantReply"]["__typename"] == "SendGrantReplyError"
assert (
response["data"]["sendGrantReply"]["message"]
== "You cannot reply to this grant"
)
def test_status_is_updated_when_reply_is_confirmed(graphql_client, user):
graphql_client.force_login(user)
grant = GrantFactory(user_id=user.id, status=Grant.Status.waiting_for_confirmation)
response = _send_grant_reply(graphql_client, grant, status="confirmed")
assert response["data"]["sendGrantReply"]["__typename"] == "Grant"
grant.refresh_from_db()
assert grant.status == Grant.Status.confirmed
# Verify audit log entry was created correctly
assert LogEntry.objects.filter(
user=user,
object_id=grant.id,
change_message="Grantee has replied with status confirmed.",
).exists()
def test_status_is_updated_when_reply_is_refused(graphql_client, user):
graphql_client.force_login(user)
grant = GrantFactory(user_id=user.id, status=Grant.Status.waiting_for_confirmation)
response = _send_grant_reply(graphql_client, grant, status="refused")
assert response["data"]["sendGrantReply"]["__typename"] == "Grant"
grant.refresh_from_db()
assert grant.status == Grant.Status.refused
# Verify audit log entry was created correctly
assert LogEntry.objects.filter(
user=user,
object_id=grant.id,
change_message="Grantee has replied with status refused.",
).exists()
def test_call_notify_new_grant_reply(graphql_client, user, mocker):
graphql_client.force_login(user)
grant = GrantFactory(user_id=user.id, status=Grant.Status.waiting_for_confirmation)
mock_publisher = mocker.patch("api.grants.mutations.notify_new_grant_reply_slack")
response = _send_grant_reply(graphql_client, grant, status="refused", message="wtf")
assert response["data"]["sendGrantReply"]["__typename"] == "Grant"
mock_publisher.delay.assert_called_once_with(grant_id=grant.id, admin_url=ANY)
def test_create_voucher_when_grant_is_confirmed(graphql_client, user, mocker):
graphql_client.force_login(user)
grant = GrantFactory(user_id=user.id, status=Grant.Status.waiting_for_confirmation)
mock_voucher_task = mocker.patch(
"api.grants.mutations.create_and_send_grant_voucher"
)
response = _send_grant_reply(graphql_client, grant, status="confirmed")
assert response["data"]["sendGrantReply"]["__typename"] == "Grant"
mock_voucher_task.delay.assert_called_once_with(grant_id=grant.id)
def test_voucher_not_created_when_grant_is_refused(graphql_client, user, mocker):
graphql_client.force_login(user)
grant = GrantFactory(user_id=user.id, status=Grant.Status.waiting_for_confirmation)
mock_voucher_task = mocker.patch(
"api.grants.mutations.create_and_send_grant_voucher"
)
response = _send_grant_reply(graphql_client, grant, status="refused")
assert response["data"]["sendGrantReply"]["__typename"] == "Grant"
mock_voucher_task.delay.assert_not_called()