|
1 | 1 | package gitlab |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
4 | 6 | "testing" |
5 | 7 |
|
6 | 8 | "github.com/openshift-pipelines/pipelines-as-code/pkg/params/info" |
@@ -155,3 +157,107 @@ func TestIsAllowed(t *testing.T) { |
155 | 157 | }) |
156 | 158 | } |
157 | 159 | } |
| 160 | + |
| 161 | +func TestMembershipCaching(t *testing.T) { |
| 162 | + ctx, _ := rtesting.SetupFakeContext(t) |
| 163 | + |
| 164 | + v := &Provider{ |
| 165 | + targetProjectID: 3030, |
| 166 | + userID: 4242, |
| 167 | + } |
| 168 | + |
| 169 | + client, mux, tearDown := thelp.Setup(t) |
| 170 | + defer tearDown() |
| 171 | + v.gitlabClient = client |
| 172 | + |
| 173 | + // Count how many times the membership API is hit. |
| 174 | + var calls int |
| 175 | + thelp.MuxAllowUserIDCounting(mux, v.targetProjectID, v.userID, &calls) |
| 176 | + |
| 177 | + ev := &info.Event{Sender: "someone", PullRequestNumber: 1} |
| 178 | + |
| 179 | + // First call should hit the API once and cache the result. |
| 180 | + allowed, err := v.IsAllowed(ctx, ev) |
| 181 | + if err != nil { |
| 182 | + t.Fatalf("unexpected error: %v", err) |
| 183 | + } |
| 184 | + if !allowed { |
| 185 | + t.Fatalf("expected allowed on first membership check") |
| 186 | + } |
| 187 | + if calls < 1 { |
| 188 | + t.Fatalf("expected at least 1 membership API call, got %d", calls) |
| 189 | + } |
| 190 | + |
| 191 | + // Second call should use the cache and not hit the API again. |
| 192 | + allowed, err = v.IsAllowed(ctx, ev) |
| 193 | + if err != nil { |
| 194 | + t.Fatalf("unexpected error: %v", err) |
| 195 | + } |
| 196 | + if !allowed { |
| 197 | + t.Fatalf("expected allowed on cached membership check") |
| 198 | + } |
| 199 | + if calls != 1 { |
| 200 | + t.Fatalf("expected cached result with no extra API call, got %d calls", calls) |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +func TestMembershipAPIFailureDoesNotCacheApiError(t *testing.T) { |
| 205 | + ctx, _ := rtesting.SetupFakeContext(t) |
| 206 | + |
| 207 | + v := &Provider{ |
| 208 | + targetProjectID: 3030, |
| 209 | + userID: 4242, |
| 210 | + } |
| 211 | + |
| 212 | + client, mux, tearDown := thelp.Setup(t) |
| 213 | + defer tearDown() |
| 214 | + v.gitlabClient = client |
| 215 | + |
| 216 | + ev := &info.Event{Sender: "someone"} |
| 217 | + |
| 218 | + var ( |
| 219 | + calls int |
| 220 | + success bool |
| 221 | + ) |
| 222 | + path := fmt.Sprintf("/projects/%d/members/all/%d", v.targetProjectID, v.userID) |
| 223 | + mux.HandleFunc(path, func(rw http.ResponseWriter, _ *http.Request) { |
| 224 | + calls++ |
| 225 | + if !success { |
| 226 | + rw.WriteHeader(http.StatusInternalServerError) |
| 227 | + _, _ = rw.Write([]byte(`{}`)) |
| 228 | + return |
| 229 | + } |
| 230 | + _, err := fmt.Fprintf(rw, `{"id": %d}`, v.userID) |
| 231 | + if err != nil { |
| 232 | + t.Fatalf("failed to write response: %v", err) |
| 233 | + } |
| 234 | + }) |
| 235 | + |
| 236 | + thelp.MuxDiscussionsNoteEmpty(mux, v.targetProjectID, ev.PullRequestNumber) |
| 237 | + |
| 238 | + allowed, err := v.IsAllowed(ctx, ev) |
| 239 | + if err != nil { |
| 240 | + t.Fatalf("unexpected error on failure path: %v", err) |
| 241 | + } |
| 242 | + if allowed { |
| 243 | + t.Fatalf("expected not allowed when membership API fails and no fallback grants access") |
| 244 | + } |
| 245 | + if calls < 1 { |
| 246 | + t.Fatalf("expected at least 1 membership API call, got %d", calls) |
| 247 | + } |
| 248 | + initialCallCount := calls |
| 249 | + |
| 250 | + // Make the next API call succeed; the provider should retry because the previous failure wasn't cached. |
| 251 | + success = true |
| 252 | + |
| 253 | + allowed, err = v.IsAllowed(ctx, ev) |
| 254 | + if err != nil { |
| 255 | + t.Fatalf("unexpected error on retry path: %v", err) |
| 256 | + } |
| 257 | + if !allowed { |
| 258 | + t.Fatalf("expected allowed when membership API succeeds on retry") |
| 259 | + } |
| 260 | + if calls <= initialCallCount { |
| 261 | + t.Fatalf("expected membership API to be called again after retry, got %d total calls (initial %d)", calls, initialCallCount) |
| 262 | + } |
| 263 | +} |
0 commit comments