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 {
37 name: "default url with git extension",
38 args: args{
39 url: "https://github.com/MichaelMure/git-bug.git",
40 },
41 want: want{
42 owner: "MichaelMure",
43 project: "git-bug",
44 err: nil,
45 },
46 },
47 {
48 name: "url with git protocol",
49 args: args{
50 url: "git://github.com/MichaelMure/git-bug.git",
51 },
52 want: want{
53 owner: "MichaelMure",
54 project: "git-bug",
55 err: nil,
56 },
57 },
58 {
59 name: "ssh url",
60 args: args{
61 url: "git@github.com:MichaelMure/git-bug.git",
62 },
63 want: want{
64 owner: "MichaelMure",
65 project: "git-bug",
66 err: nil,
67 },
68 },
69 {
70 name: "bad url",
71 args: args{
72 url: "https://githb.com/MichaelMure/git-bug.git",
73 },
74 want: want{
75 err: ErrBadProjectURL,
76 },
77 },
78 }
79
80 for _, tt := range tests {
81 t.Run(tt.name, func(t *testing.T) {
82 owner, project, err := splitURL(tt.args.url)
83 assert.Equal(t, tt.want.err, err)
84 assert.Equal(t, tt.want.owner, owner)
85 assert.Equal(t, tt.want.project, project)
86 })
87 }
88}
89
90func TestValidateProject(t *testing.T) {
91 tokenPrivateScope := os.Getenv("GITHUB_TOKEN_PRIVATE")
92 if tokenPrivateScope == "" {
93 t.Skip("Env var GITHUB_TOKEN_PRIVATE missing")
94 }
95
96 tokenPublicScope := os.Getenv("GITHUB_TOKEN_PUBLIC")
97 if tokenPublicScope == "" {
98 t.Skip("Env var GITHUB_TOKEN_PUBLIC missing")
99 }
100
101 type args struct {
102 owner string
103 project string
104 token string
105 }
106 tests := []struct {
107 name string
108 args args
109 want bool
110 }{
111 {
112 name: "public repository and token with scope 'public_repo",
113 args: args{
114 project: "git-bug",
115 owner: "MichaelMure",
116 token: tokenPublicScope,
117 },
118 want: true,
119 },
120 {
121 name: "private repository and token with scope 'repo",
122 args: args{
123 project: "git-bug-test-github-bridge",
124 owner: "MichaelMure",
125 token: tokenPrivateScope,
126 },
127 want: true,
128 },
129 {
130 name: "private repository and token with scope 'public_repo'",
131 args: args{
132 project: "git-bug-test-github-bridge",
133 owner: "MichaelMure",
134 token: tokenPublicScope,
135 },
136 want: false,
137 },
138 }
139
140 for _, tt := range tests {
141 t.Run(tt.name, func(t *testing.T) {
142 ok, _ := validateProject(tt.args.owner, tt.args.project, tt.args.token)
143 assert.Equal(t, tt.want, ok)
144 })
145 }
146}