Skip to content

Commit

Permalink
test(restapi): Added description field to groups unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewhand committed May 17, 2024
1 parent 79cdc8b commit 2b90d9e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
7 changes: 6 additions & 1 deletion tests/unit/restapi/lib/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
API endpoints.
"""

from typing import Any
from flask.testing import FlaskClient
from werkzeug.test import TestResponse

Expand Down Expand Up @@ -107,6 +108,7 @@ def register_queue(
def register_group(
client: FlaskClient,
name: str,
description: str | None = None,
) -> TestResponse:
"""Register a group using the API.
Expand All @@ -117,7 +119,10 @@ def register_group(
Returns:
The response from the API.
"""
payload = {"name": name}
payload: dict[str, Any] = {"name": name}

if description is not None:
payload["description"] = description

return client.post(
f"/{V1_ROOT}/{V1_GROUPS_ROUTE}/",
Expand Down
5 changes: 4 additions & 1 deletion tests/unit/restapi/v1/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,17 @@ def registered_groups(
group1_response = actions.register_group(
client,
name="group_one",
description="The first group.",
).get_json()
group2_response = actions.register_group(
client,
name="group_two",
description="The second group.",
).get_json()
group3_response = actions.register_group(
client,
name="team_one",
name="group_three",
description="Not retrieved.",
).get_json()
return {
"group1": group1_response,
Expand Down
22 changes: 15 additions & 7 deletions tests/unit/restapi/v1/test_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def modify_group(
client: FlaskClient,
group_id: int,
new_name: str,
new_description: str,
) -> TestResponse:
"""Rename a group using the API.
Expand All @@ -51,7 +52,7 @@ def modify_group(
Returns:
The response from the API.
"""
payload = {"name": new_name}
payload: dict[str, Any] = {"name": new_name, "description": new_description}

return client.put(
f"/{V1_ROOT}/{V1_GROUPS_ROUTE}/{group_id}",
Expand Down Expand Up @@ -80,6 +81,7 @@ def assert_group_response_contents_matches_expectations(
expected_keys = {
"id",
"name",
"description",
"user",
"members",
"createdOn",
Expand All @@ -90,10 +92,12 @@ def assert_group_response_contents_matches_expectations(
# Validate the non-Ref fields
assert isinstance(response["id"], int)
assert isinstance(response["name"], str)
assert isinstance(response["description"], str)
assert isinstance(response["createdOn"], str)
assert isinstance(response["lastModifiedOn"], str)

assert response["name"] == expected_contents["name"]
assert response["description"] == expected_contents["description"]

assert helpers.is_iso_format(response["createdOn"])
assert helpers.is_iso_format(response["lastModifiedOn"])
Expand Down Expand Up @@ -224,6 +228,7 @@ def assert_cannot_rename_group_with_existing_name(
client: FlaskClient,
group_id: int,
existing_name: str,
existing_description: str,
) -> None:
"""Assert that renaming a group with an existing name fails.
Expand All @@ -239,6 +244,7 @@ def assert_cannot_rename_group_with_existing_name(
client=client,
group_id=group_id,
new_name=existing_name,
new_description=existing_description,
)
assert response.status_code == 400

Expand All @@ -265,18 +271,19 @@ def test_create_group(
response.
"""
name = "new_group"
description ="The first group"
user_id = auth_account["user_id"]
group_response = actions.register_group(client, name=name)
group_response = actions.register_group(client, name=name, description=description)
group_expected = group_response.get_json()

assert_group_response_contents_matches_expectations(
response=group_expected,
expected_contents={
"name": name,
"description": description,
"user_id": user_id,
},
)

assert_retrieving_group_by_id_works(
client, group_id=group_expected["id"], expected=group_expected
)
Expand All @@ -294,7 +301,7 @@ def test_group_search_query(
Given an authenticated user and registered groups, this test validates the following
sequence of actions:
- The user is able to retrieve a list of all registered groups with a name
- The user is able to retrieve a list of all registered groups with a description
that contains 'group'.
- The returned list of groups matches the expected matches from the query.
"""
Expand All @@ -305,7 +312,7 @@ def test_group_search_query(
assert_retrieving_groups_works(
client,
expected=group_expected_list,
search="name:*group*",
search="description:*group*",
)


Expand Down Expand Up @@ -354,18 +361,19 @@ def test_rename_group(
updated_group_name = "updated_name"
group_to_rename = registered_groups["group1"]
existing_group = registered_groups["group2"]

modify_group(
client,
group_id=group_to_rename["id"],
new_name=updated_group_name,
new_description=group_to_rename["description"],
)

assert_group_name_matches_expected_name(
client, group_id=group_to_rename["id"], expected_name=updated_group_name
)

assert_cannot_rename_group_with_existing_name(
client,
group_id=group_to_rename["id"],
existing_name=existing_group["name"],
existing_description=group_to_rename["description"],
)

0 comments on commit 2b90d9e

Please sign in to comment.