Repository navigation
Expand file tree
/
Copy pathtest_invitations.py
More file actions
163 lines (128 loc) · 5.92 KB
/
Copy pathtest_invitations.py
File metadata and controls
163 lines (128 loc) · 5.92 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
# Copyright (C) 2021-2022 Intel Corporation
# Copyright (C) CVAT.ai Corporation
#
# SPDX-License-Identifier: MIT
import json
from http import HTTPStatus
import pytest
from cvat_sdk.api_client.api_client import ApiClient, Endpoint
from deepdiff import DeepDiff
from shared.utils.config import get_method, make_api_client, post_method
from .utils import CollectionSimpleFilterTestBase
class TestCreateInvitations:
ROLES = ["worker", "supervisor", "maintainer", "owner"]
@pytest.fixture(autouse=True)
def setup(self, restore_db_per_function, organizations, memberships, admin_user):
self.org_id = 2
self.owner = self.get_member("owner", memberships, self.org_id)
def _test_post_invitation_201(self, user, data, invitee, **kwargs):
response = post_method(user, "invitations", data, **kwargs)
assert response.status_code == HTTPStatus.CREATED, response.content
assert data["role"] == response.json()["role"]
assert invitee["id"] == response.json()["user"]["id"]
assert kwargs["org_id"] == response.json()["organization"]
def _test_post_invitation_403(self, user, data, **kwargs):
response = post_method(user, "invitations", data, **kwargs)
assert response.status_code == HTTPStatus.FORBIDDEN, response.content
assert "You do not have permission" in str(response.content)
@staticmethod
def get_non_member_users(memberships, users):
organization_users = set(m["user"]["id"] for m in memberships if m["user"] is not None)
non_member_users = [u for u in users if u["id"] not in organization_users]
return non_member_users
@staticmethod
def get_member(role, memberships, org_id):
member = [
m["user"]
for m in memberships
if m["role"] == role and m["organization"] == org_id and m["user"] is not None
][0]
return member
@pytest.mark.parametrize("org_role", ROLES)
@pytest.mark.parametrize("invitee_role", ROLES)
def test_create_invitation(self, organizations, memberships, users, org_role, invitee_role):
org_id = self.org_id
inviter_user = self.get_member(org_role, memberships, org_id)
invitee_user = self.get_non_member_users(memberships, users)[0]
if org_role in ["worker", "supervisor"]:
self._test_post_invitation_403(
inviter_user["username"],
{"role": invitee_role, "email": invitee_user["email"]},
org_id=org_id,
)
elif invitee_role in ["worker", "supervisor"]:
self._test_post_invitation_201(
inviter_user["username"],
{"role": invitee_role, "email": invitee_user["email"]},
invitee_user,
org_id=org_id,
)
elif invitee_role == "maintainer":
if org_role == "owner":
# only the owner can invite a maintainer
self._test_post_invitation_201(
inviter_user["username"],
{"role": invitee_role, "email": invitee_user["email"]},
invitee_user,
org_id=org_id,
)
else:
self._test_post_invitation_403(
inviter_user["username"],
{"role": invitee_role, "email": invitee_user["email"]},
org_id=org_id,
)
elif invitee_role == "owner":
# nobody can invite an owner
self._test_post_invitation_403(
inviter_user["username"],
{"role": invitee_role, "email": invitee_user["email"]},
org_id=org_id,
)
else:
assert False, "Unknown role"
class TestInvitationsListFilters(CollectionSimpleFilterTestBase):
field_lookups = {
"owner": ["owner", "username"],
}
@pytest.fixture(autouse=True)
def setup(self, restore_db_per_class, admin_user, invitations):
self.user = admin_user
self.samples = invitations
def _get_endpoint(self, api_client: ApiClient) -> Endpoint:
return api_client.invitations_api.list_endpoint
@pytest.mark.parametrize(
"field",
("owner",),
)
def test_can_use_simple_filter_for_object_list(self, field):
return super()._test_can_use_simple_filter_for_object_list(field)
@pytest.mark.usefixtures("restore_db_per_class")
class TestListInvitations:
def _test_can_see_invitations(self, user, data, **kwargs):
response = get_method(user, "invitations", **kwargs)
assert response.status_code == HTTPStatus.OK
assert DeepDiff(data, response.json()["results"]) == {}
def test_admin_can_see_all_invitations(self, invitations):
self._test_can_see_invitations("admin2", invitations.raw, page_size="all")
@pytest.mark.parametrize("field_value, query_value", [(1, 1), (None, "")])
def test_can_filter_by_org_id(self, field_value, query_value, invitations):
invitations = filter(lambda i: i["organization"] == field_value, invitations)
self._test_can_see_invitations(
"admin2", list(invitations), page_size="all", org_id=query_value
)
@pytest.mark.usefixtures("restore_db_per_class")
class TestGetInvitations:
def test_can_remove_owner_and_fetch_with_sdk(self, admin_user, invitations):
# test for API schema regressions
source_inv = next(
invitations
for invitations in invitations
if invitations.get("owner") and invitations["owner"]["username"] != admin_user
).copy()
with make_api_client(admin_user) as api_client:
api_client.users_api.destroy(source_inv["owner"]["id"])
(_, response) = api_client.invitations_api.retrieve(source_inv["key"])
fetched_inv = json.loads(response.data)
source_inv["owner"] = None
assert DeepDiff(source_inv, fetched_inv, ignore_order=True) == {}