Skip to content

Commit

Permalink
Add test for token file creation
Browse files Browse the repository at this point in the history
  • Loading branch information
ginal committed Jun 28, 2024
1 parent 3199e5e commit 2a233d0
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 21 deletions.
54 changes: 54 additions & 0 deletions rai/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"context"
"fmt"
"net/http"
"os"
"path/filepath"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -63,6 +65,34 @@ func findModel(models []Model, name string) *Model {
return nil
}

// deleteTokenCacheDir deletes the token file cache and the "~/.rai" directory if empty
func deleteTokenCacheDir(t *testing.T) {
fname, err := cachePath()
if err != nil {
t.Fatal("Failed to get token cache file name")
}
err = os.Remove(fname)
if err != nil {
t.Fatalf("Failed to delete token cache file %s\n", fname)
}
err = os.Remove(filepath.Dir(fname))
if err != nil {
fmt.Printf("Failed to delete token cache file directory %s\n", filepath.Dir(fname))
}
}

// assertTokenCacheFileCreated asserts that the token file has been created
func assertTokenCacheFileCreated(t *testing.T) {
fpath, err := cachePath()
if err != nil {
t.Error("Failed to get token cache file name")
}

if _, err := os.Stat(fpath); err != nil {
t.Errorf("Failed to stat token cache file %s, err: %s\n", fpath, err)
}
}

func TestNewClient(t *testing.T) {
var testClient *Client
var cfg Config
Expand Down Expand Up @@ -95,6 +125,30 @@ func TestNewClient(t *testing.T) {
assert.NotNil(t, err)
}

// Test token cache file creation
func TestTokenCacheFile(t *testing.T) {
deleteTokenCacheDir(t)

var testClient *Client
var cfg Config

err := getConfig(&cfg)
assert.Nil(t, err)

opts := ClientOptions{Config: cfg}
testClient = NewClient(context.Background(), &opts)

token, err := testClient.accessTokenHandler.GetAccessToken()
assert.Nil(t, err)
assert.NotNil(t, token)

tokenCached, err := testClient.accessTokenHandler.GetAccessToken()

Check failure on line 145 in rai/client_test.go

View workflow job for this annotation

GitHub Actions / lint

ineffectual assignment to err (ineffassign)

assert.Equal(t, token, tokenCached)

assertTokenCacheFileCreated(t)
}

// Test database management APIs.
func TestDatabase(t *testing.T) {
client := test.client
Expand Down
32 changes: 11 additions & 21 deletions rai/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"os"
"os/user"
"path"
"path/filepath"

"github.com/pkg/errors"
)
Expand Down Expand Up @@ -56,24 +57,15 @@ func NewClientCredentialsHandler(
return &ClientCredentialsHandler{client: c, creds: creds}
}

// Returns the name of the token cache file.
func cacheName() (string, error) {
// Returns the path of the token cache file.
func cachePath() (string, error) {
usr, err := user.Current()
if err != nil {
return "", err
}
return path.Join(usr.HomeDir, ".rai", "tokens.json"), nil
}

// Returns the directory of the token cache file.
func cacheDir() (string, error) {
usr, err := user.Current()
if err != nil {
return "", err
}
return path.Join(usr.HomeDir, ".rai"), nil
}

// Read the access token corresponding to the given ClientID from the local
// token cache, returns nil if the token does not exist.
func readAccessToken(creds *ClientCredentials) (*AccessToken, error) {
Expand All @@ -88,7 +80,7 @@ func readAccessToken(creds *ClientCredentials) (*AccessToken, error) {
}

func readTokenCache() (map[string]*AccessToken, error) {
fname, err := cacheName()
fname, err := cachePath()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -116,19 +108,17 @@ func writeAccessToken(clientID string, token *AccessToken) {
}

func writeTokenCache(cache map[string]*AccessToken) {
dname, err := cacheDir()
fname, err := cachePath()
if err != nil {
fmt.Println(errors.Wrapf(err, "failed to find token directory"))
} else {
err = os.MkdirAll(dname, 0775)
if err != nil {
fmt.Println(errors.Wrapf(err, "failed to create token directory"))
}
return
}
fname, err := cacheName()

dirName := filepath.Dir(fname)
err = os.MkdirAll(dirName, 0775)
if err != nil {
return
fmt.Println(errors.Wrapf(err, "failed to create token directory"))
}

f, err := os.OpenFile(fname, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
fmt.Println(errors.Wrapf(err, "failed to open token file"))
Expand Down

0 comments on commit 2a233d0

Please sign in to comment.