Skip to content

Commit

Permalink
fix(auth): create cache directory if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
streambinder committed Jul 4, 2023
1 parent 042e5d5 commit 5d801d0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions spotify/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"net/http"
"os"
"path/filepath"

"github.com/arunsworld/nursery"
"github.com/streambinder/spotitube/util"
Expand Down Expand Up @@ -142,6 +143,10 @@ func Recover(authenticator *spotifyauth.Authenticator, state string) (*Client, e
}

func (client *Client) Persist() error {
if err := os.MkdirAll(filepath.Dir(tokenPath), 0o755); err != nil {
return err
}

token, err := client.Token()
if err != nil {
return err
Expand Down
18 changes: 18 additions & 0 deletions spotify/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,24 @@ func TestAuthenticateRecoverAndPersistTokenFailure(t *testing.T) {
assert.EqualError(t, util.ErrOnly(Authenticate(nil)), "ko")
}

func TestAuthenticateRecoverAndPersistMkdirFailure(t *testing.T) {
t.Cleanup(resetPort)
port = getPort()

// monkey patching
defer gomonkey.NewPatches().
ApplyMethod(&spotify.Client{}, "Token", func() (*oauth2.Token, error) {
return nil, nil
}).
ApplyFunc(os.MkdirAll, func() error {
return errors.New("ko")
}).
Reset()

// testing
assert.EqualError(t, util.ErrOnly(Authenticate(nil)), "ko")
}

func TestAuthenticateRecoverAndPersistOpenFailure(t *testing.T) {
t.Cleanup(resetPort)
port = getPort()
Expand Down

0 comments on commit 5d801d0

Please sign in to comment.