1package adal 2 3// Copyright 2017 Microsoft Corporation 4// 5// Licensed under the Apache License, Version 2.0 (the "License"); 6// you may not use this file except in compliance with the License. 7// You may obtain a copy of the License at 8// 9// http://www.apache.org/licenses/LICENSE-2.0 10// 11// Unless required by applicable law or agreed to in writing, software 12// distributed under the License is distributed on an "AS IS" BASIS, 13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14// See the License for the specific language governing permissions and 15// limitations under the License. 16 17import ( 18 "fmt" 19 "testing" 20) 21 22const TestAuxTenantPrefix = "aux-tenant-test-" 23 24var ( 25 TestAuxTenantIDs = []string{TestAuxTenantPrefix + "0", TestAuxTenantPrefix + "1", TestAuxTenantPrefix + "2"} 26) 27 28func TestNewOAuthConfig(t *testing.T) { 29 config, err := NewOAuthConfig(TestActiveDirectoryEndpoint, TestTenantID) 30 if err != nil { 31 t.Fatalf("autorest/adal: Unexpected error while creating oauth configuration for tenant: %v.", err) 32 } 33 34 expected := fmt.Sprintf("https://login.test.com/%s/oauth2/authorize?api-version=1.0", TestTenantID) 35 if config.AuthorizeEndpoint.String() != expected { 36 t.Fatalf("autorest/adal: Incorrect authorize url for Tenant from Environment. expected(%s). actual(%v).", expected, config.AuthorizeEndpoint) 37 } 38 39 expected = fmt.Sprintf("https://login.test.com/%s/oauth2/token?api-version=1.0", TestTenantID) 40 if config.TokenEndpoint.String() != expected { 41 t.Fatalf("autorest/adal: Incorrect authorize url for Tenant from Environment. expected(%s). actual(%v).", expected, config.TokenEndpoint) 42 } 43 44 expected = fmt.Sprintf("https://login.test.com/%s/oauth2/devicecode?api-version=1.0", TestTenantID) 45 if config.DeviceCodeEndpoint.String() != expected { 46 t.Fatalf("autorest/adal Incorrect devicecode url for Tenant from Environment. expected(%s). actual(%v).", expected, config.DeviceCodeEndpoint) 47 } 48} 49 50func TestNewOAuthConfigWithAPIVersionNil(t *testing.T) { 51 config, err := NewOAuthConfigWithAPIVersion(TestActiveDirectoryEndpoint, TestTenantID, nil) 52 if err != nil { 53 t.Fatalf("autorest/adal: Unexpected error while creating oauth configuration for tenant: %v.", err) 54 } 55 56 expected := fmt.Sprintf("https://login.test.com/%s/oauth2/authorize", TestTenantID) 57 if config.AuthorizeEndpoint.String() != expected { 58 t.Fatalf("autorest/adal: Incorrect authorize url for Tenant from Environment. expected(%s). actual(%v).", expected, config.AuthorizeEndpoint) 59 } 60 61 expected = fmt.Sprintf("https://login.test.com/%s/oauth2/token", TestTenantID) 62 if config.TokenEndpoint.String() != expected { 63 t.Fatalf("autorest/adal: Incorrect authorize url for Tenant from Environment. expected(%s). actual(%v).", expected, config.TokenEndpoint) 64 } 65 66 expected = fmt.Sprintf("https://login.test.com/%s/oauth2/devicecode", TestTenantID) 67 if config.DeviceCodeEndpoint.String() != expected { 68 t.Fatalf("autorest/adal Incorrect devicecode url for Tenant from Environment. expected(%s). actual(%v).", expected, config.DeviceCodeEndpoint) 69 } 70} 71 72func TestNewOAuthConfigWithAPIVersionNotNil(t *testing.T) { 73 apiVersion := "2.0" 74 75 config, err := NewOAuthConfigWithAPIVersion(TestActiveDirectoryEndpoint, TestTenantID, &apiVersion) 76 if err != nil { 77 t.Fatalf("autorest/adal: Unexpected error while creating oauth configuration for tenant: %v.", err) 78 } 79 80 expected := fmt.Sprintf("https://login.test.com/%s/oauth2/authorize?api-version=2.0", TestTenantID) 81 if config.AuthorizeEndpoint.String() != expected { 82 t.Fatalf("autorest/adal: Incorrect authorize url for Tenant from Environment. expected(%s). actual(%v).", expected, config.AuthorizeEndpoint) 83 } 84 85 expected = fmt.Sprintf("https://login.test.com/%s/oauth2/token?api-version=2.0", TestTenantID) 86 if config.TokenEndpoint.String() != expected { 87 t.Fatalf("autorest/adal: Incorrect authorize url for Tenant from Environment. expected(%s). actual(%v).", expected, config.TokenEndpoint) 88 } 89 90 expected = fmt.Sprintf("https://login.test.com/%s/oauth2/devicecode?api-version=2.0", TestTenantID) 91 if config.DeviceCodeEndpoint.String() != expected { 92 t.Fatalf("autorest/adal Incorrect devicecode url for Tenant from Environment. expected(%s). actual(%v).", expected, config.DeviceCodeEndpoint) 93 } 94} 95 96func TestNewMultiTenantOAuthConfig(t *testing.T) { 97 cfg, err := NewMultiTenantOAuthConfig(TestActiveDirectoryEndpoint, TestTenantID, TestAuxTenantIDs, OAuthOptions{}) 98 if err != nil { 99 t.Fatalf("autorest/adal: unexpected error while creating multitenant config: %v", err) 100 } 101 expected := fmt.Sprintf("https://login.test.com/%s/oauth2/authorize?api-version=1.0", TestTenantID) 102 if ep := cfg.PrimaryTenant().AuthorizeEndpoint.String(); ep != expected { 103 t.Fatalf("autorest/adal: Incorrect authorize url for Tenant from Environment. expected(%s). actual(%v).", expected, ep) 104 } 105 aux := cfg.AuxiliaryTenants() 106 if len(aux) == 0 { 107 t.Fatal("autorest/adal: unexpected zero-length auxiliary tenants") 108 } 109 for i := range aux { 110 expected := fmt.Sprintf("https://login.test.com/aux-tenant-test-%d/oauth2/authorize?api-version=1.0", i) 111 if ep := aux[i].AuthorizeEndpoint.String(); ep != expected { 112 t.Fatalf("autorest/adal: Incorrect authorize url for Tenant from Environment. expected(%s). actual(%v).", expected, ep) 113 } 114 } 115} 116 117func TestNewMultiTenantOAuthConfigFail(t *testing.T) { 118 _, err := NewMultiTenantOAuthConfig(TestActiveDirectoryEndpoint, TestTenantID, nil, OAuthOptions{}) 119 if err == nil { 120 t.Fatal("autorest/adal: expected non-nil error") 121 } 122 _, err = NewMultiTenantOAuthConfig(TestActiveDirectoryEndpoint, TestTenantID, []string{"one", "two", "three", "four"}, OAuthOptions{}) 123 if err == nil { 124 t.Fatal("autorest/adal: expected non-nil error") 125 } 126} 127