Skip to content

Commit 2bbe2ba

Browse files
committed
test: add unit tests for job_token_scope
Signed-off-by: Tim Knight <tim.knight1@engineering.digital.dwp.gov.uk>
1 parent eabc8ef commit 2bbe2ba

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

gitlab/v4/objects/job_token_scope.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class AllowlistedGroup(ObjectDeleteMixin, RESTObject):
6262

6363
def get_id(self) -> int:
6464
"""Returns the id of the resource. This override deals with
65-
the fact that either an `id` or a `target_project_id` attribute
65+
the fact that either an `id` or a `target_group_id` attribute
6666
is returned by the server depending on the endpoint called."""
6767
try:
6868
return cast(int, getattr(self, self._id_attr))

tests/unit/objects/test_job_token_scope.py

+52
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@
4747
}
4848
]
4949

50+
project_allowlist_created_content = {
51+
"target_project_id": 2,
52+
"project_id": 1,
53+
}
54+
5055
groups_allowlist_content = [
5156
{
5257
"id": 4,
@@ -55,6 +60,11 @@
5560
}
5661
]
5762

63+
group_allowlist_created_content = {
64+
"target_group_id": 4,
65+
"project_id": 1,
66+
}
67+
5868

5969
@pytest.fixture
6070
def resp_get_job_token_scope():
@@ -82,6 +92,19 @@ def resp_get_allowlist():
8292
yield rsps
8393

8494

95+
@pytest.fixture
96+
def resp_add_to_allowlist():
97+
with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps:
98+
rsps.add(
99+
method=responses.POST,
100+
url="http://localhost/api/v4/projects/1/job_token_scope/allowlist",
101+
json=project_allowlist_created_content,
102+
content_type="application/json",
103+
status=200,
104+
)
105+
yield rsps
106+
107+
85108
@pytest.fixture
86109
def resp_get_groups_allowlist():
87110
with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps:
@@ -95,6 +118,19 @@ def resp_get_groups_allowlist():
95118
yield rsps
96119

97120

121+
@pytest.fixture
122+
def resp_add_to_groups_allowlist():
123+
with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps:
124+
rsps.add(
125+
method=responses.POST,
126+
url="http://localhost/api/v4/projects/1/job_token_scope/groups_allowlist",
127+
json=group_allowlist_created_content,
128+
content_type="application/json",
129+
status=200,
130+
)
131+
yield rsps
132+
133+
98134
@pytest.fixture
99135
def resp_patch_job_token_scope():
100136
with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps:
@@ -140,9 +176,25 @@ def test_get_projects_allowlist(job_token_scope, resp_get_allowlist):
140176
assert isinstance(allowlist_content, list)
141177

142178

179+
def test_add_project_to_allowlist(job_token_scope, resp_add_to_allowlist):
180+
allowlist = job_token_scope.allowlist
181+
assert isinstance(allowlist, AllowlistedProjectManager)
182+
183+
resp = allowlist.create({"target_project_id": 2})
184+
assert resp.get_id() == 2
185+
186+
143187
def test_get_groups_allowlist(job_token_scope, resp_get_groups_allowlist):
144188
allowlist = job_token_scope.groups_allowlist
145189
assert isinstance(allowlist, AllowlistedGroupManager)
146190

147191
allowlist_content = allowlist.list()
148192
assert isinstance(allowlist_content, list)
193+
194+
195+
def test_add_group_to_allowlist(job_token_scope, resp_add_to_groups_allowlist):
196+
allowlist = job_token_scope.groups_allowlist
197+
assert isinstance(allowlist, AllowlistedGroupManager)
198+
199+
resp = allowlist.create({"target_group_id": 4})
200+
assert resp.get_id() == 4

0 commit comments

Comments
 (0)