|
| 1 | +import axios from "axios"; |
| 2 | +import { getLoggedInUser } from '../utils/user'; |
| 3 | +import db from '../utils/db'; |
| 4 | +import app from '../Server'; |
| 5 | +import * as reqs from '../utils/request'; |
| 6 | +import AuthorizationToken from "../entities/AuthorizationToken"; |
| 7 | + |
| 8 | +const BB2_BASE_URL = "https://sandbox.bluebutton.cms.gov"; |
| 9 | + |
| 10 | +const BB2_PATIENT_URL = `${BB2_BASE_URL}/v2/fhir/Patient/`; |
| 11 | + |
| 12 | +const BB2_COVERAGE_URL = `${BB2_BASE_URL}/v2/fhir/Coverage/`; |
| 13 | + |
| 14 | +const BB2_EOB_URL = `${BB2_BASE_URL}/v2/fhir/ExplanationOfBenefit/`; |
| 15 | + |
| 16 | +const BB2_PROFILE_URL = `${BB2_BASE_URL}/v2/connect/userinfo`; |
| 17 | + |
| 18 | +const BB2_AUTH_TOKEN_URL = `${BB2_BASE_URL}/v2/o/token/`; |
| 19 | + |
| 20 | +const eob = { status: 200, data: { resource: "EOB" } }; |
| 21 | + |
| 22 | +const coverage = { status: 200, data: { resource: "Coverage" } }; |
| 23 | + |
| 24 | +const patient = { status: 200, data: { resource: "Patient" } }; |
| 25 | + |
| 26 | +const profile = { status: 200, data: { resource: "Profile" } }; |
| 27 | + |
| 28 | +const MOCK_AUTH_TOKEN_RESPONSE = { |
| 29 | + status: 200, |
| 30 | + data: { |
| 31 | + access_token: "access_token_foo_refreshed", |
| 32 | + expires_in: 36000, |
| 33 | + token_type: "Bearer", |
| 34 | + scope: ["scope1", "scope2", "scope3"], |
| 35 | + refresh_token: "refresh_token_bar_refreshed", |
| 36 | + patient: "-19990000000001", |
| 37 | + }, |
| 38 | + }; |
| 39 | + |
| 40 | +const MOCK_AUTH_TOKEN = new AuthorizationToken(MOCK_AUTH_TOKEN_RESPONSE.data); |
| 41 | + |
| 42 | +let server: any; |
| 43 | + |
| 44 | +beforeAll(() => { |
| 45 | + server = app.listen(Number(3003), () => { |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | +afterAll(() => { |
| 50 | + server.close(); |
| 51 | +}); |
| 52 | + |
| 53 | +test("expect patient end point returns patient data.", async () => { |
| 54 | + // mock patient returned at deeper layer |
| 55 | + jest.spyOn(reqs, 'get').mockImplementation((url, params, token) => |
| 56 | + { |
| 57 | + if (url === BB2_PATIENT_URL) { |
| 58 | + return Promise.resolve(patient); |
| 59 | + } else { |
| 60 | + throw Error("Invalid end point URL: " + url); |
| 61 | + } |
| 62 | + } |
| 63 | + ); |
| 64 | + |
| 65 | + const response = await axios.get("http://localhost:3003/api/data/patient"); |
| 66 | + |
| 67 | + expect(response.status).toEqual(200); |
| 68 | + expect(response.data).toEqual(patient.data); |
| 69 | +}); |
| 70 | + |
| 71 | +test("expect profile end point returns profile data.", async () => { |
| 72 | + // mock profile returned at lower level get |
| 73 | + jest.spyOn(reqs, 'get').mockImplementation((url, params, token) => |
| 74 | + { |
| 75 | + if (url === BB2_PROFILE_URL) { |
| 76 | + return Promise.resolve(profile); |
| 77 | + } else { |
| 78 | + throw Error("Invalid end point URL: " + url); |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + const response = await axios.get("http://localhost:3003/api/data/userprofile"); |
| 83 | + expect(response.status).toEqual(200); |
| 84 | + expect(response.data).toEqual(profile.data); |
| 85 | +}); |
| 86 | + |
| 87 | +test("expect coverage end point returns coverage data.", async () => { |
| 88 | + // mock coverage returned at deeper layer |
| 89 | + jest.spyOn(reqs, 'get').mockImplementation((url, params, token) => |
| 90 | + { |
| 91 | + if (url === BB2_COVERAGE_URL) { |
| 92 | + return Promise.resolve(coverage); |
| 93 | + } else { |
| 94 | + throw Error("Invalid end point URL: " + url); |
| 95 | + } |
| 96 | + } |
| 97 | + ); |
| 98 | + |
| 99 | + const response = await axios.get("http://localhost:3003/api/data/coverage"); |
| 100 | + |
| 101 | + expect(response.status).toEqual(200); |
| 102 | + expect(response.data).toEqual(coverage.data); |
| 103 | +}); |
| 104 | + |
| 105 | +test("expect eob end point returns eob data.", async () => { |
| 106 | + const loggedInUser = getLoggedInUser(db); |
| 107 | + |
| 108 | + loggedInUser.authToken = MOCK_AUTH_TOKEN; |
| 109 | + |
| 110 | + // mock eob returned at lower level get |
| 111 | + jest.spyOn(reqs, 'get').mockImplementation((url, params, token) => |
| 112 | + { |
| 113 | + if (url === BB2_EOB_URL) { |
| 114 | + return Promise.resolve(eob); |
| 115 | + } else { |
| 116 | + throw Error("Invalid end point URL: " + url); |
| 117 | + } |
| 118 | + } |
| 119 | + ); |
| 120 | + |
| 121 | + const response = await axios.get("http://localhost:3003/api/data/benefit-direct"); |
| 122 | + console.log(response); |
| 123 | + expect(response.status).toEqual(200); |
| 124 | + expect(response.data).toEqual(eob.data); |
| 125 | +}); |
0 commit comments