Skip to content

Commit

Permalink
21025 configuration GET endpoint (bcgov#2679)
Browse files Browse the repository at this point in the history
* 21025 cofiguration GET endpoint

* fix typo

* fix missing
  • Loading branch information
kzdev420 authored May 8, 2024
1 parent 9015bbd commit 1e5a8ee
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
18 changes: 18 additions & 0 deletions legal-api/src/legal_api/models/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
"""This module holds data for configurations."""
from __future__ import annotations

from typing import List

from croniter import croniter
from sqlalchemy import event

Expand All @@ -36,6 +38,22 @@ def save(self):
db.session.add(self)
db.session.commit()

@property
def json(self):
"""Return a dict of this object, with keys in JSON format."""
configuration = {
'name': self.name,
'value': self.val,
'shortDescription': self.short_description,
'fullDescription': self.full_description
}
return configuration

@classmethod
def all(cls) -> List[Configuration]:
"""Return the configuration matching the id."""
return cls.query.all()

@classmethod
def find_by_id(cls, config_id: int) -> Configuration:
"""Return the configuration matching the id."""
Expand Down
2 changes: 2 additions & 0 deletions legal-api/src/legal_api/resources/v2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from .administrative_bn import bp as administrative_bn_bp
from .business import bp as businesses_bp
from .business.business_digital_credentials import bp_dc as digital_credentials_bp
from .configuration import bp as configuration_bp
from .document_signature import bp as document_signature_bp
from .internal_services import bp as internal_bp
from .meta import bp as meta_bp
Expand Down Expand Up @@ -50,6 +51,7 @@ def init_app(self, app):
self.app.register_blueprint(naics_bp)
self.app.register_blueprint(request_tracker_bp)
self.app.register_blueprint(internal_bp)
self.app.register_blueprint(configuration_bp)


v2_endpoint = V2Endpoint()
37 changes: 37 additions & 0 deletions legal-api/src/legal_api/resources/v2/configuration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# 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.
"""API endpoints for managing Configuration resource."""
from http import HTTPStatus

from flask import Blueprint, jsonify
from flask_cors import cross_origin

from legal_api.models import Configuration, UserRoles
from legal_api.utils.auth import jwt


bp = Blueprint('CONFIGURATION', __name__, url_prefix='/api/v2/admin/configurations')


@bp.route('', methods=['GET'])
@cross_origin(origin='*')
@jwt.has_one_of_roles([UserRoles.staff])
def get_configurations():
"""Return a list of configurations."""
configurations = Configuration.all()
return jsonify({
'configurations': [
configuration.json for configuration in configurations
]
}), HTTPStatus.OK
51 changes: 51 additions & 0 deletions legal-api/tests/unit/resources/v2/test_configuration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# 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 configuration end-point.
Test-Suite to ensure that admin/configuration endpoints are working as expected.
"""
from http import HTTPStatus

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


def test_get_configurations(app, session, client, jwt):
"""Assert that get results are returned."""

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

# check
assert rv.status_code == HTTPStatus.OK
assert 'configurations' in rv.json
results = rv.json['configurations']
assert len(results) == 4

names = {'NUM_DISSOLUTIONS_ALLOWED', 'MAX_DISSOLUTIONS_ALLOWED', 'DISSOLUTIONS_ON_HOLD', 'NEW_DISSOLUTIONS_SCHEDULE'}
for res in results:
assert res['name'] in names


def test_get_configurations_with_invalid_user(app, session, client, jwt):
"""Assert that is unauthorized."""

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

# check
assert rv.status_code == HTTPStatus.UNAUTHORIZED

0 comments on commit 1e5a8ee

Please sign in to comment.