Skip to content

Commit

Permalink
test(config): improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
favonia committed Sep 28, 2024
1 parent cd1d1ff commit cc21f48
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
15 changes: 6 additions & 9 deletions internal/config/env_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ func readPlainAuthTokens(ppfmt pp.PP) (string, string, bool) {
case token1 == "" && token2 == "":
return "", "", true
case token1 != "" && token2 != "" && token1 != token2:
ppfmt.Noticef(pp.EmojiUserError, "The values of %s and %s do not match", TokenKey1, TokenKey2)
ppfmt.Noticef(pp.EmojiUserError,
"The values of %s and %s do not match; they must specify the same token", TokenKey1, TokenKey2)
return "", "", false
case token1 != "":
token, tokenKey = token1, TokenKey1
Expand Down Expand Up @@ -82,7 +83,7 @@ func readAuthTokenFiles(ppfmt pp.PP) (string, string, bool) {
switch {
case token1 != "" && token2 != "" && token1 != token2:
ppfmt.Noticef(pp.EmojiUserError,
"The files specified by %s and %s contain conflicting tokens", TokenFileKey1, TokenFileKey2)
"The files specified by %s and %s have conflicting tokens; their content must match", TokenFileKey1, TokenFileKey2)
return "", "", false
case token1 != "":
return token1, TokenFileKey1, true
Expand All @@ -95,11 +96,6 @@ func readAuthTokenFiles(ppfmt pp.PP) (string, string, bool) {
}

func readAuthToken(ppfmt pp.PP) (string, bool) {
//default:
// ppfmt.Noticef(pp.EmojiUserError, "Needs either CF_API_TOKEN or CF_API_TOKEN_FILE")
// return "", false
//}

tokenPlain, tokenPlainKey, ok := readPlainAuthTokens(ppfmt)
if !ok {
return "", false
Expand All @@ -114,7 +110,8 @@ func readAuthToken(ppfmt pp.PP) (string, bool) {
switch {
case tokenPlain != "" && tokenFile != "" && tokenPlain != tokenFile:
ppfmt.Noticef(pp.EmojiUserError,
"The value of %s does not match the token found in the file specified by %s", tokenPlainKey, tokenFileKey)
"The value of %s does not match the token found in the file specified by %s; they must specify the same token",
tokenPlainKey, tokenFileKey)

Check warning on line 114 in internal/config/env_auth.go

View check run for this annotation

Codecov / codecov/patch

internal/config/env_auth.go#L111-L114

Added lines #L111 - L114 were not covered by tests
return "", false
case tokenPlain != "":
token = tokenPlain
Expand All @@ -127,7 +124,7 @@ func readAuthToken(ppfmt pp.PP) (string, bool) {

if !oauthBearerRegex.MatchString(token) {
ppfmt.Noticef(pp.EmojiUserWarning,
"The API token appears to be invalid. It does not follow the OAuth2 bearer token format.")
"The API token appears to be invalid; it does not follow the OAuth2 bearer token format")
}

return token, true
Expand Down
40 changes: 38 additions & 2 deletions internal/config/env_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,38 @@ func TestReadAuth(t *testing.T) {
},
"empty-token": {
map[string]string{},
"", "", "", "", "account",
"", "", "", "", "",
false, "",
func(m *mocks.MockPP) {
m.EXPECT().Noticef(pp.EmojiUserError,
"Needs either %s or %s", "CLOUDFLARE_API_TOKEN", "CLOUDFLARE_API_TOKEN_FILE")
},
},
"conflicting": {
map[string]string{},
"token1", "token2", "", "", "",
false, "",
func(m *mocks.MockPP) {
m.EXPECT().Noticef(pp.EmojiUserError,
"The values of %s and %s do not match; they must specify the same token",
"CLOUDFLARE_API_TOKEN", "CF_API_TOKEN")
},
},
"old": {
map[string]string{},
"", "token2", "", "", "",
true, "token2",
func(m *mocks.MockPP) {
m.EXPECT().Hintf(pp.HintAuthTokenNewPrefix, config.HintAuthTokenNewPrefix)
},
},
"invalid": {
map[string]string{},
"!!!", "", "", "", "",
true, "!!!",
func(m *mocks.MockPP) {
m.EXPECT().Noticef(pp.EmojiUserWarning,
"The API token appears to be invalid. It does not follow the OAuth2 bearer token format.")
"The API token appears to be invalid; it does not follow the OAuth2 bearer token format")
},
},
"account": {
Expand Down Expand Up @@ -85,6 +103,24 @@ func TestReadAuth(t *testing.T) {
m.EXPECT().Noticef(pp.EmojiUserError, "Failed to read %q: %v", "wrong.txt", gomock.Any())
},
},
"file/wrong.path/2": {
map[string]string{},
"", "", "", "wrong.txt", "",
false, "",
func(m *mocks.MockPP) {
m.EXPECT().Noticef(pp.EmojiUserError, "Failed to read %q: %v", "wrong.txt", gomock.Any())
},
},
"file/conflicting": {
map[string]string{"token1.txt": "hello1", "token2.txt": "hello2"},
"", "", "token1.txt", "token2.txt", "",
false, "",
func(m *mocks.MockPP) {
m.EXPECT().Noticef(pp.EmojiUserError,
"The files specified by %s and %s have conflicting tokens; their content must match",
"CLOUDFLARE_API_TOKEN_FILE", "CF_API_TOKEN_FILE")
},
},
"file/empty": {
map[string]string{"empty.txt": ""},
"", "", "empty.txt", "", "",
Expand Down

0 comments on commit cc21f48

Please sign in to comment.