Skip to content

Commit 918dcbf

Browse files
author
Alexandre Catalan
committed
test(registry): add unit test coverage for artifactory update flags
1 parent 2733359 commit 918dcbf

1 file changed

Lines changed: 86 additions & 5 deletions

File tree

‎internal/cmd/registry/artifactory/update/update_test.go‎

Lines changed: 86 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@ const (
2323
type testCtxKey struct{}
2424

2525
var (
26-
testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo")
27-
testClient = &registry.APIClient{DefaultAPI: &registry.DefaultAPIService{}}
28-
testProjectId = uuid.NewString()
29-
testArtifactoryId = uuid.NewString()
30-
testProxyUrl = "https://registry-1.docker.io"
26+
testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo")
27+
testClient = &registry.APIClient{DefaultAPI: &registry.DefaultAPIService{}}
28+
testProjectId = uuid.NewString()
29+
testArtifactoryId = uuid.NewString()
30+
testProxyUrl = "https://registry-1.docker.io"
31+
testProxyDescription = "docker hub proxy"
32+
testProxyAccessKey = "my-key"
33+
testProxyAccessSecret = "my-secret"
3134
)
3235

3336
func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string {
@@ -86,6 +89,50 @@ func TestParseInput(t *testing.T) {
8689
isValid: true,
8790
expectedModel: fixtureInputModel(),
8891
},
92+
{
93+
description: "with description",
94+
argValues: []string{testArtifactoryId},
95+
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
96+
delete(flagValues, proxyUrlFlag)
97+
flagValues[proxyDescriptionFlag] = testProxyDescription
98+
}),
99+
isValid: true,
100+
expectedModel: fixtureInputModel(func(model *inputModel) {
101+
model.ProxyUrl = nil
102+
model.ProxyDescription = utils.Ptr(testProxyDescription)
103+
}),
104+
},
105+
{
106+
description: "with credentials",
107+
argValues: []string{testArtifactoryId},
108+
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
109+
delete(flagValues, proxyUrlFlag)
110+
flagValues[proxyAccessKeyFlag] = testProxyAccessKey
111+
flagValues[proxyAccessSecretFlag] = testProxyAccessSecret
112+
}),
113+
isValid: true,
114+
expectedModel: fixtureInputModel(func(model *inputModel) {
115+
model.ProxyUrl = nil
116+
model.ProxyAccessKey = utils.Ptr(testProxyAccessKey)
117+
model.ProxyAccessSecret = utils.Ptr(testProxyAccessSecret)
118+
}),
119+
},
120+
{
121+
description: "proxy access key without secret",
122+
argValues: []string{testArtifactoryId},
123+
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
124+
flagValues[proxyAccessKeyFlag] = testProxyAccessKey
125+
}),
126+
isValid: false,
127+
},
128+
{
129+
description: "proxy access secret without key",
130+
argValues: []string{testArtifactoryId},
131+
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
132+
flagValues[proxyAccessSecretFlag] = testProxyAccessSecret
133+
}),
134+
isValid: false,
135+
},
89136
{
90137
description: "no flags set",
91138
argValues: []string{testArtifactoryId},
@@ -138,6 +185,40 @@ func TestBuildRequest(t *testing.T) {
138185
model: fixtureInputModel(),
139186
expectedRequest: fixtureRequest(),
140187
},
188+
{
189+
description: "with description",
190+
model: fixtureInputModel(func(model *inputModel) {
191+
model.ProxyUrl = nil
192+
model.ProxyDescription = utils.Ptr(testProxyDescription)
193+
}),
194+
expectedRequest: fixtureRequest(func(request *registry.ApiPatchArtifactoryRequest) {
195+
*request = testClient.DefaultAPI.PatchArtifactory(testCtx, testProjectId, testRegion, testArtifactoryId).
196+
PatchArtifactoryPayload(registry.PatchArtifactoryPayload{
197+
Proxy: &registry.UpdateProxyRequest{
198+
Description: utils.Ptr(testProxyDescription),
199+
},
200+
})
201+
}),
202+
},
203+
{
204+
description: "with credentials",
205+
model: fixtureInputModel(func(model *inputModel) {
206+
model.ProxyUrl = nil
207+
model.ProxyAccessKey = utils.Ptr(testProxyAccessKey)
208+
model.ProxyAccessSecret = utils.Ptr(testProxyAccessSecret)
209+
}),
210+
expectedRequest: fixtureRequest(func(request *registry.ApiPatchArtifactoryRequest) {
211+
*request = testClient.DefaultAPI.PatchArtifactory(testCtx, testProjectId, testRegion, testArtifactoryId).
212+
PatchArtifactoryPayload(registry.PatchArtifactoryPayload{
213+
Proxy: &registry.UpdateProxyRequest{
214+
Credentials: &registry.Credentials{
215+
AccessKey: testProxyAccessKey,
216+
AccessSecret: testProxyAccessSecret,
217+
},
218+
},
219+
})
220+
}),
221+
},
141222
}
142223

143224
for _, tt := range tests {

0 commit comments

Comments
 (0)