Skip to content

Commit

Permalink
21929 - get reviews endpoint (#2830)
Browse files Browse the repository at this point in the history
* 21929 - get_reviews endpoint

* 21929 - update 1

* 21929 - unit test

* 21929 - update method

* 21929 - fix lint issues

* 21929 - update unit tests
  • Loading branch information
ketaki-deodhar authored Jul 16, 2024
1 parent ad68e15 commit 608b226
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 1 deletion.
21 changes: 21 additions & 0 deletions legal-api/src/legal_api/models/review.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,27 @@ def get_review(cls, filing_id) -> Review:
one_or_none())
return review

@classmethod
def get_paginated_reviews(cls, page, limit):
"""Return paginated reviews."""
query = db.session.query(Review).order_by(Review.creation_date.asc())

pagination = query.paginate(per_page=limit, page=page)
results = pagination.items
total_count = pagination.total

reviews_list = [
review.json for review in results
]

reviews = {
'reviews': reviews_list,
'page': page,
'limit': limit,
'total': total_count
}
return reviews

@property
def json(self) -> dict:
"""Return Review as a JSON object."""
Expand Down
14 changes: 13 additions & 1 deletion legal-api/src/legal_api/resources/v2/admin/reviews.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"""API endpoints for retrieving review data."""
from http import HTTPStatus

from flask import current_app, jsonify
from flask import current_app, jsonify, request
from flask_cors import cross_origin

from legal_api.models import Filing, Review, UserRoles
Expand All @@ -23,6 +23,18 @@
from .bp import bp_admin


@bp_admin.route('/reviews', methods=['GET'])
@cross_origin(origin='*')
@jwt.has_one_of_roles([UserRoles.staff])
def get_reviews():
"""Return a list of reviews."""
page = int(request.args.get('page', 1))
limit = int(request.args.get('limit', 10))
reviews = Review.get_paginated_reviews(page, limit)

return reviews, HTTPStatus.OK


@bp_admin.route('/reviews/<int:review_id>', methods=['GET', 'OPTIONS'])
@cross_origin(origin='*')
@jwt.has_one_of_roles([UserRoles.staff])
Expand Down
85 changes: 85 additions & 0 deletions legal-api/tests/unit/resources/v2/test_reviews.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# 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 reviews end-point.
Test-Suite to ensure that admin/reviews endpoints are working as expected.
"""
import copy
from registry_schemas.example_data import (
CONTINUATION_IN,
)
from http import HTTPStatus

from legal_api.services.authz import BASIC_USER, STAFF_ROLE
from tests.unit.services.utils import create_header

from legal_api.models import Review, ReviewStatus

from tests.unit.models import factory_filing


def basic_test_review():
filing_dict = {
'filing': {
'header': {
'name': 'continuationIn',
'date': '2019-04-08',
'certifiedBy': 'full name',
'email': '[email protected]',
}
}
}
filing_dict['filing']['continuationIn'] = copy.deepcopy(CONTINUATION_IN)
filing = factory_filing(None, filing_dict)

review = Review()
review.filing_id = filing.id
review.nr_number = filing_dict['filing']['continuationIn']['nameRequest']['nrNumber']
review.identifier = filing_dict['filing']['continuationIn']['foreignJurisdiction']['identifier']
review.completing_party = 'completing party'
review.status = ReviewStatus.AWAITING_REVIEW

return review


def test_get_reviews_with_invalid_user(app, session, client, jwt):
"""Assert unauthorized for BASIC_USER role."""

rv = client.get(f'/api/v2/admin/reviews',
headers=create_header(jwt, [BASIC_USER], 'user'))

assert rv.status_code == HTTPStatus.UNAUTHORIZED

def test_get_reviews_with_valid_user(app, session, client, jwt):
"""Assert review object returned for STAFF role."""

review_one = basic_test_review()
review_one.save()

review_two = basic_test_review()
review_two.save()

review_three = basic_test_review()
review_three.save()

rv = client.get(f'/api/v2/admin/reviews',
headers=create_header(jwt, [STAFF_ROLE], 'user'))

assert rv.status_code == HTTPStatus.OK
assert 'reviews' in rv.json
assert 1 == rv.json.get('page')
assert 10 == rv.json.get('limit')
assert 3 == rv.json.get('total')

0 comments on commit 608b226

Please sign in to comment.