-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiam_test.go
164 lines (159 loc) · 4.24 KB
/
iam_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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package iam
import (
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/selectel/iam-go/iamerrors"
baseclient "github.com/selectel/iam-go/internal/client"
"github.com/selectel/iam-go/service/federations/saml"
"github.com/selectel/iam-go/service/groups"
"github.com/selectel/iam-go/service/s3credentials"
"github.com/selectel/iam-go/service/serviceusers"
"github.com/selectel/iam-go/service/users"
)
const (
testToken = "test-token"
testURL = "http://example.org/"
)
//nolint:funlen // This is a test function.
func TestNew(t *testing.T) {
type args struct {
opts []Option
}
tests := []struct {
name string
args args
expectedClient func() *Client
expectedBaseClient *baseclient.BaseClient
expectedError error
}{
{
name: "Test NewIAMClientV1 only with TokenAuth and APIUrl",
args: args{
opts: []Option{
WithAPIUrl(testURL),
WithAuthOpts(&AuthOpts{
KeystoneToken: testToken,
}),
},
},
expectedClient: func() *Client {
baseClient := &baseclient.BaseClient{
HTTPClient: &http.Client{
Timeout: defaultHTTPTimeout * time.Second,
Transport: newHTTPTransport(),
},
APIUrl: testURL,
AuthMethod: &baseclient.KeystoneTokenAuth{KeystoneToken: testToken},
UserAgent: appName + "/" + findModuleVersion(),
}
return &Client{
authOpts: &AuthOpts{
KeystoneToken: testToken,
},
baseClient: baseClient,
Users: users.New(baseClient),
ServiceUsers: serviceusers.New(baseClient),
Groups: groups.New(baseClient),
S3Credentials: s3credentials.New(baseClient),
SAMLFederations: saml.New(baseClient),
}
},
expectedError: nil,
},
{
name: "Test NewIAMClientV1 only with APIUrl",
args: args{
opts: []Option{
WithAPIUrl(testURL),
},
},
expectedClient: nil,
expectedError: iamerrors.ErrClientNoAuthOpts,
},
{
name: "Test NewIAMClientV1 only with TokenAuth",
args: args{
opts: []Option{
WithAuthOpts(&AuthOpts{
KeystoneToken: testToken,
}),
},
},
expectedClient: func() *Client {
baseClient := &baseclient.BaseClient{
HTTPClient: &http.Client{
Timeout: defaultHTTPTimeout * time.Second,
Transport: newHTTPTransport(),
},
APIUrl: defaultIAMApiURL,
AuthMethod: &baseclient.KeystoneTokenAuth{KeystoneToken: testToken},
UserAgent: appName + "/" + findModuleVersion(),
}
return &Client{
authOpts: &AuthOpts{
KeystoneToken: testToken,
},
baseClient: baseClient,
Users: users.New(baseClient),
ServiceUsers: serviceusers.New(baseClient),
Groups: groups.New(baseClient),
S3Credentials: s3credentials.New(baseClient),
SAMLFederations: saml.New(baseClient),
}
},
expectedError: nil,
},
{
name: "Test NewIAMClientV1 only with TokenAuth and APIUrl and HTTPClient",
args: args{
opts: []Option{
WithAPIUrl(testURL),
WithAuthOpts(&AuthOpts{
KeystoneToken: testToken,
}),
WithCustomHTTPClient(&http.Client{
Timeout: 10 * time.Second,
}),
},
},
expectedClient: func() *Client {
baseClient := &baseclient.BaseClient{
HTTPClient: &http.Client{
Timeout: 10 * time.Second,
},
APIUrl: testURL,
AuthMethod: &baseclient.KeystoneTokenAuth{KeystoneToken: testToken},
UserAgent: appName + "/" + findModuleVersion(),
}
return &Client{
authOpts: &AuthOpts{
KeystoneToken: testToken,
},
baseClient: baseClient,
Users: users.New(baseClient),
ServiceUsers: serviceusers.New(baseClient),
Groups: groups.New(baseClient),
S3Credentials: s3credentials.New(baseClient),
SAMLFederations: saml.New(baseClient),
}
},
expectedError: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
var expected *Client
if tt.expectedClient != nil {
expected = tt.expectedClient()
}
actual, err := New(tt.args.opts...)
require.ErrorIs(err, tt.expectedError)
assert.Equal(expected, actual)
})
}
}