Skip to content

Commit

Permalink
Remove client URL assignment in tests (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
pederhan authored Jun 17, 2024
1 parent 317d0e0 commit 2fc378a
Show file tree
Hide file tree
Showing 36 changed files with 175 additions and 204 deletions.
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ def httpserver(httpserver: HTTPServer) -> Iterable[HTTPServer]:

# must be set to "function" to make sure logging is enabled for each test
@pytest.fixture(scope="function")
def async_client(httpserver: HTTPServer) -> HarborAsyncClient:
return HarborAsyncClient(
def async_client(httpserver: HTTPServer) -> Iterable[HarborAsyncClient]:
yield HarborAsyncClient(
username="username",
secret="secret",
url=httpserver.url_for("/api/v2.0"),
Expand Down
22 changes: 8 additions & 14 deletions tests/endpoints/test_artifacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ async def test_create_artifact_tag_mock(
status_code: int,
tag: Tag,
):
async_client.url = httpserver.url_for("/api/v2.0")
expect_location = (
async_client.url
+ "/api/v2.0/projects/testproj/repositories/testrepo/artifacts/latest/tags/123"
Expand Down Expand Up @@ -76,7 +75,7 @@ async def test_get_artifact_vulnerabilities_mock(
),
headers={"Content-Type": "application/json"},
)
async_client.url = httpserver.url_for("/api/v2.0")

r = await async_client.get_artifact_vulnerabilities(
"testproj", "testrepo", "latest"
)
Expand All @@ -99,7 +98,7 @@ async def test_get_artifact_build_history_mock(
json_from_list(build_history),
content_type="application/json",
)
async_client.url = httpserver.url_for("/api/v2.0")

resp = await async_client.get_artifact_build_history(
"testproj", "testrepo", "latest"
)
Expand All @@ -120,7 +119,6 @@ async def test_get_artifact_vulnerabilities_empty_mock(
"/api/v2.0/projects/testproj/repositories/testrepo/artifacts/latest/additions/vulnerabilities",
method="GET",
).respond_with_json({})
async_client.url = httpserver.url_for("/api/v2.0")

with pytest.raises(NotFound):
await async_client.get_artifact_vulnerabilities(
Expand All @@ -144,7 +142,6 @@ async def test_get_artifact_vulnerabilities_nondict_mock(
"/api/v2.0/projects/testproj/repositories/testrepo/artifacts/latest/additions/vulnerabilities",
method="GET",
).respond_with_json([{"MIME_TYPE_HERE": {"foo": "bar"}}])
async_client.url = httpserver.url_for("/api/v2.0")

with pytest.raises(UnprocessableEntity):
await async_client.get_artifact_vulnerabilities(
Expand All @@ -167,7 +164,7 @@ async def test_get_artifact_tags_mock(
json_from_list(tags),
headers={"Content-Type": "application/json"},
)
async_client.url = httpserver.url_for("/api/v2.0")

tags_resp = await async_client.get_artifact_tags("testproj", "testrepo", "latest")
# TODO: test params
assert tags_resp == tags
Expand All @@ -190,7 +187,7 @@ async def test_get_artifact_accessories_mock(
json_from_list(accessories),
headers={"Content-Type": "application/json"},
)
async_client.url = httpserver.url_for("/api/v2.0")

accessories_resp = await async_client.get_artifact_accessories(
"testproj", "testrepo", "latest"
)
Expand All @@ -210,7 +207,7 @@ async def test_delete_artifact_tag(
"/api/v2.0/projects/testproj/repositories/testrepo/artifacts/latest/tags/123",
method="DELETE",
).respond_with_data(status=status_code)
async_client.url = httpserver.url_for("/api/v2.0")

if status_code == 404:
ctx = pytest.raises(StatusError)
else:
Expand All @@ -234,7 +231,6 @@ async def test_copy_artifact(
).respond_with_data(
status=status_code, headers={"Location": "/api/v2.0/artifacts/123"}
)
async_client.url = httpserver.url_for("/api/v2.0")

location = await async_client.copy_artifact(
"testproj",
Expand All @@ -261,7 +257,7 @@ async def test_get_artifacts_mock(
json_from_list(artifacts),
headers={"Content-Type": "application/json"},
)
async_client.url = httpserver.url_for("/api/v2.0")

resp = await async_client.get_artifacts(
"testproj",
"testrepo",
Expand Down Expand Up @@ -289,7 +285,7 @@ async def test_add_artifact_label_mock(
method="POST",
json=label.model_dump(exclude_unset=True),
).respond_with_data()
async_client.url = httpserver.url_for("/api/v2.0")

await async_client.add_artifact_label(
"testproj",
"testrepo",
Expand All @@ -313,7 +309,7 @@ async def test_get_artifact_mock(
artifact.model_dump_json(),
headers={"Content-Type": "application/json"},
)
async_client.url = httpserver.url_for("/api/v2.0")

# TODO: test params
resp = await async_client.get_artifact(
"testproj",
Expand All @@ -338,7 +334,6 @@ async def test_delete_artifact_mock(
status=status_code,
)

async_client.url = httpserver.url_for("/api/v2.0")
if status_code == 404:
ctx = pytest.raises(StatusError)
else:
Expand All @@ -361,7 +356,6 @@ async def test_delete_artifact_label_mock(
status=status_code,
)

async_client.url = httpserver.url_for("/api/v2.0")
if status_code == 404:
ctx = pytest.raises(StatusError)
else:
Expand Down
1 change: 0 additions & 1 deletion tests/endpoints/test_audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ async def test_get_audit_logs_mock(
json_from_list(audit_logs),
headers={"Content-Type": "application/json"},
)
async_client.url = httpserver.url_for("/api/v2.0")

logs = await async_client.get_audit_logs()
assert logs == audit_logs
4 changes: 2 additions & 2 deletions tests/endpoints/test_configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async def test_get_config_mock(
"/api/v2.0/configurations",
method="GET",
).respond_with_json(config.model_dump(mode="json"))
async_client.url = httpserver.url_for("/api/v2.0")

resp = await async_client.get_config()
assert resp == config

Expand All @@ -42,5 +42,5 @@ async def test_update_config_mock(
method="PUT",
json=config.model_dump(mode="json", exclude_unset=True),
).respond_with_data()
async_client.url = httpserver.url_for("/api/v2.0")

await async_client.update_config(config)
4 changes: 2 additions & 2 deletions tests/endpoints/test_cveallowlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async def test_get_cve_allowlist(
"/api/v2.0/system/CVEAllowlist",
method="GET",
).respond_with_json(cve_allowlist.model_dump(mode="json"))
async_client.url = httpserver.url_for("/api/v2.0")

allowlist = await async_client.get_cve_allowlist()
assert allowlist == cve_allowlist
if allowlist.items:
Expand All @@ -46,5 +46,5 @@ async def test_update_cve_allowlist(
"/api/v2.0/system/CVEAllowlist",
method="PUT",
).respond_with_data()
async_client.url = httpserver.url_for("/api/v2.0")

await async_client.update_cve_allowlist(cve_allowlist)
10 changes: 4 additions & 6 deletions tests/endpoints/test_gc.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ async def test_get_gc_schedule_mock(
httpserver: HTTPServer,
schedule: Schedule,
):
async_client.url = httpserver.url_for("/api/v2.0")
httpserver.expect_oneshot_request(
"/api/v2.0/system/gc/schedule",
method="GET",
Expand All @@ -43,7 +42,7 @@ async def test_create_gc_schedule_mock(
schedule: Schedule,
):
expect_location = "/api/v2.0/system/gc/schedule" # idk?
async_client.url = httpserver.url_for("/api/v2.0")

httpserver.expect_oneshot_request(
"/api/v2.0/system/gc/schedule",
method="POST",
Expand All @@ -64,7 +63,6 @@ async def test_update_gc_schedule_mock(
httpserver: HTTPServer,
schedule: Schedule,
):
async_client.url = httpserver.url_for("/api/v2.0")
httpserver.expect_oneshot_request(
"/api/v2.0/system/gc/schedule",
method="PUT",
Expand All @@ -88,7 +86,7 @@ async def test_get_gc_jobs_mock(
json_from_list(jobs),
content_type="application/json",
)
async_client.url = httpserver.url_for("/api/v2.0")

resp = await async_client.get_gc_jobs()
assert resp == jobs

Expand All @@ -109,7 +107,7 @@ async def test_get_gc_job_mock(
job.model_dump_json(),
content_type="application/json",
)
async_client.url = httpserver.url_for("/api/v2.0")

resp = await async_client.get_gc_job(123)
assert resp == job

Expand All @@ -131,7 +129,7 @@ async def test_get_gc_log_mock(
log,
content_type="text/plain",
)
async_client.url = httpserver.url_for("/api/v2.0")

resp = await async_client.get_gc_log(123, as_list=as_list)
if as_list:
assert isinstance(resp, list)
Expand Down
2 changes: 1 addition & 1 deletion tests/endpoints/test_health.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async def test_health_check(
httpserver.expect_oneshot_request(
"/api/v2.0/health", method="GET"
).respond_with_json(healthstatus.model_dump(mode="json"))
async_client.url = httpserver.url_for("/api/v2.0")

health = await async_client.health_check()
assert health == healthstatus
if health.components: # TODO: add OverallHealthStatus.components strategy
Expand Down
2 changes: 1 addition & 1 deletion tests/endpoints/test_icon.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async def test_get_icon_mock(
httpserver.expect_oneshot_request(
"/api/v2.0/icons/digest", method="GET"
).respond_with_data(icon.model_dump_json(), content_type="application/json")
async_client.url = httpserver.url_for("/api/v2.0")

resp = await async_client.get_icon("digest")
assert resp.content_type == icon.content_type
assert resp.content == icon.content
6 changes: 3 additions & 3 deletions tests/endpoints/test_immutable.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ async def test_update_project_immutable_tag_rule_mock(
json=rule.model_dump(mode="json", exclude_unset=True),
headers={"X-Is-Resource-Name": "false"},
).respond_with_data()
async_client.url = httpserver.url_for("/api/v2.0")

await async_client.update_project_immutable_tag_rule(1234, 1, rule)


Expand All @@ -85,7 +85,7 @@ async def test_enable_project_immutable_tagrule(
json={"disabled": not enable},
headers={"X-Is-Resource-Name": "false"},
).respond_with_data()
async_client.url = httpserver.url_for("/api/v2.0")

await async_client.enable_project_immutable_tagrule(1234, 1, enable)


Expand All @@ -99,5 +99,5 @@ async def test_delete_project_immutable_tag_rule_mock(
method="DELETE",
headers={"X-Is-Resource-Name": "false"},
).respond_with_data()
async_client.url = httpserver.url_for("/api/v2.0")

await async_client.delete_project_immutable_tag_rule(1234, 1)
10 changes: 5 additions & 5 deletions tests/endpoints/test_labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async def test_get_label_mock(
).respond_with_data(
label.model_dump_json(), headers={"Content-Type": "application/json"}
)
async_client.url = httpserver.url_for("/api/v2.0")

resp = await async_client.get_label(123)
assert resp == label

Expand All @@ -48,7 +48,7 @@ async def test_update_label_mock(
method="PUT",
json=label.model_dump(mode="json", exclude_unset=True),
).respond_with_data()
async_client.url = httpserver.url_for("/api/v2.0")

await async_client.update_label(123, label)


Expand All @@ -67,7 +67,7 @@ async def test_create_label_mock(
method="POST",
json=label.model_dump(mode="json", exclude_unset=True),
).respond_with_data(headers={"Location": expect_location})
async_client.url = httpserver.url_for("/api/v2.0")

resp = await async_client.create_label(label)
assert resp == expect_location

Expand All @@ -81,7 +81,7 @@ async def test_delete_label_mock(
httpserver.expect_oneshot_request(
f"/api/v2.0/labels/{label_id}", method="DELETE"
).respond_with_data()
async_client.url = httpserver.url_for("/api/v2.0")

await async_client.delete_label(label_id)


Expand All @@ -99,7 +99,7 @@ async def test_get_labels_mock(
json_from_list(labels),
headers={"Content-Type": "application/json"},
)
async_client.url = httpserver.url_for("/api/v2.0")

# TODO: query parameters
resp = await async_client.get_labels()
assert resp == labels
8 changes: 3 additions & 5 deletions tests/endpoints/test_ldap.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async def test_ping_ldap_with_config_mock(
method="POST",
json=config.model_dump(mode="json", exclude_unset=True),
).respond_with_data(result.model_dump_json(), content_type="application/json")
async_client.url = httpserver.url_for("/api/v2.0")

resp = await async_client.ping_ldap(config)
assert resp == result

Expand All @@ -50,7 +50,7 @@ async def test_ping_ldap_no_config_mock(
"/api/v2.0/ldap/ping",
method="POST",
).respond_with_data(result.model_dump_json(), content_type="application/json")
async_client.url = httpserver.url_for("/api/v2.0")

resp = await async_client.ping_ldap()
assert resp == result

Expand All @@ -72,7 +72,6 @@ async def test_search_ldap_groups_mock(
"/api/v2.0/ldap/groups/search",
method="GET",
).respond_with_data(json_from_list(results), content_type="application/json")
async_client.url = httpserver.url_for("/api/v2.0")

if not group_name and not group_dn:
with pytest.raises(ValueError):
Expand All @@ -96,7 +95,6 @@ async def test_search_ldap_users_mock(
"/api/v2.0/ldap/users/search",
method="GET",
).respond_with_data(json_from_list(results), content_type="application/json")
async_client.url = httpserver.url_for("/api/v2.0")

resp = await async_client.search_ldap_users(username)
assert resp == results
Expand All @@ -115,7 +113,7 @@ async def test_import_ldap_users_mock(
method="POST",
json={"ldap_uid_list": user_ids},
).respond_with_data()
async_client.url = httpserver.url_for("/api/v2.0")

# The method constructs the request model,
# we just pass in the list of user IDs.
await async_client.import_ldap_users(user_ids)
2 changes: 1 addition & 1 deletion tests/endpoints/test_oidc.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async def test_test_oidc_mock(
method="POST",
json=oidcreq.model_dump(mode="json", exclude_unset=True),
).respond_with_data(status=status)
async_client.url = httpserver.url_for("/api/v2.0")

if status == 200:
ctx = nullcontext()
else:
Expand Down
1 change: 0 additions & 1 deletion tests/endpoints/test_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ async def test_get_permissions(
permissions.model_dump(mode="json"),
headers={"Content-Type": "application/json"},
)
async_client.url = httpserver.url_for("/api/v2.0")

resp = await async_client.get_permissions()
assert resp == permissions
2 changes: 1 addition & 1 deletion tests/endpoints/test_ping.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ async def test_ping(
httpserver: HTTPServer,
):
httpserver.expect_oneshot_request("/api/v2.0/ping").respond_with_data("pong")
async_client.url = httpserver.url_for("/api/v2.0")

pong = await async_client.ping()
assert pong == "pong"
Loading

0 comments on commit 2fc378a

Please sign in to comment.