gunk-dev/the-valley

noaccess_test.go

source88a5a486b52fmain's published tip history
2.3 KiB
1package github
2
3import (
4 "os"
5 "path/filepath"
6 "strconv"
7 "testing"
8)
9
10func TestIsNoAccess(t *testing.T) {
11 tests := []struct {
12 name string
13 stderr string
14 want bool
15 }{
16 {"PR not found", "GraphQL: Could not resolve to a PullRequest with the number of 42. (repository.pullRequest)", true},
17 {"repo not visible", "GraphQL: Could not resolve to a Repository with the name 'maintainer/site'. (repository)", true},
18 {"REST 404", "HTTP 404: Not Found (https://api.github.com/repos/maintainer/site/pulls/42)", true},
19 {"REST 403", "HTTP 403: Resource not accessible by integration (https://api.github.com/graphql)", true},
20 {"rate limited 403", "HTTP 403: API rate limit exceeded for user ID 1. (https://api.github.com/graphql)", false},
21 {"secondary rate limit", "HTTP 403: You have exceeded a secondary rate limit.", false},
22 {"network", "error connecting to api.github.com", false},
23 {"server error", "HTTP 502: Bad Gateway (https://api.github.com/graphql)", false},
24 {"auth", "To get started with GitHub CLI, please run: gh auth login", false},
25 {"empty", "", false},
26 }
27 for _, tt := range tests {
28 t.Run(tt.name, func(t *testing.T) {
29 if got := IsNoAccess(tt.stderr); got != tt.want {
30 t.Errorf("IsNoAccess(%q) = %v, want %v", tt.stderr, got, tt.want)
31 }
32 })
33 }
34}
35
36// fakeGH puts a gh on PATH that prints stderr and exits with code.
37func fakeGH(t *testing.T, stderr string, code int) {
38 t.Helper()
39 dir := t.TempDir()
40 script := "#!/bin/sh\necho '" + stderr + "' >&2\nexit " + strconv.Itoa(code) + "\n"
41 if err := os.WriteFile(filepath.Join(dir, "gh"), []byte(script), 0o755); err != nil {
42 t.Fatal(err)
43 }
44 t.Setenv("PATH", dir+string(filepath.ListSeparator)+os.Getenv("PATH"))
45}
46
47func TestGetStateNoAccess(t *testing.T) {
48 fakeGH(t, "GraphQL: Could not resolve to a Repository with the name maintainer/site. (repository)", 1)
49 if got := NewGHCLIClient("").GetState(nil, "https://github.com/maintainer/site/pull/42"); got != StateNoAccess {
50 t.Errorf("GetState() = %q, want %q", got, StateNoAccess)
51 }
52}
53
54func TestGetStateTransientFailureIsUnknown(t *testing.T) {
55 fakeGH(t, "error connecting to api.github.com", 1)
56 if got := NewGHCLIClient("").GetState(nil, "https://github.com/maintainer/site/pull/42"); got != "UNKNOWN" {
57 t.Errorf("GetState() = %q, want UNKNOWN", got)
58 }
59}