Skip to content

Commit 57f0e46

Browse files
committed
cli/command/registry: cleanup login tests
- use consts for fixed values, and rename some for clarity - remove testAuthErrors map and inline the logic (same as we do for other cases) Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 84c956a commit 57f0e46

1 file changed

Lines changed: 25 additions & 24 deletions

File tree

cli/command/registry/login_test.go

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,8 @@ import (
1717
)
1818

1919
const (
20-
userErr = "userunknownError"
21-
testAuthErrMsg = "UNKNOWN_ERR"
22-
)
23-
24-
var testAuthErrors = map[string]error{
25-
userErr: fmt.Errorf(testAuthErrMsg),
26-
}
27-
28-
var (
20+
unknownUser = "userunknownError"
21+
errUnknownUser = "UNKNOWN_ERR"
2922
expiredPassword = "I_M_EXPIRED"
3023
useToken = "I_M_TOKEN"
3124
)
@@ -47,8 +40,10 @@ func (c fakeClient) RegistryLogin(_ context.Context, auth registrytypes.AuthConf
4740
IdentityToken: auth.Password,
4841
}, nil
4942
}
50-
err := testAuthErrors[auth.Username]
51-
return registrytypes.AuthenticateOKBody{}, err
43+
if auth.Username == unknownUser {
44+
return registrytypes.AuthenticateOKBody{}, fmt.Errorf(errUnknownUser)
45+
}
46+
return registrytypes.AuthenticateOKBody{}, nil
5247
}
5348

5449
func TestLoginWithCredStoreCreds(t *testing.T) {
@@ -63,10 +58,10 @@ func TestLoginWithCredStoreCreds(t *testing.T) {
6358
},
6459
{
6560
inputAuthConfig: registrytypes.AuthConfig{
66-
Username: userErr,
61+
Username: unknownUser,
6762
},
6863
expectedMsg: "Authenticating with existing credentials...\n",
69-
expectedErr: fmt.Sprintf("Login did not succeed, error: %s\n", testAuthErrMsg),
64+
expectedErr: fmt.Sprintf("Login did not succeed, error: %s\n", errUnknownUser),
7065
},
7166
}
7267
ctx := context.Background()
@@ -83,10 +78,12 @@ func TestLoginWithCredStoreCreds(t *testing.T) {
8378
}
8479

8580
func TestRunLogin(t *testing.T) {
86-
const storedServerAddress = "reg1"
87-
const validUsername = "u1"
88-
const validPassword = "p1"
89-
const validPassword2 = "p2"
81+
const (
82+
storedServerAddress = "reg1"
83+
validUsername = "u1"
84+
validPassword = "p1"
85+
validPassword2 = "p2"
86+
)
9087

9188
validAuthConfig := configtypes.AuthConfig{
9289
ServerAddress: storedServerAddress,
@@ -104,62 +101,66 @@ func TestRunLogin(t *testing.T) {
104101
IdentityToken: useToken,
105102
}
106103
testCases := []struct {
104+
doc string
107105
inputLoginOption loginOptions
108106
inputStoredCred *configtypes.AuthConfig
109107
expectedErr string
110108
expectedSavedCred configtypes.AuthConfig
111109
}{
112110
{
111+
doc: "valid auth from store",
113112
inputLoginOption: loginOptions{
114113
serverAddress: storedServerAddress,
115114
},
116115
inputStoredCred: &validAuthConfig,
117-
expectedErr: "",
118116
expectedSavedCred: validAuthConfig,
119117
},
120118
{
119+
doc: "expired auth",
121120
inputLoginOption: loginOptions{
122121
serverAddress: storedServerAddress,
123122
},
124123
inputStoredCred: &expiredAuthConfig,
125124
expectedErr: "Error: Cannot perform an interactive login from a non TTY device",
126125
},
127126
{
127+
doc: "valid username and password",
128128
inputLoginOption: loginOptions{
129129
serverAddress: storedServerAddress,
130130
user: validUsername,
131131
password: validPassword2,
132132
},
133133
inputStoredCred: &validAuthConfig,
134-
expectedErr: "",
135134
expectedSavedCred: configtypes.AuthConfig{
136135
ServerAddress: storedServerAddress,
137136
Username: validUsername,
138137
Password: validPassword2,
139138
},
140139
},
141140
{
141+
doc: "unknown user",
142142
inputLoginOption: loginOptions{
143143
serverAddress: storedServerAddress,
144-
user: userErr,
144+
user: unknownUser,
145145
password: validPassword,
146146
},
147147
inputStoredCred: &validAuthConfig,
148-
expectedErr: testAuthErrMsg,
148+
expectedErr: errUnknownUser,
149149
},
150150
{
151+
doc: "valid token",
151152
inputLoginOption: loginOptions{
152153
serverAddress: storedServerAddress,
153154
user: validUsername,
154155
password: useToken,
155156
},
156157
inputStoredCred: &validIdentityToken,
157-
expectedErr: "",
158158
expectedSavedCred: validIdentityToken,
159159
},
160160
}
161-
for i, tc := range testCases {
162-
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
161+
for _, tc := range testCases {
162+
tc := tc
163+
t.Run(tc.doc, func(t *testing.T) {
163164
tmpFile := fs.NewFile(t, "test-run-login")
164165
defer tmpFile.Remove()
165166
cli := test.NewFakeCli(&fakeClient{})

0 commit comments

Comments
 (0)