internal/auth_token_test.go

109 lines · 3355 bytes

  1package internal
  2
  3import (
  4	"path/filepath"
  5	"testing"
  6	"time"
  7)
  8
  9func writeAnilistTokenFileForTest(t *testing.T, dir, token string) {
 10	t.Helper()
 11	tok := &AnilistToken{
 12		AccessToken: token,
 13		TokenType:   "Bearer",
 14		ExpiresIn:   31536000,
 15		ExpiresAt:   time.Now().Add(24 * time.Hour),
 16	}
 17	if err := saveToken(filepath.Join(dir, "anilist_token.json"), tok); err != nil {
 18		t.Fatalf("saveToken: %v", err)
 19	}
 20}
 21
 22func withGlobalConfig(t *testing.T, config *CurdConfig) {
 23	t.Helper()
 24	previous := GetGlobalConfig()
 25	SetGlobalConfig(config)
 26	t.Cleanup(func() { SetGlobalConfig(previous) })
 27}
 28
 29func TestAnilistTokenForAPIPrefersStoredToken(t *testing.T) {
 30	dir := t.TempDir()
 31	writeAnilistTokenFileForTest(t, dir, "stored-anilist-token")
 32	withGlobalConfig(t, &CurdConfig{StoragePath: dir})
 33
 34	// The tracker token (user.Token) may be a MyAnimeList token; the AniList
 35	// call must use the real AniList token from disk instead.
 36	if got := anilistTokenForAPI(nil, "mal-jwt-token"); got != "stored-anilist-token" {
 37		t.Fatalf("anilistTokenForAPI = %q, want stored-anilist-token", got)
 38	}
 39}
 40
 41func TestAnilistTokenForAPIFallsBackToCurrentWhenNoStoredToken(t *testing.T) {
 42	dir := t.TempDir()
 43	withGlobalConfig(t, &CurdConfig{StoragePath: dir})
 44
 45	if got := anilistTokenForAPI(nil, "some-token"); got != "some-token" {
 46		t.Fatalf("anilistTokenForAPI = %q, want some-token", got)
 47	}
 48}
 49
 50func TestAnilistTokenForAPIFallsBackWhenNoGlobalConfig(t *testing.T) {
 51	withGlobalConfig(t, nil)
 52
 53	if got := anilistTokenForAPI(nil, "some-token"); got != "some-token" {
 54		t.Fatalf("anilistTokenForAPI = %q, want some-token", got)
 55	}
 56}
 57
 58func TestTryRenewUsesStoredTokenInsteadOfBrowserReauth(t *testing.T) {
 59	dir := t.TempDir()
 60	writeAnilistTokenFileForTest(t, dir, "stored-anilist-token")
 61	withGlobalConfig(t, &CurdConfig{StoragePath: dir})
 62
 63	// A MyAnimeList tracker token must not trigger a browser re-auth: the real
 64	// AniList token is simply swapped in and the request retried.
 65	newToken, renewed, err := tryRenewAniListTokenForAPI("mal-jwt-token", "invalid token")
 66	if err != nil {
 67		t.Fatalf("tryRenewAniListTokenForAPI error: %v", err)
 68	}
 69	if !renewed {
 70		t.Fatal("expected renewed=true")
 71	}
 72	if newToken != "stored-anilist-token" {
 73		t.Fatalf("renewed token = %q, want stored-anilist-token", newToken)
 74	}
 75}
 76
 77func TestTryRenewEmptyTokenUsesStoredToken(t *testing.T) {
 78	dir := t.TempDir()
 79	writeAnilistTokenFileForTest(t, dir, "stored-anilist-token")
 80	withGlobalConfig(t, &CurdConfig{StoragePath: dir})
 81
 82	newToken, renewed, err := tryRenewAniListTokenForAPI("", "invalid token")
 83	if err != nil {
 84		t.Fatalf("tryRenewAniListTokenForAPI error: %v", err)
 85	}
 86	if !renewed {
 87		t.Fatal("expected renewed=true")
 88	}
 89	if newToken != "stored-anilist-token" {
 90		t.Fatalf("renewed token = %q, want stored-anilist-token", newToken)
 91	}
 92}
 93
 94func TestTryRenewNoGlobalConfigFallsThrough(t *testing.T) {
 95	withGlobalConfig(t, nil)
 96
 97	// Without global config there is no stored token to compare against, so the
 98	// function must not panic and simply report no renewal.
 99	newToken, renewed, err := tryRenewAniListTokenForAPI("token", "invalid token")
100	if err != nil {
101		t.Fatalf("tryRenewAniListTokenForAPI error: %v", err)
102	}
103	if renewed {
104		t.Fatal("expected renewed=false when no global config")
105	}
106	if newToken != "token" {
107		t.Fatalf("renewed token = %q, want original token", newToken)
108	}
109}