def test_filing_missing_name(session): """Assert that an AR filing can be saved.""" import copy identifier = 'CP7654321' b = factory_business(identifier) ar = copy.deepcopy(ANNUAL_REPORT) ar['filing']['header'].pop('name', None) with pytest.raises(BusinessException) as excinfo: factory_filing(b, ar) assert excinfo.value.status_code == HTTPStatus.UNPROCESSABLE_ENTITY assert excinfo.value.error == 'No filings found.'
def test_ia_completed_bcomp_original(session, app): """Assert that IA + Certificate documents with (Original) are returned for a COMPLETED IA.""" document_meta = DocumentMetaService() b = factory_business(identifier='BC1234567', entity_type=Business.LegalTypes.BCOMP.value) with app.app_context(): original_filing = factory_filing(b, INCORPORATION_FILING_TEMPLATE) CORRECTION_INCORPORATION['filing']['correction'][ 'correctedFilingId'] = original_filing.id corrected_filing = factory_filing(b, CORRECTION_INCORPORATION) filing = { 'filing': { 'header': { 'filingId': original_filing.id, 'status': 'COMPLETED', 'name': 'incorporationApplication', 'inColinOnly': False, 'availableOnPaperOnly': False, 'effectiveDate': FILING_DATE, 'date': FILING_DATE }, 'business': { 'identifier': 'BC1234567' } } } original_filing.parent_filing_id = corrected_filing.id original_filing.save() documents = document_meta.get_documents(filing) assert len(documents) == 3 assert documents[0]['type'] == 'REPORT' assert documents[0]['reportType'] is None assert documents[0]['filingId'] == original_filing.id assert documents[0]['title'] == 'Incorporation Application (Original)' assert documents[0][ 'filename'] == 'BC1234567 - Incorporation Application (Original) - 2020-07-14.pdf' assert documents[1]['type'] == 'REPORT' assert documents[1]['reportType'] == 'noa' assert documents[1]['filingId'] == original_filing.id assert documents[1]['title'] == NOA_TITLE assert documents[1]['filename'] == NOA_FILENAME assert documents[2]['type'] == 'REPORT' assert documents[2]['reportType'] == 'certificate' assert documents[2]['filingId'] == original_filing.id assert documents[2]['title'] == 'Certificate (Original)' assert documents[2][ 'filename'] == 'BC1234567 - Certificate (Original) - 2020-07-14.pdf'
def test_post_comment_mismatch_filingid_error(session, client, jwt): """Assert that error is returned when filing ID in URL doesn't match filing ID in json.""" b = factory_business('CP1111111') f = factory_filing(b, ANNUAL_REPORT) b2 = factory_business('CP2222222') f2 = factory_filing(b2, ANNUAL_REPORT) json_data = copy.deepcopy(SAMPLE_JSON_DATA) json_data['comment']['filingId'] = f2.id rv = client.post(f'/api/v2/businesses/{b.identifier}/filings/{f.id}/comments', json=json_data, headers=create_header(jwt, [STAFF_ROLE])) assert HTTPStatus.BAD_REQUEST == rv.status_code
def test_get_annual_report_pdf_error_on_multiple(session, client, jwt, role): """Assert that the businesses AR can be returned as a PDF.""" identifier = 'CP7654321' b = factory_business(identifier) factory_filing(b, ANNUAL_REPORT) rv = client.get(f'/api/v1/businesses/{identifier}/filings', headers=create_header(jwt, [role], identifier, **{'accept': 'application/pdf'})) assert rv.status_code == HTTPStatus.NOT_ACCEPTABLE assert rv.content_type == 'application/json' assert rv.json == { 'message': 'Cannot return a single PDF of multiple filing submissions.' }
def test_get_receipt_request_mock(session, client, jwt, requests_mock): """Assert that a receipt is generated.""" from legal_api.resources.v2.business.business_filings.business_documents import _get_receipt # Setup identifier = 'CP7654321' business = factory_business(identifier) filing_name = 'incorporationApplication' payment_id = '12345' filing_json = copy.deepcopy(FILING_HEADER) filing_json['filing']['header']['name'] = filing_name filing_json['filing'][filing_name] = INCORPORATION filing_json['filing'].pop('business') filing_date = datetime.utcnow() filing = factory_filing(business, filing_json, filing_date=filing_date) filing.skip_status_listener = True filing._status = 'PAID' filing._payment_token = payment_id filing.save() requests_mock.post(f"{current_app.config.get('PAYMENT_SVC_URL')}/{payment_id}/receipts", json={'foo': 'bar'}, status_code=HTTPStatus.CREATED) rv = client.get(f'/api/v2/businesses/{identifier}/filings/{filing.id}/documents/receipt', headers=create_header(jwt, [STAFF_ROLE], identifier, **{'accept': 'application/pdf'}) ) assert rv.status_code == HTTPStatus.CREATED assert requests_mock.called_once
def test_get_receipt(session, client, jwt, requests_mock): """Assert that a receipt is generated.""" from legal_api.resources.v2.business.business_filings.business_documents import _get_receipt # Setup identifier = 'CP7654321' business = factory_business(identifier) filing_name = 'incorporationApplication' payment_id = '12345' filing_json = copy.deepcopy(FILING_HEADER) filing_json['filing']['header']['name'] = filing_name filing_json['filing'][filing_name] = INCORPORATION filing_json['filing'].pop('business') filing_date = datetime.utcnow() filing = factory_filing(business, filing_json, filing_date=filing_date) filing.skip_status_listener = True filing._status = 'PAID' filing._payment_token = payment_id filing.save() filing_core = Filing() filing_core._storage = filing requests_mock.post(f"{current_app.config.get('PAYMENT_SVC_URL')}/{payment_id}/receipts", json={'foo': 'bar'}, status_code=HTTPStatus.CREATED) token = helper_create_jwt(jwt, roles=[STAFF_ROLE], username='******') content, status_code = _get_receipt(business, filing_core, token) assert status_code == HTTPStatus.CREATED assert requests_mock.called_once
def test_comment_json_output(session, client, jwt): """Assert the json output of a comment is correctly formatted.""" identifier = 'CP7654321' b = factory_business(identifier) f = factory_filing(b, ANNUAL_REPORT) u = User(username='******', firstname='firstname', lastname='lastname', sub='sub', iss='iss') u.save() c = factory_comment(b, f, 'some specific text', u) system_timezone = datetime.datetime.now().astimezone().tzinfo expected_timestamp = \ datetime.datetime(1970, 1, 1, 0, 0).replace(tzinfo=datetime.timezone.utc).astimezone(tz=system_timezone) rv = client.get( f'/api/v1/businesses/{identifier}/filings/{f.id}/comments/{c.id}', headers=create_header(jwt, [STAFF_ROLE])) assert HTTPStatus.OK == rv.status_code assert 'some specific text' == rv.json.get('comment').get('comment') assert 'firstname lastname' == rv.json.get('comment').get( 'submitterDisplayName') assert expected_timestamp.isoformat() == rv.json.get('comment').get( 'timestamp')
def test_get_internal_filings(session, client, jwt): """Assert that the internal filings get endpoint returns all completed filings without colin ids.""" from legal_api.models import Filing from tests.unit.models import factory_error_filing, factory_pending_filing # setup identifier = 'CP7654321' b = factory_business(identifier) factory_business_mailing_address(b) filing1 = factory_completed_filing(b, ANNUAL_REPORT) filing2 = factory_completed_filing(b, ANNUAL_REPORT) filing3 = factory_pending_filing(b, ANNUAL_REPORT) filing4 = factory_filing(b, ANNUAL_REPORT) filing5 = factory_error_filing(b, ANNUAL_REPORT) assert filing1.status == Filing.Status.COMPLETED.value # completed with colin_event_id filing2.colin_event_id = 1234 filing2.save() assert filing2.status == Filing.Status.COMPLETED.value assert filing2.colin_event_id is not None # pending with no colin_event_id assert filing3.status == Filing.Status.PENDING.value # draft with no colin_event_id assert filing4.status == Filing.Status.DRAFT.value # error with no colin_event_id assert filing5.status == Filing.Status.ERROR.value # test endpoint returned filing1 only (completed with no colin id set) rv = client.get(f'/api/v1/businesses/internal/filings') assert rv.status_code == HTTPStatus.OK assert len(rv.json) == 1 assert rv.json[0]['filing']['header']['filingId'] == filing1.id
def test_update_annual_report_to_a_business(session, client, jwt): """Assert that a filing can be updated if not paid.""" identifier = 'CP7654321' business = factory_business(identifier, founding_date=(datetime.utcnow() - datedelta.YEAR)) factory_business_mailing_address(business) ar = copy.deepcopy(ANNUAL_REPORT) ar['filing']['header']['date'] = (datetime.utcnow().date() - datedelta.MONTH).isoformat() ar['filing']['annualReport']['annualReportDate'] = datetime.utcnow().date( ).isoformat() ar['filing']['annualReport']['annualGeneralMeetingDate'] = datetime.utcnow( ).date().isoformat() filings = factory_filing(business, ar) ar['filing']['header']['date'] = datetime.utcnow().date().isoformat() rv = client.put(f'/api/v1/businesses/{identifier}/filings/{filings.id}', json=ar, headers=create_header(jwt, [STAFF_ROLE], identifier)) ar['filing']['header']['submitter'] = identifier ar['filing']['header']['date'] = rv.json['filing']['header']['date'] assert rv.status_code == HTTPStatus.ACCEPTED assert rv.json['filing']['business'] == ar['filing']['business'] assert rv.json['filing']['annualReport'] == ar['filing']['annualReport'] assert rv.json['filing']['header']['filingId'] assert rv.json['filing']['header']['submitter'] assert rv.json['filing']['header']['paymentToken']
def test_post_comment_missing_text_error(session, client, jwt): """Assert that the post fails when missing comment text in json (null and missing).""" b = factory_business('CP1111111') f = factory_filing(b, ANNUAL_REPORT) # test null comment text json_data = copy.deepcopy(SAMPLE_JSON_DATA) json_data['comment']['comment'] = None rv = client.post( f'/api/v1/businesses/{b.identifier}/filings/{f.id}/comments', json=json_data, headers=create_header(jwt, [STAFF_ROLE])) assert HTTPStatus.UNPROCESSABLE_ENTITY == rv.status_code # test missing comment text json_data = copy.deepcopy(SAMPLE_JSON_DATA) del json_data['comment']['comment'] rv = client.post( f'/api/v1/businesses/{b.identifier}/filings/{f.id}/comments', json=json_data, headers=create_header(jwt, [STAFF_ROLE])) assert HTTPStatus.UNPROCESSABLE_ENTITY == rv.status_code
def test_update_ar_with_a_missing_filing_id_fails(session, client, jwt): """Assert that updating a missing filing fails.""" import copy identifier = 'CP7654321' business = factory_business( identifier, founding_date=(datetime.utcnow() - datedelta.datedelta(years=2)), last_ar_date=datetime(datetime.utcnow().year - 1, 4, 20).date()) factory_business_mailing_address(business) ar = copy.deepcopy(ANNUAL_REPORT) ar['filing']['annualReport']['annualReportDate'] = datetime( datetime.utcnow().year, 2, 20).date().isoformat() ar['filing']['annualReport']['annualGeneralMeetingDate'] = datetime.utcnow( ).date().isoformat() filings = factory_filing(business, ar) rv = client.put(f'/api/v1/businesses/{identifier}/filings/{filings.id+1}', json=ar, headers=create_header(jwt, [STAFF_ROLE], identifier)) assert rv.status_code == HTTPStatus.NOT_FOUND assert rv.json['errors'][0] == { 'message': f'{identifier} no filings found' }
def test_correction_ia_with_named_to_numbered(session, app): """Assert that IA + NOA + Certificate documents are returned for a Correction filing with name change.""" document_meta = DocumentMetaService() b = factory_business(identifier='BC1234567', entity_type=Business.LegalTypes.BCOMP.value) original_filing_json = copy.deepcopy(INCORPORATION_FILING_TEMPLATE) original_filing_json['filing']['incorporationApplication'][ 'nameRequest'] = {} original_filing_json['filing']['incorporationApplication']['nameRequest'][ 'nrNumber'] = 'NR 1234567' original_filing_json['filing']['incorporationApplication']['nameRequest'][ 'legalName'] = 'abc' original_filing = factory_filing(b, original_filing_json) with app.app_context(): filing = { 'filing': { 'header': { 'filingId': 12357, 'status': 'COMPLETED', 'name': 'correction', 'inColinOnly': False, 'availableOnPaperOnly': False, 'date': FILING_DATE }, 'business': { 'identifier': 'BC1234567' }, 'correction': { 'correctedFilingId': original_filing.id }, 'incorporationApplication': { 'nameRequest': { 'legalType': Business.LegalTypes.BCOMP.value } } } } documents = document_meta.get_documents(filing) assert len(documents) == 3 assert documents[0]['type'] == 'REPORT' assert documents[0]['reportType'] is None assert documents[0]['filingId'] == 12357 assert documents[0]['title'] == 'Incorporation Application (Corrected)' assert documents[0][ 'filename'] == 'BC1234567 - Incorporation Application (Corrected) - 2020-07-14.pdf' assert documents[1]['type'] == 'REPORT' assert documents[1]['reportType'] == 'certificate' assert documents[1]['filingId'] == 12357 assert documents[1]['title'] == 'Certificate (Corrected)' assert documents[1][ 'filename'] == 'BC1234567 - Certificate (Corrected) - 2020-07-14.pdf' assert documents[2]['type'] == 'REPORT' assert documents[2]['reportType'] == 'noa' assert documents[2]['filingId'] == 12357 assert documents[2]['title'] == NOA_TITLE assert documents[2]['filename'] == NOA_FILENAME
def test_get_filing_submission_pdf(requests_mock, session, client, jwt, role, filing_submission): """Assert that the businesses AR can be returned as a PDF.""" from flask import current_app identifier = 'CP7654321' b = factory_business(identifier) filings = factory_filing(b, filing_submission) print('test_get_all_business_filings - filing:', filings) requests_mock.post(current_app.config.get('REPORT_SVC_URL'), json={'foo': 'bar'}) rv = client.get(f'/api/v1/businesses/{identifier}/filings/{filings.id}', headers=create_header(jwt, [role], identifier, **{'accept': 'application/pdf'})) ignore_vars = {'templateVars': {'environment': 'ignored'}} assert rv.status_code == HTTPStatus.OK assert requests_mock.called_once assert requests_mock.last_request._request.headers.get( 'Content-Type') == 'application/json' assert matches_sent_snapshot(filing_submission, requests_mock.last_request.json(), **ignore_vars)
def test_update_ar_with_colin_id_set(session, client, jwt): """Assert that when a filing with colinId set (as when colin updates legal api) that colin_event_id is set.""" identifier = 'CP7654321' business = factory_business(identifier, founding_date=(datetime.utcnow() - datedelta.YEAR)) factory_business_mailing_address(business) ar = copy.deepcopy(ANNUAL_REPORT) ar['filing']['annualReport']['annualReportDate'] = datetime.utcnow().date( ).isoformat() ar['filing']['annualReport']['annualGeneralMeetingDate'] = datetime.utcnow( ).date().isoformat() ar['filing']['header']['date'] = datetime.utcnow().date().isoformat() filings = factory_filing(business, ar) ar['filing']['header']['colinId'] = 1234 rv = client.put(f'/api/v1/businesses/{identifier}/filings/{filings.id}', json=ar, headers=create_header(jwt, [STAFF_ROLE], identifier)) assert rv.status_code == HTTPStatus.ACCEPTED assert rv.json['filing']['business'] == ar['filing']['business'] assert rv.json['filing']['annualReport'] == ar['filing']['annualReport'] assert not rv.json['filing']['header'].get('colinId') assert rv.json['filing']['header']['filingId'] == filings.id
def test_bcorp_get_tasks_prev_year_incomplete_filing_exists(session, client, jwt): """Assert that the one incomplete filing for previous year and a to-do for current year are returned.""" identifier = 'CP7654321' b = factory_business(identifier, datetime.now() - datedelta.datedelta(years=2), last_ar_date=datetime(2018, 3, 3)) filings = factory_filing(b, AR_FILING_PREVIOUS_YEAR, datetime(2018, 8, 5, 7, 7, 58, 272362)) print('test_get_all_business_filings - filing:', filings) rv = client.get(f'/api/v1/businesses/{identifier}/tasks', headers=create_header(jwt, [STAFF_ROLE], identifier)) assert rv.status_code == HTTPStatus.OK
def test_delete_comment_error(session, client, jwt): """Assert that the DELETE endpoint isn't allowed.""" b = factory_business('CP1111111') f = factory_filing(b, ANNUAL_REPORT) c = factory_comment(b, f) rv = client.delete(f'/api/v2/businesses/{b.identifier}/filings/{f.id}/comments/{c.id}', headers=create_header(jwt, [STAFF_ROLE])) assert HTTPStatus.METHOD_NOT_ALLOWED == rv.status_code
def test_delete_filing_not_authorized(session, client, jwt): """Assert that a users is authorized to delete a filing.""" identifier = 'CP7654321' b = factory_business(identifier) filings = factory_filing(b, ANNUAL_REPORT) headers = create_header(jwt, ['BAD ROLE'], identifier) rv = client.delete(f'/api/v1/businesses/{identifier}/filings/{filings.id}', headers=headers) assert rv.status_code == HTTPStatus.UNAUTHORIZED
def test_get_comments_invalid_business_error(session, client, jwt): """Assert that error is returned when business doesn't exist.""" b = factory_business('CP1111111') f = factory_filing(b, ANNUAL_REPORT) rv = client.get(f'/api/v1/businesses/CP2222222/filings/{f.id}/comments', headers=create_header(jwt, [STAFF_ROLE])) assert HTTPStatus.NOT_FOUND == rv.status_code assert 'CP2222222 not found' == rv.json.get('message')
def test_filing_delete_is_blocked(session): """Assert that an AR filing can be saved.""" b = factory_business('CP1234567') filing = factory_filing(b, AR_FILING) with pytest.raises(BusinessException) as excinfo: filing.delete() assert excinfo.value.status_code == HTTPStatus.FORBIDDEN assert excinfo.value.error == 'Deletion not allowed.'
def test_get_tasks_current_year_filing_exists(session, client, jwt): """Assert that only the filing for the current year is returned when only current year filing exists.""" identifier = 'CP7654321' b = factory_business(identifier=identifier, last_ar_date=datetime(2018, 8, 13)) filings = factory_filing(b, AR_FILING_CURRENT_YEAR, datetime(2019, 8, 5, 7, 7, 58, 272362), 'annualReport') print('test_get_all_business_filings - filing:', filings) rv = client.get(f'/api/v1/businesses/{identifier}/tasks', headers=create_header(jwt, [STAFF_ROLE], identifier)) assert rv.status_code == HTTPStatus.OK
def test_delete_filing_in_draft(session, client, jwt): """Assert that a draft filing can be deleted.""" identifier = 'CP7654321' b = factory_business(identifier) filings = factory_filing(b, ANNUAL_REPORT) headers = create_header(jwt, [STAFF_ROLE], identifier) rv = client.delete(f'/api/v1/businesses/{identifier}/filings/{filings.id}', headers=headers) assert rv.status_code == HTTPStatus.OK
def test_bcorp_get_tasks_prev_year_incomplete_filing_exists(session, client): """Assert that the one incomplete filing for previous year and a to-do for current year are returned.""" identifier = 'CP7654321' b = factory_business(identifier, datetime.now() - datedelta.datedelta(years=2), last_ar_date='2018-03-03') filings = factory_filing(b, AR_FILING_PREVIOUS_YEAR, datetime(2018, 8, 5, 7, 7, 58, 272362)) print('test_get_all_business_filings - filing:', filings) rv = client.get(f'/api/v1/businesses/{identifier}/tasks') assert rv.status_code == HTTPStatus.OK assert len(rv.json.get('tasks')) == 2 # Previous year filing and a disabled to-do for current year.
def test_get_comments_mismatch_business_filing_error(session, client, jwt): """Assert that error is returned when filing isn't owned by business.""" b1 = factory_business('CP1111111') b2 = factory_business('CP2222222') f = factory_filing(b2, ANNUAL_REPORT) rv = client.get(f'/api/v2/businesses/{b1.identifier}/filings/{f.id}/comments', headers=create_header(jwt, [STAFF_ROLE])) assert HTTPStatus.NOT_FOUND == rv.status_code assert f'Filing {f.id} not found' == rv.json.get('message')
def test_get_all_filing_comments_no_results(session, client, jwt): """Assert that endpoint returns no-results correctly.""" identifier = 'CP7654321' b = factory_business(identifier) f = factory_filing(b, ANNUAL_REPORT) rv = client.get(f'/api/v1/businesses/{identifier}/filings/{f.id}/comments', headers=create_header(jwt, [STAFF_ROLE])) assert rv.status_code == HTTPStatus.OK assert 0 == len(rv.json.get('comments'))
def test_get_tasks_current_year_filing_exists(session, client): """Assert that only the filing for the current year is returned when only current year filing exists.""" identifier = 'CP7654321' b = factory_business(identifier=identifier, last_ar_date='2019-08-13') filings = factory_filing(b, AR_FILING_CURRENT_YEAR, datetime(2019, 8, 5, 7, 7, 58, 272362)) print('test_get_all_business_filings - filing:', filings) rv = client.get(f'/api/v1/businesses/{identifier}/tasks') assert rv.status_code == HTTPStatus.OK assert len(rv.json.get('tasks')) == 1 # Current year incomplete filing only
def test_get_one_filing_comment_by_id(session, client, jwt): """Assert that a single comment is returned correctly.""" identifier = 'CP7654321' b = factory_business(identifier) f = factory_filing(b, ANNUAL_REPORT) c = factory_comment(b, f, 'some specific text') rv = client.get(f'/api/v2/businesses/{identifier}/filings/{f.id}/comments/{c.id}', headers=create_header(jwt, [STAFF_ROLE])) assert HTTPStatus.OK == rv.status_code assert 'some specific text' == rv.json.get('comment').get('comment')
def test_get_all_filing_comments_only_one(session, client, jwt): """Assert that a list of comments with a single comment is returned correctly.""" identifier = 'CP7654321' b = factory_business(identifier) f = factory_filing(b, ANNUAL_REPORT) factory_comment(b, f) rv = client.get(f'/api/v1/businesses/{identifier}/filings/{f.id}/comments', headers=create_header(jwt, [STAFF_ROLE])) assert HTTPStatus.OK == rv.status_code assert 1 == len(rv.json.get('comments'))
def test_post_comment_basic_user_error(session, client, jwt): """Assert that the post fails when sent from Basic (non-staff) user.""" b = factory_business('CP1111111') f = factory_filing(b, ANNUAL_REPORT) json_data = copy.deepcopy(SAMPLE_JSON_DATA) rv = client.post(f'/api/v2/businesses/{b.identifier}/filings/{f.id}/comments', json=json_data, headers=create_header(jwt, [BASIC_USER])) assert HTTPStatus.UNAUTHORIZED == rv.status_code
def test_get_all_business_filings_multiple(session, client, jwt): """Assert that multiple filings are returned correctly.""" identifier = 'CP7654321' b = factory_business(identifier) f = factory_filing(b, ANNUAL_REPORT) factory_comment(b, f) factory_comment(b, f, 'other text') rv = client.get(f'/api/v1/businesses/{identifier}/filings/{f.id}/comments', headers=create_header(jwt, [STAFF_ROLE])) assert HTTPStatus.OK == rv.status_code assert 2 == len(rv.json.get('comments'))
def test_comments_in_filing_response(session, client, jwt): """Assert that the list of comments is the filing GET response.""" b = factory_business('CP1111111') f = factory_filing(b, ANNUAL_REPORT) factory_comment(b, f, 'comment 1') factory_comment(b, f, 'comment 2') rv = client.get(f'/api/v1/businesses/{b.identifier}/filings/{f.id}', headers=create_header(jwt, [STAFF_ROLE])) assert rv.status_code == HTTPStatus.OK assert None is not rv.json['filing']['header'].get('comments') assert 2 == len(rv.json['filing']['header'].get('comments'))