@@ -21,11 +21,14 @@ const mockAdapter = {
2121
2222// Small additional tests to improve overall coverage
2323describe ( 'FilesController' , ( ) => {
24- it ( 'should properly expand objects' , done => {
24+ it ( 'should properly expand objects with sync getFileLocation ' , async ( ) => {
2525 const config = Config . get ( Parse . applicationId ) ;
2626 const gridFSAdapter = new GridFSBucketAdapter ( 'mongodb://localhost:27017/parse' ) ;
27+ gridFSAdapter . getFileLocation = ( config , filename ) => {
28+ return config . mount + '/files/' + config . applicationId + '/' + encodeURIComponent ( filename ) ;
29+ }
2730 const filesController = new FilesController ( gridFSAdapter ) ;
28- const result = filesController . expandFilesInObject ( config , function ( ) { } ) ;
31+ const result = await filesController . expandFilesInObject ( config , function ( ) { } ) ;
2932
3033 expect ( result ) . toBeUndefined ( ) ;
3134
@@ -37,12 +40,69 @@ describe('FilesController', () => {
3740 const anObject = {
3841 aFile : fullFile ,
3942 } ;
40- filesController . expandFilesInObject ( config , anObject ) ;
43+ await filesController . expandFilesInObject ( config , anObject ) ;
4144 expect ( anObject . aFile . url ) . toEqual ( 'http://an.url' ) ;
45+ } ) ;
4246
43- done ( ) ;
47+ it ( 'should properly expand objects with async getFileLocation' , async ( ) => {
48+ const config = Config . get ( Parse . applicationId ) ;
49+ const gridFSAdapter = new GridFSBucketAdapter ( 'mongodb://localhost:27017/parse' ) ;
50+ gridFSAdapter . getFileLocation = async ( config , filename ) => {
51+ await Promise . resolve ( ) ;
52+ return config . mount + '/files/' + config . applicationId + '/' + encodeURIComponent ( filename ) ;
53+ }
54+ const filesController = new FilesController ( gridFSAdapter ) ;
55+ const result = await filesController . expandFilesInObject ( config , function ( ) { } ) ;
56+
57+ expect ( result ) . toBeUndefined ( ) ;
58+
59+ const fullFile = {
60+ type : '__type' ,
61+ url : 'http://an.url' ,
62+ } ;
63+
64+ const anObject = {
65+ aFile : fullFile ,
66+ } ;
67+ await filesController . expandFilesInObject ( config , anObject ) ;
68+ expect ( anObject . aFile . url ) . toEqual ( 'http://an.url' ) ;
69+ } ) ;
70+
71+ it ( 'should call getFileLocation when config.fileKey is undefined' , async ( ) => {
72+ const config = { } ;
73+ const gridFSAdapter = new GridFSBucketAdapter ( 'mongodb://localhost:27017/parse' ) ;
74+
75+ const fullFile = {
76+ name : 'mock-name' ,
77+ __type : 'File' ,
78+ } ;
79+ gridFSAdapter . getFileLocation = jasmine . createSpy ( 'getFileLocation' ) . and . returnValue ( Promise . resolve ( 'mock-url' ) ) ;
80+ const filesController = new FilesController ( gridFSAdapter ) ;
81+
82+ const anObject = { aFile : fullFile } ;
83+ await filesController . expandFilesInObject ( config , anObject ) ;
84+ expect ( gridFSAdapter . getFileLocation ) . toHaveBeenCalledWith ( config , fullFile . name ) ;
85+ expect ( anObject . aFile . url ) . toEqual ( 'mock-url' ) ;
4486 } ) ;
4587
88+ it ( 'should call getFileLocation when config.fileKey is defined' , async ( ) => {
89+ const config = { fileKey : 'mock-key' } ;
90+ const gridFSAdapter = new GridFSBucketAdapter ( 'mongodb://localhost:27017/parse' ) ;
91+
92+ const fullFile = {
93+ name : 'mock-name' ,
94+ __type : 'File' ,
95+ } ;
96+ gridFSAdapter . getFileLocation = jasmine . createSpy ( 'getFileLocation' ) . and . returnValue ( Promise . resolve ( 'mock-url' ) ) ;
97+ const filesController = new FilesController ( gridFSAdapter ) ;
98+
99+ const anObject = { aFile : fullFile } ;
100+ await filesController . expandFilesInObject ( config , anObject ) ;
101+ expect ( gridFSAdapter . getFileLocation ) . toHaveBeenCalledWith ( config , fullFile . name ) ;
102+ expect ( anObject . aFile . url ) . toEqual ( 'mock-url' ) ;
103+ } ) ;
104+
105+
46106 it_only_db ( 'mongo' ) ( 'should pass databaseOptions to GridFSBucketAdapter' , async ( ) => {
47107 await reconfigureServer ( {
48108 databaseURI : 'mongodb://localhost:27017/parse' ,
@@ -101,7 +161,7 @@ describe('FilesController', () => {
101161 done ( ) ;
102162 } ) ;
103163
104- it ( 'should add a unique hash to the file name when the preserveFileName option is false' , done => {
164+ it ( 'should add a unique hash to the file name when the preserveFileName option is false' , async ( ) => {
105165 const config = Config . get ( Parse . applicationId ) ;
106166 const gridFSAdapter = new GridFSBucketAdapter ( 'mongodb://localhost:27017/parse' ) ;
107167 spyOn ( gridFSAdapter , 'createFile' ) ;
@@ -112,17 +172,15 @@ describe('FilesController', () => {
112172 preserveFileName : false ,
113173 } ) ;
114174
115- filesController . createFile ( config , fileName ) ;
175+ await filesController . createFile ( config , fileName ) ;
116176
117177 expect ( gridFSAdapter . createFile ) . toHaveBeenCalledTimes ( 1 ) ;
118178 expect ( gridFSAdapter . createFile . calls . mostRecent ( ) . args [ 0 ] ) . toMatch (
119179 `^.{32}_${ regexEscapedFileName } $`
120180 ) ;
121-
122- done ( ) ;
123181 } ) ;
124182
125- it ( 'should not add a unique hash to the file name when the preserveFileName option is true' , done => {
183+ it ( 'should not add a unique hash to the file name when the preserveFileName option is true' , async ( ) => {
126184 const config = Config . get ( Parse . applicationId ) ;
127185 const gridFSAdapter = new GridFSBucketAdapter ( 'mongodb://localhost:27017/parse' ) ;
128186 spyOn ( gridFSAdapter , 'createFile' ) ;
@@ -132,12 +190,10 @@ describe('FilesController', () => {
132190 preserveFileName : true ,
133191 } ) ;
134192
135- filesController . createFile ( config , fileName ) ;
193+ await filesController . createFile ( config , fileName ) ;
136194
137195 expect ( gridFSAdapter . createFile ) . toHaveBeenCalledTimes ( 1 ) ;
138196 expect ( gridFSAdapter . createFile . calls . mostRecent ( ) . args [ 0 ] ) . toEqual ( fileName ) ;
139-
140- done ( ) ;
141197 } ) ;
142198
143199 it ( 'should handle adapter without getMetadata' , async ( ) => {
0 commit comments