@@ -4,12 +4,15 @@ import (
44 "bytes"
55 "context"
66 "encoding/json"
7+ "errors"
78 "net/http"
89 "net/http/httptest"
910 "strings"
1011 "testing"
1112
1213 youtube "google.golang.org/api/youtube/v3"
14+
15+ "github.com/steipete/gogcli/internal/secrets"
1316)
1417
1518func TestYouTubeChannelsListWithAPIKey (t * testing.T ) {
@@ -102,6 +105,125 @@ func TestYouTubeMineUsesOAuthService(t *testing.T) {
102105 }
103106}
104107
108+ func TestYouTubeVideosListWithAccountUsesOAuthService (t * testing.T ) {
109+ origOAuth := newYouTubeForAccount
110+ origAPIKey := newYouTubeWithAPIKey
111+ t .Cleanup (func () {
112+ newYouTubeForAccount = origOAuth
113+ newYouTubeWithAPIKey = origAPIKey
114+ })
115+
116+ var gotAccount string
117+ srv := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
118+ if r .URL .Path != "/youtube/v3/videos" {
119+ t .Fatalf ("path = %s" , r .URL .Path )
120+ }
121+ if got := r .URL .Query ().Get ("id" ); got != "dQw4w9WgXcQ" {
122+ t .Fatalf ("id = %q" , got )
123+ }
124+ _ = json .NewEncoder (w ).Encode (map [string ]any {"items" : []map [string ]any {}})
125+ }))
126+ defer srv .Close ()
127+
128+ svc := newGoogleTestServiceWithEndpoint (t , srv .Client (), srv .URL + "/" , youtube .NewService )
129+ newYouTubeForAccount = func (_ context.Context , account string ) (* youtube.Service , error ) {
130+ gotAccount = account
131+ return svc , nil
132+ }
133+ newYouTubeWithAPIKey = func (context.Context , string ) (* youtube.Service , error ) {
134+ t .Fatal ("API key service should not be used when account is configured" )
135+ return nil , errors .New ("unexpected API key service" )
136+ }
137+
138+ err := runKong (
t ,
& YouTubeVideosListCmd {}, []
string {
"--id" ,
"dQw4w9WgXcQ" ,
"--max" ,
"1" },
newQuietUIContext (
t ),
& RootFlags {
Account :
"[email protected] " })
139+ if err != nil {
140+ t .Fatalf ("runKong: %v" , err )
141+ }
142+ if gotAccount != "[email protected] " {
143+ t .Fatalf ("account = %q" , gotAccount )
144+ }
145+ }
146+
147+ func TestYouTubeCommentsListWithAccountUsesOAuthService (t * testing.T ) {
148+ origOAuth := newYouTubeCommentsForAccount
149+ origAPIKey := newYouTubeWithAPIKey
150+ t .Cleanup (func () {
151+ newYouTubeCommentsForAccount = origOAuth
152+ newYouTubeWithAPIKey = origAPIKey
153+ })
154+
155+ var gotAccount string
156+ srv := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
157+ if r .URL .Path != "/youtube/v3/commentThreads" {
158+ t .Fatalf ("path = %s" , r .URL .Path )
159+ }
160+ if got := r .URL .Query ().Get ("videoId" ); got != "dQw4w9WgXcQ" {
161+ t .Fatalf ("videoId = %q" , got )
162+ }
163+ _ = json .NewEncoder (w ).Encode (map [string ]any {"items" : []map [string ]any {}})
164+ }))
165+ defer srv .Close ()
166+
167+ svc := newGoogleTestServiceWithEndpoint (t , srv .Client (), srv .URL + "/" , youtube .NewService )
168+ newYouTubeCommentsForAccount = func (_ context.Context , account string ) (* youtube.Service , error ) {
169+ gotAccount = account
170+ return svc , nil
171+ }
172+ newYouTubeWithAPIKey = func (context.Context , string ) (* youtube.Service , error ) {
173+ t .Fatal ("API key service should not be used when account is configured" )
174+ return nil , errors .New ("unexpected API key service" )
175+ }
176+
177+ err := runKong (
t ,
& YouTubeCommentsListCmd {}, []
string {
"--video-id" ,
"dQw4w9WgXcQ" ,
"--max" ,
"1" },
newQuietUIContext (
t ),
& RootFlags {
Account :
"[email protected] " })
178+ if err != nil {
179+ t .Fatalf ("runKong: %v" , err )
180+ }
181+ if gotAccount != "[email protected] " {
182+ t .Fatalf ("account = %q" , gotAccount )
183+ }
184+ }
185+
186+ func TestYouTubeVideosListWithAutoAccountUsesOAuthService (t * testing.T ) {
187+ origOAuth := newYouTubeForAccount
188+ origAPIKey := newYouTubeWithAPIKey
189+ origStore := openSecretsStoreForAccount
190+ t .Cleanup (func () {
191+ newYouTubeForAccount = origOAuth
192+ newYouTubeWithAPIKey = origAPIKey
193+ openSecretsStoreForAccount = origStore
194+ })
195+ openSecretsStoreForAccount = func () (secrets.Store , error ) {
196+ return & fakeSecretsStore {
defaultAccount :
"[email protected] " },
nil 197+ }
198+
199+ var gotAccount string
200+ srv := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
201+ if r .URL .Path != "/youtube/v3/videos" {
202+ t .Fatalf ("path = %s" , r .URL .Path )
203+ }
204+ _ = json .NewEncoder (w ).Encode (map [string ]any {"items" : []map [string ]any {}})
205+ }))
206+ defer srv .Close ()
207+
208+ svc := newGoogleTestServiceWithEndpoint (t , srv .Client (), srv .URL + "/" , youtube .NewService )
209+ newYouTubeForAccount = func (_ context.Context , account string ) (* youtube.Service , error ) {
210+ gotAccount = account
211+ return svc , nil
212+ }
213+ newYouTubeWithAPIKey = func (context.Context , string ) (* youtube.Service , error ) {
214+ t .Fatal ("API key service should not be used when --account auto is configured" )
215+ return nil , errors .New ("unexpected API key service" )
216+ }
217+
218+ err := runKong (t , & YouTubeVideosListCmd {}, []string {"--id" , "dQw4w9WgXcQ" , "--max" , "1" }, newQuietUIContext (t ), & RootFlags {Account : "auto" })
219+ if err != nil {
220+ t .Fatalf ("runKong: %v" , err )
221+ }
222+ if gotAccount != "[email protected] " {
223+ t .Fatalf ("account = %q" , gotAccount )
224+ }
225+ }
226+
105227func TestYouTubeValidation (t * testing.T ) {
106228 err := runKong (t , & YouTubeChannelsListCmd {}, []string {"--id" , "UC123" , "--max" , "51" }, newQuietUIContext (t ), & RootFlags {})
107229 if err == nil || ! strings .Contains (err .Error (), "--max must be between 1 and 50" ) {
0 commit comments