-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadvanced_auth_client_test.go
77 lines (70 loc) · 2.57 KB
/
advanced_auth_client_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
package httpclient
import (
"errors"
"testing"
"time"
"github.com/cloudtrust/httpclient/mock"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
)
func TestNewMultiRealmTokenClient(t *testing.T) {
var mockCtrl = gomock.NewController(t)
defer mockCtrl.Finish()
var mockTokenProvider = mock.NewOidcTokenProvider((mockCtrl))
var tokenError = errors.New("token error")
var realm = "my-realm"
t.Run("Invalid URL", func(t *testing.T) {
var _, err = NewMultiRealmTokenClient(":/\000/", time.Minute, mockTokenProvider)
assert.NotNil(t, err)
})
var client, err = NewMultiRealmTokenClient("http://localhost", time.Minute, mockTokenProvider)
assert.Nil(t, err)
t.Run("GET", func(t *testing.T) {
t.Run("can't get token", func(t *testing.T) {
mockTokenProvider.EXPECT().ProvideToken(gomock.Any()).Return("", tokenError)
var err = client.Get(nil)
assert.Equal(t, tokenError, err)
})
t.Run("success", func(t *testing.T) {
mockTokenProvider.EXPECT().ProvideToken(gomock.Any()).Return("default-token", nil)
var err = client.Get(nil)
assert.NotEqual(t, tokenError, err)
})
})
t.Run("POST", func(t *testing.T) {
t.Run("can't get token", func(t *testing.T) {
mockTokenProvider.EXPECT().ProvideTokenForRealm(gomock.Any(), realm).Return("", tokenError)
var _, err = client.ForRealm(realm).Post(nil)
assert.Equal(t, tokenError, err)
})
t.Run("success", func(t *testing.T) {
mockTokenProvider.EXPECT().ProvideTokenForRealm(gomock.Any(), realm).Return("token-for-"+realm, nil)
var _, err = client.ForRealm(realm).Post(nil)
assert.NotEqual(t, tokenError, err)
})
})
t.Run("DELETE", func(t *testing.T) {
t.Run("can't get token", func(t *testing.T) {
mockTokenProvider.EXPECT().ProvideTokenForRealm(gomock.Any(), realm).Return("", tokenError)
var err = client.ForRealm(realm).Delete()
assert.Equal(t, tokenError, err)
})
t.Run("success", func(t *testing.T) {
mockTokenProvider.EXPECT().ProvideTokenForRealm(gomock.Any(), realm).Return("token-for-"+realm, nil)
var err = client.ForRealm(realm).Delete()
assert.NotEqual(t, tokenError, err)
})
})
t.Run("PUT", func(t *testing.T) {
t.Run("can't get token", func(t *testing.T) {
mockTokenProvider.EXPECT().ProvideTokenForRealm(gomock.Any(), realm).Return("", tokenError)
var err = client.ForRealm(realm).Put()
assert.Equal(t, tokenError, err)
})
t.Run("success", func(t *testing.T) {
mockTokenProvider.EXPECT().ProvideTokenForRealm(gomock.Any(), realm).Return("token-for-"+realm, nil)
var err = client.ForRealm(realm).Put()
assert.NotEqual(t, tokenError, err)
})
})
}