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