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>
This commit is contained in:
Sebastiaan van Stijn 2023-08-08 17:31:55 +02:00
parent 84c956a171
commit 57f0e46de1
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
1 changed files with 25 additions and 24 deletions

View File

@ -17,15 +17,8 @@ import (
) )
const ( const (
userErr = "userunknownError" unknownUser = "userunknownError"
testAuthErrMsg = "UNKNOWN_ERR" errUnknownUser = "UNKNOWN_ERR"
)
var testAuthErrors = map[string]error{
userErr: fmt.Errorf(testAuthErrMsg),
}
var (
expiredPassword = "I_M_EXPIRED" expiredPassword = "I_M_EXPIRED"
useToken = "I_M_TOKEN" useToken = "I_M_TOKEN"
) )
@ -47,8 +40,10 @@ func (c fakeClient) RegistryLogin(_ context.Context, auth registrytypes.AuthConf
IdentityToken: auth.Password, IdentityToken: auth.Password,
}, nil }, nil
} }
err := testAuthErrors[auth.Username] if auth.Username == unknownUser {
return registrytypes.AuthenticateOKBody{}, err return registrytypes.AuthenticateOKBody{}, fmt.Errorf(errUnknownUser)
}
return registrytypes.AuthenticateOKBody{}, nil
} }
func TestLoginWithCredStoreCreds(t *testing.T) { func TestLoginWithCredStoreCreds(t *testing.T) {
@ -63,10 +58,10 @@ func TestLoginWithCredStoreCreds(t *testing.T) {
}, },
{ {
inputAuthConfig: registrytypes.AuthConfig{ inputAuthConfig: registrytypes.AuthConfig{
Username: userErr, Username: unknownUser,
}, },
expectedMsg: "Authenticating with existing credentials...\n", expectedMsg: "Authenticating with existing credentials...\n",
expectedErr: fmt.Sprintf("Login did not succeed, error: %s\n", testAuthErrMsg), expectedErr: fmt.Sprintf("Login did not succeed, error: %s\n", errUnknownUser),
}, },
} }
ctx := context.Background() ctx := context.Background()
@ -83,10 +78,12 @@ func TestLoginWithCredStoreCreds(t *testing.T) {
} }
func TestRunLogin(t *testing.T) { func TestRunLogin(t *testing.T) {
const storedServerAddress = "reg1" const (
const validUsername = "u1" storedServerAddress = "reg1"
const validPassword = "p1" validUsername = "u1"
const validPassword2 = "p2" validPassword = "p1"
validPassword2 = "p2"
)
validAuthConfig := configtypes.AuthConfig{ validAuthConfig := configtypes.AuthConfig{
ServerAddress: storedServerAddress, ServerAddress: storedServerAddress,
@ -104,20 +101,22 @@ func TestRunLogin(t *testing.T) {
IdentityToken: useToken, IdentityToken: useToken,
} }
testCases := []struct { testCases := []struct {
doc string
inputLoginOption loginOptions inputLoginOption loginOptions
inputStoredCred *configtypes.AuthConfig inputStoredCred *configtypes.AuthConfig
expectedErr string expectedErr string
expectedSavedCred configtypes.AuthConfig expectedSavedCred configtypes.AuthConfig
}{ }{
{ {
doc: "valid auth from store",
inputLoginOption: loginOptions{ inputLoginOption: loginOptions{
serverAddress: storedServerAddress, serverAddress: storedServerAddress,
}, },
inputStoredCred: &validAuthConfig, inputStoredCred: &validAuthConfig,
expectedErr: "",
expectedSavedCred: validAuthConfig, expectedSavedCred: validAuthConfig,
}, },
{ {
doc: "expired auth",
inputLoginOption: loginOptions{ inputLoginOption: loginOptions{
serverAddress: storedServerAddress, serverAddress: storedServerAddress,
}, },
@ -125,13 +124,13 @@ func TestRunLogin(t *testing.T) {
expectedErr: "Error: Cannot perform an interactive login from a non TTY device", expectedErr: "Error: Cannot perform an interactive login from a non TTY device",
}, },
{ {
doc: "valid username and password",
inputLoginOption: loginOptions{ inputLoginOption: loginOptions{
serverAddress: storedServerAddress, serverAddress: storedServerAddress,
user: validUsername, user: validUsername,
password: validPassword2, password: validPassword2,
}, },
inputStoredCred: &validAuthConfig, inputStoredCred: &validAuthConfig,
expectedErr: "",
expectedSavedCred: configtypes.AuthConfig{ expectedSavedCred: configtypes.AuthConfig{
ServerAddress: storedServerAddress, ServerAddress: storedServerAddress,
Username: validUsername, Username: validUsername,
@ -139,27 +138,29 @@ func TestRunLogin(t *testing.T) {
}, },
}, },
{ {
doc: "unknown user",
inputLoginOption: loginOptions{ inputLoginOption: loginOptions{
serverAddress: storedServerAddress, serverAddress: storedServerAddress,
user: userErr, user: unknownUser,
password: validPassword, password: validPassword,
}, },
inputStoredCred: &validAuthConfig, inputStoredCred: &validAuthConfig,
expectedErr: testAuthErrMsg, expectedErr: errUnknownUser,
}, },
{ {
doc: "valid token",
inputLoginOption: loginOptions{ inputLoginOption: loginOptions{
serverAddress: storedServerAddress, serverAddress: storedServerAddress,
user: validUsername, user: validUsername,
password: useToken, password: useToken,
}, },
inputStoredCred: &validIdentityToken, inputStoredCred: &validIdentityToken,
expectedErr: "",
expectedSavedCred: validIdentityToken, expectedSavedCred: validIdentityToken,
}, },
} }
for i, tc := range testCases { for _, tc := range testCases {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { tc := tc
t.Run(tc.doc, func(t *testing.T) {
tmpFile := fs.NewFile(t, "test-run-login") tmpFile := fs.NewFile(t, "test-run-login")
defer tmpFile.Remove() defer tmpFile.Remove()
cli := test.NewFakeCli(&fakeClient{}) cli := test.NewFakeCli(&fakeClient{})