@@ -21,11 +21,14 @@ const mockAdapter = {
21
21
22
22
// Small additional tests to improve overall coverage
23
23
describe ( 'FilesController' , ( ) => {
24
- it ( 'should properly expand objects' , done => {
24
+ it ( 'should properly expand objects with sync getFileLocation ' , async ( ) => {
25
25
const config = Config . get ( Parse . applicationId ) ;
26
26
const gridFSAdapter = new GridFSBucketAdapter ( 'mongodb://localhost:27017/parse' ) ;
27
+ gridFSAdapter . getFileLocation = ( config , filename ) => {
28
+ return config . mount + '/files/' + config . applicationId + '/' + encodeURIComponent ( filename ) ;
29
+ }
27
30
const filesController = new FilesController ( gridFSAdapter ) ;
28
- const result = filesController . expandFilesInObject ( config , function ( ) { } ) ;
31
+ const result = await filesController . expandFilesInObject ( config , function ( ) { } ) ;
29
32
30
33
expect ( result ) . toBeUndefined ( ) ;
31
34
@@ -37,12 +40,69 @@ describe('FilesController', () => {
37
40
const anObject = {
38
41
aFile : fullFile ,
39
42
} ;
40
- filesController . expandFilesInObject ( config , anObject ) ;
43
+ await filesController . expandFilesInObject ( config , anObject ) ;
41
44
expect ( anObject . aFile . url ) . toEqual ( 'http://an.url' ) ;
45
+ } ) ;
42
46
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' ) ;
44
86
} ) ;
45
87
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
+
46
106
it_only_db ( 'mongo' ) ( 'should pass databaseOptions to GridFSBucketAdapter' , async ( ) => {
47
107
await reconfigureServer ( {
48
108
databaseURI : 'mongodb://localhost:27017/parse' ,
@@ -101,7 +161,7 @@ describe('FilesController', () => {
101
161
done ( ) ;
102
162
} ) ;
103
163
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 ( ) => {
105
165
const config = Config . get ( Parse . applicationId ) ;
106
166
const gridFSAdapter = new GridFSBucketAdapter ( 'mongodb://localhost:27017/parse' ) ;
107
167
spyOn ( gridFSAdapter , 'createFile' ) ;
@@ -112,17 +172,15 @@ describe('FilesController', () => {
112
172
preserveFileName : false ,
113
173
} ) ;
114
174
115
- filesController . createFile ( config , fileName ) ;
175
+ await filesController . createFile ( config , fileName ) ;
116
176
117
177
expect ( gridFSAdapter . createFile ) . toHaveBeenCalledTimes ( 1 ) ;
118
178
expect ( gridFSAdapter . createFile . calls . mostRecent ( ) . args [ 0 ] ) . toMatch (
119
179
`^.{32}_${ regexEscapedFileName } $`
120
180
) ;
121
-
122
- done ( ) ;
123
181
} ) ;
124
182
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 ( ) => {
126
184
const config = Config . get ( Parse . applicationId ) ;
127
185
const gridFSAdapter = new GridFSBucketAdapter ( 'mongodb://localhost:27017/parse' ) ;
128
186
spyOn ( gridFSAdapter , 'createFile' ) ;
@@ -132,12 +190,10 @@ describe('FilesController', () => {
132
190
preserveFileName : true ,
133
191
} ) ;
134
192
135
- filesController . createFile ( config , fileName ) ;
193
+ await filesController . createFile ( config , fileName ) ;
136
194
137
195
expect ( gridFSAdapter . createFile ) . toHaveBeenCalledTimes ( 1 ) ;
138
196
expect ( gridFSAdapter . createFile . calls . mostRecent ( ) . args [ 0 ] ) . toEqual ( fileName ) ;
139
-
140
- done ( ) ;
141
197
} ) ;
142
198
143
199
it ( 'should handle adapter without getMetadata' , async ( ) => {
0 commit comments