1
2
3
4
5
6 package github
7
8 import (
9 "context"
10 "fmt"
11 "net/http"
12 "strings"
13 "testing"
14
15 "github.com/google/go-cmp/cmp"
16 )
17
18 func TestMigrationService_StartUserMigration(t *testing.T) {
19 client, mux, _, teardown := setup()
20 defer teardown()
21
22 mux.HandleFunc("/user/migrations", func(w http.ResponseWriter, r *http.Request) {
23 testMethod(t, r, "POST")
24 testHeader(t, r, "Accept", mediaTypeMigrationsPreview)
25
26 w.WriteHeader(http.StatusCreated)
27 w.Write(userMigrationJSON)
28 })
29
30 opt := &UserMigrationOptions{
31 LockRepositories: true,
32 ExcludeAttachments: false,
33 }
34
35 ctx := context.Background()
36 got, _, err := client.Migrations.StartUserMigration(ctx, []string{"r"}, opt)
37 if err != nil {
38 t.Errorf("StartUserMigration returned error: %v", err)
39 }
40
41 want := wantUserMigration
42 if !cmp.Equal(want, got) {
43 t.Errorf("StartUserMigration = %v, want = %v", got, want)
44 }
45
46 const methodName = "StartUserMigration"
47 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
48 got, resp, err := client.Migrations.StartUserMigration(ctx, []string{"r"}, opt)
49 if got != nil {
50 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
51 }
52 return resp, err
53 })
54 }
55
56 func TestMigrationService_ListUserMigrations(t *testing.T) {
57 client, mux, _, teardown := setup()
58 defer teardown()
59
60 mux.HandleFunc("/user/migrations", func(w http.ResponseWriter, r *http.Request) {
61 testMethod(t, r, "GET")
62 testHeader(t, r, "Accept", mediaTypeMigrationsPreview)
63
64 w.WriteHeader(http.StatusOK)
65 w.Write([]byte(fmt.Sprintf("[%s]", userMigrationJSON)))
66 })
67
68 ctx := context.Background()
69 got, _, err := client.Migrations.ListUserMigrations(ctx, &ListOptions{Page: 1, PerPage: 2})
70 if err != nil {
71 t.Errorf("ListUserMigrations returned error %v", err)
72 }
73
74 want := []*UserMigration{wantUserMigration}
75 if !cmp.Equal(want, got) {
76 t.Errorf("ListUserMigrations = %v, want = %v", got, want)
77 }
78
79 const methodName = "ListUserMigrations"
80 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
81 got, resp, err := client.Migrations.ListUserMigrations(ctx, &ListOptions{Page: 1, PerPage: 2})
82 if got != nil {
83 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
84 }
85 return resp, err
86 })
87 }
88
89 func TestMigrationService_UserMigrationStatus(t *testing.T) {
90 client, mux, _, teardown := setup()
91 defer teardown()
92
93 mux.HandleFunc("/user/migrations/1", func(w http.ResponseWriter, r *http.Request) {
94 testMethod(t, r, "GET")
95 testHeader(t, r, "Accept", mediaTypeMigrationsPreview)
96
97 w.WriteHeader(http.StatusOK)
98 w.Write(userMigrationJSON)
99 })
100
101 ctx := context.Background()
102 got, _, err := client.Migrations.UserMigrationStatus(ctx, 1)
103 if err != nil {
104 t.Errorf("UserMigrationStatus returned error %v", err)
105 }
106
107 want := wantUserMigration
108 if !cmp.Equal(want, got) {
109 t.Errorf("UserMigrationStatus = %v, want = %v", got, want)
110 }
111
112 const methodName = "UserMigrationStatus"
113 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
114 got, resp, err := client.Migrations.UserMigrationStatus(ctx, 1)
115 if got != nil {
116 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
117 }
118 return resp, err
119 })
120 }
121
122 func TestMigrationService_UserMigrationArchiveURL(t *testing.T) {
123 client, mux, _, teardown := setup()
124 defer teardown()
125
126 mux.HandleFunc("/user/migrations/1/archive", func(w http.ResponseWriter, r *http.Request) {
127 testMethod(t, r, "GET")
128 testHeader(t, r, "Accept", mediaTypeMigrationsPreview)
129
130 http.Redirect(w, r, "/go-github", http.StatusFound)
131 })
132
133 mux.HandleFunc("/go-github", func(w http.ResponseWriter, r *http.Request) {
134 testMethod(t, r, "GET")
135
136 w.WriteHeader(http.StatusOK)
137 })
138
139 ctx := context.Background()
140 got, err := client.Migrations.UserMigrationArchiveURL(ctx, 1)
141 if err != nil {
142 t.Errorf("UserMigrationArchiveURL returned error %v", err)
143 }
144
145 want := "/go-github"
146 if !strings.HasSuffix(got, want) {
147 t.Errorf("UserMigrationArchiveURL = %v, want = %v", got, want)
148 }
149 }
150
151 func TestMigrationService_DeleteUserMigration(t *testing.T) {
152 client, mux, _, teardown := setup()
153 defer teardown()
154
155 mux.HandleFunc("/user/migrations/1/archive", func(w http.ResponseWriter, r *http.Request) {
156 testMethod(t, r, "DELETE")
157 testHeader(t, r, "Accept", mediaTypeMigrationsPreview)
158
159 w.WriteHeader(http.StatusNoContent)
160 })
161
162 ctx := context.Background()
163 got, err := client.Migrations.DeleteUserMigration(ctx, 1)
164 if err != nil {
165 t.Errorf("DeleteUserMigration returned error %v", err)
166 }
167
168 if got.StatusCode != http.StatusNoContent {
169 t.Errorf("DeleteUserMigration returned status = %v, want = %v", got.StatusCode, http.StatusNoContent)
170 }
171
172 const methodName = "DeleteUserMigration"
173 testBadOptions(t, methodName, func() (err error) {
174 _, err = client.Migrations.DeleteUserMigration(ctx, -1)
175 return err
176 })
177
178 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
179 return client.Migrations.DeleteUserMigration(ctx, 1)
180 })
181 }
182
183 func TestMigrationService_UnlockUserRepo(t *testing.T) {
184 client, mux, _, teardown := setup()
185 defer teardown()
186
187 mux.HandleFunc("/user/migrations/1/repos/r/lock", func(w http.ResponseWriter, r *http.Request) {
188 testMethod(t, r, "DELETE")
189 testHeader(t, r, "Accept", mediaTypeMigrationsPreview)
190
191 w.WriteHeader(http.StatusNoContent)
192 })
193
194 ctx := context.Background()
195 got, err := client.Migrations.UnlockUserRepo(ctx, 1, "r")
196 if err != nil {
197 t.Errorf("UnlockUserRepo returned error %v", err)
198 }
199
200 if got.StatusCode != http.StatusNoContent {
201 t.Errorf("UnlockUserRepo returned status = %v, want = %v", got.StatusCode, http.StatusNoContent)
202 }
203
204 const methodName = "UnlockUserRepo"
205 testBadOptions(t, methodName, func() (err error) {
206 _, err = client.Migrations.UnlockUserRepo(ctx, -1, "\n")
207 return err
208 })
209
210 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
211 return client.Migrations.UnlockUserRepo(ctx, 1, "r")
212 })
213 }
214
215 var userMigrationJSON = []byte(`{
216 "id": 79,
217 "guid": "0b989ba4-242f-11e5-81e1-c7b6966d2516",
218 "state": "pending",
219 "lock_repositories": true,
220 "exclude_attachments": false,
221 "url": "https://api.github.com/orgs/octo-org/migrations/79",
222 "created_at": "2015-07-06T15:33:38-07:00",
223 "updated_at": "2015-07-06T15:33:38-07:00",
224 "repositories": [
225 {
226 "id": 1296269,
227 "name": "Hello-World",
228 "full_name": "octocat/Hello-World",
229 "description": "This your first repo!"
230 }
231 ]
232 }`)
233
234 var wantUserMigration = &UserMigration{
235 ID: Int64(79),
236 GUID: String("0b989ba4-242f-11e5-81e1-c7b6966d2516"),
237 State: String("pending"),
238 LockRepositories: Bool(true),
239 ExcludeAttachments: Bool(false),
240 URL: String("https://api.github.com/orgs/octo-org/migrations/79"),
241 CreatedAt: String("2015-07-06T15:33:38-07:00"),
242 UpdatedAt: String("2015-07-06T15:33:38-07:00"),
243 Repositories: []*Repository{
244 {
245 ID: Int64(1296269),
246 Name: String("Hello-World"),
247 FullName: String("octocat/Hello-World"),
248 Description: String("This your first repo!"),
249 },
250 },
251 }
252
253 func TestUserMigration_Marshal(t *testing.T) {
254 testJSONMarshal(t, &UserMigration{}, "{}")
255
256 u := &UserMigration{
257 ID: Int64(1),
258 GUID: String("guid"),
259 State: String("state"),
260 LockRepositories: Bool(false),
261 ExcludeAttachments: Bool(false),
262 URL: String("url"),
263 CreatedAt: String("ca"),
264 UpdatedAt: String("ua"),
265 Repositories: []*Repository{{ID: Int64(1)}},
266 }
267
268 want := `{
269 "id": 1,
270 "guid": "guid",
271 "state": "state",
272 "lock_repositories": false,
273 "exclude_attachments": false,
274 "url": "url",
275 "created_at": "ca",
276 "updated_at": "ua",
277 "repositories": [
278 {
279 "id": 1
280 }
281 ]
282 }`
283
284 testJSONMarshal(t, u, want)
285 }
286
287 func TestStartUserMigration_Marshal(t *testing.T) {
288 testJSONMarshal(t, &startUserMigration{}, "{}")
289
290 u := &startUserMigration{
291 Repositories: []string{"r"},
292 LockRepositories: Bool(false),
293 ExcludeAttachments: Bool(false),
294 }
295
296 want := `{
297 "repositories": [
298 "r"
299 ],
300 "lock_repositories": false,
301 "exclude_attachments": false
302 }`
303
304 testJSONMarshal(t, u, want)
305 }
306
View as plain text