Skip to content

Commit

Permalink
wip - testing
Browse files Browse the repository at this point in the history
  • Loading branch information
favonia committed Sep 27, 2024
1 parent f408d31 commit 0cf5946
Showing 1 changed file with 42 additions and 22 deletions.
64 changes: 42 additions & 22 deletions internal/config/env_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ func TestReadAuth(t *testing.T) {
unset(t, "CF_API_TOKEN", "CF_API_TOKEN_FILE", "CF_ACCOUNT_ID")

for name, tc := range map[string]struct {
token string
token1 string

Check failure on line 23 in internal/config/env_auth_test.go

View workflow job for this annotation

GitHub Actions / Lint

other declaration of token1

Check failure on line 23 in internal/config/env_auth_test.go

View workflow job for this annotation

GitHub Actions / Test

other declaration of token1
token2 string

Check failure on line 24 in internal/config/env_auth_test.go

View workflow job for this annotation

GitHub Actions / Lint

other declaration of token2

Check failure on line 24 in internal/config/env_auth_test.go

View workflow job for this annotation

GitHub Actions / Test

other declaration of token2
token1 string

Check failure on line 25 in internal/config/env_auth_test.go

View workflow job for this annotation

GitHub Actions / Lint

token1 redeclared

Check failure on line 25 in internal/config/env_auth_test.go

View workflow job for this annotation

GitHub Actions / Test

token1 redeclared
token2 string

Check failure on line 26 in internal/config/env_auth_test.go

View workflow job for this annotation

GitHub Actions / Lint

token2 redeclared

Check failure on line 26 in internal/config/env_auth_test.go

View workflow job for this annotation

GitHub Actions / Test

token2 redeclared
account string
ok bool
prepareMockPP func(*mocks.MockPP)
Expand Down Expand Up @@ -79,24 +82,29 @@ func useMemFS(memfs fstest.MapFS) {
}

//nolint:paralleltest // environment vars and file system are global
func TestReadAuthWithFile(t *testing.T) {
func TestReadAuth(t *testing.T) {
unset(t, "CF_API_TOKEN", "CF_API_TOKEN_FILE", "CF_ACCOUNT_ID")

for name, tc := range map[string]struct {
token string
tokenFile string
actualPath string
actualContent string
expected string
ok bool
prepareMockPP func(*mocks.MockPP)
token1 string
token2 string
fileToken1Path string
fileToken1Valid bool
fileToken1 string
fileToken2Path string
fileToken2Valid bool
fileToken2 string
account string
ok bool
expected string
prepareMockPP func(*mocks.MockPP)
}{
"ok": {"", "test.txt", "test.txt", "hello", "hello", true, nil},
"both": {
"123456789", "test.txt", "test.txt", "hello", "", false,
func(m *mocks.MockPP) {
m.EXPECT().Noticef(pp.EmojiUserError, "Cannot have both CF_API_TOKEN and CF_API_TOKEN_FILE set")
},
"ok": {
"", "",
"test.token", true, "hello",
"", false, "",
"",
true, "hello", nil,
},
"wrong.path": {
"", "wrong.txt", "actual.txt", "hello", "", false,
Expand All @@ -120,17 +128,29 @@ func TestReadAuthWithFile(t *testing.T) {
t.Run(name, func(t *testing.T) {
mockCtrl := gomock.NewController(t)

store(t, "CF_API_TOKEN", tc.token)
store(t, "CF_API_TOKEN_FILE", tc.tokenFile)
store(t, "CLOUDFLARE_API_TOKEN", tc.token1)
store(t, "CLOUDFLARE_API_TOKEN_FILE", tc.fileToken1Path)
store(t, "CF_API_TOKEN", tc.token2)
store(t, "CF_API_TOKEN_FILE", tc.fileToken2Path)

useMemFS(fstest.MapFS{
tc.actualPath: &fstest.MapFile{
Data: []byte(tc.actualContent),
mapFS := fstest.MapFS{}
if tc.fileToken1Valid {
mapFS[tc.fileToken1Path] = &fstest.MapFile{
Data: []byte(tc.fileToken1),
Mode: 0o644,
ModTime: time.Unix(1234, 5678),
Sys: nil,
},
})
}
}
if tc.fileToken2Valid {
mapFS[tc.fileToken2Path] = &fstest.MapFile{
Data: []byte(tc.fileToken2),
Mode: 0o644,
ModTime: time.Unix(1234, 5678),
Sys: nil,
}
}
useMemFS(mapFS)

var field api.Auth
mockPP := mocks.NewMockPP(mockCtrl)
Expand Down

0 comments on commit 0cf5946

Please sign in to comment.