forked from google/go-github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apps_test.go
270 lines (228 loc) · 9.19 KB
/
apps_test.go
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Copyright 2016 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package github
import (
"context"
"fmt"
"net/http"
"reflect"
"testing"
"time"
)
func TestAppsService_Get_authenticatedApp(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/app", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testHeader(t, r, "Accept", mediaTypeIntegrationPreview)
fmt.Fprint(w, `{"id":1}`)
})
app, _, err := client.Apps.Get(context.Background(), "")
if err != nil {
t.Errorf("Apps.Get returned error: %v", err)
}
want := &App{ID: Int64(1)}
if !reflect.DeepEqual(app, want) {
t.Errorf("Apps.Get returned %+v, want %+v", app, want)
}
}
func TestAppsService_Get_specifiedApp(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/apps/a", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testHeader(t, r, "Accept", mediaTypeIntegrationPreview)
fmt.Fprint(w, `{"html_url":"https://github.com/apps/a"}`)
})
app, _, err := client.Apps.Get(context.Background(), "a")
if err != nil {
t.Errorf("Apps.Get returned error: %v", err)
}
want := &App{HTMLURL: String("https://github.com/apps/a")}
if !reflect.DeepEqual(app, want) {
t.Errorf("Apps.Get returned %+v, want %+v", *app.HTMLURL, *want.HTMLURL)
}
}
func TestAppsService_ListInstallations(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/app/installations", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testHeader(t, r, "Accept", mediaTypeIntegrationPreview)
testFormValues(t, r, values{
"page": "1",
"per_page": "2",
})
fmt.Fprint(w, `[{
"id":1,
"app_id":1,
"target_id":1,
"target_type": "Organization",
"permissions": {
"metadata": "read",
"contents": "read",
"issues": "write",
"single_file": "write"
},
"events": [
"push",
"pull_request"
],
"single_file_name": "config.yml",
"repository_selection": "selected",
"created_at": "2018-01-01T00:00:00Z",
"updated_at": "2018-01-01T00:00:00Z"}]`,
)
})
opt := &ListOptions{Page: 1, PerPage: 2}
installations, _, err := client.Apps.ListInstallations(context.Background(), opt)
if err != nil {
t.Errorf("Apps.ListInstallations returned error: %v", err)
}
date := Timestamp{Time: time.Date(2018, time.January, 1, 0, 0, 0, 0, time.UTC)}
want := []*Installation{{
ID: Int64(1),
AppID: Int64(1),
TargetID: Int64(1),
TargetType: String("Organization"),
SingleFileName: String("config.yml"),
RepositorySelection: String("selected"),
Permissions: &InstallationPermissions{
Metadata: String("read"),
Contents: String("read"),
Issues: String("write"),
SingleFile: String("write")},
Events: []string{"push", "pull_request"},
CreatedAt: &date,
UpdatedAt: &date,
}}
if !reflect.DeepEqual(installations, want) {
t.Errorf("Apps.ListInstallations returned %+v, want %+v", installations, want)
}
}
func TestAppsService_GetInstallation(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/app/installations/1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testHeader(t, r, "Accept", mediaTypeIntegrationPreview)
fmt.Fprint(w, `{"id":1, "app_id":1, "target_id":1, "target_type": "Organization"}`)
})
installation, _, err := client.Apps.GetInstallation(context.Background(), 1)
if err != nil {
t.Errorf("Apps.GetInstallation returned error: %v", err)
}
want := &Installation{ID: Int64(1), AppID: Int64(1), TargetID: Int64(1), TargetType: String("Organization")}
if !reflect.DeepEqual(installation, want) {
t.Errorf("Apps.GetInstallation returned %+v, want %+v", installation, want)
}
}
func TestAppsService_ListUserInstallations(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/user/installations", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testHeader(t, r, "Accept", mediaTypeIntegrationPreview)
testFormValues(t, r, values{
"page": "1",
"per_page": "2",
})
fmt.Fprint(w, `{"installations":[{"id":1, "app_id":1, "target_id":1, "target_type": "Organization"}]}`)
})
opt := &ListOptions{Page: 1, PerPage: 2}
installations, _, err := client.Apps.ListUserInstallations(context.Background(), opt)
if err != nil {
t.Errorf("Apps.ListUserInstallations returned error: %v", err)
}
want := []*Installation{{ID: Int64(1), AppID: Int64(1), TargetID: Int64(1), TargetType: String("Organization")}}
if !reflect.DeepEqual(installations, want) {
t.Errorf("Apps.ListUserInstallations returned %+v, want %+v", installations, want)
}
}
func TestAppsService_CreateInstallationToken(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/app/installations/1/access_tokens", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
testHeader(t, r, "Accept", mediaTypeIntegrationPreview)
fmt.Fprint(w, `{"token":"t"}`)
})
token, _, err := client.Apps.CreateInstallationToken(context.Background(), 1)
if err != nil {
t.Errorf("Apps.CreateInstallationToken returned error: %v", err)
}
want := &InstallationToken{Token: String("t")}
if !reflect.DeepEqual(token, want) {
t.Errorf("Apps.CreateInstallationToken returned %+v, want %+v", token, want)
}
}
func TestAppsService_FindOrganizationInstallation(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/orgs/o/installation", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testHeader(t, r, "Accept", mediaTypeIntegrationPreview)
fmt.Fprint(w, `{"id":1, "app_id":1, "target_id":1, "target_type": "Organization"}`)
})
installation, _, err := client.Apps.FindOrganizationInstallation(context.Background(), "o")
if err != nil {
t.Errorf("Apps.FindOrganizationInstallation returned error: %v", err)
}
want := &Installation{ID: Int64(1), AppID: Int64(1), TargetID: Int64(1), TargetType: String("Organization")}
if !reflect.DeepEqual(installation, want) {
t.Errorf("Apps.FindOrganizationInstallation returned %+v, want %+v", installation, want)
}
}
func TestAppsService_FindRepositoryInstallation(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/repos/o/r/installation", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testHeader(t, r, "Accept", mediaTypeIntegrationPreview)
fmt.Fprint(w, `{"id":1, "app_id":1, "target_id":1, "target_type": "Organization"}`)
})
installation, _, err := client.Apps.FindRepositoryInstallation(context.Background(), "o", "r")
if err != nil {
t.Errorf("Apps.FindRepositoryInstallation returned error: %v", err)
}
want := &Installation{ID: Int64(1), AppID: Int64(1), TargetID: Int64(1), TargetType: String("Organization")}
if !reflect.DeepEqual(installation, want) {
t.Errorf("Apps.FindRepositoryInstallation returned %+v, want %+v", installation, want)
}
}
func TestAppsService_FindRepositoryInstallationByID(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/repositories/1/installation", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testHeader(t, r, "Accept", mediaTypeIntegrationPreview)
fmt.Fprint(w, `{"id":1, "app_id":1, "target_id":1, "target_type": "Organization"}`)
})
installation, _, err := client.Apps.FindRepositoryInstallationByID(context.Background(), 1)
if err != nil {
t.Errorf("Apps.FindRepositoryInstallationByID returned error: %v", err)
}
want := &Installation{ID: Int64(1), AppID: Int64(1), TargetID: Int64(1), TargetType: String("Organization")}
if !reflect.DeepEqual(installation, want) {
t.Errorf("Apps.FindRepositoryInstallationByID returned %+v, want %+v", installation, want)
}
}
func TestAppsService_FindUserInstallation(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/users/u/installation", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testHeader(t, r, "Accept", mediaTypeIntegrationPreview)
fmt.Fprint(w, `{"id":1, "app_id":1, "target_id":1, "target_type": "User"}`)
})
installation, _, err := client.Apps.FindUserInstallation(context.Background(), "u")
if err != nil {
t.Errorf("Apps.FindUserInstallation returned error: %v", err)
}
want := &Installation{ID: Int64(1), AppID: Int64(1), TargetID: Int64(1), TargetType: String("User")}
if !reflect.DeepEqual(installation, want) {
t.Errorf("Apps.FindUserInstallation returned %+v, want %+v", installation, want)
}
}