1package github
2
3import (
4 "os"
5 "testing"
6
7 "github.com/stretchr/testify/assert"
8
9 "github.com/git-bug/git-bug/bridge/core/auth"
10)
11
12func TestSplitURL(t *testing.T) {
13 type args struct {
14 url string
15 }
16 type want struct {
17 owner string
18 project string
19 err error
20 }
21 tests := []struct {
22 name string
23 args args
24 want want
25 }{
26 {
27 name: "default url",
28 args: args{
29 url: "https://github.com/git-bug/git-bug",
30 },
31 want: want{
32 owner: "git-bug",
33 project: "git-bug",
34 err: nil,
35 },
36 },
37 {
38 name: "default issues url",
39 args: args{
40 url: "https://github.com/git-bug/git-bug/issues",
41 },
42 want: want{
43 owner: "git-bug",
44 project: "git-bug",
45 err: nil,
46 },
47 },
48 {
49 name: "default url with git extension",
50 args: args{
51 url: "https://github.com/git-bug/git-bug.git",
52 },
53 want: want{
54 owner: "git-bug",
55 project: "git-bug",
56 err: nil,
57 },
58 },
59 {
60 name: "url with git protocol",
61 args: args{
62 url: "git://github.com/git-bug/git-bug.git",
63 },
64 want: want{
65 owner: "git-bug",
66 project: "git-bug",
67 err: nil,
68 },
69 },
70 {
71 name: "ssh url",
72 args: args{
73 url: "git@github.com:git-bug/git-bug.git",
74 },
75 want: want{
76 owner: "git-bug",
77 project: "git-bug",
78 err: nil,
79 },
80 },
81 {
82 name: "bad url",
83 args: args{
84 url: "https://githb.com/git-bug/git-bug.git",
85 },
86 want: want{
87 err: ErrBadProjectURL,
88 },
89 },
90 }
91
92 for _, tt := range tests {
93 t.Run(tt.name, func(t *testing.T) {
94 owner, project, err := splitURL(tt.args.url)
95 assert.Equal(t, tt.want.err, err)
96 assert.Equal(t, tt.want.owner, owner)
97 assert.Equal(t, tt.want.project, project)
98 })
99 }
100}
101
102func TestValidateUsername(t *testing.T) {
103 if env := os.Getenv("TRAVIS"); env == "true" {
104 t.Skip("Travis environment: avoiding non authenticated requests")
105 }
106 if _, has := os.LookupEnv("CI"); has {
107 t.Skip("Github action environment: avoiding non authenticated requests")
108 }
109
110 tests := []struct {
111 name string
112 input string
113 fixed string
114 ok bool
115 }{
116 {
117 name: "existing username",
118 input: "git-bug",
119 fixed: "git-bug",
120 ok: true,
121 },
122 {
123 name: "existing username with bad case",
124 input: "GiT-bUg",
125 fixed: "git-bug",
126 ok: true,
127 },
128 {
129 name: "existing organisation",
130 input: "git-bug",
131 fixed: "git-bug",
132 ok: true,
133 },
134 {
135 name: "existing organisation with bad case",
136 input: "gIt-BuG",
137 fixed: "git-bug",
138 ok: true,
139 },
140 {
141 name: "non existing username",
142 input: "cant-find-this",
143 fixed: "",
144 ok: false,
145 },
146 }
147
148 for _, tt := range tests {
149 t.Run(tt.name, func(t *testing.T) {
150 ok, fixed, err := validateUsername(tt.input)
151 assert.NoError(t, err)
152 assert.Equal(t, tt.ok, ok)
153 assert.Equal(t, tt.fixed, fixed)
154 })
155 }
156}
157
158func TestValidateProject(t *testing.T) {
159 envPrivate := os.Getenv("GITHUB_TOKEN_PRIVATE")
160 if envPrivate == "" {
161 t.Skip("Env var GITHUB_TOKEN_PRIVATE missing")
162 }
163
164 envPublic := os.Getenv("GITHUB_TOKEN_PUBLIC")
165 if envPublic == "" {
166 t.Skip("Env var GITHUB_TOKEN_PUBLIC missing")
167 }
168
169 tokenPrivate := auth.NewToken(target, envPrivate)
170 tokenPublic := auth.NewToken(target, envPublic)
171
172 type args struct {
173 owner string
174 project string
175 token *auth.Token
176 }
177 tests := []struct {
178 name string
179 args args
180 want bool
181 }{
182 {
183 name: "public repository and token with scope 'public_repo'",
184 args: args{
185 project: "git-bug",
186 owner: "git-bug",
187 token: tokenPublic,
188 },
189 want: true,
190 },
191 {
192 name: "private repository and token with scope 'repo'",
193 args: args{
194 project: "test-github-bridge",
195 owner: "git-bug",
196 token: tokenPrivate,
197 },
198 want: true,
199 },
200 {
201 name: "private repository and token with scope 'public_repo'",
202 args: args{
203 project: "test-github-bridge",
204 owner: "git-bug",
205 token: tokenPublic,
206 },
207 want: false,
208 },
209 {
210 name: "project not existing",
211 args: args{
212 project: "cant-find-this",
213 owner: "organisation-not-found",
214 token: tokenPublic,
215 },
216 want: false,
217 },
218 }
219
220 for _, tt := range tests {
221 t.Run(tt.name, func(t *testing.T) {
222 ok, _ := validateProject(tt.args.owner, tt.args.project, tt.args.token)
223 assert.Equal(t, tt.want, ok)
224 })
225 }
226}