Skip to content

Commit

Permalink
21442 Legal API - Add filter to configurations endpoint (bcgov#2731)
Browse files Browse the repository at this point in the history
* Add filter param to configurations endpoint

* Add unit tests for configurations endpoint with filter name

* Add method to find configurations by a list of names

* Update get_configurations function to filter by single or multiple names

* Update tests for get_configurations with various filter names

* Update get_configuration to work without case sensitive

* Update get_configuration tests to test work without case sensitive
  • Loading branch information
AimeeGao authored Jun 4, 2024
1 parent 7ea8f5d commit 25992fe
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
5 changes: 5 additions & 0 deletions legal-api/src/legal_api/models/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ def find_by_name(cls, config_name: str) -> Configuration:
configuration = cls.query.filter_by(name=config_name).one_or_none()
return configuration

@classmethod
def find_by_names(cls, config_names: List[str]) -> List[Configuration]:
"""Return the configurations matching the names."""
return cls.query.filter(cls.name.in_(config_names)).all()

def validate_value(self):
"""Ensure the value is the correct type before insert or update."""
# Define keys that should have specific value types
Expand Down
15 changes: 13 additions & 2 deletions legal-api/src/legal_api/resources/v2/admin/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,19 @@
@cross_origin(origin='*')
@jwt.has_one_of_roles([UserRoles.staff])
def get_configurations():
"""Return a list of configurations."""
configurations = Configuration.all()
"""Return a list of configurations, optionally filtered by names."""
filter_names = request.args.get('names', None)
if filter_names:
names_list = [name.strip().upper() for name in filter_names.split(',') if name.strip()]
if not names_list:
return {'message': 'Configuration names are invalid'}, HTTPStatus.BAD_REQUEST

configurations = Configuration.find_by_names(names_list)
if not configurations:
return {'message': 'Configurations not found'}, HTTPStatus.NOT_FOUND
else:
configurations = Configuration.all()

return jsonify({
'configurations': [
configuration.json for configuration in configurations
Expand Down
61 changes: 61 additions & 0 deletions legal-api/tests/unit/resources/v2/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,67 @@ def test_get_configurations_with_invalid_user(app, session, client, jwt):
assert rv.status_code == HTTPStatus.UNAUTHORIZED


def test_get_configurations_with_single_filter_name(app, session, client, jwt):
"""Assert that get results with a single filter name are returned."""
filter_name = 'DISSOLUTIONS_SUMMARY_EMAIL'

# test
rv = client.get(f'/api/v2/admin/configurations?names={filter_name}',
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) == 1
for res in results:
assert res['name'] == filter_name


def test_get_configurations_with_multiple_filter_names(app, session, client, jwt):
"""Assert that get results with multiple filter names are returned."""
filter_names = 'DISSOLUTIONS_SUMMARY_EMAIL, NUM_DISSOLUTIONS_ALLOWED, dissolutions_on_hold'
expected_names = [name.strip().upper() for name in filter_names.split(',') if name.strip()]

# test
rv = client.get(f'/api/v2/admin/configurations?names={filter_names}',
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) == len(expected_names)
for res in results:
assert res['name'] in expected_names


def test_get_configurations_with_empty_filter_names(app, session, client, jwt):
"""Assert that a bad request is returned when configuration names are invalid."""
empty_names = ' '

# Test
rv = client.get(f'/api/v2/admin/configurations?names={empty_names}',
headers=create_header(jwt, [STAFF_ROLE], 'user'))

# Check
assert rv.status_code == HTTPStatus.BAD_REQUEST
assert rv.json['message'] == 'Configuration names are invalid'


def test_get_configurations_with_filter_names_no_matching_configurations(app, session, client, jwt):
"""Assert that not found is returned when configuration names have no matches."""
filter_names = 'NON_EXISTENT_CONFIGURATION_NAME1, NON_EXISTENT_CONFIGURATION_NAME2'

# Test
rv = client.get(f'/api/v2/admin/configurations?names={filter_names}',
headers=create_header(jwt, [STAFF_ROLE], 'user'))

# Check
assert rv.status_code == HTTPStatus.NOT_FOUND
assert rv.json['message'] == 'Configurations not found'


def test_put_configurations_with_valid_data(app, session, client, jwt):
"""Assert that update values successfully."""

Expand Down

0 comments on commit 25992fe

Please sign in to comment.