From cc21f48e69860ad5de159fa0c21cd39f1019c79d Mon Sep 17 00:00:00 2001 From: favonia Date: Fri, 27 Sep 2024 22:30:56 -0500 Subject: [PATCH] test(config): improve coverage --- internal/config/env_auth.go | 15 +++++------- internal/config/env_auth_test.go | 40 ++++++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/internal/config/env_auth.go b/internal/config/env_auth.go index 68eda3f9..b0e4e330 100644 --- a/internal/config/env_auth.go +++ b/internal/config/env_auth.go @@ -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 @@ -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 @@ -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 @@ -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) return "", false case tokenPlain != "": token = tokenPlain @@ -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 diff --git a/internal/config/env_auth_test.go b/internal/config/env_auth_test.go index 6f8ff35c..f740f5ee 100644 --- a/internal/config/env_auth_test.go +++ b/internal/config/env_auth_test.go @@ -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": { @@ -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", "", "",