forked from mch1307/vaultlib
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathauth_test.go
95 lines (87 loc) · 2.18 KB
/
auth_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package vaultlib
import (
"net/http"
"net/url"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestVaultClient_setTokenFromAppRole(t *testing.T) {
rightURL, _ := url.Parse("http://localhost:8200")
badURL, _ := url.Parse("https://localhost:8200")
conf := NewConfig()
anyMountPoint := "anyMountPoint"
anyCreds := NewConfig().AppRoleCredentials
anyCreds.MountPoint = anyMountPoint
htCli := new(http.Client)
type fields struct {
Address *url.URL
HTTPClient *http.Client
AppRoleCredentials *AppRoleCredentials
//Config *Config
Token string
Status string
}
tests := []struct {
name string
fields fields
wantErr bool
}{
{"tokenKO",
fields{
rightURL,
htCli,
conf.AppRoleCredentials,
"bad-token",
""},
true},
{"badUrl",
fields{
badURL,
htCli,
conf.AppRoleCredentials,
"bad-token",
""},
true},
{"anyMountPoint",
fields{
rightURL,
htCli,
anyCreds,
"bad-token",
""},
true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &Client{
address: tt.fields.Address,
httpClient: tt.fields.HTTPClient,
appRoleCredentials: tt.fields.AppRoleCredentials,
//config: tt.fields.Config,
token: &VaultTokenInfo{ID: tt.fields.Token},
status: tt.fields.Status,
}
if err := c.setTokenFromAppRole(); (err != nil) != tt.wantErr {
t.Errorf("Client.setTokenFromAppRole() error = %v, wantErr %v", c.token.ID, tt.fields.Token)
}
})
}
// Renewal test
t.Run("hardRenewal", func(t *testing.T) {
c := &Client{
address: rightURL,
httpClient: htCli,
appRoleCredentials: &AppRoleCredentials{RoleID: longLivedRoleID, SecretID: longLivedSecretID},
token: &VaultTokenInfo{ID: "good-token", Renewable: true},
status: "",
}
// Initial login
err := c.setTokenFromAppRole()
assert.Nil(t, err, "Initial login failed")
assert.Equalf(t, "token ready", c.GetStatus(), "Token init failure")
// Wait for refresh cycle
time.Sleep(time.Second * time.Duration(c.token.TTL))
assert.Equal(t, "token ready (new)", c.GetStatus(), "Token renewal mismatch")
})
}