| 1 |
// Copyright 2016 The go-github AUTHORS. All rights reserved. |
| 2 |
// |
| 3 |
// Use of this source code is governed by a BSD-style |
| 4 |
// license that can be found in the LICENSE file. |
| 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_StartMigration(t *testing.T) { |
| 19 |
client, mux, _, teardown := setup() |
| 20 |
defer teardown() |
| 21 |
|
| 22 |
mux.HandleFunc("/orgs/o/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 |
assertWrite(t, w, migrationJSON) |
| 28 |
}) |
| 29 |
|
| 30 |
opt := &MigrationOptions{ |
| 31 |
LockRepositories: true, |
| 32 |
ExcludeAttachments: false, |
| 33 |
} |
| 34 |
ctx := context.Background() |
| 35 |
got, _, err := client.Migrations.StartMigration(ctx, "o", []string{"r"}, opt) |
| 36 |
if err != nil { |
| 37 |
t.Errorf("StartMigration returned error: %v", err) |
| 38 |
} |
| 39 |
if want := wantMigration; !cmp.Equal(got, want) { |
| 40 |
t.Errorf("StartMigration = %+v, want %+v", got, want) |
| 41 |
} |
| 42 |
|
| 43 |
const methodName = "StartMigration" |
| 44 |
testBadOptions(t, methodName, func() (err error) { |
| 45 |
_, _, err = client.Migrations.StartMigration(ctx, "\n", []string{"\n"}, opt) |
| 46 |
return err |
| 47 |
}) |
| 48 |
|
| 49 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 50 |
got, resp, err := client.Migrations.StartMigration(ctx, "o", []string{"r"}, opt) |
| 51 |
if got != nil { |
| 52 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 53 |
} |
| 54 |
return resp, err |
| 55 |
}) |
| 56 |
} |
| 57 |
|
| 58 |
func TestMigrationService_ListMigrations(t *testing.T) { |
| 59 |
client, mux, _, teardown := setup() |
| 60 |
defer teardown() |
| 61 |
|
| 62 |
mux.HandleFunc("/orgs/o/migrations", func(w http.ResponseWriter, r *http.Request) { |
| 63 |
testMethod(t, r, "GET") |
| 64 |
testHeader(t, r, "Accept", mediaTypeMigrationsPreview) |
| 65 |
|
| 66 |
w.WriteHeader(http.StatusOK) |
| 67 |
assertWrite(t, w, []byte(fmt.Sprintf("[%s]", migrationJSON))) |
| 68 |
}) |
| 69 |
|
| 70 |
ctx := context.Background() |
| 71 |
got, _, err := client.Migrations.ListMigrations(ctx, "o", &ListOptions{Page: 1, PerPage: 2}) |
| 72 |
if err != nil { |
| 73 |
t.Errorf("ListMigrations returned error: %v", err) |
| 74 |
} |
| 75 |
if want := []*Migration{wantMigration}; !cmp.Equal(got, want) { |
| 76 |
t.Errorf("ListMigrations = %+v, want %+v", got, want) |
| 77 |
} |
| 78 |
|
| 79 |
const methodName = "ListMigrations" |
| 80 |
testBadOptions(t, methodName, func() (err error) { |
| 81 |
_, _, err = client.Migrations.ListMigrations(ctx, "\n", &ListOptions{Page: 1, PerPage: 2}) |
| 82 |
return err |
| 83 |
}) |
| 84 |
|
| 85 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 86 |
got, resp, err := client.Migrations.ListMigrations(ctx, "o", &ListOptions{Page: 1, PerPage: 2}) |
| 87 |
if got != nil { |
| 88 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 89 |
} |
| 90 |
return resp, err |
| 91 |
}) |
| 92 |
} |
| 93 |
|
| 94 |
func TestMigrationService_MigrationStatus(t *testing.T) { |
| 95 |
client, mux, _, teardown := setup() |
| 96 |
defer teardown() |
| 97 |
|
| 98 |
mux.HandleFunc("/orgs/o/migrations/1", func(w http.ResponseWriter, r *http.Request) { |
| 99 |
testMethod(t, r, "GET") |
| 100 |
testHeader(t, r, "Accept", mediaTypeMigrationsPreview) |
| 101 |
|
| 102 |
w.WriteHeader(http.StatusOK) |
| 103 |
assertWrite(t, w, migrationJSON) |
| 104 |
}) |
| 105 |
|
| 106 |
ctx := context.Background() |
| 107 |
got, _, err := client.Migrations.MigrationStatus(ctx, "o", 1) |
| 108 |
if err != nil { |
| 109 |
t.Errorf("MigrationStatus returned error: %v", err) |
| 110 |
} |
| 111 |
if want := wantMigration; !cmp.Equal(got, want) { |
| 112 |
t.Errorf("MigrationStatus = %+v, want %+v", got, want) |
| 113 |
} |
| 114 |
|
| 115 |
const methodName = "MigrationStatus" |
| 116 |
testBadOptions(t, methodName, func() (err error) { |
| 117 |
_, _, err = client.Migrations.MigrationStatus(ctx, "\n", -1) |
| 118 |
return err |
| 119 |
}) |
| 120 |
|
| 121 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 122 |
got, resp, err := client.Migrations.MigrationStatus(ctx, "o", 1) |
| 123 |
if got != nil { |
| 124 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 125 |
} |
| 126 |
return resp, err |
| 127 |
}) |
| 128 |
} |
| 129 |
|
| 130 |
func TestMigrationService_MigrationArchiveURL(t *testing.T) { |
| 131 |
client, mux, _, teardown := setup() |
| 132 |
defer teardown() |
| 133 |
|
| 134 |
mux.HandleFunc("/orgs/o/migrations/1/archive", func(w http.ResponseWriter, r *http.Request) { |
| 135 |
testMethod(t, r, "GET") |
| 136 |
testHeader(t, r, "Accept", mediaTypeMigrationsPreview) |
| 137 |
|
| 138 |
http.Redirect(w, r, "/yo", http.StatusFound) |
| 139 |
}) |
| 140 |
mux.HandleFunc("/yo", func(w http.ResponseWriter, r *http.Request) { |
| 141 |
testMethod(t, r, "GET") |
| 142 |
|
| 143 |
w.WriteHeader(http.StatusOK) |
| 144 |
assertWrite(t, w, []byte("0123456789abcdef")) |
| 145 |
}) |
| 146 |
|
| 147 |
ctx := context.Background() |
| 148 |
got, err := client.Migrations.MigrationArchiveURL(ctx, "o", 1) |
| 149 |
if err != nil { |
| 150 |
t.Errorf("MigrationStatus returned error: %v", err) |
| 151 |
} |
| 152 |
if want := "/yo"; !strings.HasSuffix(got, want) { |
| 153 |
t.Errorf("MigrationArchiveURL = %+v, want %+v", got, want) |
| 154 |
} |
| 155 |
|
| 156 |
const methodName = "MigrationArchiveURL" |
| 157 |
testBadOptions(t, methodName, func() (err error) { |
| 158 |
_, err = client.Migrations.MigrationArchiveURL(ctx, "\n", -1) |
| 159 |
return err |
| 160 |
}) |
| 161 |
} |
| 162 |
|
| 163 |
func TestMigrationService_DeleteMigration(t *testing.T) { |
| 164 |
client, mux, _, teardown := setup() |
| 165 |
defer teardown() |
| 166 |
|
| 167 |
mux.HandleFunc("/orgs/o/migrations/1/archive", func(w http.ResponseWriter, r *http.Request) { |
| 168 |
testMethod(t, r, "DELETE") |
| 169 |
testHeader(t, r, "Accept", mediaTypeMigrationsPreview) |
| 170 |
|
| 171 |
w.WriteHeader(http.StatusNoContent) |
| 172 |
}) |
| 173 |
|
| 174 |
ctx := context.Background() |
| 175 |
if _, err := client.Migrations.DeleteMigration(ctx, "o", 1); err != nil { |
| 176 |
t.Errorf("DeleteMigration returned error: %v", err) |
| 177 |
} |
| 178 |
|
| 179 |
const methodName = "DeleteMigration" |
| 180 |
testBadOptions(t, methodName, func() (err error) { |
| 181 |
_, err = client.Migrations.DeleteMigration(ctx, "\n", -1) |
| 182 |
return err |
| 183 |
}) |
| 184 |
|
| 185 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 186 |
return client.Migrations.DeleteMigration(ctx, "o", 1) |
| 187 |
}) |
| 188 |
} |
| 189 |
|
| 190 |
func TestMigrationService_UnlockRepo(t *testing.T) { |
| 191 |
client, mux, _, teardown := setup() |
| 192 |
defer teardown() |
| 193 |
|
| 194 |
mux.HandleFunc("/orgs/o/migrations/1/repos/r/lock", func(w http.ResponseWriter, r *http.Request) { |
| 195 |
testMethod(t, r, "DELETE") |
| 196 |
testHeader(t, r, "Accept", mediaTypeMigrationsPreview) |
| 197 |
|
| 198 |
w.WriteHeader(http.StatusNoContent) |
| 199 |
}) |
| 200 |
|
| 201 |
ctx := context.Background() |
| 202 |
if _, err := client.Migrations.UnlockRepo(ctx, "o", 1, "r"); err != nil { |
| 203 |
t.Errorf("UnlockRepo returned error: %v", err) |
| 204 |
} |
| 205 |
|
| 206 |
const methodName = "UnlockRepo" |
| 207 |
testBadOptions(t, methodName, func() (err error) { |
| 208 |
_, err = client.Migrations.UnlockRepo(ctx, "\n", -1, "\n") |
| 209 |
return err |
| 210 |
}) |
| 211 |
|
| 212 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 213 |
return client.Migrations.UnlockRepo(ctx, "o", 1, "r") |
| 214 |
}) |
| 215 |
} |
| 216 |
|
| 217 |
var migrationJSON = []byte(`{ |
| 218 |
"id": 79, |
| 219 |
"guid": "0b989ba4-242f-11e5-81e1-c7b6966d2516", |
| 220 |
"state": "pending", |
| 221 |
"lock_repositories": true, |
| 222 |
"exclude_attachments": false, |
| 223 |
"url": "https://api.github.com/orgs/octo-org/migrations/79", |
| 224 |
"created_at": "2015-07-06T15:33:38-07:00", |
| 225 |
"updated_at": "2015-07-06T15:33:38-07:00", |
| 226 |
"repositories": [ |
| 227 |
{ |
| 228 |
"id": 1296269, |
| 229 |
"name": "Hello-World", |
| 230 |
"full_name": "octocat/Hello-World", |
| 231 |
"description": "This your first repo!" |
| 232 |
} |
| 233 |
] |
| 234 |
}`) |
| 235 |
|
| 236 |
var wantMigration = &Migration{ |
| 237 |
ID: Int64(79), |
| 238 |
GUID: String("0b989ba4-242f-11e5-81e1-c7b6966d2516"), |
| 239 |
State: String("pending"), |
| 240 |
LockRepositories: Bool(true), |
| 241 |
ExcludeAttachments: Bool(false), |
| 242 |
URL: String("https://api.github.com/orgs/octo-org/migrations/79"), |
| 243 |
CreatedAt: String("2015-07-06T15:33:38-07:00"), |
| 244 |
UpdatedAt: String("2015-07-06T15:33:38-07:00"), |
| 245 |
Repositories: []*Repository{ |
| 246 |
{ |
| 247 |
ID: Int64(1296269), |
| 248 |
Name: String("Hello-World"), |
| 249 |
FullName: String("octocat/Hello-World"), |
| 250 |
Description: String("This your first repo!"), |
| 251 |
}, |
| 252 |
}, |
| 253 |
} |
| 254 |
|
| 255 |
func TestMigration_Marshal(t *testing.T) { |
| 256 |
testJSONMarshal(t, &Migration{}, "{}") |
| 257 |
|
| 258 |
u := &Migration{ |
| 259 |
ID: Int64(1), |
| 260 |
GUID: String("guid"), |
| 261 |
State: String("state"), |
| 262 |
LockRepositories: Bool(false), |
| 263 |
ExcludeAttachments: Bool(false), |
| 264 |
URL: String("url"), |
| 265 |
CreatedAt: String("ca"), |
| 266 |
UpdatedAt: String("ua"), |
| 267 |
Repositories: []*Repository{{ID: Int64(1)}}, |
| 268 |
} |
| 269 |
|
| 270 |
want := `{ |
| 271 |
"id": 1, |
| 272 |
"guid": "guid", |
| 273 |
"state": "state", |
| 274 |
"lock_repositories": false, |
| 275 |
"exclude_attachments": false, |
| 276 |
"url": "url", |
| 277 |
"created_at": "ca", |
| 278 |
"updated_at": "ua", |
| 279 |
"repositories": [ |
| 280 |
{ |
| 281 |
"id": 1 |
| 282 |
} |
| 283 |
] |
| 284 |
}` |
| 285 |
|
| 286 |
testJSONMarshal(t, u, want) |
| 287 |
} |
| 288 |
|
| 289 |
func TestStartMigration_Marshal(t *testing.T) { |
| 290 |
testJSONMarshal(t, &startMigration{}, "{}") |
| 291 |
|
| 292 |
u := &startMigration{ |
| 293 |
Repositories: []string{"r"}, |
| 294 |
LockRepositories: Bool(false), |
| 295 |
ExcludeAttachments: Bool(false), |
| 296 |
} |
| 297 |
|
| 298 |
want := `{ |
| 299 |
"repositories": [ |
| 300 |
"r" |
| 301 |
], |
| 302 |
"lock_repositories": false, |
| 303 |
"exclude_attachments": false |
| 304 |
}` |
| 305 |
|
| 306 |
testJSONMarshal(t, u, want) |
| 307 |
} |