1// Copyright 2013 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	"time"
16)
17
18func TestPullComments_marshall(t *testing.T) {
19	testJSONMarshal(t, &PullRequestComment{}, "{}")
20
21	createdAt := time.Date(2002, time.February, 10, 15, 30, 0, 0, time.UTC)
22	updatedAt := time.Date(2002, time.February, 10, 15, 30, 0, 0, time.UTC)
23	reactions := &Reactions{
24		TotalCount: Int(1),
25		PlusOne:    Int(1),
26		MinusOne:   Int(0),
27		Laugh:      Int(0),
28		Confused:   Int(0),
29		Heart:      Int(0),
30		Hooray:     Int(0),
31		URL:        String("u"),
32	}
33
34	u := &PullRequestComment{
35		ID:                  Int64(10),
36		InReplyTo:           Int64(8),
37		Body:                String("Test comment"),
38		Path:                String("file1.txt"),
39		DiffHunk:            String("@@ -16,33 +16,40 @@ fmt.Println()"),
40		PullRequestReviewID: Int64(42),
41		Position:            Int(1),
42		OriginalPosition:    Int(4),
43		CommitID:            String("ab"),
44		OriginalCommitID:    String("9c"),
45		User: &User{
46			Login:       String("ll"),
47			ID:          Int64(123),
48			AvatarURL:   String("a"),
49			GravatarID:  String("g"),
50			Name:        String("n"),
51			Company:     String("c"),
52			Blog:        String("b"),
53			Location:    String("l"),
54			Email:       String("e"),
55			Hireable:    Bool(true),
56			PublicRepos: Int(1),
57			Followers:   Int(1),
58			Following:   Int(1),
59			CreatedAt:   &Timestamp{referenceTime},
60			URL:         String("u"),
61		},
62		Reactions:      reactions,
63		CreatedAt:      &createdAt,
64		UpdatedAt:      &updatedAt,
65		URL:            String("pullrequestcommentUrl"),
66		HTMLURL:        String("pullrequestcommentHTMLUrl"),
67		PullRequestURL: String("pullrequestcommentPullRequestURL"),
68	}
69
70	want := `{
71		"id": 10,
72		"in_reply_to_id": 8,
73		"body": "Test comment",
74		"path": "file1.txt",
75		"diff_hunk": "@@ -16,33 +16,40 @@ fmt.Println()",
76		"pull_request_review_id": 42,
77		"position": 1,
78		"original_position": 4,
79		"commit_id": "ab",
80		"original_commit_id": "9c",
81		"user": {
82			"login": "ll",
83			"id": 123,
84			"avatar_url": "a",
85			"gravatar_id": "g",
86			"name": "n",
87			"company": "c",
88			"blog": "b",
89			"location": "l",
90			"email": "e",
91			"hireable": true,
92			"public_repos": 1,
93			"followers": 1,
94			"following": 1,
95			"created_at": ` + referenceTimeStr + `,
96			"url": "u"
97		},
98		"reactions": {
99			"total_count": 1,
100			"+1": 1,
101			"-1": 0,
102			"laugh": 0,
103			"confused": 0,
104			"heart": 0,
105			"hooray": 0,
106			"url": "u"
107		},
108		"created_at": "2002-02-10T15:30:00Z",
109		"updated_at": "2002-02-10T15:30:00Z",
110		"url": "pullrequestcommentUrl",
111		"html_url": "pullrequestcommentHTMLUrl",
112		"pull_request_url": "pullrequestcommentPullRequestURL"
113	}`
114
115	testJSONMarshal(t, u, want)
116}
117
118func TestPullRequestsService_ListComments_allPulls(t *testing.T) {
119	client, mux, _, teardown := setup()
120	defer teardown()
121
122	mux.HandleFunc("/repos/o/r/pulls/comments", func(w http.ResponseWriter, r *http.Request) {
123		testMethod(t, r, "GET")
124		testHeader(t, r, "Accept", mediaTypeReactionsPreview)
125		testFormValues(t, r, values{
126			"sort":      "updated",
127			"direction": "desc",
128			"since":     "2002-02-10T15:30:00Z",
129			"page":      "2",
130		})
131		fmt.Fprint(w, `[{"id":1}]`)
132	})
133
134	opt := &PullRequestListCommentsOptions{
135		Sort:        "updated",
136		Direction:   "desc",
137		Since:       time.Date(2002, time.February, 10, 15, 30, 0, 0, time.UTC),
138		ListOptions: ListOptions{Page: 2},
139	}
140	pulls, _, err := client.PullRequests.ListComments(context.Background(), "o", "r", 0, opt)
141	if err != nil {
142		t.Errorf("PullRequests.ListComments returned error: %v", err)
143	}
144
145	want := []*PullRequestComment{{ID: Int64(1)}}
146	if !reflect.DeepEqual(pulls, want) {
147		t.Errorf("PullRequests.ListComments returned %+v, want %+v", pulls, want)
148	}
149}
150
151func TestPullRequestsService_ListComments_specificPull(t *testing.T) {
152	client, mux, _, teardown := setup()
153	defer teardown()
154
155	mux.HandleFunc("/repos/o/r/pulls/1/comments", func(w http.ResponseWriter, r *http.Request) {
156		testMethod(t, r, "GET")
157		testHeader(t, r, "Accept", mediaTypeReactionsPreview)
158		fmt.Fprint(w, `[{"id":1, "pull_request_review_id":42}]`)
159	})
160
161	pulls, _, err := client.PullRequests.ListComments(context.Background(), "o", "r", 1, nil)
162	if err != nil {
163		t.Errorf("PullRequests.ListComments returned error: %v", err)
164	}
165
166	want := []*PullRequestComment{{ID: Int64(1), PullRequestReviewID: Int64(42)}}
167	if !reflect.DeepEqual(pulls, want) {
168		t.Errorf("PullRequests.ListComments returned %+v, want %+v", pulls, want)
169	}
170}
171
172func TestPullRequestsService_ListComments_invalidOwner(t *testing.T) {
173	client, _, _, teardown := setup()
174	defer teardown()
175
176	_, _, err := client.PullRequests.ListComments(context.Background(), "%", "r", 1, nil)
177	testURLParseError(t, err)
178}
179
180func TestPullRequestsService_GetComment(t *testing.T) {
181	client, mux, _, teardown := setup()
182	defer teardown()
183
184	mux.HandleFunc("/repos/o/r/pulls/comments/1", func(w http.ResponseWriter, r *http.Request) {
185		testMethod(t, r, "GET")
186		testHeader(t, r, "Accept", mediaTypeReactionsPreview)
187		fmt.Fprint(w, `{"id":1}`)
188	})
189
190	comment, _, err := client.PullRequests.GetComment(context.Background(), "o", "r", 1)
191	if err != nil {
192		t.Errorf("PullRequests.GetComment returned error: %v", err)
193	}
194
195	want := &PullRequestComment{ID: Int64(1)}
196	if !reflect.DeepEqual(comment, want) {
197		t.Errorf("PullRequests.GetComment returned %+v, want %+v", comment, want)
198	}
199}
200
201func TestPullRequestsService_GetComment_invalidOwner(t *testing.T) {
202	client, _, _, teardown := setup()
203	defer teardown()
204
205	_, _, err := client.PullRequests.GetComment(context.Background(), "%", "r", 1)
206	testURLParseError(t, err)
207}
208
209func TestPullRequestsService_CreateComment(t *testing.T) {
210	client, mux, _, teardown := setup()
211	defer teardown()
212
213	input := &PullRequestComment{Body: String("b")}
214
215	mux.HandleFunc("/repos/o/r/pulls/1/comments", func(w http.ResponseWriter, r *http.Request) {
216		v := new(PullRequestComment)
217		json.NewDecoder(r.Body).Decode(v)
218
219		testMethod(t, r, "POST")
220		if !reflect.DeepEqual(v, input) {
221			t.Errorf("Request body = %+v, want %+v", v, input)
222		}
223
224		fmt.Fprint(w, `{"id":1}`)
225	})
226
227	comment, _, err := client.PullRequests.CreateComment(context.Background(), "o", "r", 1, input)
228	if err != nil {
229		t.Errorf("PullRequests.CreateComment returned error: %v", err)
230	}
231
232	want := &PullRequestComment{ID: Int64(1)}
233	if !reflect.DeepEqual(comment, want) {
234		t.Errorf("PullRequests.CreateComment returned %+v, want %+v", comment, want)
235	}
236}
237
238func TestPullRequestsService_CreateComment_invalidOwner(t *testing.T) {
239	client, _, _, teardown := setup()
240	defer teardown()
241
242	_, _, err := client.PullRequests.CreateComment(context.Background(), "%", "r", 1, nil)
243	testURLParseError(t, err)
244}
245
246func TestPullRequestsService_EditComment(t *testing.T) {
247	client, mux, _, teardown := setup()
248	defer teardown()
249
250	input := &PullRequestComment{Body: String("b")}
251
252	mux.HandleFunc("/repos/o/r/pulls/comments/1", func(w http.ResponseWriter, r *http.Request) {
253		v := new(PullRequestComment)
254		json.NewDecoder(r.Body).Decode(v)
255
256		testMethod(t, r, "PATCH")
257		if !reflect.DeepEqual(v, input) {
258			t.Errorf("Request body = %+v, want %+v", v, input)
259		}
260
261		fmt.Fprint(w, `{"id":1}`)
262	})
263
264	comment, _, err := client.PullRequests.EditComment(context.Background(), "o", "r", 1, input)
265	if err != nil {
266		t.Errorf("PullRequests.EditComment returned error: %v", err)
267	}
268
269	want := &PullRequestComment{ID: Int64(1)}
270	if !reflect.DeepEqual(comment, want) {
271		t.Errorf("PullRequests.EditComment returned %+v, want %+v", comment, want)
272	}
273}
274
275func TestPullRequestsService_EditComment_invalidOwner(t *testing.T) {
276	client, _, _, teardown := setup()
277	defer teardown()
278
279	_, _, err := client.PullRequests.EditComment(context.Background(), "%", "r", 1, nil)
280	testURLParseError(t, err)
281}
282
283func TestPullRequestsService_DeleteComment(t *testing.T) {
284	client, mux, _, teardown := setup()
285	defer teardown()
286
287	mux.HandleFunc("/repos/o/r/pulls/comments/1", func(w http.ResponseWriter, r *http.Request) {
288		testMethod(t, r, "DELETE")
289	})
290
291	_, err := client.PullRequests.DeleteComment(context.Background(), "o", "r", 1)
292	if err != nil {
293		t.Errorf("PullRequests.DeleteComment returned error: %v", err)
294	}
295}
296
297func TestPullRequestsService_DeleteComment_invalidOwner(t *testing.T) {
298	client, _, _, teardown := setup()
299	defer teardown()
300
301	_, err := client.PullRequests.DeleteComment(context.Background(), "%", "r", 1)
302	testURLParseError(t, err)
303}
304