-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added tests for work issues * added tests for work issues * added tests for work status * added more tests
- Loading branch information
1 parent
2d2e4e0
commit bc19398
Showing
5 changed files
with
441 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.