From b7ff019b6077ae8a2b65c28a0629f168b54af402 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Nordstr=C3=B6m?= Date: Mon, 23 Sep 2024 21:20:56 +0200 Subject: [PATCH] add test for file write --- libs/auth/cache/file_test.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/libs/auth/cache/file_test.go b/libs/auth/cache/file_test.go index bd3f84ac..48ab5c98 100644 --- a/libs/auth/cache/file_test.go +++ b/libs/auth/cache/file_test.go @@ -1,6 +1,7 @@ package cache import ( + "encoding/json" "os" "path/filepath" "runtime" @@ -140,3 +141,27 @@ func TestDeleteKeyNotExist(t *testing.T) { _, err = c.Lookup("x") assert.Equal(t, ErrNotConfigured, err) } + +func TestWrite(t *testing.T) { + tempFile := filepath.Join(t.TempDir(), "token-cache.json") + + tokenMap := map[string]*oauth2.Token{} + token := &oauth2.Token{ + AccessToken: "some-access-token", + } + tokenMap["test"] = token + + cache := &FileTokenCache{ + fileLocation: tempFile, + Tokens: tokenMap, + } + + err := cache.write() + assert.NoError(t, err) + + content, err := os.ReadFile(tempFile) + require.NoError(t, err) + + expected, _ := json.MarshalIndent(&cache, "", " ") + assert.Equal(t, content, expected) +}