MCPcopy Index your code
hub / github.com/docker/cli / TestGetAutoPAT

Function TestGetAutoPAT

internal/oauth/api/api_test.go:336–428  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

334}
335
336func TestGetAutoPAT(t *testing.T) {
337 t.Parallel()
338
339 t.Run("success", func(t *testing.T) {
340 t.Parallel()
341 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
342 assert.Equal(t, "POST", r.Method)
343 assert.Equal(t, "/v2/access-tokens/desktop-generate", r.URL.Path)
344 assert.Equal(t, "Bearer bork", r.Header.Get("Authorization"))
345 assert.Equal(t, "application/json", r.Header.Get("Content-Type"))
346
347 marshalledResponse, err := json.Marshal(patGenerateResponse{
348 Data: struct {
349 Token string `json:"token"`
350 }{
351 Token: "a-docker-pat",
352 },
353 })
354 assert.NilError(t, err)
355 w.WriteHeader(http.StatusCreated)
356 w.Write(marshalledResponse)
357 }))
358 defer ts.Close()
359 api := API{
360 TenantURL: ts.URL,
361 ClientID: "aClientID",
362 Scopes: []string{"bork", "meow"},
363 }
364
365 pat, err := api.GetAutoPAT(context.Background(), ts.URL, TokenResponse{
366 AccessToken: "bork",
367 })
368 assert.NilError(t, err)
369
370 assert.Equal(t, "a-docker-pat", pat)
371 })
372
373 t.Run("general error", func(t *testing.T) {
374 t.Parallel()
375 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
376 w.WriteHeader(http.StatusInternalServerError)
377 }))
378 defer ts.Close()
379 api := API{
380 TenantURL: ts.URL,
381 ClientID: "aClientID",
382 Scopes: []string{"bork", "meow"},
383 }
384
385 _, err := api.GetAutoPAT(context.Background(), ts.URL, TokenResponse{
386 AccessToken: "bork",
387 })
388 assert.ErrorContains(t, err, "unexpected response from Hub: 500 Internal Server Error")
389 })
390
391 t.Run("context canceled", func(t *testing.T) {
392 t.Parallel()
393 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

Callers

nothing calls this directly

Calls 4

GetAutoPATMethod · 0.95
GetMethod · 0.65
WriteMethod · 0.45
CloseMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…