forked from microsoft/kiota-authentication-azure-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathazure_identity_access_token_provider_test.go
79 lines (63 loc) · 2.74 KB
/
azure_identity_access_token_provider_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package microsoft_kiota_authentication_azure
import (
"context"
u "net/url"
"testing"
azcore "github.com/Azure/azure-sdk-for-go/sdk/azcore"
policy "github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
assert "github.com/stretchr/testify/assert"
)
type MockTokenCredential struct {
TokenValue string
}
func (m *MockTokenCredential) GetToken(ctx context.Context, options policy.TokenRequestOptions) (azcore.AccessToken, error) {
return azcore.AccessToken{
Token: m.TokenValue,
}, nil
}
func TestAddsTokenOnValidHost(t *testing.T) {
provider, err := NewAzureIdentityAccessTokenProvider(&MockTokenCredential{TokenValue: "token"})
assert.Nil(t, err)
assert.NotNil(t, provider)
token, err := provider.GetAuthorizationToken(context.Background(), &u.URL{Host: "graph.microsoft.com", Scheme: "https"}, nil)
assert.Nil(t, err)
assert.Equal(t, "token", token)
}
func TestAddsTokenOnValidHostFromParse(t *testing.T) {
provider, err := NewAzureIdentityAccessTokenProvider(&MockTokenCredential{TokenValue: "token"})
assert.Nil(t, err)
assert.NotNil(t, provider)
url, err := u.Parse("https://graph.microsoft.com")
assert.Nil(t, err)
token, err := provider.GetAuthorizationToken(context.Background(), url, nil)
assert.Nil(t, err)
assert.Equal(t, "token", token)
}
func TestDoesntAddTokenOnDifferentHost(t *testing.T) {
provider, err := NewAzureIdentityAccessTokenProviderWithScopesAndValidHosts(&MockTokenCredential{TokenValue: "token"}, []string{}, []string{"graph.microsoft.com"})
assert.Nil(t, err)
assert.NotNil(t, provider)
token, err := provider.GetAuthorizationToken(context.Background(), &u.URL{Host: "differenthost.com", Scheme: "https"}, nil)
assert.Nil(t, err)
assert.Empty(t, token)
}
func TestDoesntAddTokenOnHttp(t *testing.T) {
provider, err := NewAzureIdentityAccessTokenProvider(&MockTokenCredential{TokenValue: "token"})
assert.Nil(t, err)
assert.NotNil(t, provider)
token, err := provider.GetAuthorizationToken(context.Background(), &u.URL{Host: "differenthost.com", Scheme: "http"}, nil)
assert.NotNil(t, err)
assert.Empty(t, token)
}
func TestAddsClaimsToTokenRequest(t *testing.T) {
provider, err := NewAzureIdentityAccessTokenProvider(&MockTokenCredential{TokenValue: "token"})
assert.Nil(t, err)
assert.NotNil(t, provider)
url, err := u.Parse("https://graph.microsoft.com")
assert.Nil(t, err)
additionalContext := make(map[string]interface{})
additionalContext["claims"] = "eyJhY2Nlc3NfdG9rZW4iOnsibmJmIjp7ImVzc2VudGlhbCI6dHJ1ZSwgInZhbHVlIjoiMTY1MjgxMzUwOCJ9fX0="
token, err := provider.GetAuthorizationToken(context.Background(), url, additionalContext)
assert.NotNil(t, err) //TODO update when azure identity has added the field
assert.Empty(t, token)
}