Skip to content

Commit 836c41e

Browse files
committed
update tests
1 parent 3109915 commit 836c41e

4 files changed

Lines changed: 95 additions & 137 deletions

File tree

pkg/github/issues_test.go

Lines changed: 28 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,14 @@ import (
2323
)
2424

2525
var defaultGQLClient *githubv4.Client = githubv4.NewClient(newRepoAccessHTTPClient())
26-
var repoAccessCache *lockdown.RepoAccessCache = stubRepoAccessCache(defaultGQLClient, 15*time.Minute)
2726

2827
type repoAccessKey struct {
29-
owner string
30-
repo string
31-
username string
28+
owner string
29+
repo string
3230
}
3331

3432
type repoAccessValue struct {
35-
isPrivate bool
36-
permission string
33+
isPrivate bool
3734
}
3835

3936
type repoAccessMockTransport struct {
@@ -42,8 +39,8 @@ type repoAccessMockTransport struct {
4239

4340
func newRepoAccessHTTPClient() *http.Client {
4441
responses := map[repoAccessKey]repoAccessValue{
45-
{owner: "owner2", repo: "repo2", username: "testuser2"}: {isPrivate: true},
46-
{owner: "owner", repo: "repo", username: "testuser"}: {isPrivate: false, permission: "READ"},
42+
{owner: "owner2", repo: "repo2"}: {isPrivate: true},
43+
{owner: "owner", repo: "repo"}: {isPrivate: false},
4744
}
4845

4946
return &http.Client{Transport: &repoAccessMockTransport{responses: responses}}
@@ -66,30 +63,19 @@ func (rt *repoAccessMockTransport) RoundTrip(req *http.Request) (*http.Response,
6663

6764
owner := toString(payload.Variables["owner"])
6865
repo := toString(payload.Variables["name"])
69-
username := toString(payload.Variables["username"])
7066

71-
value, ok := rt.responses[repoAccessKey{owner: owner, repo: repo, username: username}]
67+
value, ok := rt.responses[repoAccessKey{owner: owner, repo: repo}]
7268
if !ok {
73-
value = repoAccessValue{isPrivate: false, permission: "WRITE"}
74-
}
75-
76-
edges := []any{}
77-
if value.permission != "" {
78-
edges = append(edges, map[string]any{
79-
"permission": value.permission,
80-
"node": map[string]any{
81-
"login": username,
82-
},
83-
})
69+
value = repoAccessValue{isPrivate: false}
8470
}
8571

8672
responseBody, err := json.Marshal(map[string]any{
8773
"data": map[string]any{
74+
"viewer": map[string]any{
75+
"login": "test-viewer",
76+
},
8877
"repository": map[string]any{
8978
"isPrivate": value.isPrivate,
90-
"collaborators": map[string]any{
91-
"edges": edges,
92-
},
9379
},
9480
},
9581
})
@@ -170,13 +156,13 @@ func Test_GetIssue(t *testing.T) {
170156
tests := []struct {
171157
name string
172158
mockedClient *http.Client
173-
gqlHTTPClient *http.Client
174159
requestArgs map[string]any
175160
expectHandlerError bool
176161
expectResultError bool
177162
expectedIssue *github.Issue
178163
expectedErrMsg string
179164
lockdownEnabled bool
165+
restPermission string
180166
}{
181167
{
182168
name: "successful issue retrieval",
@@ -210,36 +196,6 @@ func Test_GetIssue(t *testing.T) {
210196
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
211197
GetReposIssuesByOwnerByRepoByIssueNumber: mockResponse(t, http.StatusOK, mockIssue2),
212198
}),
213-
gqlHTTPClient: githubv4mock.NewMockedHTTPClient(
214-
githubv4mock.NewQueryMatcher(
215-
struct {
216-
Repository struct {
217-
IsPrivate githubv4.Boolean
218-
Collaborators struct {
219-
Edges []struct {
220-
Permission githubv4.String
221-
Node struct {
222-
Login githubv4.String
223-
}
224-
}
225-
} `graphql:"collaborators(query: $username, first: 1)"`
226-
} `graphql:"repository(owner: $owner, name: $name)"`
227-
}{},
228-
map[string]any{
229-
"owner": githubv4.String("owner2"),
230-
"name": githubv4.String("repo2"),
231-
"username": githubv4.String("testuser2"),
232-
},
233-
githubv4mock.DataResponse(map[string]any{
234-
"repository": map[string]any{
235-
"isPrivate": true,
236-
"collaborators": map[string]any{
237-
"edges": []any{},
238-
},
239-
},
240-
}),
241-
),
242-
),
243199
requestArgs: map[string]any{
244200
"method": "get",
245201
"owner": "owner2",
@@ -248,49 +204,13 @@ func Test_GetIssue(t *testing.T) {
248204
},
249205
expectedIssue: mockIssue2,
250206
lockdownEnabled: true,
207+
restPermission: "none",
251208
},
252209
{
253210
name: "lockdown enabled - user lacks push access",
254211
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
255212
GetReposIssuesByOwnerByRepoByIssueNumber: mockResponse(t, http.StatusOK, mockIssue),
256213
}),
257-
gqlHTTPClient: githubv4mock.NewMockedHTTPClient(
258-
githubv4mock.NewQueryMatcher(
259-
struct {
260-
Repository struct {
261-
IsPrivate githubv4.Boolean
262-
Collaborators struct {
263-
Edges []struct {
264-
Permission githubv4.String
265-
Node struct {
266-
Login githubv4.String
267-
}
268-
}
269-
} `graphql:"collaborators(query: $username, first: 1)"`
270-
} `graphql:"repository(owner: $owner, name: $name)"`
271-
}{},
272-
map[string]any{
273-
"owner": githubv4.String("owner"),
274-
"name": githubv4.String("repo"),
275-
"username": githubv4.String("testuser"),
276-
},
277-
githubv4mock.DataResponse(map[string]any{
278-
"repository": map[string]any{
279-
"isPrivate": false,
280-
"collaborators": map[string]any{
281-
"edges": []any{
282-
map[string]any{
283-
"permission": "READ",
284-
"node": map[string]any{
285-
"login": "testuser",
286-
},
287-
},
288-
},
289-
},
290-
},
291-
}),
292-
),
293-
),
294214
requestArgs: map[string]any{
295215
"method": "get",
296216
"owner": "owner",
@@ -300,20 +220,21 @@ func Test_GetIssue(t *testing.T) {
300220
expectResultError: true,
301221
expectedErrMsg: "access to issue details is restricted by lockdown mode",
302222
lockdownEnabled: true,
223+
restPermission: "read",
303224
},
304225
}
305226

306227
for _, tc := range tests {
307228
t.Run(tc.name, func(t *testing.T) {
308229
client := github.NewClient(tc.mockedClient)
309230

310-
var gqlClient *githubv4.Client
311-
cache := repoAccessCache
312-
if tc.gqlHTTPClient != nil {
313-
gqlClient = githubv4.NewClient(tc.gqlHTTPClient)
314-
cache = stubRepoAccessCache(gqlClient, 15*time.Minute)
231+
gqlClient := defaultGQLClient
232+
var cache *lockdown.RepoAccessCache
233+
if tc.restPermission != "" {
234+
restClient := mockRESTPermissionServer(t, tc.restPermission, nil)
235+
cache = stubLockdownCache(t, restClient, 15*time.Minute)
315236
} else {
316-
gqlClient = githubv4.NewClient(nil)
237+
cache = stubRepoAccessCache(gqlClient, 15*time.Minute)
317238
}
318239

319240
flags := stubFeatureFlags(map[string]bool{"lockdown-mode": tc.lockdownEnabled})
@@ -1997,7 +1918,6 @@ func Test_GetIssueComments(t *testing.T) {
19971918
tests := []struct {
19981919
name string
19991920
mockedClient *http.Client
2000-
gqlHTTPClient *http.Client
20011921
requestArgs map[string]any
20021922
expectError bool
20031923
expectedComments []*github.IssueComment
@@ -2069,7 +1989,6 @@ func Test_GetIssueComments(t *testing.T) {
20691989
},
20701990
}),
20711991
}),
2072-
gqlHTTPClient: newRepoAccessHTTPClient(),
20731992
requestArgs: map[string]any{
20741993
"method": "get_comments",
20751994
"owner": "owner",
@@ -2092,13 +2011,17 @@ func Test_GetIssueComments(t *testing.T) {
20922011
t.Run(tc.name, func(t *testing.T) {
20932012
// Setup client with mock
20942013
client := github.NewClient(tc.mockedClient)
2095-
var gqlClient *githubv4.Client
2096-
if tc.gqlHTTPClient != nil {
2097-
gqlClient = githubv4.NewClient(tc.gqlHTTPClient)
2014+
gqlClient := defaultGQLClient
2015+
var cache *lockdown.RepoAccessCache
2016+
if tc.lockdownEnabled {
2017+
restClient := mockRESTPermissionServer(t, "read", map[string]string{
2018+
"maintainer": "write",
2019+
"testuser": "read",
2020+
})
2021+
cache = stubLockdownCache(t, restClient, 15*time.Minute)
20982022
} else {
2099-
gqlClient = githubv4.NewClient(nil)
2023+
cache = stubRepoAccessCache(gqlClient, 15*time.Minute)
21002024
}
2101-
cache := stubRepoAccessCache(gqlClient, 15*time.Minute)
21022025
flags := stubFeatureFlags(map[string]bool{"lockdown-mode": tc.lockdownEnabled})
21032026
deps := BaseDeps{
21042027
Client: client,

pkg/github/pullrequests_test.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1939,7 +1939,12 @@ func Test_GetPullRequestComments(t *testing.T) {
19391939
// Setup cache for lockdown mode
19401940
var cache *lockdown.RepoAccessCache
19411941
if tc.lockdownEnabled {
1942-
cache = stubRepoAccessCache(githubv4.NewClient(newRepoAccessHTTPClient()), 5*time.Minute)
1942+
restClient := mockRESTPermissionServer(t, "read", map[string]string{
1943+
"maintainer": "write",
1944+
"external-user": "read",
1945+
"testuser": "read",
1946+
})
1947+
cache = stubLockdownCache(t, restClient, 5*time.Minute)
19431948
} else {
19441949
cache = stubRepoAccessCache(gqlClient, 5*time.Minute)
19451950
}
@@ -2083,7 +2088,6 @@ func Test_GetPullRequestReviews(t *testing.T) {
20832088
},
20842089
}),
20852090
}),
2086-
gqlHTTPClient: newRepoAccessHTTPClient(),
20872091
requestArgs: map[string]any{
20882092
"method": "get_reviews",
20892093
"owner": "owner",
@@ -2107,13 +2111,20 @@ func Test_GetPullRequestReviews(t *testing.T) {
21072111
t.Run(tc.name, func(t *testing.T) {
21082112
// Setup client with mock
21092113
client := github.NewClient(tc.mockedClient)
2110-
var gqlClient *githubv4.Client
2114+
gqlClient := defaultGQLClient
21112115
if tc.gqlHTTPClient != nil {
21122116
gqlClient = githubv4.NewClient(tc.gqlHTTPClient)
2117+
}
2118+
var cache *lockdown.RepoAccessCache
2119+
if tc.lockdownEnabled {
2120+
restClient := mockRESTPermissionServer(t, "read", map[string]string{
2121+
"maintainer": "write",
2122+
"testuser": "read",
2123+
})
2124+
cache = stubLockdownCache(t, restClient, 5*time.Minute)
21132125
} else {
2114-
gqlClient = githubv4.NewClient(nil)
2126+
cache = stubRepoAccessCache(gqlClient, 5*time.Minute)
21152127
}
2116-
cache := stubRepoAccessCache(gqlClient, 5*time.Minute)
21172128
flags := stubFeatureFlags(map[string]bool{"lockdown-mode": tc.lockdownEnabled})
21182129
serverTool := PullRequestRead(translations.NullTranslationHelper)
21192130
deps := BaseDeps{

pkg/github/server_test.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"log/slog"
99
"net/http"
10+
"strings"
1011
"testing"
1112
"time"
1213

@@ -99,7 +100,35 @@ func stubGQLClientFnErr(errMsg string) func(context.Context) (*githubv4.Client,
99100

100101
func stubRepoAccessCache(client *githubv4.Client, ttl time.Duration) *lockdown.RepoAccessCache {
101102
cacheName := fmt.Sprintf("repo-access-cache-test-%d", time.Now().UnixNano())
102-
return lockdown.GetInstance(client, lockdown.WithTTL(ttl), lockdown.WithCacheName(cacheName))
103+
return lockdown.NewRepoAccessCache(client, nil, lockdown.WithTTL(ttl), lockdown.WithCacheName(cacheName))
104+
}
105+
106+
func stubLockdownCache(t *testing.T, restClient *gogithub.Client, ttl time.Duration) *lockdown.RepoAccessCache {
107+
t.Helper()
108+
cacheName := fmt.Sprintf("repo-access-cache-test-%d", time.Now().UnixNano())
109+
return lockdown.NewRepoAccessCache(
110+
githubv4.NewClient(newRepoAccessHTTPClient()),
111+
restClient,
112+
lockdown.WithTTL(ttl),
113+
lockdown.WithCacheName(cacheName),
114+
)
115+
}
116+
117+
func mockRESTPermissionServer(t *testing.T, defaultPerm string, overrides map[string]string) *gogithub.Client {
118+
t.Helper()
119+
return gogithub.NewClient(MockHTTPClientWithHandler(func(w http.ResponseWriter, r *http.Request) {
120+
perm := defaultPerm
121+
for user, p := range overrides {
122+
if strings.Contains(r.URL.Path, "/collaborators/"+user+"/") {
123+
perm = p
124+
break
125+
}
126+
}
127+
w.Header().Set("Content-Type", "application/json")
128+
_ = json.NewEncoder(w).Encode(gogithub.RepositoryPermissionLevel{
129+
Permission: gogithub.Ptr(perm),
130+
})
131+
}))
103132
}
104133

105134
func stubFeatureFlags(enabledFlags map[string]bool) FeatureFlags {

0 commit comments

Comments
 (0)