config_test.go

  1package github
  2
  3import (
  4	"os"
  5	"testing"
  6
  7	"github.com/stretchr/testify/assert"
  8
  9	"github.com/MichaelMure/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/MichaelMure/git-bug",
 30			},
 31			want: want{
 32				owner:   "MichaelMure",
 33				project: "git-bug",
 34				err:     nil,
 35			},
 36		},
 37		{
 38			name: "default issues url",
 39			args: args{
 40				url: "https://github.com/MichaelMure/git-bug/issues",
 41			},
 42			want: want{
 43				owner:   "MichaelMure",
 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/MichaelMure/git-bug.git",
 52			},
 53			want: want{
 54				owner:   "MichaelMure",
 55				project: "git-bug",
 56				err:     nil,
 57			},
 58		},
 59		{
 60			name: "url with git protocol",
 61			args: args{
 62				url: "git://github.com/MichaelMure/git-bug.git",
 63			},
 64			want: want{
 65				owner:   "MichaelMure",
 66				project: "git-bug",
 67				err:     nil,
 68			},
 69		},
 70		{
 71			name: "ssh url",
 72			args: args{
 73				url: "git@github.com:MichaelMure/git-bug.git",
 74			},
 75			want: want{
 76				owner:   "MichaelMure",
 77				project: "git-bug",
 78				err:     nil,
 79			},
 80		},
 81		{
 82			name: "bad url",
 83			args: args{
 84				url: "https://githb.com/MichaelMure/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
107	tests := []struct {
108		name  string
109		input string
110		fixed string
111		ok    bool
112	}{
113		{
114			name:  "existing username",
115			input: "MichaelMure",
116			fixed: "MichaelMure",
117			ok:    true,
118		},
119		{
120			name:  "existing username with bad case",
121			input: "MicHaelmurE",
122			fixed: "MichaelMure",
123			ok:    true,
124		},
125		{
126			name:  "existing organisation",
127			input: "ipfs",
128			fixed: "ipfs",
129			ok:    true,
130		},
131		{
132			name:  "existing organisation with bad case",
133			input: "iPfS",
134			fixed: "ipfs",
135			ok:    true,
136		},
137		{
138			name:  "non existing username",
139			input: "cant-find-this",
140			fixed: "",
141			ok:    false,
142		},
143	}
144
145	for _, tt := range tests {
146		t.Run(tt.name, func(t *testing.T) {
147			ok, fixed, err := validateUsername(tt.input)
148			assert.NoError(t, err)
149			assert.Equal(t, tt.ok, ok)
150			assert.Equal(t, tt.fixed, fixed)
151		})
152	}
153}
154
155func TestValidateProject(t *testing.T) {
156	envPrivate := os.Getenv("GITHUB_TOKEN_PRIVATE")
157	if envPrivate == "" {
158		t.Skip("Env var GITHUB_TOKEN_PRIVATE missing")
159	}
160
161	envPublic := os.Getenv("GITHUB_TOKEN_PUBLIC")
162	if envPublic == "" {
163		t.Skip("Env var GITHUB_TOKEN_PUBLIC missing")
164	}
165
166	tokenPrivate := auth.NewToken(target, envPrivate)
167	tokenPublic := auth.NewToken(target, envPublic)
168
169	type args struct {
170		owner   string
171		project string
172		token   *auth.Token
173	}
174	tests := []struct {
175		name string
176		args args
177		want bool
178	}{
179		{
180			name: "public repository and token with scope 'public_repo'",
181			args: args{
182				project: "git-bug",
183				owner:   "MichaelMure",
184				token:   tokenPublic,
185			},
186			want: true,
187		},
188		{
189			name: "private repository and token with scope 'repo'",
190			args: args{
191				project: "git-bug-test-github-bridge",
192				owner:   "MichaelMure",
193				token:   tokenPrivate,
194			},
195			want: true,
196		},
197		{
198			name: "private repository and token with scope 'public_repo'",
199			args: args{
200				project: "git-bug-test-github-bridge",
201				owner:   "MichaelMure",
202				token:   tokenPublic,
203			},
204			want: false,
205		},
206		{
207			name: "project not existing",
208			args: args{
209				project: "cant-find-this",
210				owner:   "organisation-not-found",
211				token:   tokenPublic,
212			},
213			want: false,
214		},
215	}
216
217	for _, tt := range tests {
218		t.Run(tt.name, func(t *testing.T) {
219			ok, _ := validateProject(tt.args.owner, tt.args.project, tt.args.token)
220			assert.Equal(t, tt.want, ok)
221		})
222	}
223}