Debian Codegraph @

tests/functional/services/groups/test_group_memberships.py

raw
1 import json
2 import uuid
3
4 import pytest
5
6 from globus_sdk import BatchMembershipActions, GroupRole
7 from globus_sdk.testing import RegisteredResponse, get_last_request, load_response
8 from tests.common import register_api_route_fixture_file
9
10
11 def test_approve_pending(groups_manager):
12 register_api_route_fixture_file(
13 "groups",
14 "/v2/groups/d3974728-6458-11e4-b72d-123139141556",
15 "approve_pending.json",
16 method="POST",
17 )
18
19 res = groups_manager.approve_pending(
20 "d3974728-6458-11e4-b72d-123139141556", "ae332d86-d274-11e5-b885-b31714a110e9"
21 )
22 assert res.http_status == 200
23
24 data = res.data
25 assert isinstance(data, dict)
26 assert "approve" in data
27 assert data["approve"][0]["status"] == "active"
28
29
30 @pytest.mark.parametrize("role", (GroupRole.admin, GroupRole.member, "member", "admin"))
31 def test_add_member(groups_manager, role):
32 rolestr = role if isinstance(role, str) else role.value
33 register_api_route_fixture_file(
34 "groups",
35 "/v2/groups/d3974728-6458-11e4-b72d-123139141556",
36 "add_member.json",
37 method="POST",
38 )
39
40 res = groups_manager.add_member(
41 "d3974728-6458-11e4-b72d-123139141556",
42 "ae332d86-d274-11e5-b885-b31714a110e9",
43 role=role,
44 )
45 assert res.http_status == 200
46
47 data = res.data
48 assert isinstance(data, dict)
49 assert "add" in data
50 assert data["add"][0]["status"] == "active"
51 # FIXME: this should be the line
52 # assert data["add"][0]["role"] == rolestr
53 # but the response is fixed right now
54 assert data["add"][0]["role"] == "admin"
55
56 req = get_last_request()
57 req_body = json.loads(req.body)
58 assert req_body["add"][0]["role"] == rolestr
59
60
61 @pytest.mark.parametrize("role", (GroupRole.admin, GroupRole.member, "member", "admin"))
62 def test_batch_action_payload(groups_client, role):
63 group_id = str(uuid.uuid1())
64 load_response(
65 RegisteredResponse(
66 service="groups", method="POST", path=f"/v2/groups/{group_id}", json={}
67 )
68 )
69 rolestr = role if isinstance(role, str) else role.value
70
71 batch_action = (
72 BatchMembershipActions()
73 .accept_invites(uuid.uuid1())
74 .add_members(
75 [uuid.uuid1(), uuid.uuid1()],
76 role=role,
77 )
78 .change_roles("admin", [uuid.uuid1(), uuid.uuid1()])
79 .invite_members([uuid.uuid1(), uuid.uuid1()])
80 .join([uuid.uuid1(), uuid.uuid1()])
81 )
82
83 assert "add" in batch_action
84 assert len(batch_action["add"]) == 2
85 assert all(member["role"] == role for member in batch_action["add"])
86
87 assert "accept" in batch_action
88 assert len(batch_action["accept"]) == 1
89
90 assert "change_role" in batch_action
91 assert len(batch_action["change_role"]) == 2
92 for change_role in batch_action["change_role"]:
93 assert change_role["role"] == "admin"
94
95 assert "invite" in batch_action
96 assert len(batch_action["invite"]) == 2
97
98 assert "join" in batch_action
99 assert len(batch_action["invite"]) == 2
100
101 # send the request and confirm that the data is serialized correctly
102 groups_client.batch_membership_action(group_id, batch_action)
103 req = get_last_request()
104 req_body = json.loads(req.body)
105 # role should be stringified if it was an enum member
106 assert all(member["role"] == rolestr for member in req_body["add"])
107 # UUIDs should have been stringified
108 for action in ["add", "accept", "invite", "join"]:
109 assert all(isinstance(value["identity_id"], str) for value in req_body[action])