github.com/google/go-github/v69@v69.2.0/github/migrations_test.go (about) 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 t.Parallel() 20 client, mux, _ := setup(t) 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 t.Parallel() 60 client, mux, _ := setup(t) 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 t.Parallel() 96 client, mux, _ := setup(t) 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 t.Parallel() 132 client, mux, _ := setup(t) 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 t.Parallel() 165 client, mux, _ := setup(t) 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 t.Parallel() 192 client, mux, _ := setup(t) 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: Ptr(int64(79)), 238 GUID: Ptr("0b989ba4-242f-11e5-81e1-c7b6966d2516"), 239 State: Ptr("pending"), 240 LockRepositories: Ptr(true), 241 ExcludeAttachments: Ptr(false), 242 URL: Ptr("https://api.github.com/orgs/octo-org/migrations/79"), 243 CreatedAt: Ptr("2015-07-06T15:33:38-07:00"), 244 UpdatedAt: Ptr("2015-07-06T15:33:38-07:00"), 245 Repositories: []*Repository{ 246 { 247 ID: Ptr(int64(1296269)), 248 Name: Ptr("Hello-World"), 249 FullName: Ptr("octocat/Hello-World"), 250 Description: Ptr("This your first repo!"), 251 }, 252 }, 253 } 254 255 func TestMigration_Marshal(t *testing.T) { 256 t.Parallel() 257 testJSONMarshal(t, &Migration{}, "{}") 258 259 u := &Migration{ 260 ID: Ptr(int64(1)), 261 GUID: Ptr("guid"), 262 State: Ptr("state"), 263 LockRepositories: Ptr(false), 264 ExcludeAttachments: Ptr(false), 265 URL: Ptr("url"), 266 CreatedAt: Ptr("ca"), 267 UpdatedAt: Ptr("ua"), 268 Repositories: []*Repository{{ID: Ptr(int64(1))}}, 269 } 270 271 want := `{ 272 "id": 1, 273 "guid": "guid", 274 "state": "state", 275 "lock_repositories": false, 276 "exclude_attachments": false, 277 "url": "url", 278 "created_at": "ca", 279 "updated_at": "ua", 280 "repositories": [ 281 { 282 "id": 1 283 } 284 ] 285 }` 286 287 testJSONMarshal(t, u, want) 288 } 289 290 func TestStartMigration_Marshal(t *testing.T) { 291 t.Parallel() 292 testJSONMarshal(t, &startMigration{}, "{}") 293 294 u := &startMigration{ 295 Repositories: []string{"r"}, 296 LockRepositories: Ptr(false), 297 ExcludeAttachments: Ptr(false), 298 } 299 300 want := `{ 301 "repositories": [ 302 "r" 303 ], 304 "lock_repositories": false, 305 "exclude_attachments": false 306 }` 307 308 testJSONMarshal(t, u, want) 309 }