Skip to content

Commit

Permalink
Merge branch 'main' into feature-gcp-migration
Browse files Browse the repository at this point in the history
  • Loading branch information
seeker25 authored Oct 8, 2024
2 parents d873fd9 + 8439b7c commit 636edcd
Show file tree
Hide file tree
Showing 24 changed files with 682 additions and 409 deletions.
9 changes: 6 additions & 3 deletions auth-api/src/auth_api/resources/v1/org_products.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import json
from http import HTTPStatus

from flask import Blueprint, request
from flask import Blueprint, g, request
from flask_cors import cross_origin

from auth_api.exceptions import BusinessException
Expand Down Expand Up @@ -63,9 +63,12 @@ def post_org_product_subscription(org_id):
return {"message": schema_utils.serialize(errors)}, HTTPStatus.BAD_REQUEST

try:
subscriptions = ProductService.create_product_subscription(int(org_id), request_json)
roles = g.jwt_oidc_token_info.get('realm_access').get('roles')
subscriptions = ProductService.create_product_subscription(int(org_id), request_json,
skip_auth=Role.SYSTEM.value in roles,
auto_approve=Role.SYSTEM.value in roles)
ProductService.update_org_product_keycloak_groups(int(org_id))
response, status = {"subscriptions": subscriptions}, HTTPStatus.CREATED
response, status = {'subscriptions': subscriptions}, HTTPStatus.CREATED
except BusinessException as exception:
response, status = {"code": exception.code, "message": exception.message}, exception.status_code
return response, status
Expand Down
5 changes: 3 additions & 2 deletions auth-api/src/auth_api/services/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ def create_product_subscription(
subscription_data: Dict[str, Any], # pylint: disable=too-many-locals
is_new_transaction: bool = True,
skip_auth=False,
auto_approve=False
):
"""Create product subscription for the user.
Expand Down Expand Up @@ -176,7 +177,7 @@ def create_product_subscription(
if product_model.premium_only and org.type_code not in PREMIUM_ORG_TYPES:
continue

subscription_status = Product.find_subscription_status(org, product_model)
subscription_status = Product.find_subscription_status(org, product_model, auto_approve)
product_subscription = Product._subscribe_and_publish_activity(
org_id, product_code, subscription_status, product_model.description
)
Expand Down Expand Up @@ -306,7 +307,7 @@ def _create_review_task(review_task: ProductReviewTask):
TaskService.create_task(task_info, False)

@staticmethod
def find_subscription_status(org, product_model):
def find_subscription_status(org, product_model, auto_approve=False):
"""Return the subscriptions status based on org type."""
# GOVM accounts has default active subscriptions
skip_review_types = [AccessType.GOVM.value]
Expand Down
31 changes: 16 additions & 15 deletions auth-api/tests/unit/api/test_org_products.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,14 @@ def assert_product_parent_and_child_statuses(client, jwt, org_id, parent_code, p
assert parent_mhr_product.get("subscriptionStatus") == parent_status


@pytest.mark.parametrize(
"org_product_info",
[
TestOrgProductsInfo.mhr_qs_lawyer_and_notaries,
TestOrgProductsInfo.mhr_qs_home_manufacturers,
TestOrgProductsInfo.mhr_qs_home_dealers,
],
)
def test_add_single_org_product_mhr_qualified_supplier_approve(client, jwt, session, keycloak_mock, org_product_info):
@pytest.mark.parametrize('test_name, org_product_info', [
('lawyer_notary', TestOrgProductsInfo.mhr_qs_lawyer_and_notaries),
('home_manufacturers', TestOrgProductsInfo.mhr_qs_home_manufacturers),
('home_dealers', TestOrgProductsInfo.mhr_qs_home_dealers),
('system_no_approval', TestOrgProductsInfo.mhr_qs_home_manufacturers)
])
def test_add_single_org_product_mhr_qualified_supplier_approve(client, jwt, session, keycloak_mock,
test_name, org_product_info):
"""Assert that MHR sub products subscriptions can be created and approved."""
# setup user and org
staff_headers = factory_auth_header(jwt=jwt, claims=TestJwtClaims.staff_role)
Expand All @@ -174,7 +173,8 @@ def test_add_single_org_product_mhr_qualified_supplier_approve(client, jwt, sess
assert rv.status_code == HTTPStatus.CREATED
dictionary = json.loads(rv.data)

# Create product subscription
if test_name == 'system_no_approval':
user_headers = factory_auth_header(jwt=jwt, claims=TestJwtClaims.system_role)
rv_products = client.post(
f"/api/v1/orgs/{dictionary.get('id')}/products",
data=json.dumps(org_product_info),
Expand All @@ -184,20 +184,21 @@ def test_add_single_org_product_mhr_qualified_supplier_approve(client, jwt, sess
assert rv_products.status_code == HTTPStatus.CREATED
assert schema_utils.validate(rv_products.json, "org_product_subscriptions_response")[0]

# Fetch org products and validate subscription status
subscription_status = 'ACTIVE' if test_name == 'system_no_approval' else 'PENDING_STAFF_REVIEW'
assert_product_parent_and_child_statuses(
client,
jwt,
dictionary.get("id"),
"MHR",
"PENDING_STAFF_REVIEW",
subscription_status,
org_product_info["subscriptions"][0]["productCode"],
"PENDING_STAFF_REVIEW",
subscription_status,
)

# Should show up as a review task for staff
if test_name == 'system_no_approval':
return

rv = client.get("/api/v1/tasks", headers=staff_headers, content_type="application/json")

item_list = rv.json
assert schema_utils.validate(item_list, "paged_response")[0]
assert rv.status_code == HTTPStatus.OK
Expand Down
Loading

0 comments on commit 636edcd

Please sign in to comment.