-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathgithub_test.go
More file actions
86 lines (79 loc) · 2.1 KB
/
Copy pathgithub_test.go
File metadata and controls
86 lines (79 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// SPDX-FileCopyrightText: Copyright 2025 The SLSA Authors
// SPDX-License-Identifier: Apache-2.0
package github
import (
"errors"
"fmt"
"net/http"
"testing"
"github.com/google/go-github/v69/github"
"github.com/stretchr/testify/require"
"github.com/slsa-framework/source-tool/pkg/sourcetool/models"
)
func TestAsUnsupportedPlanError(t *testing.T) {
t.Parallel()
// This is the shape GitHub returns when reading branch rules on a private
// repo that is on a free plan (see slsa-framework/source-tool#326). The
// detection must key off the typed response and status code, not the
// message text, so the test uses the real go-github error type.
forbidden := &github.ErrorResponse{
Response: &http.Response{StatusCode: http.StatusForbidden},
Message: "Upgrade to GitHub Pro or make this repository public to enable this feature.",
}
for _, tc := range []struct {
name string
err error
expectPlan bool
}{
{
name: "nil",
err: nil,
expectPlan: false,
},
{
name: "plain-403",
err: forbidden,
expectPlan: true,
},
{
name: "wrapped-403",
err: fmt.Errorf("checking status: %w", forbidden),
expectPlan: true,
},
{
name: "404-not-plan",
err: &github.ErrorResponse{
Response: &http.Response{StatusCode: http.StatusNotFound},
Message: "Not Found",
},
expectPlan: false,
},
{
name: "non-github-error",
err: errors.New("some other failure"),
expectPlan: false,
},
{
name: "403-without-response",
err: &github.ErrorResponse{
Message: "Forbidden but no response attached",
},
expectPlan: false,
},
} {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
got := asUnsupportedPlanError(tc.err)
if !tc.expectPlan {
require.NoError(t, got)
return
}
require.Error(t, got)
// The actionable sentinel must be detectable with errors.Is so the
// CLI can switch on it...
require.ErrorIs(t, got, models.ErrUnsupportedRepoPlan)
// ...and the original API error must still be reachable for debugging.
require.ErrorIs(t, got, tc.err)
})
}
}