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