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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package auth0
import (
"os"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vespa-engine/vespa/client/go/internal/mock"
)
func TestConfigWriting(t *testing.T) {
configPath := filepath.Join(t.TempDir(), "config")
httpClient := mock.HTTPClient{}
flowConfigResponse := `{
"audience": "https://example.com/api/v2/",
"client-id": "some-id",
"device-code-endpoint": "https://example.com/oauth/device/code",
"oauth-token-endpoint": "https://example.com/oauth/token"
}`
httpClient.NextResponseString(200, flowConfigResponse)
client, err := NewClient(&httpClient, Options{ConfigPath: configPath, SystemName: "public", SystemURL: "http://example.com"})
require.Nil(t, err)
assert.Equal(t, "https://example.com/api/v2/", client.Authenticator.Audience)
assert.Equal(t, "some-id", client.Authenticator.ClientID)
assert.Equal(t, "https://example.com/oauth/device/code", client.Authenticator.DeviceCodeEndpoint)
assert.Equal(t, "https://example.com/oauth/token", client.Authenticator.OauthTokenEndpoint)
creds1 := Credentials{
AccessToken: "some-token",
Scopes: []string{"foo", "bar"},
ExpiresAt: time.Date(2022, 03, 01, 15, 45, 50, 0, time.UTC),
}
require.Nil(t, client.WriteCredentials(creds1))
expected := `{
"version": 1,
"providers": {
"auth0": {
"version": 1,
"systems": {
"public": {
"access_token": "some-token",
"scopes": [
"foo",
"bar"
],
"expires_at": "2022-03-01T15:45:50Z"
}
}
}
}
}`
assertConfig(t, expected, configPath)
// Switch to another system
httpClient.NextResponseString(200, flowConfigResponse)
client, err = NewClient(&httpClient, Options{ConfigPath: configPath, SystemName: "publiccd", SystemURL: "http://example.com"})
require.Nil(t, err)
creds2 := Credentials{
AccessToken: "another-token",
Scopes: []string{"baz"},
ExpiresAt: time.Date(2022, 03, 01, 15, 45, 50, 0, time.UTC),
}
require.Nil(t, client.WriteCredentials(creds2))
expected = `{
"version": 1,
"providers": {
"auth0": {
"version": 1,
"systems": {
"public": {
"access_token": "some-token",
"scopes": [
"foo",
"bar"
],
"expires_at": "2022-03-01T15:45:50Z"
},
"publiccd": {
"access_token": "another-token",
"scopes": [
"baz"
],
"expires_at": "2022-03-01T15:45:50Z"
}
}
}
}
}`
assertConfig(t, expected, configPath)
}
func assertConfig(t *testing.T, expected, path string) {
data, err := os.ReadFile(path)
require.Nil(t, err)
assert.Equal(t, expected, string(data))
}
|