1
1
const path = require ( 'path' )
2
2
3
+ const fs = require ( 'fs' )
3
4
const mockFs = require ( 'mock-fs' )
4
5
const build = require ( 'netlify-lambda/lib/build' )
5
6
@@ -33,6 +34,51 @@ afterEach(() => {
33
34
} )
34
35
35
36
describe ( 'preBuild()' , ( ) => {
37
+ test ( 'Login to default api host' , async ( ) => {
38
+ // Prepare
39
+ process . env . NIMBELLA_LOGIN_TOKEN = 'somevalue'
40
+ delete process . env . NIMBELLA_API_HOST
41
+ utils . cache . has . mockResolvedValue ( false )
42
+
43
+ const mockFiles = {
44
+ [ require ( 'os' ) . homedir ( ) ] : { }
45
+ }
46
+ mockFs ( mockFiles )
47
+ await plugin . onPreBuild ( {
48
+ utils,
49
+ constants : { } ,
50
+ inputs : { }
51
+ } )
52
+ mockFs . restore ( )
53
+
54
+ expect ( utils . run . command . mock . calls [ 0 ] [ 0 ] ) . toEqual (
55
+ `nim auth login somevalue`
56
+ )
57
+ } )
58
+
59
+ test ( 'Login to specified api host' , async ( ) => {
60
+ // Prepare
61
+ process . env . NIMBELLA_LOGIN_TOKEN = 'somevalue'
62
+ process . env . NIMBELLA_API_HOST = 'somehost'
63
+ utils . cache . has . mockResolvedValue ( false )
64
+
65
+ const mockFiles = {
66
+ [ require ( 'os' ) . homedir ( ) ] : { }
67
+ }
68
+
69
+ mockFs ( mockFiles )
70
+ await plugin . onPreBuild ( {
71
+ utils,
72
+ constants : { } ,
73
+ inputs : { }
74
+ } )
75
+ mockFs . restore ( )
76
+
77
+ expect ( utils . run . command . mock . calls [ 0 ] [ 0 ] ) . toEqual (
78
+ `nim auth login somevalue --apihost somehost`
79
+ )
80
+ } )
81
+
36
82
test ( 'show token not available message when login token not set' , async ( ) => {
37
83
// Prepare
38
84
process . env . NIMBELLA_LOGIN_TOKEN = ''
@@ -173,9 +219,12 @@ describe('onPostBuild()', () => {
173
219
test ( 'should rewrite existing redirects to .netlify/functions/ in netlify.toml if functions are used' , async ( ) => {
174
220
process . env . NIMBELLA_LOGIN_TOKEN = 'somevalue'
175
221
process . env . CONTEXT = 'production'
176
- utils . run . command . mockReturnValue ( {
177
- stdout : 'namespace'
222
+ utils . run . command = jest . fn ( ( cmd ) => {
223
+ if ( cmd === 'nim auth current' ) return { stdout : 'namespace' }
224
+ if ( cmd === 'nim auth current --apihost' ) return { stdout : 'somehost' }
225
+ return { stdout : '???' }
178
226
} )
227
+
179
228
const pluginInputs = {
180
229
utils,
181
230
constants : { CONFIG_PATH : 'netlify.toml' , PUBLISH_DIR : '' } ,
@@ -199,9 +248,15 @@ describe('onPostBuild()', () => {
199
248
} )
200
249
await plugin . onPreBuild ( pluginInputs )
201
250
await plugin . onPostBuild ( pluginInputs )
251
+ const redirects = String ( fs . readFileSync ( '_redirects' ) )
202
252
mockFs . restore ( )
253
+
203
254
expect ( console . log . mock . calls [ 1 ] [ 0 ] ) . toEqual (
204
255
"Found redirect rules in netlify.toml. We will rewrite rules that redirect (200 rewrites) to '/.netlify/functions/*'."
205
256
)
257
+
258
+ expect ( redirects ) . toEqual (
259
+ '/* https://somehost/api/v1/web/namespace/default/:splat 200!'
260
+ )
206
261
} )
207
262
} )
0 commit comments