from flask_jwt_extended import create_access_token import json from app import model from .conftest import MOCK_USER_1, assert_200, assert_response_json_equal, assert_not_none, assert_equal, assert_400, assert_402 def test_get_application(client): url = '/api/ajax/application' headers = {'Auth-Token': f'Bearer {create_access_token(MOCK_USER_1)}'} resp = client.get(url, headers=headers) assert_200(resp) assert_response_json_equal(resp, { 'applicants': [ { 'email': MOCK_USER_1.email, 'firstName': MOCK_USER_1.first_name, 'middleInitial': '', 'lastName': MOCK_USER_1.last_name, 'numberDependents': 0, 'agesDependents': [], 'marriageStatus': 'single', 'dateOfBirth': MOCK_USER_1.date_of_birth.date().strftime('%b %d, %Y'), 'liabilities': [], 'otherIncomes': [], 'homePhone': MOCK_USER_1.phone_number, 'previousAddresses': [], 'previousJobs': [], 'assets': { 'assets': [{ 'depositHeldBy': '', 'value': 0.0, 'description': '' }], 'checkingSavings': [], 'lifeInsurance': [], 'propertyOwned': [], 'retirementFunds': [], 'stocksBonds': [], 'vehiclesOwned': [], 'otherAssets': '' } } ], 'finalized': False, 'propertyInfo': { 'existingLiens': [], 'fundingPurpose': 'purchase' }, 'filingType': 'single', 'userID': MOCK_USER_1.user_id }) # There should now be an application object with this user in it application = model.Application.objects(applicants__match={'email': MOCK_USER_1.email}).first() assert_not_none(application) application.property_info = model.Property(original_cost=100000.00, address1='123 Example Lane', city='Boulder', state='CO', zip='80303', number_of_units=1) applicant = model.Applicant(email='test@test.com', first_name='another', last_name='applicant', assets=model.Assets(life_insurance=[model.LifeInsurance(value=10000.0, face_amount=0.0)]), previous_jobs=[model.Job(date_from='Mar 10, 2020', date_to='Apr 10, 2021')]) application.applicants.append(applicant) application.save() resp = client.get(url, headers=headers) assert_200(resp) assert_response_json_equal(resp, { 'applicants': [ { 'email': MOCK_USER_1.email, 'firstName': MOCK_USER_1.first_name, 'middleInitial': '', 'lastName': MOCK_USER_1.last_name, 'numberDependents': 0, 'agesDependents': [], 'marriageStatus': 'single', 'dateOfBirth': MOCK_USER_1.date_of_birth.date().strftime('%b %d, %Y'), 'liabilities': [], 'otherIncomes': [], 'homePhone': MOCK_USER_1.phone_number, 'previousAddresses': [], 'previousJobs': [], 'assets': { 'assets': [{ 'depositHeldBy': '', 'description': '', 'value': 0.0 }], 'checkingSavings': [], 'lifeInsurance': [], 'propertyOwned': [], 'retirementFunds': [], 'stocksBonds': [], 'vehiclesOwned': [], 'otherAssets': '' } }, { 'email': 'test@test.com', 'firstName': 'another', 'lastName': 'applicant', 'numberDependents': 0, 'agesDependents': [], 'liabilities': [], 'otherIncomes': [], 'previousAddresses': [], 'previousJobs': [{ 'dateFrom': 'Mar 10, 2020', 'dateTo': 'Apr 10, 2021', 'yearsMonthsOnJob': '1 Year(s), 1 Month(s)' }], 'marriageStatus': 'single', 'assets': { 'assets': [{ 'depositHeldBy': '', 'description': '', 'value': 0.0 }], 'lifeInsurance': [{ 'value': 10000.0, 'faceAmount': 0.0 }], 'propertyOwned': [], 'retirementFunds': [], 'stocksBonds': [], 'vehiclesOwned': [], 'checkingSavings': [], 'otherAssets': '' } } ], 'finalized': False, 'propertyInfo': { 'fundingPurpose': 'purchase', 'originalCost': 100000.00, 'address1': '123 Example Lane', 'city': 'Boulder', 'state': 'CO', 'zip': '80303', 'numberOfUnits': 1, 'existingLiens': [] }, 'filingType': 'single', 'userID': MOCK_USER_1.user_id }) # clean up the applications from this test for app in model.Application.objects().all(): app.delete() def test_post_application(client): url = 'api/ajax/application' headers = {'Auth-Token': f'Bearer {create_access_token(MOCK_USER_1)}'} test_post_data = { "filingType": "single", "depositHeldBy": "", "applicants": [ { "userName": "", "firstName": "", "middleInitial": "", "lastName": "", "email": f'{MOCK_USER_1.email}', "pin": "", "role": "", "socialSecurityNumber": "", "currentAddress": "", "homePhone": "", "dateOfBirth": "", "yearsSchool": "", "degreeEarned": "", "schoolName": "", "marriageStatus": "married", "numberDependants": "", "agesDependents": [], "previousJobs": [{ "employerInfo": "", "selfEmployed": False, "yearsOnJob": "", "dateFrom": "", "dateTo": "", "monthlyIncome": None, "positionTitle": "", "busPhone": "" }], "previousAddresses": [{ "ownOrRent": "", "years": "" }], "expenseInfo": { "baseEmpInc": None, "overTime": None, "bonuses": None, "commissions": None, "dividendsInterest": None, "netRentalIncome": None, "other": { "description": "", "value": None } }, "otherIncomes": [ { "description": "", "value": None } ], "assets": { "assets": [], "checkingSavings": [{ "bankSlCu": "", "acctNumber": "", "value": None }], "stocksBonds": [{ "description": "", "value": None }], "lifeInsurance": [{ "faceAmount": None, "value": None } ], "retirementFunds": [{ "description": "", "value": None }], "vehiclesOwned": [{ "description": "", "value": None }], "propertyOwned": [{ "address": "", "status": "", "type": "", "presentMarketValue": None, "amountOfMortgages": None, "grossRentalIncome": None, "mortgagePayments": None, "insuranceMainTaxesMisc": None, "netRentalIncome": None }] }, "liabilities": [{ "description": "", "value": None }], "declarations": { "anyJudgements": False, "declaredBankruptcy": False, "propertyForeclosed": False, "partyToLawsuit": False, "obligatedLoanForeclosure": False, "delinquentOrDefault": False, "alimonyChildSupportMaintenance": False, "downPaymentBorrowed": False, "comakerOrEndorser": False, "intendPrimaryResidence": False }, "acknowledgeAndAgree": { "signature": None, "date": "" }, "governmentInfo": { "willNotFurnish": False, "ethnicity": False, "sex": "", "race": "" } } ], "propertyInfo": {}, "finalized": False, "userID": MOCK_USER_1.user_id } resp = client.post(url, headers=headers, data=json.dumps(test_post_data)) assert_200(resp) application = model.Application.objects(applicants__match={'email': MOCK_USER_1.email}).first() assert_not_none(application) resp = client.get(url, headers=headers) assert_200(resp) # If we send bad data, we should get a 400 and a nice message back test_post_data['applicants'].append({ 'email': 'someone@somewhere.com', 'firstName': 'another', 'lastName': 'applicant', 'dateOfBirth': 'Mar 17, 1995', 'expenseInfo': {'other': {"value": "bad-value"}} }) resp = client.post(url, headers=headers, data=json.dumps(test_post_data)) assert_400(resp) assert_equal(resp.json['error'], 'Field "applicants.[1].expenseInfo.other.value": \'bad-value\' is not of type \'number\'') # if we remove the bad data, we should be able to add a new applicant to the application. test_post_data['applicants'][1].pop('expenseInfo') resp = client.post(url, headers=headers, data=json.dumps(test_post_data)) assert_200(resp) applications = list(model.Application.objects().all()) # we should have edited an application instead of crating a new one assert_equal(len(applications), 1) application = model.Application.objects(applicants__match={'email': MOCK_USER_1.email}).first() assert_not_none(application) assert_equal(len(application.applicants), 2) assert_equal(application.applicants[1].first_name, 'another') assert_equal(application.applicants[1].last_name, 'applicant') assert_equal(application.applicants[1].email, 'someone@somewhere.com') # we should be able to post JSON data too test_post_data['applicants'][1]['middleInitial'] = 'G' resp = client.post(url, headers=headers, json=test_post_data) assert_200(resp) # this should have updated the second applicant... application = model.Application.objects(applicants__match={'email': MOCK_USER_1.email}).first() assert_equal(len(application.applicants), 2) assert_equal(application.applicants[1].middle_init_or_name, 'G') assert_equal(application.applicants[1].email, 'someone@somewhere.com') # we should be able to update the property info test_post_data['propertyInfo'] = { 'yearBuilt': '1990', 'yearAcquired': '2000' } resp = client.post(url, headers=headers, json=test_post_data) assert_200(resp) application = model.Application.objects(applicants__match={'email': MOCK_USER_1.email}).first() assert_not_none(application) assert_equal(application.property_info.year_built, '1990') assert_equal(application.property_info.year_acquired, '2000') # we should be able to update the first applicant... test_post_data['applicants'][0]['dateOfBirth'] = 'Jan 01, 1995' resp = client.post(url, headers=headers, json=test_post_data) assert_200(resp) application = model.Application.objects(applicants__match={'email': MOCK_USER_1.email}).first() assert_equal(application.applicants[0].date_of_birth, 'Jan 01, 1995') # trying to post an applicant with no email should cause a 400 email = test_post_data['applicants'][1].pop('email') resp = client.post(url, headers=headers, json=test_post_data) assert_400(resp) assert_equal(resp.json['error'], 'Field "applicants.[1]": \'email\' is a required property') # if the request is finalized, it shouldn't be editable test_post_data['applicants'][1]['email'] = email resp = client.post(url, headers=headers, json=test_post_data) assert_200(resp) # removing an applicant should be final del test_post_data['applicants'][1] resp = client.post(f'{url}/true', headers=headers, json=test_post_data) assert_200(resp) application = model.Application.objects(applicants__match={'email': MOCK_USER_1.email}).first() assert_equal(len(application.applicants), 1) # after finalizing, the request should fail resp = client.post(url, headers=headers, json=test_post_data) assert_402(resp) # clean up after the test for app in model.Application.objects().all(): app.delete()