Skip to content

Commit

Permalink
added tests for work issues (#1666)
Browse files Browse the repository at this point in the history
* added tests for work issues

* added tests for work issues

* added tests for work status

* added more tests
  • Loading branch information
saravanpa-aot authored Jan 15, 2024
1 parent 2d2e4e0 commit bc19398
Show file tree
Hide file tree
Showing 5 changed files with 441 additions and 2 deletions.
2 changes: 1 addition & 1 deletion epictrack-api/src/api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class TestConfig(_Config): # pylint: disable=too-few-public-methods
JWT_OIDC_TEST_KEYS = {
'keys': [
{
'kid': 'forms-flow-ai',
'kid': 'epictrack',
'kty': 'RSA',
'alg': 'RS256',
'use': 'sig',
Expand Down
132 changes: 132 additions & 0 deletions epictrack-api/tests/unit/apis/test_work_issues.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# Copyright © 2019 Province of British Columbia
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Test suite for projects."""

from http import HTTPStatus
from urllib.parse import urljoin

from faker import Faker

from tests.utilities.factory_scenarios import (TestIssues, TestWorkIssuesInfo,
TestWorkIssueUpdatesInfo, TestJwtClaims)
from tests.utilities.factory_utils import factory_work_model, factory_work_issues_model, \
factory_work_issue_updates_model, factory_auth_header

API_BASE_URL = "/api/v1/"

fake = Faker()


def test_get_empty_work(client):
"""Test get empty project."""
work = factory_work_model()
url = urljoin(API_BASE_URL, f'work/{work.id}/issues')
result = client.get(url)
assert result.status_code == HTTPStatus.OK


def test_create_work(client):
"""Test create new project."""
work = factory_work_model()
url = urljoin(API_BASE_URL, f'work/{work.id}/issues')
issue_data = TestIssues.issue2.value
result = client.post(url, json=issue_data)

assert result.status_code == HTTPStatus.CREATED
result_json = result.json
assert "id" in result.json
assert result_json.get('work_id') == work.id

result_get = client.get(url)
assert result_get.status_code == HTTPStatus.OK
assert len(result_get.json) == 1, 'only one issue got created'
retrieved_issue_json = result_get.json[0]

assert "id" in result_json
assert retrieved_issue_json["work_id"] == work.id
assert issue_data['title'] == retrieved_issue_json["title"]

created_updates = result_json.get('updates')[0]
assert created_updates["description"] == issue_data.get("updates")[0]
assert created_updates["work_issue_id"] == result_json["id"]


def test_create_and_fetch_work_issues(client):
"""Test create and fetch WorkIssues with updates."""
work = factory_work_model()
issue_data = TestWorkIssuesInfo.issue1.value
update_data = TestWorkIssueUpdatesInfo.update1.value

work_issue = factory_work_issues_model(work.id, issue_data)
factory_work_issue_updates_model(work_issue.id, update_data)

url = urljoin(API_BASE_URL, f'work/{work.id}/issues')
result_get = client.get(url)
retrieved_issue_json = result_get.json[0]
assert "id" in retrieved_issue_json
assert retrieved_issue_json["work_id"] == work.id
assert retrieved_issue_json["title"] == work_issue.title


def test_create_and_update_work_issues(client):
"""Test create and update WorkIssues with updates."""
work = factory_work_model()
issue_data = TestWorkIssuesInfo.issue1.value
update_data = TestWorkIssueUpdatesInfo.update1.value
work_issue = factory_work_issues_model(work.id, issue_data)
work_issue_update = factory_work_issue_updates_model(work_issue.id, update_data)
url = urljoin(API_BASE_URL, f'work/{work.id}/issues')
result_get = client.get(url)
assert work_issue_update.description == result_get.json[0].get('updates')[0]['description']

new_description = fake.sentence()
updated_update_data = {"id": work_issue_update.id, "description": new_description}
url_update = urljoin(API_BASE_URL, f'work/{work.id}/issues/{work_issue.id}')
result_update = client.put(url_update, json={"updates": [updated_update_data]})
assert result_update.status_code == HTTPStatus.CREATED

url = urljoin(API_BASE_URL, f'work/{work.id}/issues')
result_get = client.get(url)
assert new_description == result_get.json[0].get('updates')[0]['description']


def test_approve_work_issue_update(client, jwt):
"""Test approve work issue description update."""
work = factory_work_model()
issue_data = TestWorkIssuesInfo.issue1.value
update_data = TestWorkIssueUpdatesInfo.update1.value

work_issue = factory_work_issues_model(work.id, issue_data)
work_issue_update = factory_work_issue_updates_model(work_issue.id, update_data)

# Assert that the initial state is not approved and approved_by is null
assert not work_issue_update.is_approved
assert work_issue_update.approved_by is None

staff_user = TestJwtClaims.staff_admin_role
user_name = staff_user['preferred_username']
headers = factory_auth_header(jwt=jwt, claims=staff_user)
url_approve = urljoin(API_BASE_URL, f'work/{work.id}/issues/{work_issue.id}/update/{work_issue_update.id}/approve')

result_approve = client.patch(url_approve, headers=headers)

assert result_approve.status_code == HTTPStatus.OK

url = urljoin(API_BASE_URL, f'work/{work.id}/issues')
result_get = client.get(url)

# Assert that the WorkIssueUpdate is now approved and approved_by is set
updated_update = result_get.json[0].get('updates')[0]
assert updated_update["is_approved"]
assert updated_update["approved_by"] == user_name
96 changes: 96 additions & 0 deletions epictrack-api/tests/unit/apis/test_work_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Copyright © 2019 Province of British Columbia
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Test suite for projects."""

from http import HTTPStatus
from urllib.parse import urljoin

from faker import Faker
from flask import g

from tests.utilities.factory_scenarios import (TestJwtClaims, TestStatus)
from tests.utilities.factory_utils import factory_work_model, factory_auth_header, factory_work_status_model

API_BASE_URL = "/api/v1/"

fake = Faker()


def test_get_empty_work(client):
"""Test get empty project."""
work = factory_work_model()
url = urljoin(API_BASE_URL, f'work/{work.id}/statuses')
result = client.get(url)
assert result.status_code == HTTPStatus.OK


def test_create_and_fetch_work_status(client, jwt):
"""Test create and fetch WorkStatus."""
work = factory_work_model()
url_create_status = urljoin(API_BASE_URL, f'work/{work.id}/statuses')
status_data = TestStatus.status1.value

staff_user = TestJwtClaims.staff_admin_role
headers = factory_auth_header(jwt=jwt, claims=staff_user)
g.token_info = staff_user
result_create_status = client.post(url_create_status, headers=headers, json=status_data)
assert result_create_status.status_code == HTTPStatus.CREATED
result_json = result_create_status.json
assert "id" in result_json
assert result_json.get('work_id') == work.id

# Fetch WorkStatus
url_fetch_status = urljoin(API_BASE_URL, f'work/{work.id}/statuses')
result_fetch_status = client.get(url_fetch_status, headers=headers)
assert result_fetch_status.status_code == HTTPStatus.OK
assert len(result_fetch_status.json) == 1, 'only one status got created'

retrieved_status_json = result_fetch_status.json[0]

assert "id" in retrieved_status_json
assert retrieved_status_json["work_id"] == work.id
assert status_data['description'] == retrieved_status_json["description"]
assert staff_user['preferred_username'] == retrieved_status_json["posted_by"]

assert not retrieved_status_json["is_approved"]
assert retrieved_status_json["approved_by"] is None
assert retrieved_status_json["approved_date"] is None


def test_approve_work_status(client, jwt):
"""Test approve work status."""
work = factory_work_model()
status_data = TestStatus.status1.value
work_status = factory_work_status_model(work.id, status_data)

# Assert that the initial state is not approved
assert not work_status.is_approved

# Approve the work status
staff_user = TestJwtClaims.staff_admin_role
headers = factory_auth_header(jwt=jwt, claims=staff_user)
g.token_info = staff_user
url_approve = urljoin(API_BASE_URL, f'work/{work.id}/statuses/{work_status.id}/approve')
result_approve = client.patch(url_approve, headers=headers)

assert result_approve.status_code == HTTPStatus.OK

# Fetch the updated work status
url_get = urljoin(API_BASE_URL, f'work/{work.id}/statuses')
result_get = client.get(url_get)
approved_work_status = result_get.json[0]
# Assert that the WorkStatus is now approved and approved_by and approved_date are set
assert approved_work_status["is_approved"]
assert approved_work_status["approved_by"] == staff_user["preferred_username"]
assert "approved_date" in approved_work_status
108 changes: 108 additions & 0 deletions epictrack-api/tests/utilities/factory_scenarios.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
from enum import Enum
from faker import Faker

from api.config import get_named_config

fake = Faker()
CONFIG = get_named_config('testing')


class TestProjectInfo(Enum):
Expand All @@ -38,3 +41,108 @@ class TestProjectInfo(Enum):
"abbreviation": fake.word().upper()[:10]

}


class TestWorkInfo(Enum):
"""Test scenarios of Project Creation."""

work1 = {
"title": fake.word(),
"report_description": fake.sentence(),
"epic_description": fake.paragraph(),
"is_active": True,
"start_date": "2024-01-11T00:00:00-08:00",
"project_id": 1,
"ministry_id": 1,
"ea_act_id": 3,
"eao_team_id": 1,
"federal_involvement_id": 1,
"responsible_epd_id": 55,
"work_lead_id": 30,
"work_type_id": 1,
"substitution_act_id": 1,
"decision_by_id": 171
}


class TestStaffInfo(Enum):
"""Test scenarios of Staff Creation."""

staff1 = {
"first_name": fake.first_name(),
"last_name": fake.last_name(),
"phone": fake.phone_number(),
"email": fake.email(),
"is_active": True
}


class TestIssues(Enum):
"""Test scenarios of Issues Creation."""

issue2 = {
"title": fake.word(),
"description": fake.sentence(),
"start_date": fake.date_time_this_decade(tzinfo=None).isoformat(),
"expected_resolution_date": fake.date_time_between(start_date='now', end_date='+10d', tzinfo=None).isoformat(),
"is_active": True,
"is_high_priority": True,
"updates": [fake.sentence()]
}


class TestWorkIssuesInfo(Enum):
"""Test scenarios for WorkIssues."""

issue1 = {
"title": fake.word(),
"is_active": True,
"is_high_priority": False,
"start_date": fake.date_time_this_decade(tzinfo=None),
"expected_resolution_date": fake.date_time_this_decade(tzinfo=None),
}


class TestStatus(Enum):
"""Test scenarios of WorkStatus."""

status1 = {
"description": fake.sentence(),
"posted_date": fake.date_time_this_decade(tzinfo=None).isoformat(),
"is_approved": False,
"approved_by": None,
"approved_date": None,
}


class TestWorkIssueUpdatesInfo(Enum):
"""Test scenarios for WorkIssueUpdates."""

update1 = {
"description": fake.sentence(),
"is_approved": False,
"approved_by": None,
}


class TestJwtClaims(dict, Enum):
"""Test scenarios of jwt claims."""

staff_admin_role = {
'iss': CONFIG.JWT_OIDC_TEST_ISSUER,
'sub': 'f7a4a1d3-73a8-4cbc-a40f-bb1145302064',
'idp_userid': 'f7a4a1d3-73a8-4cbc-a40f-bb1145302064',
'preferred_username': f'{fake.user_name()}@idir',
'given_name': fake.first_name(),
'family_name': fake.last_name(),
'tenant_id': 1,
'email': 'staff@gov.bc.ca',
'realm_access': {
'roles': [
'staff',
'create',
'edit'

]
}
}
Loading

0 comments on commit bc19398

Please sign in to comment.