github.com/google/go-github/v69@v69.2.0/github/migrations_user_test.go (about) 1 // Copyright 2018 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_StartUserMigration(t *testing.T) { 19 t.Parallel() 20 client, mux, _ := setup(t) 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 assertWrite(t, w, 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 t.Parallel() 58 client, mux, _ := setup(t) 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 assertWrite(t, w, []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 t.Parallel() 91 client, mux, _ := setup(t) 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 assertWrite(t, w, 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 t.Parallel() 124 client, mux, _ := setup(t) 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 t.Parallel() 153 client, mux, _ := setup(t) 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 t.Parallel() 185 client, mux, _ := setup(t) 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: Ptr(int64(79)), 236 GUID: Ptr("0b989ba4-242f-11e5-81e1-c7b6966d2516"), 237 State: Ptr("pending"), 238 LockRepositories: Ptr(true), 239 ExcludeAttachments: Ptr(false), 240 URL: Ptr("https://api.github.com/orgs/octo-org/migrations/79"), 241 CreatedAt: Ptr("2015-07-06T15:33:38-07:00"), 242 UpdatedAt: Ptr("2015-07-06T15:33:38-07:00"), 243 Repositories: []*Repository{ 244 { 245 ID: Ptr(int64(1296269)), 246 Name: Ptr("Hello-World"), 247 FullName: Ptr("octocat/Hello-World"), 248 Description: Ptr("This your first repo!"), 249 }, 250 }, 251 } 252 253 func TestUserMigration_Marshal(t *testing.T) { 254 t.Parallel() 255 testJSONMarshal(t, &UserMigration{}, "{}") 256 257 u := &UserMigration{ 258 ID: Ptr(int64(1)), 259 GUID: Ptr("guid"), 260 State: Ptr("state"), 261 LockRepositories: Ptr(false), 262 ExcludeAttachments: Ptr(false), 263 URL: Ptr("url"), 264 CreatedAt: Ptr("ca"), 265 UpdatedAt: Ptr("ua"), 266 Repositories: []*Repository{{ID: Ptr(int64(1))}}, 267 } 268 269 want := `{ 270 "id": 1, 271 "guid": "guid", 272 "state": "state", 273 "lock_repositories": false, 274 "exclude_attachments": false, 275 "url": "url", 276 "created_at": "ca", 277 "updated_at": "ua", 278 "repositories": [ 279 { 280 "id": 1 281 } 282 ] 283 }` 284 285 testJSONMarshal(t, u, want) 286 } 287 288 func TestStartUserMigration_Marshal(t *testing.T) { 289 t.Parallel() 290 testJSONMarshal(t, &startUserMigration{}, "{}") 291 292 u := &startUserMigration{ 293 Repositories: []string{"r"}, 294 LockRepositories: Ptr(false), 295 ExcludeAttachments: Ptr(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 }