-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathauth_repo_test.go
230 lines (208 loc) · 6.25 KB
/
auth_repo_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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package repository
import (
"context"
"testing"
"github.com/harness/ff-proxy/v2/cache"
"github.com/stretchr/testify/assert"
"github.com/harness/ff-proxy/v2/domain"
)
func TestAuthRepo_Get(t *testing.T) {
populated := []domain.AuthConfig{
{
APIKey: domain.AuthAPIKey("apikey-foo"),
EnvironmentID: domain.EnvironmentID("env-approved"),
},
{
APIKey: domain.AuthAPIKey("apikey-2"),
EnvironmentID: domain.EnvironmentID("env-not-approved"),
},
}
unpopulated := []domain.AuthConfig{}
type expected struct {
strVal string
boolVal bool
}
testCases := map[string]struct {
cache cache.Cache
data []domain.AuthConfig
key string
shouldErr bool
expected expected
}{
"Given I have an empty AuthRepo": {
cache: cache.NewMemCache(),
data: unpopulated,
key: "apikey-foo",
shouldErr: true,
expected: expected{strVal: "", boolVal: false},
},
"Given I have a populated AuthRepo but try to get a key that doesn't exist": {
cache: cache.NewMemCache(),
data: populated,
key: "foo",
shouldErr: true,
expected: expected{strVal: "", boolVal: false},
},
"Given I have a populated AuthRepo and try to get a key that does exist": {
cache: cache.NewMemCache(),
data: populated,
key: "apikey-foo",
shouldErr: false,
expected: expected{strVal: "env-approved", boolVal: true},
},
}
for desc, tc := range testCases {
tc := tc
t.Run(desc, func(t *testing.T) {
ctx := context.Background()
repo := NewAuthRepo(tc.cache)
assert.Nil(t, repo.Add(ctx, tc.data...))
actual, ok, err := repo.Get(ctx, domain.AuthAPIKey(tc.key))
if tc.shouldErr {
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
}
assert.Equal(t, tc.expected.boolVal, ok)
assert.Equal(t, tc.expected.strVal, actual)
})
}
}
func TestAPIRepo_Remove(t *testing.T) {
populatedConfig := []domain.AuthConfig{
{
APIKey: domain.AuthAPIKey("apikey-foo"),
EnvironmentID: domain.EnvironmentID("env-approved"),
},
{
APIKey: domain.AuthAPIKey("apikey-2"),
EnvironmentID: domain.EnvironmentID("env-not-approved"),
},
}
emptyConfig := []domain.AuthConfig{}
testCases := map[string]struct {
cache cache.MemCache
repoConfig []domain.AuthConfig
shouldErr bool
}{
"Given I call Remove with and the ApiKey config does not exist": {
cache: cache.NewMemCache(),
repoConfig: emptyConfig,
shouldErr: true,
},
"Given I call Remove with and the ApiKey config does exist": {
cache: cache.NewMemCache(),
repoConfig: populatedConfig,
shouldErr: false,
},
}
for desc, tc := range testCases {
tc := tc
t.Run(desc, func(t *testing.T) {
ctx := context.Background()
repo := NewAuthRepo(tc.cache)
if tc.shouldErr {
assert.Error(t, repo.RemoveAllKeysForEnvironment(ctx, "env-approved"))
} else {
assert.Nil(t, repo.Add(ctx, tc.repoConfig...))
assert.Nil(t, repo.AddAPIConfigsForEnvironment(ctx, "env-approved", []string{"apikey-foo", "apikey-bar"}))
assert.Nil(t, repo.RemoveAllKeysForEnvironment(ctx, "env-approved"))
}
})
}
}
func TestAPIRepo_PatchAPIConfigForEnvironment(t *testing.T) {
populatedConfig := []domain.AuthConfig{
{
APIKey: domain.AuthAPIKey("auth-key-apikey-foo"),
EnvironmentID: domain.EnvironmentID("env-approved"),
},
{
APIKey: domain.AuthAPIKey("auth-key-apikey-bar"),
EnvironmentID: domain.EnvironmentID("env-approved"),
},
}
oneKeyConfig := []domain.AuthConfig{
{
APIKey: domain.AuthAPIKey("auth-key-apikey-foo"),
EnvironmentID: domain.EnvironmentID("env-approved"),
},
}
emptyConfig := []domain.AuthConfig{}
testCases := map[string]struct {
action string
key string
cache cache.MemCache
repoConfig []domain.AuthConfig
shouldErr bool
}{
"Given I call PatchAPIConfigForEnvironment with 'invalid'": {
action: "invalid",
cache: cache.NewMemCache(),
repoConfig: populatedConfig,
shouldErr: true,
},
"Given I call PatchAPIConfigForEnvironment with 'apiKeyAdded' action and config does not exist": {
action: "apiKeyAdded",
key: "apikey-foo",
cache: cache.NewMemCache(),
repoConfig: emptyConfig,
shouldErr: false,
},
"Given I call PatchAPIConfigForEnvironment with 'apiKeyAdded' action and config does exist but already contains key": {
action: "apiKeyAdded",
key: "apikey-foo",
cache: cache.NewMemCache(),
repoConfig: populatedConfig,
shouldErr: false,
},
"Given I call PatchAPIConfigForEnvironment with 'apiKeyAdded' action and config does and does not contain key": {
action: "apiKeyAdded",
key: "apikey-baz",
cache: cache.NewMemCache(),
repoConfig: populatedConfig,
shouldErr: false,
},
"Given I call PatchAPIConfigForEnvironment with 'apiKeyRemoved' action and config does not exist": {
action: "apiKeyRemoved",
key: "apikey-foo",
cache: cache.NewMemCache(),
repoConfig: emptyConfig,
shouldErr: false,
},
"Given I call PatchAPIConfigForEnvironment with 'apiKeyRemoved' action and config does exist but does not contain key": {
action: "apiKeyRemoved",
key: "apikey-baz",
cache: cache.NewMemCache(),
repoConfig: populatedConfig,
shouldErr: false,
},
"Given I call PatchAPIConfigForEnvironment with 'apiKeyRemoved' action and config does and contains key only one key": {
action: "apiKeyRemoved",
key: "apikey-foo",
cache: cache.NewMemCache(),
repoConfig: oneKeyConfig,
shouldErr: false,
},
"Given I call PatchAPIConfigForEnvironment with 'apiKeyRemoved' action and config does exit and contains target key": {
action: "apiKeyRemoved",
key: "apikey-foo",
cache: cache.NewMemCache(),
repoConfig: populatedConfig,
shouldErr: false,
},
}
for desc, tc := range testCases {
tc := tc
t.Run(desc, func(t *testing.T) {
ctx := context.Background()
repo := NewAuthRepo(tc.cache)
if tc.shouldErr {
assert.Error(t, repo.PatchAPIConfigForEnvironment(ctx, "123", "key12", tc.action))
} else {
assert.Nil(t, repo.Add(ctx, tc.repoConfig...))
assert.Nil(t, repo.PatchAPIConfigForEnvironment(ctx, "env-approved", tc.key, tc.action))
}
})
}
}