-
Notifications
You must be signed in to change notification settings - Fork 311
Expand file tree
/
Copy pathgithub_test.go
More file actions
176 lines (164 loc) · 4.57 KB
/
github_test.go
File metadata and controls
176 lines (164 loc) · 4.57 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package plugin
import (
"net/http"
"strings"
"testing"
"github.com/samber/lo"
"github.com/stretchr/testify/assert"
"go.jetify.com/devbox/nix/flake"
)
func TestNewGithubPlugin(t *testing.T) {
testCases := []struct {
name string
Include string
expected githubPlugin
expectedURL string
}{
{
name: "parse basic github plugin",
Include: "github:jetify-com/devbox-plugins",
expected: githubPlugin{
ref: flake.Ref{
Type: "github",
Owner: "jetify-com",
Repo: "devbox-plugins",
},
name: "jetify-com.devbox-plugins",
},
expectedURL: "https://raw.githubusercontent.com/jetify-com/devbox-plugins/master",
},
{
name: "parse github plugin with dir param",
Include: "github:jetify-com/devbox-plugins?dir=mongodb",
expected: githubPlugin{
ref: flake.Ref{
Type: "github",
Owner: "jetify-com",
Repo: "devbox-plugins",
Dir: "mongodb",
},
name: "jetify-com.devbox-plugins.mongodb",
},
expectedURL: "https://raw.githubusercontent.com/jetify-com/devbox-plugins/master/mongodb",
},
{
name: "parse github plugin with dir param and rev",
Include: "github:jetify-com/devbox-plugins/my-branch?dir=mongodb",
expected: githubPlugin{
ref: flake.Ref{
Type: "github",
Owner: "jetify-com",
Repo: "devbox-plugins",
Ref: "my-branch",
Dir: "mongodb",
},
name: "jetify-com.devbox-plugins.mongodb",
},
expectedURL: "https://raw.githubusercontent.com/jetify-com/devbox-plugins/my-branch/mongodb",
},
{
name: "parse github plugin with dir param and rev",
Include: "github:jetify-com/devbox-plugins/initials/my-branch?dir=mongodb",
expected: githubPlugin{
ref: flake.Ref{
Type: "github",
Owner: "jetify-com",
Repo: "devbox-plugins",
Ref: "initials/my-branch",
Dir: "mongodb",
},
name: "jetify-com.devbox-plugins.mongodb",
},
expectedURL: "https://raw.githubusercontent.com/jetify-com/devbox-plugins/initials/my-branch/mongodb",
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
actual, err := newGithubPluginForTest(testCase.Include)
assert.NoError(t, err)
assert.Equal(t, &testCase.expected, actual)
u, err := testCase.expected.url("")
assert.Nil(t, err)
assert.Equal(t, testCase.expectedURL, u)
})
}
}
// keep in sync with newGithubPlugin
func newGithubPluginForTest(include string) (*githubPlugin, error) {
ref, err := flake.ParseRef(include)
if err != nil {
return nil, err
}
plugin := &githubPlugin{ref: ref}
name := strings.ReplaceAll(ref.Dir, "/", "-")
plugin.name = githubNameRegexp.ReplaceAllString(
strings.Join(lo.Compact([]string{ref.Owner, ref.Repo, name}), "."),
" ",
)
return plugin, nil
}
func TestGithubPluginAuth(t *testing.T) {
githubPlugin := githubPlugin{
ref: flake.Ref{
Type: "github",
Owner: "jetpack-io",
Repo: "devbox-plugins",
},
name: "jetpack-io.devbox-plugins",
}
expectedURL := "https://raw.githubusercontent.com/jetpack-io/devbox-plugins/master/test"
t.Run("generate request for public Github repository", func(t *testing.T) {
t.Setenv("GITHUB_TOKEN", "")
url, err := githubPlugin.url("test")
assert.NoError(t, err)
actual, err := githubPlugin.request(url)
assert.NoError(t, err)
assert.Equal(t, expectedURL, actual.URL.String())
assert.Equal(t, "", actual.Header.Get("Authorization"))
})
t.Run("generate request for private Github repository", func(t *testing.T) {
t.Setenv("GITHUB_TOKEN", "gh_abcd")
url, err := githubPlugin.url("test")
assert.NoError(t, err)
actual, err := githubPlugin.request(url)
assert.NoError(t, err)
assert.Equal(t, expectedURL, actual.URL.String())
assert.Equal(t, "token gh_abcd", actual.Header.Get("Authorization"))
})
}
func TestGetRedactedAuthHeader(t *testing.T) {
testCases := []struct {
name string
authHeader string
expected string
}{
{
"normal length token partially readable for debugging",
"token ghp_61b296fb898349778e20532cb65ce38e",
"token ghp_********************************",
},
{
"short token redacted",
"token ghp_61b29",
"token *********",
},
{
"short header fully redacted",
"token xyz",
"*********",
},
{
"no token returns empty string",
"",
"",
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, "https://example.com", nil)
assert.NoError(t, err)
req.Header.Add("Authorization", testCase.authHeader)
assert.Equal(t, testCase.expected, getRedactedAuthHeader(req))
})
}
}