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 6package github 7 8import ( 9 "context" 10 "fmt" 11 "net/http" 12 "reflect" 13 "strings" 14 "testing" 15) 16 17func TestMigrationService_StartUserMigration(t *testing.T) { 18 client, mux, _, teardown := setup() 19 defer teardown() 20 21 mux.HandleFunc("/user/migrations", func(w http.ResponseWriter, r *http.Request) { 22 testMethod(t, r, "POST") 23 testHeader(t, r, "Accept", mediaTypeMigrationsPreview) 24 25 w.WriteHeader(http.StatusCreated) 26 w.Write(userMigrationJSON) 27 }) 28 29 opt := &UserMigrationOptions{ 30 LockRepositories: true, 31 ExcludeAttachments: false, 32 } 33 34 got, _, err := client.Migrations.StartUserMigration(context.Background(), []string{"r"}, opt) 35 if err != nil { 36 t.Errorf("StartUserMigration returned error: %v", err) 37 } 38 39 want := wantUserMigration 40 if !reflect.DeepEqual(want, got) { 41 t.Errorf("StartUserMigration = %v, want = %v", got, want) 42 } 43} 44 45func TestMigrationService_ListUserMigrations(t *testing.T) { 46 client, mux, _, teardown := setup() 47 defer teardown() 48 49 mux.HandleFunc("/user/migrations", func(w http.ResponseWriter, r *http.Request) { 50 testMethod(t, r, "GET") 51 testHeader(t, r, "Accept", mediaTypeMigrationsPreview) 52 53 w.WriteHeader(http.StatusOK) 54 w.Write([]byte(fmt.Sprintf("[%s]", userMigrationJSON))) 55 }) 56 57 got, _, err := client.Migrations.ListUserMigrations(context.Background()) 58 if err != nil { 59 t.Errorf("ListUserMigrations returned error %v", err) 60 } 61 62 want := []*UserMigration{wantUserMigration} 63 if !reflect.DeepEqual(want, got) { 64 t.Errorf("ListUserMigrations = %v, want = %v", got, want) 65 } 66} 67 68func TestMigrationService_UserMigrationStatus(t *testing.T) { 69 client, mux, _, teardown := setup() 70 defer teardown() 71 72 mux.HandleFunc("/user/migrations/1", func(w http.ResponseWriter, r *http.Request) { 73 testMethod(t, r, "GET") 74 testHeader(t, r, "Accept", mediaTypeMigrationsPreview) 75 76 w.WriteHeader(http.StatusOK) 77 w.Write(userMigrationJSON) 78 }) 79 80 got, _, err := client.Migrations.UserMigrationStatus(context.Background(), 1) 81 if err != nil { 82 t.Errorf("UserMigrationStatus returned error %v", err) 83 } 84 85 want := wantUserMigration 86 if !reflect.DeepEqual(want, got) { 87 t.Errorf("UserMigrationStatus = %v, want = %v", got, want) 88 } 89} 90 91func TestMigrationService_UserMigrationArchiveURL(t *testing.T) { 92 client, mux, _, teardown := setup() 93 defer teardown() 94 95 mux.HandleFunc("/user/migrations/1/archive", func(w http.ResponseWriter, r *http.Request) { 96 testMethod(t, r, "GET") 97 testHeader(t, r, "Accept", mediaTypeMigrationsPreview) 98 99 http.Redirect(w, r, "/go-github", http.StatusFound) 100 }) 101 102 mux.HandleFunc("/go-github", func(w http.ResponseWriter, r *http.Request) { 103 testMethod(t, r, "GET") 104 105 w.WriteHeader(http.StatusOK) 106 }) 107 108 got, err := client.Migrations.UserMigrationArchiveURL(context.Background(), 1) 109 if err != nil { 110 t.Errorf("UserMigrationArchiveURL returned error %v", err) 111 } 112 113 want := "/go-github" 114 if !strings.HasSuffix(got, want) { 115 t.Errorf("UserMigrationArchiveURL = %v, want = %v", got, want) 116 } 117} 118 119func TestMigrationService_DeleteUserMigration(t *testing.T) { 120 client, mux, _, teardown := setup() 121 defer teardown() 122 123 mux.HandleFunc("/user/migrations/1/archive", func(w http.ResponseWriter, r *http.Request) { 124 testMethod(t, r, "DELETE") 125 testHeader(t, r, "Accept", mediaTypeMigrationsPreview) 126 127 w.WriteHeader(http.StatusNoContent) 128 }) 129 130 got, err := client.Migrations.DeleteUserMigration(context.Background(), 1) 131 if err != nil { 132 t.Errorf("DeleteUserMigration returned error %v", err) 133 } 134 135 if got.StatusCode != http.StatusNoContent { 136 t.Errorf("DeleteUserMigration returned status = %v, want = %v", got.StatusCode, http.StatusNoContent) 137 } 138} 139 140func TestMigrationService_UnlockUserRepo(t *testing.T) { 141 client, mux, _, teardown := setup() 142 defer teardown() 143 144 mux.HandleFunc("/user/migrations/1/repos/r/lock", func(w http.ResponseWriter, r *http.Request) { 145 testMethod(t, r, "DELETE") 146 testHeader(t, r, "Accept", mediaTypeMigrationsPreview) 147 148 w.WriteHeader(http.StatusNoContent) 149 }) 150 151 got, err := client.Migrations.UnlockUserRepo(context.Background(), 1, "r") 152 if err != nil { 153 t.Errorf("UnlockUserRepo returned error %v", err) 154 } 155 156 if got.StatusCode != http.StatusNoContent { 157 t.Errorf("UnlockUserRepo returned status = %v, want = %v", got.StatusCode, http.StatusNoContent) 158 } 159} 160 161var userMigrationJSON = []byte(`{ 162 "id": 79, 163 "guid": "0b989ba4-242f-11e5-81e1-c7b6966d2516", 164 "state": "pending", 165 "lock_repositories": true, 166 "exclude_attachments": false, 167 "url": "https://api.github.com/orgs/octo-org/migrations/79", 168 "created_at": "2015-07-06T15:33:38-07:00", 169 "updated_at": "2015-07-06T15:33:38-07:00", 170 "repositories": [ 171 { 172 "id": 1296269, 173 "name": "Hello-World", 174 "full_name": "octocat/Hello-World", 175 "description": "This your first repo!" 176 } 177 ] 178}`) 179 180var wantUserMigration = &UserMigration{ 181 ID: Int64(79), 182 GUID: String("0b989ba4-242f-11e5-81e1-c7b6966d2516"), 183 State: String("pending"), 184 LockRepositories: Bool(true), 185 ExcludeAttachments: Bool(false), 186 URL: String("https://api.github.com/orgs/octo-org/migrations/79"), 187 CreatedAt: String("2015-07-06T15:33:38-07:00"), 188 UpdatedAt: String("2015-07-06T15:33:38-07:00"), 189 Repositories: []*Repository{ 190 { 191 ID: Int64(1296269), 192 Name: String("Hello-World"), 193 FullName: String("octocat/Hello-World"), 194 Description: String("This your first repo!"), 195 }, 196 }, 197} 198