@@ -4,6 +4,7 @@ import { MockFactory } from "../../test/mockFactory";
4
4
import { invokeHook , setEnvVariables , unsetEnvVariables } from "../../test/utils" ;
5
5
import { AzureLoginPlugin } from "./azureLoginPlugin" ;
6
6
import { loginHooks } from "./loginHooks" ;
7
+ import { ServerlessAzureConfig } from "../../models/serverless" ;
7
8
8
9
describe ( "Login Plugin" , ( ) => {
9
10
@@ -36,8 +37,8 @@ describe("Login Plugin", () => {
36
37
}
37
38
38
39
beforeEach ( ( ) => {
39
- AzureLoginService . interactiveLogin = createMockLoginFunction ( ) ;
40
- AzureLoginService . servicePrincipalLogin = createMockLoginFunction ( ) ;
40
+ AzureLoginService . prototype . interactiveLogin = createMockLoginFunction ( ) ;
41
+ AzureLoginService . prototype . servicePrincipalLogin = createMockLoginFunction ( ) ;
41
42
} ) ;
42
43
43
44
it ( "contains the hooks as contained in loginHooks" , ( ) => {
@@ -46,28 +47,28 @@ describe("Login Plugin", () => {
46
47
47
48
it ( "returns if azure credentials are set" , async ( ) => {
48
49
await invokeLoginHook ( true ) ;
49
- expect ( AzureLoginService . interactiveLogin ) . not . toBeCalled ( ) ;
50
- expect ( AzureLoginService . servicePrincipalLogin ) . not . toBeCalled ( ) ;
50
+ expect ( AzureLoginService . prototype . interactiveLogin ) . not . toBeCalled ( ) ;
51
+ expect ( AzureLoginService . prototype . servicePrincipalLogin ) . not . toBeCalled ( ) ;
51
52
} ) ;
52
53
53
54
it ( "calls login if azure credentials are not set" , async ( ) => {
54
55
unsetServicePrincipalEnvVariables ( ) ;
55
56
await invokeLoginHook ( ) ;
56
- expect ( AzureLoginService . interactiveLogin ) . toBeCalled ( ) ;
57
- expect ( AzureLoginService . servicePrincipalLogin ) . not . toBeCalled ( ) ;
57
+ expect ( AzureLoginService . prototype . interactiveLogin ) . toBeCalled ( ) ;
58
+ expect ( AzureLoginService . prototype . servicePrincipalLogin ) . not . toBeCalled ( ) ;
58
59
} ) ;
59
60
60
61
it ( "calls service principal login if environment variables are set" , async ( ) => {
61
62
setServicePrincipalEnvVariables ( ) ;
62
63
const sls = MockFactory . createTestServerless ( ) ;
63
64
await invokeLoginHook ( false , sls ) ;
64
- expect ( AzureLoginService . servicePrincipalLogin ) . toBeCalledWith (
65
+ expect ( AzureLoginService . prototype . servicePrincipalLogin ) . toBeCalledWith (
65
66
"azureServicePrincipalClientId" ,
66
67
"azureServicePrincipalPassword" ,
67
68
"azureServicePrincipalTenantId" ,
68
69
undefined // would be options
69
70
)
70
- expect ( AzureLoginService . interactiveLogin ) . not . toBeCalled ( ) ;
71
+ expect ( AzureLoginService . prototype . interactiveLogin ) . not . toBeCalled ( ) ;
71
72
expect ( JSON . stringify ( sls . variables [ "azureCredentials" ] ) ) . toEqual ( JSON . stringify ( credentials ) ) ;
72
73
expect ( sls . variables [ "subscriptionId" ] ) . toEqual ( "azureSubId" ) ;
73
74
} ) ;
@@ -76,41 +77,57 @@ describe("Login Plugin", () => {
76
77
unsetServicePrincipalEnvVariables ( ) ;
77
78
const sls = MockFactory . createTestServerless ( ) ;
78
79
await invokeLoginHook ( false , sls ) ;
79
- expect ( AzureLoginService . servicePrincipalLogin ) . not . toBeCalled ( ) ;
80
- expect ( AzureLoginService . interactiveLogin ) . toBeCalled ( ) ;
80
+ expect ( AzureLoginService . prototype . servicePrincipalLogin ) . not . toBeCalled ( ) ;
81
+ expect ( AzureLoginService . prototype . interactiveLogin ) . toBeCalled ( ) ;
81
82
expect ( JSON . stringify ( sls . variables [ "azureCredentials" ] ) ) . toEqual ( JSON . stringify ( credentials ) ) ;
82
83
expect ( sls . variables [ "subscriptionId" ] ) . toEqual ( "azureSubId" ) ;
83
84
} ) ;
84
85
85
86
it ( "logs an error from authentication and crashes with it" , async ( ) => {
86
87
unsetServicePrincipalEnvVariables ( ) ;
87
88
const error = new Error ( "This is my error message" )
88
- AzureLoginService . interactiveLogin = jest . fn ( ( ) => {
89
+ AzureLoginService . prototype . interactiveLogin = jest . fn ( ( ) => {
89
90
throw error ;
90
91
} ) ;
91
92
const sls = MockFactory . createTestServerless ( ) ;
92
93
await expect ( invokeLoginHook ( false , sls ) ) . rejects . toThrow ( error ) ;
93
- expect ( AzureLoginService . interactiveLogin ) . toBeCalled ( ) ;
94
- expect ( AzureLoginService . servicePrincipalLogin ) . not . toBeCalled ( ) ;
94
+ expect ( AzureLoginService . prototype . interactiveLogin ) . toBeCalled ( ) ;
95
+ expect ( AzureLoginService . prototype . servicePrincipalLogin ) . not . toBeCalled ( ) ;
95
96
expect ( sls . cli . log ) . lastCalledWith ( "Error logging into azure" ) ;
96
97
} ) ;
97
98
98
99
it ( "Uses the default subscription ID" , async ( ) => {
99
100
const sls = MockFactory . createTestServerless ( ) ;
100
101
const opt = MockFactory . createTestServerlessOptions ( ) ;
101
102
await invokeLoginHook ( false , sls , opt ) ;
102
- expect ( AzureLoginService . interactiveLogin ) . toBeCalled ( ) ;
103
+ expect ( AzureLoginService . prototype . interactiveLogin ) . toBeCalled ( ) ;
103
104
expect ( sls . variables [ "subscriptionId" ] ) . toEqual ( "azureSubId" ) ;
104
105
expect ( sls . cli . log ) . toBeCalledWith ( "Using subscription ID: azureSubId" ) ;
105
106
} ) ;
106
107
107
108
it ( "Throws an error with empty subscription list" , async ( ) => {
109
+ unsetServicePrincipalEnvVariables ( ) ;
108
110
const authResponse = MockFactory . createTestAuthResponse ( ) ;
109
111
authResponse . subscriptions = [ ] ;
110
- AzureLoginService . interactiveLogin = createMockLoginFunction ( authResponse ) ;
112
+ AzureLoginService . prototype . interactiveLogin = createMockLoginFunction ( authResponse ) ;
111
113
const sls = MockFactory . createTestServerless ( ) ;
114
+ delete sls . variables [ "subscriptionId" ] ;
112
115
const opt = MockFactory . createTestServerlessOptions ( ) ;
113
116
await expect ( invokeLoginHook ( false , sls , opt ) ) . rejects . toThrow ( ) ;
114
- expect ( AzureLoginService . interactiveLogin ) . toBeCalled ( ) ;
117
+ expect ( AzureLoginService . prototype . interactiveLogin ) . toBeCalled ( ) ;
118
+ } ) ;
119
+
120
+ it ( "Does not throw an error with empty subscription list if subscription previously specified" , async ( ) => {
121
+ unsetServicePrincipalEnvVariables ( ) ;
122
+ const authResponse = MockFactory . createTestAuthResponse ( ) ;
123
+ authResponse . subscriptions = [ ] ;
124
+ AzureLoginService . prototype . interactiveLogin = createMockLoginFunction ( authResponse ) ;
125
+ const sls = MockFactory . createTestServerless ( ) ;
126
+ delete sls . variables [ "subscriptionId" ] ;
127
+ const subId = "my subscription id" ;
128
+ ( sls . service as any as ServerlessAzureConfig ) . provider . subscriptionId = subId ;
129
+ const opt = MockFactory . createTestServerlessOptions ( ) ;
130
+ await invokeLoginHook ( false , sls , opt )
131
+ expect ( AzureLoginService . prototype . interactiveLogin ) . toBeCalled ( ) ;
115
132
} ) ;
116
133
} ) ;
0 commit comments