|
| 1 | +package lockdown |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/shurcooL/githubv4" |
| 10 | +) |
| 11 | + |
| 12 | +func TestShouldRemoveContentCachesResultsWithinInterval(t *testing.T) { |
| 13 | + clearRepoAccessCache() |
| 14 | + defer clearRepoAccessCache() |
| 15 | + |
| 16 | + originalInfoFunc := repoAccessInfoFunc |
| 17 | + defer func() { repoAccessInfoFunc = originalInfoFunc }() |
| 18 | + |
| 19 | + originalTimeNow := timeNow |
| 20 | + defer func() { timeNow = originalTimeNow }() |
| 21 | + |
| 22 | + fixed := time.Now() |
| 23 | + timeNow = func() time.Time { return fixed } |
| 24 | + |
| 25 | + callCount := 0 |
| 26 | + repoAccessInfoFunc = func(_ context.Context, _ *githubv4.Client, _, _, _ string) (bool, bool, error) { |
| 27 | + callCount++ |
| 28 | + return false, true, nil |
| 29 | + } |
| 30 | + |
| 31 | + ctx := context.Background() |
| 32 | + |
| 33 | + remove, err := ShouldRemoveContent(ctx, nil, "User", "Owner", "Repo") |
| 34 | + if err != nil { |
| 35 | + t.Fatalf("unexpected error on first call: %v", err) |
| 36 | + } |
| 37 | + if remove { |
| 38 | + t.Fatalf("expected remove=false when user has push access") |
| 39 | + } |
| 40 | + |
| 41 | + remove, err = ShouldRemoveContent(ctx, nil, "user", "owner", "repo") |
| 42 | + if err != nil { |
| 43 | + t.Fatalf("unexpected error on cached call: %v", err) |
| 44 | + } |
| 45 | + if remove { |
| 46 | + t.Fatalf("expected remove=false when cached entry reused") |
| 47 | + } |
| 48 | + if callCount != 1 { |
| 49 | + t.Fatalf("expected cached result to prevent additional repo access queries, got %d", callCount) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func TestShouldRemoveContentRefreshesAfterInterval(t *testing.T) { |
| 54 | + clearRepoAccessCache() |
| 55 | + defer clearRepoAccessCache() |
| 56 | + |
| 57 | + originalInfoFunc := repoAccessInfoFunc |
| 58 | + defer func() { repoAccessInfoFunc = originalInfoFunc }() |
| 59 | + |
| 60 | + originalTimeNow := timeNow |
| 61 | + defer func() { timeNow = originalTimeNow }() |
| 62 | + |
| 63 | + base := time.Now() |
| 64 | + current := base |
| 65 | + timeNow = func() time.Time { return current } |
| 66 | + |
| 67 | + callCount := 0 |
| 68 | + repoAccessInfoFunc = func(_ context.Context, _ *githubv4.Client, _, _, _ string) (bool, bool, error) { |
| 69 | + callCount++ |
| 70 | + if callCount == 1 { |
| 71 | + return false, false, nil |
| 72 | + } |
| 73 | + return false, true, nil |
| 74 | + } |
| 75 | + |
| 76 | + ctx := context.Background() |
| 77 | + |
| 78 | + remove, err := ShouldRemoveContent(ctx, nil, "user", "owner", "repo") |
| 79 | + if err != nil { |
| 80 | + t.Fatalf("unexpected error on first call: %v", err) |
| 81 | + } |
| 82 | + if !remove { |
| 83 | + t.Fatalf("expected remove=true when user lacks push access") |
| 84 | + } |
| 85 | + if callCount != 1 { |
| 86 | + t.Fatalf("expected first call to query once, got %d", callCount) |
| 87 | + } |
| 88 | + |
| 89 | + current = base.Add(9 * time.Minute) |
| 90 | + remove, err = ShouldRemoveContent(ctx, nil, "user", "owner", "repo") |
| 91 | + if err != nil { |
| 92 | + t.Fatalf("unexpected error before refresh interval: %v", err) |
| 93 | + } |
| 94 | + if !remove { |
| 95 | + t.Fatalf("expected remove=true before refresh interval expires") |
| 96 | + } |
| 97 | + if callCount != 1 { |
| 98 | + t.Fatalf("expected cached value before refresh interval, got %d calls", callCount) |
| 99 | + } |
| 100 | + |
| 101 | + current = base.Add(11 * time.Minute) |
| 102 | + remove, err = ShouldRemoveContent(ctx, nil, "user", "owner", "repo") |
| 103 | + if err != nil { |
| 104 | + t.Fatalf("unexpected error after refresh interval: %v", err) |
| 105 | + } |
| 106 | + if remove { |
| 107 | + t.Fatalf("expected remove=false after permissions refreshed") |
| 108 | + } |
| 109 | + if callCount != 2 { |
| 110 | + t.Fatalf("expected refreshed access info after interval, got %d calls", callCount) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +func TestShouldRemoveContentDoesNotCacheErrors(t *testing.T) { |
| 115 | + clearRepoAccessCache() |
| 116 | + defer clearRepoAccessCache() |
| 117 | + |
| 118 | + originalInfoFunc := repoAccessInfoFunc |
| 119 | + defer func() { repoAccessInfoFunc = originalInfoFunc }() |
| 120 | + |
| 121 | + originalTimeNow := timeNow |
| 122 | + defer func() { timeNow = originalTimeNow }() |
| 123 | + |
| 124 | + now := time.Now() |
| 125 | + timeNow = func() time.Time { return now } |
| 126 | + |
| 127 | + callCount := 0 |
| 128 | + repoAccessInfoFunc = func(_ context.Context, _ *githubv4.Client, _, _, _ string) (bool, bool, error) { |
| 129 | + callCount++ |
| 130 | + if callCount == 1 { |
| 131 | + return false, false, errors.New("boom") |
| 132 | + } |
| 133 | + return false, false, nil |
| 134 | + } |
| 135 | + |
| 136 | + ctx := context.Background() |
| 137 | + |
| 138 | + if _, err := ShouldRemoveContent(ctx, nil, "user", "owner", "repo"); err == nil { |
| 139 | + t.Fatal("expected error on first call") |
| 140 | + } |
| 141 | + if callCount != 1 { |
| 142 | + t.Fatalf("expected single call after error, got %d", callCount) |
| 143 | + } |
| 144 | + |
| 145 | + remove, err := ShouldRemoveContent(ctx, nil, "user", "owner", "repo") |
| 146 | + if err != nil { |
| 147 | + t.Fatalf("unexpected error on retry: %v", err) |
| 148 | + } |
| 149 | + if !remove { |
| 150 | + t.Fatalf("expected remove=true when user lacks push access") |
| 151 | + } |
| 152 | + if callCount != 2 { |
| 153 | + t.Fatalf("expected repo access to be queried again after error, got %d calls", callCount) |
| 154 | + } |
| 155 | +} |
0 commit comments