Repository navigation
Expand file tree
/
Copy pathgithub_test.go
More file actions
173 lines (136 loc) · 4.57 KB
/
Copy pathgithub_test.go
File metadata and controls
173 lines (136 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
package github
import (
"context"
"net/http"
"testing"
"time"
"github.com/pkg/browser"
"github.com/rockbears/log"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/ovh/cds/engine/cache"
"github.com/ovh/cds/engine/test"
"github.com/ovh/cds/sdk"
)
// TestNew needs githubClientID and githubClientSecret
func TestNewClient(t *testing.T) {
ghConsummer := getNewConsumer(t)
assert.NotNil(t, ghConsummer)
}
func getNewConsumer(t *testing.T) sdk.VCSServer {
log.Factory = log.NewTestingWrapper(t)
cfg := test.LoadTestingConf(t, sdk.TypeAPI)
clientID := cfg["githubClientID"]
clientSecret := cfg["githubClientSecret"]
redisHost := cfg["redisHost"]
redisPassword := cfg["redisPassword"]
if clientID == "" && clientSecret == "" {
t.Logf("Unable to read github configuration. Skipping this tests.")
t.SkipNow()
}
cache, err := cache.New(redisHost, redisPassword, 30)
if err != nil {
t.Fatalf("Unable to init cache (%s): %v", redisHost, err)
}
ghConsummer := New(clientID, clientSecret, "", "", "http://localhost", "", "", "", "", cache, true, true)
return ghConsummer
}
func getNewAuthorizedClient(t *testing.T) sdk.VCSAuthorizedClient {
log.Factory = log.NewTestingWrapper(t)
cfg := test.LoadTestingConf(t, sdk.TypeAPI)
clientID := cfg["githubClientID"]
clientSecret := cfg["githubClientSecret"]
accessToken := cfg["githubAccessToken"]
redisHost := cfg["redisHost"]
redisPassword := cfg["redisPassword"]
if clientID == "" && clientSecret == "" {
t.Logf("Unable to read github configuration. Skipping this tests.")
t.SkipNow()
}
cache, err := cache.New(redisHost, redisPassword, 30)
if err != nil {
t.Fatalf("Unable to init cache (%s): %v", redisHost, err)
}
ghConsummer := New(clientID, clientSecret, "", "", "http://localhost", "", "", "", "", cache, true, true)
cli, err := ghConsummer.GetAuthorizedClient(context.Background(), accessToken, "", 0)
if err != nil {
t.Fatalf("Unable to init authorized client (%s): %v", redisHost, err)
}
return cli
}
func TestClientAuthorizeToken(t *testing.T) {
ghConsummer := getNewConsumer(t)
token, url, err := ghConsummer.AuthorizeRedirect(context.Background())
t.Logf("token: %s", token)
t.Logf("url: %s", url)
assert.NotEmpty(t, token)
assert.NotEmpty(t, url)
require.NoError(t, err)
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
out := make(chan http.Request, 1)
go callbackServer(ctx, t, out)
err = browser.OpenURL(url)
require.NoError(t, err)
r, ok := <-out
t.Logf("Chan request closed? %v", !ok)
t.Logf("OAuth request 2: %+v", r)
assert.NotNil(t, r)
cberr := r.FormValue("error")
errDescription := r.FormValue("error_description")
errURI := r.FormValue("error_uri")
assert.Empty(t, cberr)
assert.Empty(t, errDescription)
assert.Empty(t, errURI)
code := r.FormValue("code")
state := r.FormValue("state")
assert.NotEmpty(t, code)
assert.NotEmpty(t, state)
accessToken, accessTokenSecret, err := ghConsummer.AuthorizeToken(context.Background(), state, code)
assert.NotEmpty(t, accessToken)
assert.NotEmpty(t, accessTokenSecret)
require.NoError(t, err)
t.Logf("Token is %s", accessToken)
ghClient, err := ghConsummer.GetAuthorizedClient(context.Background(), accessToken, accessTokenSecret, 0)
require.NoError(t, err)
assert.NotNil(t, ghClient)
}
func TestAuthorizedClient(t *testing.T) {
ghClient := getNewAuthorizedClient(t)
assert.NotNil(t, ghClient)
}
func TestRepos(t *testing.T) {
ghClient := getNewAuthorizedClient(t)
assert.NotNil(t, ghClient)
repos, err := ghClient.Repos(context.Background())
require.NoError(t, err)
assert.NotEmpty(t, repos)
}
func TestRepoByFullname(t *testing.T) {
ghClient := getNewAuthorizedClient(t)
assert.NotNil(t, ghClient)
repo, err := ghClient.RepoByFullname(context.Background(), "ovh/cds")
require.NoError(t, err)
assert.NotNil(t, repo)
}
func TestBranches(t *testing.T) {
ghClient := getNewAuthorizedClient(t)
assert.NotNil(t, ghClient)
branches, err := ghClient.Branches(context.Background(), "ovh/cds", sdk.VCSBranchesFilter{})
require.NoError(t, err)
assert.NotEmpty(t, branches)
}
func TestBranch(t *testing.T) {
ghClient := getNewAuthorizedClient(t)
assert.NotNil(t, ghClient)
branch, err := ghClient.Branch(context.Background(), "ovh/cds", sdk.VCSBranchFilters{BranchName: "master"})
require.NoError(t, err)
assert.NotNil(t, branch)
}
func TestCommits(t *testing.T) {
ghClient := getNewAuthorizedClient(t)
assert.NotNil(t, ghClient)
commits, err := ghClient.Commits(context.Background(), "ovh/cds", "master", "", "")
require.NoError(t, err)
assert.NotNil(t, commits)
}