@@ -164,6 +164,76 @@ func TestAcquireTokenByCredential(t *testing.T) {
164164 }
165165}
166166
167+ func TestRegionAutoEnable (t * testing.T ) {
168+ cred , err := NewCredFromSecret (fakeSecret )
169+ if err != nil {
170+ t .Fatal (err )
171+ }
172+ tests := []struct {
173+ region string
174+ envRegion string
175+ }{
176+ {
177+ region : "" ,
178+ envRegion : "envRegion" ,
179+ },
180+ {
181+ region : "region" ,
182+ envRegion : "envRegion" ,
183+ },
184+ {
185+ region : "DisableMsalForceRegion" ,
186+ envRegion : "envRegion" ,
187+ },
188+ }
189+
190+ for _ , test := range tests {
191+ lmo := "login.microsoftonline.com"
192+ tenant := "tenant"
193+ mockClient := mock.Client {}
194+ if test .envRegion != "" {
195+ err := os .Setenv ("MSAL_FORCE_REGION" , test .envRegion )
196+ if err != nil {
197+ t .Fatal (err )
198+ }
199+ }
200+ var client Client
201+ if test .region != "" {
202+ client , err = New (fmt .Sprintf (authorityFmt , lmo , tenant ), fakeClientID , cred , WithHTTPClient (& mockClient ), WithAzureRegion (test .region ))
203+ if err != nil {
204+ t .Fatal (err )
205+ }
206+ } else {
207+ client , err = New (fmt .Sprintf (authorityFmt , lmo , tenant ), fakeClientID , cred , WithHTTPClient (& mockClient ))
208+ if err != nil {
209+ t .Fatal (err )
210+ }
211+ }
212+
213+ t .Cleanup (func () {
214+ os .Unsetenv ("MSAL_FORCE_REGION" )
215+ })
216+ if test .region == "" {
217+ if test .envRegion != "" {
218+ if client .base .AuthParams .AuthorityInfo .Region != test .envRegion {
219+ t .Fatalf ("wanted %q, got %q" , test .envRegion , client .base .AuthParams .AuthorityInfo .Region )
220+ }
221+ }
222+ } else {
223+ if test .region == "DisableMsalForceRegion" {
224+ if client .base .AuthParams .AuthorityInfo .Region != "" {
225+ t .Fatalf ("wanted empty, got %q" , client .base .AuthParams .AuthorityInfo .Region )
226+ }
227+ } else {
228+
229+ if client .base .AuthParams .AuthorityInfo .Region != test .region {
230+ t .Fatalf ("wanted %q, got %q" , test .region , client .base .AuthParams .AuthorityInfo .Region )
231+ }
232+ }
233+ }
234+ }
235+ }
236+
167237func TestAcquireTokenOnBehalfOf (t * testing.T ) {
168238 // this test is an offline version of TestOnBehalfOf in integration_test.go
169239 cred , err := NewCredFromSecret (fakeSecret )
0 commit comments