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
6package github
7
8import (
9	"context"
10	"encoding/json"
11	"fmt"
12	"net/http"
13	"reflect"
14	"testing"
15)
16
17func TestMigrationService_StartImport(t *testing.T) {
18	client, mux, _, teardown := setup()
19	defer teardown()
20
21	input := &Import{
22		VCS:         String("git"),
23		VCSURL:      String("url"),
24		VCSUsername: String("u"),
25		VCSPassword: String("p"),
26	}
27
28	mux.HandleFunc("/repos/o/r/import", func(w http.ResponseWriter, r *http.Request) {
29		v := new(Import)
30		json.NewDecoder(r.Body).Decode(v)
31
32		testMethod(t, r, "PUT")
33		testHeader(t, r, "Accept", mediaTypeImportPreview)
34		if !reflect.DeepEqual(v, input) {
35			t.Errorf("Request body = %+v, want %+v", v, input)
36		}
37
38		w.WriteHeader(http.StatusCreated)
39		fmt.Fprint(w, `{"status":"importing"}`)
40	})
41
42	got, _, err := client.Migrations.StartImport(context.Background(), "o", "r", input)
43	if err != nil {
44		t.Errorf("StartImport returned error: %v", err)
45	}
46	want := &Import{Status: String("importing")}
47	if !reflect.DeepEqual(got, want) {
48		t.Errorf("StartImport = %+v, want %+v", got, want)
49	}
50}
51
52func TestMigrationService_ImportProgress(t *testing.T) {
53	client, mux, _, teardown := setup()
54	defer teardown()
55
56	mux.HandleFunc("/repos/o/r/import", func(w http.ResponseWriter, r *http.Request) {
57		testMethod(t, r, "GET")
58		testHeader(t, r, "Accept", mediaTypeImportPreview)
59		fmt.Fprint(w, `{"status":"complete"}`)
60	})
61
62	got, _, err := client.Migrations.ImportProgress(context.Background(), "o", "r")
63	if err != nil {
64		t.Errorf("ImportProgress returned error: %v", err)
65	}
66	want := &Import{Status: String("complete")}
67	if !reflect.DeepEqual(got, want) {
68		t.Errorf("ImportProgress = %+v, want %+v", got, want)
69	}
70}
71
72func TestMigrationService_UpdateImport(t *testing.T) {
73	client, mux, _, teardown := setup()
74	defer teardown()
75
76	input := &Import{
77		VCS:         String("git"),
78		VCSURL:      String("url"),
79		VCSUsername: String("u"),
80		VCSPassword: String("p"),
81	}
82
83	mux.HandleFunc("/repos/o/r/import", func(w http.ResponseWriter, r *http.Request) {
84		v := new(Import)
85		json.NewDecoder(r.Body).Decode(v)
86
87		testMethod(t, r, "PATCH")
88		testHeader(t, r, "Accept", mediaTypeImportPreview)
89		if !reflect.DeepEqual(v, input) {
90			t.Errorf("Request body = %+v, want %+v", v, input)
91		}
92
93		w.WriteHeader(http.StatusCreated)
94		fmt.Fprint(w, `{"status":"importing"}`)
95	})
96
97	got, _, err := client.Migrations.UpdateImport(context.Background(), "o", "r", input)
98	if err != nil {
99		t.Errorf("UpdateImport returned error: %v", err)
100	}
101	want := &Import{Status: String("importing")}
102	if !reflect.DeepEqual(got, want) {
103		t.Errorf("UpdateImport = %+v, want %+v", got, want)
104	}
105}
106
107func TestMigrationService_CommitAuthors(t *testing.T) {
108	client, mux, _, teardown := setup()
109	defer teardown()
110
111	mux.HandleFunc("/repos/o/r/import/authors", func(w http.ResponseWriter, r *http.Request) {
112		testMethod(t, r, "GET")
113		testHeader(t, r, "Accept", mediaTypeImportPreview)
114		fmt.Fprint(w, `[{"id":1,"name":"a"},{"id":2,"name":"b"}]`)
115	})
116
117	got, _, err := client.Migrations.CommitAuthors(context.Background(), "o", "r")
118	if err != nil {
119		t.Errorf("CommitAuthors returned error: %v", err)
120	}
121	want := []*SourceImportAuthor{
122		{ID: Int64(1), Name: String("a")},
123		{ID: Int64(2), Name: String("b")},
124	}
125	if !reflect.DeepEqual(got, want) {
126		t.Errorf("CommitAuthors = %+v, want %+v", got, want)
127	}
128}
129
130func TestMigrationService_MapCommitAuthor(t *testing.T) {
131	client, mux, _, teardown := setup()
132	defer teardown()
133
134	input := &SourceImportAuthor{Name: String("n"), Email: String("e")}
135
136	mux.HandleFunc("/repos/o/r/import/authors/1", func(w http.ResponseWriter, r *http.Request) {
137		v := new(SourceImportAuthor)
138		json.NewDecoder(r.Body).Decode(v)
139
140		testMethod(t, r, "PATCH")
141		testHeader(t, r, "Accept", mediaTypeImportPreview)
142		if !reflect.DeepEqual(v, input) {
143			t.Errorf("Request body = %+v, want %+v", v, input)
144		}
145
146		fmt.Fprint(w, `{"id": 1}`)
147	})
148
149	got, _, err := client.Migrations.MapCommitAuthor(context.Background(), "o", "r", 1, input)
150	if err != nil {
151		t.Errorf("MapCommitAuthor returned error: %v", err)
152	}
153	want := &SourceImportAuthor{ID: Int64(1)}
154	if !reflect.DeepEqual(got, want) {
155		t.Errorf("MapCommitAuthor = %+v, want %+v", got, want)
156	}
157}
158
159func TestMigrationService_SetLFSPreference(t *testing.T) {
160	client, mux, _, teardown := setup()
161	defer teardown()
162
163	input := &Import{UseLFS: String("opt_in")}
164
165	mux.HandleFunc("/repos/o/r/import/lfs", func(w http.ResponseWriter, r *http.Request) {
166		v := new(Import)
167		json.NewDecoder(r.Body).Decode(v)
168
169		testMethod(t, r, "PATCH")
170		testHeader(t, r, "Accept", mediaTypeImportPreview)
171		if !reflect.DeepEqual(v, input) {
172			t.Errorf("Request body = %+v, want %+v", v, input)
173		}
174
175		w.WriteHeader(http.StatusCreated)
176		fmt.Fprint(w, `{"status":"importing"}`)
177	})
178
179	got, _, err := client.Migrations.SetLFSPreference(context.Background(), "o", "r", input)
180	if err != nil {
181		t.Errorf("SetLFSPreference returned error: %v", err)
182	}
183	want := &Import{Status: String("importing")}
184	if !reflect.DeepEqual(got, want) {
185		t.Errorf("SetLFSPreference = %+v, want %+v", got, want)
186	}
187}
188
189func TestMigrationService_LargeFiles(t *testing.T) {
190	client, mux, _, teardown := setup()
191	defer teardown()
192
193	mux.HandleFunc("/repos/o/r/import/large_files", func(w http.ResponseWriter, r *http.Request) {
194		testMethod(t, r, "GET")
195		testHeader(t, r, "Accept", mediaTypeImportPreview)
196		fmt.Fprint(w, `[{"oid":"a"},{"oid":"b"}]`)
197	})
198
199	got, _, err := client.Migrations.LargeFiles(context.Background(), "o", "r")
200	if err != nil {
201		t.Errorf("LargeFiles returned error: %v", err)
202	}
203	want := []*LargeFile{
204		{OID: String("a")},
205		{OID: String("b")},
206	}
207	if !reflect.DeepEqual(got, want) {
208		t.Errorf("LargeFiles = %+v, want %+v", got, want)
209	}
210}
211
212func TestMigrationService_CancelImport(t *testing.T) {
213	client, mux, _, teardown := setup()
214	defer teardown()
215
216	mux.HandleFunc("/repos/o/r/import", func(w http.ResponseWriter, r *http.Request) {
217		testMethod(t, r, "DELETE")
218		testHeader(t, r, "Accept", mediaTypeImportPreview)
219		w.WriteHeader(http.StatusNoContent)
220	})
221
222	_, err := client.Migrations.CancelImport(context.Background(), "o", "r")
223	if err != nil {
224		t.Errorf("CancelImport returned error: %v", err)
225	}
226}
227