(t *testing.T)
| 13 | ) |
| 14 | |
| 15 | func TestExecute_AuthAdd_JSON(t *testing.T) { |
| 16 | store := newMemSecretsStore() |
| 17 | |
| 18 | var gotOpts googleauth.AuthorizeOptions |
| 19 | runtime := &app.Runtime{Auth: app.AuthOperations{ |
| 20 | OpenSecretsStore: func() (secrets.Store, error) { return store, nil }, |
| 21 | EnsureKeychainAccess: func(context.Context) error { return nil }, |
| 22 | AuthorizeGoogle: func(_ context.Context, opts googleauth.AuthorizeOptions) (string, error) { |
| 23 | gotOpts = opts |
| 24 | gotOpts.Services = append([]googleauth.Service{}, opts.Services...) |
| 25 | gotOpts.Scopes = append([]string{}, opts.Scopes...) |
| 26 | return "rt", nil |
| 27 | }, |
| 28 | FetchAuthorizedIdentity: func(context.Context, string, string, []string, time.Duration) (googleauth.Identity, error) { |
| 29 | return googleauth.Identity{Email: "a@b.com"}, nil |
| 30 | }, |
| 31 | }} |
| 32 | |
| 33 | result := executeWithTestRuntime(t, []string{ |
| 34 | "--json", "auth", "add", "a@b.com", "--services", "calendar,gmail", |
| 35 | }, runtime) |
| 36 | if result.err != nil { |
| 37 | t.Fatalf("Execute: %v", result.err) |
| 38 | } |
| 39 | |
| 40 | var parsed struct { |
| 41 | Stored bool `json:"stored"` |
| 42 | Email string `json:"email"` |
| 43 | Services []string `json:"services"` |
| 44 | } |
| 45 | if err := json.Unmarshal([]byte(result.stdout), &parsed); err != nil { |
| 46 | t.Fatalf("json parse: %v\nout=%q", err, result.stdout) |
| 47 | } |
| 48 | if !parsed.Stored || parsed.Email != "a@b.com" || len(parsed.Services) != 2 { |
| 49 | t.Fatalf("unexpected: %#v", parsed) |
| 50 | } |
| 51 | |
| 52 | tok, err := store.GetToken(config.DefaultClientName, "a@b.com") |
| 53 | if err != nil { |
| 54 | t.Fatalf("GetToken: %v", err) |
| 55 | } |
| 56 | if tok.RefreshToken != "rt" { |
| 57 | t.Fatalf("unexpected token: %#v", tok) |
| 58 | } |
| 59 | |
| 60 | _ = gotOpts // keep for future assertions; ensures auth add actually called authorizeGoogle. |
| 61 | } |
nothing calls this directly
no test coverage detected