| 1 | package provider |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "testing" |
| 9 | "time" |
| 10 | ) |
| 11 | |
| 12 | func TestSourceRefreshBeforeStageUnderLock(t *testing.T) { |
| 13 | b, rules := accountFixture(t) |
| 14 | calls := 0 |
| 15 | refresh := func(ctx context.Context, source Binding) (Identity, error) { |
| 16 | calls++ |
| 17 | if source.Home != b.Home { |
| 18 | t.Fatal("refresh ran outside source home") |
| 19 | } |
| 20 | if other, err := openAccount(ctx, b, rules); !errors.Is(err, ErrAccountBusy) { |
| 21 | if other != nil { |
| 22 | other.Close() |
| 23 | } |
| 24 | t.Fatalf("source not locked: %v", err) |
| 25 | } |
| 26 | writeFake(t, filepath.Join(source.Home, "auth.json"), `{"token":"fresh"}`) |
| 27 | return b.Expected, nil |
| 28 | } |
| 29 | l, err := openAccountRefreshing(context.Background(), b, rules, refresh) |
| 30 | if err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | defer l.Close() |
| 34 | raw, err := os.ReadFile(filepath.Join(l.Binding().Home, "auth.json")) |
| 35 | if err != nil || string(raw) != `{"token":"fresh"}` || calls != 1 { |
| 36 | t.Fatal("staged stale login", err, calls) |
| 37 | } |
| 38 | if other, err := openAccountRefreshing(context.Background(), b, rules, refresh); !errors.Is(err, ErrAccountBusy) { |
| 39 | if other != nil { |
| 40 | other.Close() |
| 41 | } |
| 42 | t.Fatalf("active lease refreshed: %v", err) |
| 43 | } |
| 44 | if calls != 1 { |
| 45 | t.Fatal("refresh ran while leased") |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func TestSourceRefreshFailureDoesNotStage(t *testing.T) { |
| 50 | for _, kind := range []string{"transient", "identity"} { |
| 51 | t.Run(kind, func(t *testing.T) { |
| 52 | b, rules := accountFixture(t) |
| 53 | l, err := openAccountRefreshing(context.Background(), b, rules, func(context.Context, Binding) (Identity, error) { |
| 54 | if kind == "transient" { |
| 55 | return Identity{}, ErrLoginUnavailable |
| 56 | } |
| 57 | return Identity{AccountID: "other"}, nil |
| 58 | }) |
| 59 | if err == nil || l != nil { |
| 60 | t.Fatal("failed refresh staged") |
| 61 | } |
| 62 | entries, _ := os.ReadDir(b.RuntimeRoot) |
| 63 | if len(entries) != 0 { |
| 64 | t.Fatal("failed refresh left runtime credentials") |
| 65 | } |
| 66 | }) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | func TestSourceRefreshLeaseExpiresAndCannotWriteBack(t *testing.T) { |
| 71 | b, rules := accountFixture(t) |
| 72 | l, err := openAccountRefreshing(context.Background(), b, rules, func(context.Context, Binding) (Identity, error) { return b.Expected, nil }) |
| 73 | if err != nil { |
| 74 | t.Fatal(err) |
| 75 | } |
| 76 | defer l.Close() |
| 77 | preflightFixture(t, l) |
| 78 | l.sourceFreshUntil = time.Now().Add(-time.Second) |
| 79 | if err = l.BeginTurn(context.Background()); err == nil { |
| 80 | t.Fatal("expired staging began turn") |
| 81 | } |
| 82 | if err = l.CommitRefresh(context.Background(), func(context.Context, Binding) (Identity, error) { return b.Expected, nil }); err == nil { |
| 83 | t.Fatal("staged refresh could overwrite source") |
| 84 | } |
| 85 | } |
| 86 | |