11import axios from "axios" ;
22import MockAdapter from "axios-mock-adapter" ;
33import mockFs from "mock-fs" ;
4+ import path from "path" ;
45import Serverless from "serverless" ;
56import { MockFactory } from "../test/mockFactory" ;
67import { FunctionAppService } from "./functionAppService" ;
@@ -55,6 +56,9 @@ describe("Function App Service", () => {
5556
5657 mockFs ( {
5758 "app.zip" : "contents" ,
59+ ".serverless" : {
60+ "serviceName.zip" : "contents" ,
61+ }
5862 } , { createCwd : true , createTmp : true } ) ;
5963 } ) ;
6064
@@ -222,6 +226,37 @@ describe("Function App Service", () => {
222226 expect ( uploadCall [ 2 ] ) . toMatch ( / .* - t ( [ 0 - 9 ] + ) / )
223227 } ) ;
224228
229+ it ( "uploads functions to function app and blob storage with default naming convention" , async ( ) => {
230+ const scmDomain = app . enabledHostNames . find ( ( hostname ) => hostname . endsWith ( "scm.azurewebsites.net" ) ) ;
231+ const expectedUploadUrl = `https://${ scmDomain } /api/zipdeploy/` ;
232+
233+ const sls = MockFactory . createTestServerless ( ) ;
234+ delete sls . service [ "artifact" ]
235+ const service = createService ( sls ) ;
236+ await service . uploadFunctions ( app ) ;
237+
238+ const defaultArtifact = path . join ( ".serverless" , `${ sls . service . getServiceName ( ) } .zip` ) ;
239+
240+ expect ( ( FunctionAppService . prototype as any ) . sendFile ) . toBeCalledWith ( {
241+ method : "POST" ,
242+ uri : expectedUploadUrl ,
243+ json : true ,
244+ headers : {
245+ Authorization : `Bearer ${ variables [ "azureCredentials" ] . tokenCache . _entries [ 0 ] . accessToken } ` ,
246+ Accept : "*/*" ,
247+ ContentType : "application/octet-stream" ,
248+ }
249+ } , defaultArtifact ) ;
250+ const expectedArtifactName = service . getDeploymentName ( ) . replace ( "rg-deployment" , "artifact" ) ;
251+ expect ( ( AzureBlobStorageService . prototype as any ) . uploadFile ) . toBeCalledWith (
252+ defaultArtifact ,
253+ configConstants . deploymentConfig . container ,
254+ `${ expectedArtifactName } .zip` ,
255+ )
256+ const uploadCall = ( ( AzureBlobStorageService . prototype as any ) . uploadFile ) . mock . calls [ 0 ] ;
257+ expect ( uploadCall [ 2 ] ) . toMatch ( / .* - t ( [ 0 - 9 ] + ) / )
258+ } ) ;
259+
225260 it ( "uploads functions with custom SCM domain (aka App service environments)" , async ( ) => {
226261 const customApp = {
227262 ...MockFactory . createTestSite ( "CustomAppWithinASE" ) ,
@@ -248,10 +283,19 @@ describe("Function App Service", () => {
248283 } , slsService [ "artifact" ] )
249284 } ) ;
250285
251- it ( "throws an error with no zip file " , async ( ) => {
286+ it ( "uses default name when no artifact provided " , async ( ) => {
252287 const sls = MockFactory . createTestServerless ( ) ;
253288 delete sls . service [ "artifact" ] ;
254289 const service = createService ( sls ) ;
255- await expect ( service . uploadFunctions ( app ) ) . rejects . not . toBeNull ( )
290+ expect ( service . getFunctionZipFile ( ) ) . toEqual ( path . join ( ".serverless" , `${ sls . service . getServiceName ( ) } .zip` ) )
291+ } ) ;
292+
293+ it ( "uses package param from options if provided" , async ( ) => {
294+ const sls = MockFactory . createTestServerless ( ) ;
295+ const options = MockFactory . createTestServerlessOptions ( {
296+ package : "fake.zip" ,
297+ } ) ;
298+ const service = createService ( sls , options ) ;
299+ expect ( service . getFunctionZipFile ( ) ) . toEqual ( "fake.zip" )
256300 } ) ;
257301} ) ;
0 commit comments