add test for file write

This commit is contained in:
Richard Nordström 2024-09-23 21:20:56 +02:00
parent bb35ca090f
commit b7ff019b60
No known key found for this signature in database
GPG Key ID: ACCB352EC60AF27C
1 changed files with 25 additions and 0 deletions

View File

@ -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)
}