Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 31 additions & 9 deletions legal-api/src/legal_api/resources/v2/business/business.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
@jwt.requires_auth
def get_businesses(identifier: str):
"""Return a JSON object with meta information about the Service."""
if str(request.args.get("slim", None)).lower() == "true":
# getting all business info is expensive so returning the slim version is desirable for some flows
# - (i.e. business/person search updates)
return get_businesses_public(identifier, True)

if identifier.startswith("T"):
return {"message": babel("No information on temp registrations.")}, 200

Expand All @@ -53,15 +58,6 @@ def get_businesses(identifier: str):
if not business:
return jsonify({"message": f"{identifier} not found"}), HTTPStatus.NOT_FOUND

# getting all business info is expensive so returning the slim version is desirable for some flows
# - (i.e. business/person search updates)
if str(request.args.get("slim", None)).lower() == "true":
business_json = business.json(slim=True)
# need to add the alternateNames array here because it is not a part of slim JSON
business_json["alternateNames"] = business.get_alternate_names()

return jsonify(business=business_json)

# check authorization if required
if flags.is_on("enable-auth-v2-business") and not authorized(identifier, jwt, action=["view"]):
current_app.logger.warning(
Expand Down Expand Up @@ -113,6 +109,32 @@ def get_businesses(identifier: str):
return jsonify(business=business_json)


@bp.route("/<string:identifier>/public", methods=["GET"])
@cross_origin(origin="*")
@jwt.requires_auth
def get_businesses_public(identifier: str, slim = False):
"""Return a JSON object with public meta information about the business."""
if identifier.startswith("T"):
return {"message": babel("No information on temp registrations.")}, 200

business = Business.find_by_identifier(identifier)

if not business:
return jsonify({"message": f"{identifier} not found"}), HTTPStatus.NOT_FOUND

business_json = business.json(slim=True)
# add extras here that are not a part of the initial slim JSON
business_json["alternateNames"] = business.get_alternate_names()

if slim or str(request.args.get("slim", None)).lower() == "true":
return jsonify(business=business_json)

# NOTE: warnings can take longer to process which is why its not returned in the slim business call
business_json["warnings"] = check_warnings(business)

return jsonify(business=business_json)


@bp.route("", methods=["POST"])
@cross_origin(origin="*")
@jwt.requires_auth
Expand Down
82 changes: 80 additions & 2 deletions legal-api/tests/unit/resources/v2/test_business.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
INCORPORATION
)

from legal_api.models import Amalgamation, Business, Filing, RegistrationBootstrap
from legal_api.models import Amalgamation, Batch, Business, Filing, RegistrationBootstrap
from legal_api.services.authz import ACCOUNT_IDENTITY, PUBLIC_USER, STAFF_ROLE, SYSTEM_ROLE
from legal_api.services import flags
from legal_api.utils.datetime import datetime
from tests import integration_affiliation
from tests.unit.models import factory_business, factory_pending_filing
from tests.unit.models import factory_batch, factory_batch_processing, factory_business, factory_pending_filing
from tests.unit.services.warnings import create_business
from tests.unit.services.utils import create_header
from tests.unit.models import factory_completed_filing
Expand Down Expand Up @@ -257,6 +257,84 @@ def test_get_business_slim_info(app, session, client, jwt, requests_mock, test_n
assert rv.json['business'].get('amalgamatedInto') is not None


@pytest.mark.parametrize('test_name,role,amalgamated,slim', [
('regular', PUBLIC_USER, False, True),
('amalgamated', PUBLIC_USER, True, True),
('regular', PUBLIC_USER, True, False),
])
def test_get_business_public(monkeypatch, app, session, client, jwt, requests_mock, test_name, role, amalgamated, slim):
"""Assert that the business public info can be received with the expected data."""
identifier = 'BC7654321'
legal_type = 'BC'
legal_name = identifier + ' legal name'
tax_id = '123'
business = factory_business_model(legal_name=legal_name,
legal_type=legal_type,
identifier=identifier,
founding_date=datetime.fromtimestamp(0),
last_ledger_timestamp=datetime.fromtimestamp(0),
last_modified=datetime.fromtimestamp(0),
fiscal_year_end_date=None,
tax_id=tax_id,
dissolution_date=None)

if amalgamated:
filing = copy.deepcopy(FILING_TEMPLATE)
filing['filing'].pop('business')
filing['filing']['amalgamationApplication'] = copy.deepcopy(AMALGAMATION_APPLICATION)
filing['filing']['header']['name'] = 'amalgamationApplication'
filing = factory_completed_filing(business, filing)
business.state_filing_id = filing.id
business.state = 'HISTORICAL'
amalgamation = Amalgamation(
amalgamation_type=Amalgamation.AmalgamationTypes.regular,
business_id=business.id,
filing_id=filing.id,
amalgamation_date=datetime.utcnow(),
court_approval=True
)
amalgamation.save()
business.save()

def mocked_check_warnings(_business):
return [{ 'warning': 'mocked' }]

monkeypatch.setattr('legal_api.resources.v2.business.business.check_warnings', mocked_check_warnings)

rv = client.get(f'/api/v2/businesses/{identifier}/public?slim={slim}' ,
headers=create_header(jwt, [role], identifier))

assert rv.status_code == HTTPStatus.OK
assert rv.json['business']['identifier'] == identifier
assert rv.json['business']['legalType'] == legal_type
assert rv.json['business']['taxId'] == tax_id
assert rv.json['business'].get('state') is not None
assert rv.json['business'].get('goodStanding') is not None
if amalgamated:
assert rv.json['business'].get('amalgamatedInto') is not None
if not slim:
assert rv.json['business'].get('warnings') is not None
assert len(rv.json['business']['warnings']) == 1
else:
assert rv.json['business'].get('warnings') is None
# check expected data is not returned
assert not rv.json['business'].get('submitter')
assert not rv.json['business'].get('nextAnnualReport')
assert not rv.json['business'].get('lastDirectorChangeDate')
assert not rv.json['business'].get('lastAnnualReportDate')
assert not rv.json['business'].get('lastAnnualGeneralMeetingDate')
assert not rv.json['business'].get('lastAddressChangeDate')
assert not rv.json['business'].get('fiscalYearEndDate')
assert not rv.json['business'].get('arMaxDate')
assert not rv.json['business'].get('arMinDate')
assert not rv.json['business'].get('hasRestrictions')
assert not rv.json['business'].get('hasCourtOrders')
assert not rv.json['business'].get('hasCorrections')
assert not rv.json['business'].get('naicsCode')
assert not rv.json['business'].get('naicsKey')
assert not rv.json['business'].get('allowedActions')


@pytest.mark.parametrize('test_name, slim_version, auth_check_on', [
('slim business request', True, True),
('regular business request', False, True),
Expand Down