forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_invite_user_to_org.py
More file actions
261 lines (224 loc) · 6.57 KB
/
Copy pathtest_invite_user_to_org.py
File metadata and controls
261 lines (224 loc) · 6.57 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import logging
import pytest
import pytest_mock
from pytest import fail
from db import DB
from models import Organizations, Users, User_affiliations
from json_web_token import tokenize
from tests.test_functions import json, run
@pytest.fixture()
def db():
s, cleanup, db_session = DB()
yield s, db_session
cleanup()
def test_successful_invite_existing_user_to_org_existing_user(caplog, db, mocker):
"""
Test that an admin can successfully invite a user to their organization
"""
save, session = db
mocker.patch(
"schemas.invite_user_to_org.invite_user_to_org.send_invite_to_org_notification_email",
autospec=True,
return_value=True,
)
org_one = Organizations(
acronym="ORG1", name="Organization 1", slug="organization-1"
)
save(org_one)
user = Users(
display_name="testuser",
user_name="testuser@testemail.ca",
password="testpassword123",
preferred_lang="English",
tfa_validated=False,
)
save(user)
admin = Users(
display_name="testadmin",
user_name="testadmin@testemail.ca",
password="testpassword123",
preferred_lang="English",
tfa_validated=False,
)
save(admin)
admin_affiliation = User_affiliations(
user_id=admin.id, organization_id=org_one.id, permission="admin",
)
save(admin_affiliation)
caplog.set_level(logging.INFO)
result = run(
mutation=f"""
mutation {{
inviteUserToOrg (
input: {{
userName: "{user.user_name}"
orgSlug: "{org_one.slug}"
requestedRole: USER_READ
preferredLanguage: ENGLISH
}}
) {{
status
}}
}}
""",
as_user=admin,
)
if "errors" in result:
fail(
"Expected successful invitation to organization, instead: {}".format(
json(result)
)
)
expected_result = {
"data": {
"inviteUserToOrg": {
"status": "Successfully invited user to organization, and sent notification email."
}
}
}
affiliation = (
session.query(User_affiliations)
.filter(User_affiliations.user_id == user.id)
.filter(User_affiliations.organization_id == org_one.id)
.filter(User_affiliations.permission == "user_read")
.first()
)
assert affiliation is not None
assert result == expected_result
assert (
f"User: {admin.id} successfully added {user.id} to {org_one.slug}."
in caplog.text
)
def test_un_successful_invite_existing_user_to_org_user_does_not_have_admin_existing_user(
caplog, db, mocker
):
"""
Test that a user without admin cannot invite a user
"""
save, session = db
mocker.patch(
"schemas.invite_user_to_org.invite_user_to_org.send_invite_to_org_notification_email",
autospec=True,
return_value=True,
)
org_one = Organizations(
acronym="ORG1", name="Organization 1", slug="organization-1"
)
save(org_one)
user = Users(
display_name="testuser",
user_name="testuser@testemail.ca",
password="testpassword123",
preferred_lang="English",
tfa_validated=False,
)
save(user)
user_2 = Users(
display_name="testuser2",
user_name="testuser2@testemail.ca",
password="testpassword123",
preferred_lang="English",
tfa_validated=False,
)
save(user_2)
user_2_affiliation = User_affiliations(
user_id=user_2.id, organization_id=org_one.id, permission="user_read",
)
save(user_2_affiliation)
caplog.set_level(logging.INFO)
result = run(
mutation=f"""
mutation {{
inviteUserToOrg (
input: {{
userName: "{user.user_name}"
orgSlug: "{org_one.slug}"
requestedRole: USER_READ
preferredLanguage: ENGLISH
}}
) {{
status
}}
}}
""",
as_user=user_2,
)
if "errors" not in result:
fail(
"Expected permission error when non admin tries to invite user, instead: {}".format(
json(result)
)
)
[error] = result["errors"]
assert (
error["message"]
== "Error, you do not have access to invite users this organization."
)
assert (
f"User: {user_2.id} attempted to invite user to organization-1, but does not have admin access."
in caplog.text
)
def test_successful_invite_non_existing_user_to_org_existing_user(caplog, db, mocker):
"""
Test that an admin can successfully invite a user to their organization,
and the service
"""
save, session = db
mocker.patch(
"schemas.invite_user_to_org.invite_user_to_org.send_invite_to_service_email",
autospec=True,
return_value=True,
)
org_one = Organizations(
acronym="ORG1", name="Organization 1", slug="organization-1"
)
save(org_one)
admin = Users(
display_name="testadmin",
user_name="testadmin@testemail.ca",
password="testpassword123",
preferred_lang="English",
tfa_validated=False,
)
save(admin)
admin_affiliation = User_affiliations(
user_id=admin.id, organization_id=org_one.id, permission="admin",
)
save(admin_affiliation)
user_name = "testuser@testemail.ca"
caplog.set_level(logging.INFO)
result = run(
mutation=f"""
mutation {{
inviteUserToOrg (
input: {{
userName: "{user_name}"
orgSlug: "{org_one.slug}"
requestedRole: USER_READ
preferredLanguage: ENGLISH
}}
) {{
status
}}
}}
""",
as_user=admin,
)
if "errors" in result:
fail(
"Expected successful invitation to organization, instead: {}".format(
json(result)
)
)
expected_result = {
"data": {
"inviteUserToOrg": {
"status": "Successfully sent invitation to service, and organization email."
}
}
}
assert result == expected_result
assert (
f"User: {admin.id}, sent an invitation email to {user_name}, for the {org_one.slug} organization."
in caplog.text
)