@@ -9,14 +9,23 @@ import {
99 generateGetFilingResponse ,
1010 generateUserData ,
1111} from "../../test/factories" ;
12+ import { saveFileFromUrl } from "../domain/s3Writer" ;
1213import { FormationClient , UserDataClient } from "../domain/types" ;
1314import { formationRouterFactory } from "./formationRouter" ;
14- import { getSignedInUserId } from "./userRouter" ;
15+ import { getSignedInUser , getSignedInUserId } from "./userRouter" ;
1516
1617jest . mock ( "./userRouter" , ( ) => ( {
1718 getSignedInUserId : jest . fn ( ) ,
19+ getSignedInUser : jest . fn ( ) ,
1820} ) ) ;
21+
22+ jest . mock ( "../domain/s3Writer.ts" , ( ) => ( {
23+ saveFileFromUrl : jest . fn ( ) ,
24+ } ) ) ;
25+
26+ const fakeSaveFileFromUrl = saveFileFromUrl as jest . Mock ;
1927const fakeSignedInUserId = getSignedInUserId as jest . Mock ;
28+ const fakeSignedInUser = getSignedInUser as jest . Mock ;
2029
2130describe ( "formationRouter" , ( ) => {
2231 let app : Express ;
@@ -26,6 +35,13 @@ describe("formationRouter", () => {
2635 beforeEach ( async ( ) => {
2736 jest . resetAllMocks ( ) ;
2837 fakeSignedInUserId . mockReturnValue ( "some-id" ) ;
38+ fakeSignedInUser . mockReturnValue ( {
39+ sub : "1234" ,
40+ "custom:myNJUserKey" : "1234myNJUserKey" ,
41+ "custom:identityId" : "us-east-1:identityId" ,
42+ email : "whatever@gmail.com" ,
43+ identities : undefined ,
44+ } ) ;
2945 stubFormationClient = {
3046 form : jest . fn ( ) ,
3147 getCompletedFiling : jest . fn ( ) ,
@@ -99,6 +115,18 @@ describe("formationRouter", () => {
99115 } ) ;
100116
101117 describe ( "/completed-filing" , ( ) => {
118+ let dateNowSpy : jest . SpyInstance ;
119+ beforeEach ( ( ) => {
120+ dateNowSpy = jest . spyOn ( Date , "now" ) . mockImplementation ( ( ) => 1487076708000 ) ;
121+ fakeSaveFileFromUrl . mockReset ( ) ;
122+ fakeSaveFileFromUrl . mockImplementation ( ( link , location , bucket ) =>
123+ Promise . resolve ( `http://${ location } ` )
124+ ) ;
125+ } ) ;
126+ afterAll ( ( ) => {
127+ dateNowSpy . mockRestore ( ) ;
128+ } ) ;
129+
102130 it ( "saves and returns updated user data with get-filing response" , async ( ) => {
103131 const getFilingResponse = generateGetFilingResponse ( { success : true } ) ;
104132 stubFormationClient . getCompletedFiling . mockResolvedValue ( getFilingResponse ) ;
@@ -127,9 +155,19 @@ describe("formationRouter", () => {
127155 ...userData . profileData ,
128156 entityId : getFilingResponse . entityId ,
129157 dateOfFormation : userData . formationData . formationFormData . businessStartDate ,
158+ documents : {
159+ ...userData . profileData . documents ,
160+ formationDoc : `http://us-east-1:identityId/formationDoc-1487076708000.pdf` ,
161+ certifiedDoc : `http://us-east-1:identityId/certifiedDoc-1487076708000.pdf` ,
162+ standingDoc : `http://us-east-1:identityId/standingDoc-1487076708000.pdf` ,
163+ } ,
130164 } ,
131165 } ;
132-
166+ expect ( fakeSaveFileFromUrl ) . toHaveBeenCalledWith (
167+ getFilingResponse . formationDoc ,
168+ `us-east-1:identityId/formationDoc-1487076708000.pdf` ,
169+ undefined
170+ ) ;
133171 expect ( response . body ) . toEqual ( expectedNewUserData ) ;
134172 expect ( stubUserDataClient . put ) . toHaveBeenCalledWith ( expectedNewUserData ) ;
135173 expect ( stubFormationClient . getCompletedFiling ) . toHaveBeenCalledWith ( "some-formation-id" ) ;
@@ -155,5 +193,57 @@ describe("formationRouter", () => {
155193 } ,
156194 } ) ;
157195 } ) ;
196+
197+ it ( "only fetches files that are in the filingResponse" , async ( ) => {
198+ const getFilingResponse = generateGetFilingResponse ( { success : true , certifiedDoc : "" } ) ;
199+ stubFormationClient . getCompletedFiling . mockResolvedValue ( getFilingResponse ) ;
200+
201+ const userData = generateUserData ( {
202+ formationData : generateFormationData ( {
203+ formationResponse : generateFormationSubmitResponse ( { formationId : "some-formation-id" } ) ,
204+ } ) ,
205+ } ) ;
206+ stubUserDataClient . get . mockResolvedValue ( userData ) ;
207+ await request ( app ) . get ( `/completed-filing` ) . send ( ) ;
208+
209+ const expectedNewUserData = {
210+ ...userData ,
211+ formationData : {
212+ ...userData . formationData ,
213+ getFilingResponse : getFilingResponse ,
214+ } ,
215+ taskProgress : {
216+ ...userData . taskProgress ,
217+ "form-business-entity" : "COMPLETED" ,
218+ } ,
219+ profileData : {
220+ ...userData . profileData ,
221+ entityId : getFilingResponse . entityId ,
222+ dateOfFormation : userData . formationData . formationFormData . businessStartDate ,
223+ documents : {
224+ ...userData . profileData . documents ,
225+ formationDoc : `http://us-east-1:identityId/formationDoc-1487076708000.pdf` ,
226+ standingDoc : `http://us-east-1:identityId/standingDoc-1487076708000.pdf` ,
227+ } ,
228+ } ,
229+ } ;
230+
231+ expect ( fakeSaveFileFromUrl ) . toHaveBeenCalledWith (
232+ getFilingResponse . formationDoc ,
233+ `us-east-1:identityId/formationDoc-1487076708000.pdf` ,
234+ process . env . DOCUMENT_S3_BUCKET
235+ ) ;
236+ expect ( fakeSaveFileFromUrl ) . toHaveBeenCalledWith (
237+ getFilingResponse . standingDoc ,
238+ `us-east-1:identityId/standingDoc-1487076708000.pdf` ,
239+ process . env . DOCUMENT_S3_BUCKET
240+ ) ;
241+ expect ( fakeSaveFileFromUrl ) . not . toHaveBeenCalledWith (
242+ getFilingResponse . certifiedDoc ,
243+ `us-east-1:identityId/certifiedDoc-1487076708000.pdf` ,
244+ process . env . DOCUMENT_S3_BUCKET
245+ ) ;
246+ expect ( stubUserDataClient . put ) . toHaveBeenCalledWith ( expectedNewUserData ) ;
247+ } ) ;
158248 } ) ;
159249} ) ;
0 commit comments