From 8f623f7e1996050c4e673246ca8d4ccfb1b79c5b Mon Sep 17 00:00:00 2001 From: OpenWaygate <27287694+OpenWaygate@users.noreply.github.com> Date: Fri, 24 Oct 2025 23:42:08 +0800 Subject: [PATCH] :white_check_mark: Pass auth test --- pkg/auth/service_test.go | 45 ++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/pkg/auth/service_test.go b/pkg/auth/service_test.go index ffde49a..112ecdd 100644 --- a/pkg/auth/service_test.go +++ b/pkg/auth/service_test.go @@ -5,22 +5,31 @@ package auth import ( "context" + "io/fs" + "os" + "path/filepath" "reflect" "testing" "testing/fstest" ) -func TestNewY2BService(t *testing.T) { - credential := `{"client_id":"test"}` - cacheToken := `{"access_token":"test"}` +const ( + credFile = "client_secret.json" + tokenFile = "youtube.token.json" + credential = `{"client_id":"test"}` + cacheToken = `{"access_token":"test"}` + credB64 = "eyJjbGllbnRfaWQiOiJ0ZXN0In0=" + tokenB64 = "eyJhY2Nlc3NfdG9rZW4iOiJ0ZXN0In0=" +) +func TestNewY2BService(t *testing.T) { + rootDir, _ := os.Getwd() + absCred := filepath.Join(rootDir, credFile) + absToken := filepath.Join(rootDir, tokenFile) mockFS := fstest.MapFS{ - "youtube.token.json": &fstest.MapFile{ - Data: []byte(cacheToken), - }, - "client_secrets.json": &fstest.MapFile{ - Data: []byte(credential), - }, + rootDir: &fstest.MapFile{Mode: fs.ModeDir}, + credFile: &fstest.MapFile{Data: []byte(credential)}, + tokenFile: &fstest.MapFile{Data: []byte(cacheToken)}, } type args struct { @@ -43,7 +52,7 @@ func TestNewY2BService(t *testing.T) { want: &svc{ Credential: credential, CacheToken: cacheToken, - credFile: "client_secret.json", + credFile: credFile, ctx: context.Background(), }, }, @@ -51,14 +60,14 @@ func TestNewY2BService(t *testing.T) { name: "with all options - base64", args: args{ opts: []Option{ - WithCredential("eyJjbGllbnRfaWQiOiJ0ZXN0In0=", mockFS), - WithCacheToken("eyJhY2Nlc3NfdG9rZW4iOiJ0ZXN0In0=", mockFS), + WithCredential(credB64, mockFS), + WithCacheToken(tokenB64, mockFS), }, }, want: &svc{ Credential: credential, CacheToken: cacheToken, - credFile: "client_secret.json", + credFile: credFile, ctx: context.Background(), }, }, @@ -66,15 +75,15 @@ func TestNewY2BService(t *testing.T) { name: "with all options - file", args: args{ opts: []Option{ - WithCredential("/client_secrets.json", mockFS), - WithCacheToken("/youtube.token.json", mockFS), + WithCredential(absCred, mockFS), + WithCacheToken(absToken, mockFS), }, }, want: &svc{ Credential: credential, CacheToken: cacheToken, - credFile: "/client_secrets.json", - tokenFile: "youtube.token.json", + credFile: absCred, + tokenFile: tokenFile, ctx: context.Background(), }, }, @@ -84,7 +93,7 @@ func TestNewY2BService(t *testing.T) { opts: []Option{}, }, want: &svc{ - credFile: "client_secret.json", + credFile: credFile, ctx: context.Background(), }, },