MCPcopy Create free account
hub / github.com/dropbox/dbxcli / TestGetAccessTokenRefreshFailureLeavesAuthFileUnchanged

Function TestGetAccessTokenRefreshFailureLeavesAuthFileUnchanged

cmd/auth_test.go:344–390  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

342}
343
344func TestGetAccessTokenRefreshFailureLeavesAuthFileUnchanged(t *testing.T) {
345 expired := time.Now().Add(-time.Hour).UTC()
346 authFile := filepath.Join(t.TempDir(), "auth.json")
347 tokens := TokenMap{
348 "": {
349 tokenPersonal: {
350 AccessToken: "old-access",
351 RefreshToken: "old-refresh",
352 TokenType: "Bearer",
353 Expiry: &expired,
354 AppKey: "stored-app-key",
355 },
356 },
357 }
358 if err := writeTokens(authFile, tokens); err != nil {
359 t.Fatal(err)
360 }
361 before, err := os.ReadFile(authFile)
362 if err != nil {
363 t.Fatal(err)
364 }
365 t.Setenv(envAuthFile, authFile)
366
367 restoreOAuthCredentials(t)
368 refreshOAuthToken = func(ctx context.Context, conf *oauth2.Config, token *oauth2.Token) (*oauth2.Token, error) {
369 return nil, errors.New("refresh failed")
370 }
371
372 _, _, err = getAccessToken(tokenPersonal, "", false)
373 if err == nil {
374 t.Fatal("expected refresh failure")
375 }
376 if got, want := jsonErrorCode(err), jsonErrorCodeAuthRefreshFailed; got != want {
377 t.Fatalf("jsonErrorCode = %q, want %q", got, want)
378 }
379 if !strings.Contains(err.Error(), "dbxcli login") {
380 t.Fatalf("expected login hint, got %q", err)
381 }
382
383 after, err := os.ReadFile(authFile)
384 if err != nil {
385 t.Fatal(err)
386 }
387 if string(after) != string(before) {
388 t.Fatalf("expected auth file to remain unchanged\nbefore: %s\nafter: %s", before, after)
389 }
390}
391
392func TestGetAccessTokenRefreshWithoutAppKeyReturnsAppKeyRequired(t *testing.T) {
393 expired := time.Now().Add(-time.Hour).UTC()

Callers

nothing calls this directly

Calls 5

writeTokensFunction · 0.85
restoreOAuthCredentialsFunction · 0.85
getAccessTokenFunction · 0.85
jsonErrorCodeFunction · 0.85
ErrorMethod · 0.45

Tested by

no test coverage detected