5
5
6
6
import { AzureLogger , setLogLevel } from "@azure/logger" ;
7
7
import { MsalTestCleanup , msalNodeTestSetup } from "../../node/msalNodeTestSetup" ;
8
- import { Recorder , env , isLiveMode , isPlaybackMode } from "@azure-tools/test-recorder" ;
8
+ import { Recorder , isPlaybackMode } from "@azure-tools/test-recorder" ;
9
9
import { Context } from "mocha" ;
10
- import { DeveloperSignOnClientId } from "../../../src/constants" ;
11
- import { GetTokenOptions } from "@azure/core-auth" ;
12
- import { MsalNode } from "../../../src/msal/nodeFlows/msalNodeCommon" ;
13
10
import { PublicClientApplication } from "@azure/msal-node" ;
14
11
import Sinon from "sinon" ;
15
12
import { UsernamePasswordCredential } from "../../../src" ;
16
13
import { assert } from "chai" ;
14
+ import { getUsernamePasswordStaticResources } from "../../msalTestUtils" ;
17
15
18
16
describe ( "UsernamePasswordCredential (internal)" , function ( ) {
19
17
let cleanup : MsalTestCleanup ;
@@ -26,9 +24,10 @@ describe("UsernamePasswordCredential (internal)", function () {
26
24
cleanup = setup . cleanup ;
27
25
recorder = setup . recorder ;
28
26
29
- getTokenSilentSpy = setup . sandbox . spy ( MsalNode . prototype , "getTokenSilent" ) ;
27
+ // MsalClient calls to this method underneath when silent authentication can be attempted.
28
+ getTokenSilentSpy = setup . sandbox . spy ( PublicClientApplication . prototype , "acquireTokenSilent" ) ;
30
29
31
- // MsalClientSecret calls to this method underneath.
30
+ // MsalClient calls to this method underneath for interactive auth .
32
31
doGetTokenSpy = setup . sandbox . spy (
33
32
PublicClientApplication . prototype ,
34
33
"acquireTokenByUsernamePassword" ,
@@ -46,38 +45,38 @@ describe("UsernamePasswordCredential (internal)", function () {
46
45
try {
47
46
new UsernamePasswordCredential (
48
47
undefined as any ,
49
- env . AZURE_CLIENT_ID ! ,
50
- env . AZURE_USERNAME ! ,
51
- env . AZURE_PASSWORD ! ,
48
+ "azure_client_id" ,
49
+ "azure_username" ,
50
+ "azure_password" ,
52
51
) ;
53
52
} catch ( e : any ) {
54
53
errors . push ( e ) ;
55
54
}
56
55
try {
57
56
new UsernamePasswordCredential (
58
- env . AZURE_TENANT_ID ! ,
57
+ "azure_tenant_id" ,
59
58
undefined as any ,
60
- env . AZURE_USERNAME ! ,
61
- env . AZURE_PASSWORD ! ,
59
+ "azure_username" ,
60
+ "azure_password" ,
62
61
) ;
63
62
} catch ( e : any ) {
64
63
errors . push ( e ) ;
65
64
}
66
65
try {
67
66
new UsernamePasswordCredential (
68
- env . AZURE_TENANT_ID ! ,
69
- env . AZURE_CLIENT_ID ! ,
67
+ "azure_tenant_id" ,
68
+ "azure_client_id" ,
70
69
undefined as any ,
71
- env . AZURE_PASSWORD ! ,
70
+ "azure_password" ,
72
71
) ;
73
72
} catch ( e : any ) {
74
73
errors . push ( e ) ;
75
74
}
76
75
try {
77
76
new UsernamePasswordCredential (
78
- env . AZURE_TENANT_ID ! ,
79
- env . AZURE_CLIENT_ID ! ,
80
- env . AZURE_USERNAME ! ,
77
+ "azure_tenant_id" ,
78
+ "azure_client_id" ,
79
+ "azure_username" ,
81
80
undefined as any ,
82
81
) ;
83
82
} catch ( e : any ) {
@@ -103,65 +102,55 @@ describe("UsernamePasswordCredential (internal)", function () {
103
102
} ) ;
104
103
} ) ;
105
104
106
- // This is not the way to test persistence with acquireTokenByClientCredential,
107
- // since acquireTokenByClientCredential caches at the method level, and not with the same cache used for acquireTokenSilent.
108
- // I'm leaving this here so I can remember about this in the future.
109
- it . skip ( "Authenticates silently after the initial request" , async function ( this : Context ) {
110
- // These tests should not run live because this credential requires user interaction.
111
- if ( isLiveMode ( ) ) {
112
- this . skip ( ) ;
113
- }
105
+ it ( "Authenticates silently after the initial request" , async function ( this : Context ) {
106
+ const { clientId, password, tenantId, username } = getUsernamePasswordStaticResources ( ) ;
114
107
const credential = new UsernamePasswordCredential (
115
- env . AZURE_TENANT_ID ! ,
116
- env . AZURE_CLIENT_ID ! ,
117
- env . AZURE_USERNAME ! ,
118
- env . AZURE_PASSWORD ! ,
108
+ tenantId ,
109
+ clientId ,
110
+ username ,
111
+ password ,
112
+ recorder . configureClientOptions ( { } ) ,
119
113
) ;
120
114
121
115
await credential . getToken ( scope ) ;
122
- assert . equal ( getTokenSilentSpy . callCount , 1 ) ;
123
116
assert . equal ( doGetTokenSpy . callCount , 1 ) ;
124
117
125
118
await credential . getToken ( scope ) ;
126
- assert . equal ( getTokenSilentSpy . callCount , 2 ) ;
127
- assert . equal ( doGetTokenSpy . callCount , 1 ) ;
119
+ assert . equal (
120
+ getTokenSilentSpy . callCount ,
121
+ 1 ,
122
+ "getTokenSilentSpy.callCount should have been 1 (Silent authentication after the initial request)." ,
123
+ ) ;
124
+ assert . equal (
125
+ doGetTokenSpy . callCount ,
126
+ 1 ,
127
+ "Expected no additional calls to doGetTokenSpy after the initial request." ,
128
+ ) ;
128
129
} ) ;
129
130
130
131
it ( "Authenticates with tenantId on getToken" , async function ( this : Context ) {
131
- // The live environment isn't ready for this test
132
- if ( isLiveMode ( ) ) {
133
- this . skip ( ) ;
134
- }
132
+ const { clientId, password, tenantId, username } = getUsernamePasswordStaticResources ( ) ;
135
133
const credential = new UsernamePasswordCredential (
136
- env . AZURE_IDENTITY_TEST_TENANTID || env . AZURE_TENANT_ID ! ,
137
- env . AZURE_IDENTITY_TEST_CLIENTID || env . AZURE_CLIENT_ID ! ,
138
- env . AZURE_IDENTITY_TEST_USERNAME || env . AZURE_USERNAME ! ,
139
- env . AZURE_IDENTITY_TEST_PASSWORD || env . AZURE_PASSWORD ! ,
134
+ tenantId ,
135
+ clientId ,
136
+ username ,
137
+ password ,
140
138
recorder . configureClientOptions ( { } ) ,
141
139
) ;
142
140
143
- await credential . getToken ( scope , { tenantId : env . AZURE_TENANT_ID } as GetTokenOptions ) ;
144
- assert . equal ( getTokenSilentSpy . callCount , 1 ) ;
141
+ await credential . getToken ( scope ) ;
145
142
assert . equal ( doGetTokenSpy . callCount , 1 ) ;
146
143
} ) ;
147
144
148
145
it ( "authenticates (with allowLoggingAccountIdentifiers set to true)" , async function ( this : Context ) {
146
+ const { clientId, password, tenantId, username } = getUsernamePasswordStaticResources ( ) ;
149
147
if ( isPlaybackMode ( ) ) {
150
148
// The recorder clears the access tokens.
151
149
this . skip ( ) ;
152
150
}
153
- const tenantId = env . AZURE_IDENTITY_TEST_TENANTID || env . AZURE_TENANT_ID ! ;
154
- const clientId = isLiveMode ( ) ? DeveloperSignOnClientId : env . AZURE_CLIENT_ID ! ;
155
-
156
- const credential = new UsernamePasswordCredential (
157
- tenantId ,
158
- clientId ,
159
- env . AZURE_IDENTITY_TEST_USERNAME || env . AZURE_USERNAME ! ,
160
- env . AZURE_IDENTITY_TEST_PASSWORD || env . AZURE_PASSWORD ! ,
161
- recorder . configureClientOptions ( {
162
- loggingOptions : { allowLoggingAccountIdentifiers : true } ,
163
- } ) ,
164
- ) ;
151
+ const credential = new UsernamePasswordCredential ( tenantId , clientId , username , password , {
152
+ loggingOptions : { allowLoggingAccountIdentifiers : true } ,
153
+ } ) ;
165
154
setLogLevel ( "info" ) ;
166
155
const spy = Sinon . spy ( process . stderr , "write" ) ;
167
156
0 commit comments