|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/github/gh-actions-lock/internal/ghapi/httpmock" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +func TestRun_ParitySSOGuidance(t *testing.T) { |
| 14 | + const message = "Resource protected by organization SAML enforcement. You must grant your Personal Access token access to this organization." |
| 15 | + for _, mode := range []struct { |
| 16 | + name string |
| 17 | + args []string |
| 18 | + }{ |
| 19 | + {"fix", nil}, |
| 20 | + {"no-fix", []string{"--no-fix"}}, |
| 21 | + {"verify", []string{"--verify"}}, |
| 22 | + } { |
| 23 | + t.Run(mode.name, func(t *testing.T) { |
| 24 | + t.Setenv("CODESPACES", "") |
| 25 | + reg := &httpmock.Registry{} |
| 26 | + reg.Register(httpmock.GraphQL(`commit: object\(oid`), func(req *http.Request) (*http.Response, error) { |
| 27 | + resp, err := httpmock.JSONResponse(map[string]string{"message": message})(req) |
| 28 | + if err != nil { |
| 29 | + return nil, err |
| 30 | + } |
| 31 | + resp.StatusCode = http.StatusForbidden |
| 32 | + return resp, nil |
| 33 | + }) |
| 34 | + reg.Register(httpmock.REST("GET", `^/repos/krzema12/github-actions-typing$`), httpmock.StatusResponse(http.StatusNotFound)) |
| 35 | + path := writeTempWorkflow(t, transferWorkflow, "krzema12/github-actions-typing@v2.2.2=sha1-"+parityTestSHA) |
| 36 | + lockBefore := readTempLockfilePins(t) |
| 37 | + |
| 38 | + _, stderr, err := runCommandWithHTTP(t, reg, append(mode.args, "--no-interactive", path)...) |
| 39 | + require.ErrorIs(t, err, errSilent) |
| 40 | + assert.Contains(t, stderr, "GitHub returned 403 Forbidden: "+message) |
| 41 | + assert.NotContains(t, stderr, "https://api.github.com/") |
| 42 | + assert.Equal(t, 1, strings.Count(stderr, message)) |
| 43 | + if mode.name != "fix" { |
| 44 | + assert.Equal(t, 1, strings.Count(stderr, "gh auth refresh -h github.com")) |
| 45 | + } |
| 46 | + assert.Equal(t, lockBefore, readTempLockfilePins(t)) |
| 47 | + reg.Verify(t) |
| 48 | + }) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func TestRun_ParitySSORateLimitedGuidance(t *testing.T) { |
| 53 | + const message = "Resource protected by organization SAML enforcement." |
| 54 | + for _, mode := range []struct { |
| 55 | + name string |
| 56 | + args []string |
| 57 | + }{ |
| 58 | + {"fix", nil}, |
| 59 | + {"no-fix", []string{"--no-fix"}}, |
| 60 | + {"verify", []string{"--verify"}}, |
| 61 | + } { |
| 62 | + t.Run(mode.name, func(t *testing.T) { |
| 63 | + t.Setenv("CODESPACES", "true") |
| 64 | + reg := &httpmock.Registry{} |
| 65 | + reg.Register(httpmock.GraphQL(`commit: object\(oid`), httpmock.JSONResponse(map[string]any{ |
| 66 | + "data": map[string]any{"a0": map[string]any{ |
| 67 | + "nameWithOwner": "krzema12/github-actions-typing", |
| 68 | + "databaseId": 1, |
| 69 | + "commit": map[string]any{"oid": parityTestSHA}, |
| 70 | + "tag": map[string]any{"oid": parityTestMoved}, |
| 71 | + "tagRef": map[string]any{"oid": parityTestMoved}, |
| 72 | + }}, |
| 73 | + })) |
| 74 | + reg.RegisterReusable(httpmock.REST("HEAD", `^/orgs/krzema12$`), httpmock.StatusResponse(http.StatusOK)) |
| 75 | + reg.Register(httpmock.REST("GET", `^/repos/krzema12/github-actions-typing/compare/`), func(req *http.Request) (*http.Response, error) { |
| 76 | + resp, err := httpmock.JSONResponse(map[string]string{"message": message})(req) |
| 77 | + if err != nil { |
| 78 | + return nil, err |
| 79 | + } |
| 80 | + resp.StatusCode = http.StatusForbidden |
| 81 | + return resp, nil |
| 82 | + }) |
| 83 | + reg.Register(httpmock.REST("GET", `^/repos/krzema12/github-actions-typing/compare/`), httpmock.StatusResponse(http.StatusTooManyRequests)) |
| 84 | + useDefaultTransport(t, reg) |
| 85 | + path := writeTempWorkflow(t, transferWorkflow, "krzema12/github-actions-typing@v2.2.2=sha1-"+parityTestSHA) |
| 86 | + lockBefore := readTempLockfilePins(t) |
| 87 | + |
| 88 | + _, stderr, err := runCommandWithHTTP(t, reg, append(mode.args, "--no-interactive", path)...) |
| 89 | + require.ErrorIs(t, err, errSilent) |
| 90 | + assert.Contains(t, stderr, "SSO authorization required") |
| 91 | + assert.Contains(t, stderr, "Authorize it at https://github.com/orgs/krzema12/sso and retry") |
| 92 | + assert.Contains(t, stderr, "anonymous fallback for public repositories was rate-limited") |
| 93 | + assert.NotContains(t, stderr, "https://api.github.com/") |
| 94 | + assert.Equal(t, 1, strings.Count(stderr, "Authorize it at")) |
| 95 | + if mode.name != "fix" { |
| 96 | + assert.Equal(t, 1, strings.Count(stderr, "Your codespace's GITHUB_TOKEN cannot be SSO-authorized.")) |
| 97 | + } |
| 98 | + assert.Equal(t, lockBefore, readTempLockfilePins(t)) |
| 99 | + reg.Verify(t) |
| 100 | + }) |
| 101 | + } |
| 102 | +} |
0 commit comments