-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
194 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
"""Rationale behind creating these DTOS below. | ||
1. To ensure that the request and response payloads are validated before they are passed to the service layer. | ||
2. To ensure that the request and response payloads are consistent across the application. | ||
3. To ensure that the request and response payloads are consistent with the API documentation. | ||
In the near future, will find a library that generates our API spec based off of these DTOs. | ||
""" | ||
|
||
from typing import Optional | ||
|
||
from attrs import define | ||
|
||
from pay_api.utils.serializable import Serializable | ||
|
||
|
||
@define | ||
class DocumentsGetRequest(Serializable): | ||
"""Retrieve documents DTO.""" | ||
|
||
document_type: Optional[str] = None |
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,47 @@ | ||
# Copyright © 2024 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. | ||
"""Resource for documents endpoints.""" | ||
from http import HTTPStatus | ||
|
||
from flask import Blueprint, Response, current_app, request | ||
from flask_cors import cross_origin | ||
|
||
from pay_api.dtos.documents import DocumentsGetRequest | ||
from pay_api.exceptions import BusinessException | ||
from pay_api.services.documents_service import DocumentsService | ||
from pay_api.utils.auth import jwt as _jwt | ||
from pay_api.utils.endpoints_enums import EndpointEnum | ||
from pay_api.utils.enums import ContentType | ||
|
||
bp = Blueprint("DOCUMENTS", __name__, url_prefix=f"{EndpointEnum.API_V1.value}/documents") | ||
|
||
|
||
@bp.route("", methods=["GET", "OPTIONS"]) | ||
@cross_origin(origins="*", methods=["GET"]) | ||
@_jwt.requires_auth | ||
def get_documents(): | ||
"""Get Pay documents.""" | ||
current_app.logger.info("<get_documents") | ||
request_data = DocumentsGetRequest.from_dict(request.args.to_dict()) | ||
|
||
try: | ||
report, document_name = DocumentsService.get_document(request_data.document_type) | ||
response = Response(report, HTTPStatus.OK.value) | ||
response.headers.set("Content-Disposition", "attachment", filename=document_name) | ||
response.headers.set("Content-Type", ContentType.PDF.value) | ||
response.headers.set("Access-Control-Expose-Headers", "Content-Disposition") | ||
current_app.logger.debug(">get_documents") | ||
return response | ||
except BusinessException as exception: | ||
return exception.response() |
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,53 @@ | ||
# Copyright © 2024 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. | ||
|
||
"""This manages the retrieval of report-api documents.""" | ||
from flask import current_app | ||
|
||
from pay_api.exceptions import BusinessException | ||
from pay_api.services import ReportService | ||
from pay_api.services.report_service import ReportRequest | ||
from pay_api.utils.enums import ContentType, DocumentTemplate, DocumentType | ||
from pay_api.utils.errors import Error | ||
|
||
|
||
class DocumentsService: | ||
"""Service to manage document retrieval.""" | ||
|
||
@classmethod | ||
def get_document(cls, document_type: str): | ||
"""Get document file.""" | ||
current_app.logger.debug("<get_document") | ||
|
||
if not document_type or document_type not in [doc_type.value for doc_type in DocumentType]: | ||
raise BusinessException(Error.DOCUMENT_TYPE_INVALID) | ||
|
||
report_name, template_name = cls._get_document_report_params(document_type) | ||
report_response = ReportService.get_report_response( | ||
ReportRequest( | ||
report_name=report_name, | ||
template_name=template_name, | ||
content_type=ContentType.PDF.value, | ||
populate_page_number=True, | ||
) | ||
) | ||
current_app.logger.debug(">get_document") | ||
return report_response, report_name | ||
|
||
@classmethod | ||
def _get_document_report_params(cls, document_type: str): | ||
"""Get document report parameters.""" | ||
if document_type == DocumentType.EFT_INSTRUCTIONS.value: | ||
return "bcrs_eft_instructions.pdf", DocumentTemplate.EFT_INSTRUCTIONS.value | ||
return None, None |
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
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
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,43 @@ | ||
# Copyright © 2024 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. | ||
|
||
"""Tests to assure the documents end-point. | ||
Test-Suite to ensure that the /documents endpoint is working as expected. | ||
""" | ||
import pytest | ||
|
||
from pay_api.utils.enums import DocumentType | ||
from pay_api.utils.errors import Error | ||
from tests.utilities.base_test import get_claims, token_header | ||
|
||
|
||
@pytest.mark.parametrize("document_type", [None, "ABC"]) | ||
def test_documents_invalid(session, client, jwt, app, document_type): | ||
"""Assert that the endpoint returns 400 for invalid documents.""" | ||
token = jwt.create_jwt(get_claims(), token_header) | ||
headers = {"Authorization": f"Bearer {token}", "content-type": "application/json"} | ||
document_url = "/api/v1/documents" if document_type is None else f"/api/v1/documents?documentType={document_type}" | ||
rv = client.get(document_url, headers=headers) | ||
assert rv.status_code == 400 | ||
assert rv.json["type"] == Error.DOCUMENT_TYPE_INVALID.name | ||
|
||
|
||
@pytest.mark.parametrize("document_type", [DocumentType.EFT_INSTRUCTIONS.value]) | ||
def test_documents(session, client, jwt, app, document_type): | ||
"""Assert that the endpoint returns 200 for valid documents.""" | ||
token = jwt.create_jwt(get_claims(), token_header) | ||
headers = {"Authorization": f"Bearer {token}", "content-type": "application/json"} | ||
rv = client.get(f"/api/v1/documents?documentType={document_type}", headers=headers) | ||
assert rv.status_code == 200 |