...

Source file src/github.com/google/go-github/v55/github/pulls_comments_test.go

Documentation: github.com/google/go-github/v55/github

     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  
     6  package github
     7  
     8  import (
     9  	"context"
    10  	"encoding/json"
    11  	"fmt"
    12  	"net/http"
    13  	"strings"
    14  	"testing"
    15  	"time"
    16  
    17  	"github.com/google/go-cmp/cmp"
    18  )
    19  
    20  func TestPullComments_Marshal(t *testing.T) {
    21  	testJSONMarshal(t, &PullRequestComment{}, "{}")
    22  
    23  	createdAt := Timestamp{time.Date(2002, time.February, 10, 15, 30, 0, 0, time.UTC)}
    24  	updatedAt := Timestamp{time.Date(2002, time.February, 10, 15, 30, 0, 0, time.UTC)}
    25  	reactions := &Reactions{
    26  		TotalCount: Int(1),
    27  		PlusOne:    Int(1),
    28  		MinusOne:   Int(0),
    29  		Laugh:      Int(0),
    30  		Confused:   Int(0),
    31  		Heart:      Int(0),
    32  		Hooray:     Int(0),
    33  		Rocket:     Int(0),
    34  		Eyes:       Int(0),
    35  		URL:        String("u"),
    36  	}
    37  
    38  	u := &PullRequestComment{
    39  		ID:                  Int64(10),
    40  		InReplyTo:           Int64(8),
    41  		Body:                String("Test comment"),
    42  		Path:                String("file1.txt"),
    43  		DiffHunk:            String("@@ -16,33 +16,40 @@ fmt.Println()"),
    44  		PullRequestReviewID: Int64(42),
    45  		Position:            Int(1),
    46  		OriginalPosition:    Int(4),
    47  		StartLine:           Int(2),
    48  		Line:                Int(3),
    49  		OriginalLine:        Int(2),
    50  		OriginalStartLine:   Int(2),
    51  		Side:                String("RIGHT"),
    52  		StartSide:           String("LEFT"),
    53  		CommitID:            String("ab"),
    54  		OriginalCommitID:    String("9c"),
    55  		User: &User{
    56  			Login:       String("ll"),
    57  			ID:          Int64(123),
    58  			AvatarURL:   String("a"),
    59  			GravatarID:  String("g"),
    60  			Name:        String("n"),
    61  			Company:     String("c"),
    62  			Blog:        String("b"),
    63  			Location:    String("l"),
    64  			Email:       String("e"),
    65  			Hireable:    Bool(true),
    66  			PublicRepos: Int(1),
    67  			Followers:   Int(1),
    68  			Following:   Int(1),
    69  			CreatedAt:   &Timestamp{referenceTime},
    70  			URL:         String("u"),
    71  		},
    72  		Reactions:      reactions,
    73  		CreatedAt:      &createdAt,
    74  		UpdatedAt:      &updatedAt,
    75  		URL:            String("pullrequestcommentUrl"),
    76  		HTMLURL:        String("pullrequestcommentHTMLUrl"),
    77  		PullRequestURL: String("pullrequestcommentPullRequestURL"),
    78  	}
    79  
    80  	want := `{
    81  		"id": 10,
    82  		"in_reply_to_id": 8,
    83  		"body": "Test comment",
    84  		"path": "file1.txt",
    85  		"diff_hunk": "@@ -16,33 +16,40 @@ fmt.Println()",
    86  		"pull_request_review_id": 42,
    87  		"position": 1,
    88  		"original_position": 4,
    89  		"start_line": 2,
    90  		"line": 3,
    91  		"original_line": 2,
    92  		"original_start_line": 2,
    93  		"side": "RIGHT",
    94  		"start_side": "LEFT",
    95  		"commit_id": "ab",
    96  		"original_commit_id": "9c",
    97  		"user": {
    98  			"login": "ll",
    99  			"id": 123,
   100  			"avatar_url": "a",
   101  			"gravatar_id": "g",
   102  			"name": "n",
   103  			"company": "c",
   104  			"blog": "b",
   105  			"location": "l",
   106  			"email": "e",
   107  			"hireable": true,
   108  			"public_repos": 1,
   109  			"followers": 1,
   110  			"following": 1,
   111  			"created_at": ` + referenceTimeStr + `,
   112  			"url": "u"
   113  		},
   114  		"reactions": {
   115  			"total_count": 1,
   116  			"+1": 1,
   117  			"-1": 0,
   118  			"laugh": 0,
   119  			"confused": 0,
   120  			"heart": 0,
   121  			"hooray": 0,
   122  			"rocket": 0,
   123  			"eyes": 0,
   124  			"url": "u"
   125  		},
   126  		"created_at": "2002-02-10T15:30:00Z",
   127  		"updated_at": "2002-02-10T15:30:00Z",
   128  		"url": "pullrequestcommentUrl",
   129  		"html_url": "pullrequestcommentHTMLUrl",
   130  		"pull_request_url": "pullrequestcommentPullRequestURL"
   131  	}`
   132  
   133  	testJSONMarshal(t, u, want)
   134  }
   135  
   136  func TestPullRequestsService_ListComments_allPulls(t *testing.T) {
   137  	client, mux, _, teardown := setup()
   138  	defer teardown()
   139  
   140  	wantAcceptHeaders := []string{mediaTypeReactionsPreview, mediaTypeMultiLineCommentsPreview}
   141  	mux.HandleFunc("/repos/o/r/pulls/comments", func(w http.ResponseWriter, r *http.Request) {
   142  		testMethod(t, r, "GET")
   143  		testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", "))
   144  		testFormValues(t, r, values{
   145  			"sort":      "updated",
   146  			"direction": "desc",
   147  			"since":     "2002-02-10T15:30:00Z",
   148  			"page":      "2",
   149  		})
   150  		fmt.Fprint(w, `[{"id":1}]`)
   151  	})
   152  
   153  	opt := &PullRequestListCommentsOptions{
   154  		Sort:        "updated",
   155  		Direction:   "desc",
   156  		Since:       time.Date(2002, time.February, 10, 15, 30, 0, 0, time.UTC),
   157  		ListOptions: ListOptions{Page: 2},
   158  	}
   159  	ctx := context.Background()
   160  	pulls, _, err := client.PullRequests.ListComments(ctx, "o", "r", 0, opt)
   161  	if err != nil {
   162  		t.Errorf("PullRequests.ListComments returned error: %v", err)
   163  	}
   164  
   165  	want := []*PullRequestComment{{ID: Int64(1)}}
   166  	if !cmp.Equal(pulls, want) {
   167  		t.Errorf("PullRequests.ListComments returned %+v, want %+v", pulls, want)
   168  	}
   169  
   170  	const methodName = "ListComments"
   171  	testBadOptions(t, methodName, func() (err error) {
   172  		_, _, err = client.PullRequests.ListComments(ctx, "\n", "\n", -1, opt)
   173  		return err
   174  	})
   175  
   176  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   177  		got, resp, err := client.PullRequests.ListComments(ctx, "o", "r", 0, opt)
   178  		if got != nil {
   179  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   180  		}
   181  		return resp, err
   182  	})
   183  }
   184  
   185  func TestPullRequestsService_ListComments_specificPull(t *testing.T) {
   186  	client, mux, _, teardown := setup()
   187  	defer teardown()
   188  
   189  	wantAcceptHeaders := []string{mediaTypeReactionsPreview, mediaTypeMultiLineCommentsPreview}
   190  	mux.HandleFunc("/repos/o/r/pulls/1/comments", func(w http.ResponseWriter, r *http.Request) {
   191  		testMethod(t, r, "GET")
   192  		testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", "))
   193  		fmt.Fprint(w, `[{"id":1, "pull_request_review_id":42}]`)
   194  	})
   195  
   196  	ctx := context.Background()
   197  	pulls, _, err := client.PullRequests.ListComments(ctx, "o", "r", 1, nil)
   198  	if err != nil {
   199  		t.Errorf("PullRequests.ListComments returned error: %v", err)
   200  	}
   201  
   202  	want := []*PullRequestComment{{ID: Int64(1), PullRequestReviewID: Int64(42)}}
   203  	if !cmp.Equal(pulls, want) {
   204  		t.Errorf("PullRequests.ListComments returned %+v, want %+v", pulls, want)
   205  	}
   206  }
   207  
   208  func TestPullRequestsService_ListComments_invalidOwner(t *testing.T) {
   209  	client, _, _, teardown := setup()
   210  	defer teardown()
   211  
   212  	ctx := context.Background()
   213  	_, _, err := client.PullRequests.ListComments(ctx, "%", "r", 1, nil)
   214  	testURLParseError(t, err)
   215  }
   216  
   217  func TestPullRequestsService_GetComment(t *testing.T) {
   218  	client, mux, _, teardown := setup()
   219  	defer teardown()
   220  
   221  	wantAcceptHeaders := []string{mediaTypeReactionsPreview, mediaTypeMultiLineCommentsPreview}
   222  	mux.HandleFunc("/repos/o/r/pulls/comments/1", func(w http.ResponseWriter, r *http.Request) {
   223  		testMethod(t, r, "GET")
   224  		testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", "))
   225  		fmt.Fprint(w, `{"id":1}`)
   226  	})
   227  
   228  	ctx := context.Background()
   229  	comment, _, err := client.PullRequests.GetComment(ctx, "o", "r", 1)
   230  	if err != nil {
   231  		t.Errorf("PullRequests.GetComment returned error: %v", err)
   232  	}
   233  
   234  	want := &PullRequestComment{ID: Int64(1)}
   235  	if !cmp.Equal(comment, want) {
   236  		t.Errorf("PullRequests.GetComment returned %+v, want %+v", comment, want)
   237  	}
   238  
   239  	const methodName = "GetComment"
   240  	testBadOptions(t, methodName, func() (err error) {
   241  		_, _, err = client.PullRequests.GetComment(ctx, "\n", "\n", -1)
   242  		return err
   243  	})
   244  
   245  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   246  		got, resp, err := client.PullRequests.GetComment(ctx, "o", "r", 1)
   247  		if got != nil {
   248  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   249  		}
   250  		return resp, err
   251  	})
   252  }
   253  
   254  func TestPullRequestsService_GetComment_invalidOwner(t *testing.T) {
   255  	client, _, _, teardown := setup()
   256  	defer teardown()
   257  
   258  	ctx := context.Background()
   259  	_, _, err := client.PullRequests.GetComment(ctx, "%", "r", 1)
   260  	testURLParseError(t, err)
   261  }
   262  
   263  func TestPullRequestsService_CreateComment(t *testing.T) {
   264  	client, mux, _, teardown := setup()
   265  	defer teardown()
   266  
   267  	input := &PullRequestComment{Body: String("b")}
   268  
   269  	wantAcceptHeaders := []string{mediaTypeReactionsPreview, mediaTypeMultiLineCommentsPreview}
   270  	mux.HandleFunc("/repos/o/r/pulls/1/comments", func(w http.ResponseWriter, r *http.Request) {
   271  		v := new(PullRequestComment)
   272  		json.NewDecoder(r.Body).Decode(v)
   273  
   274  		// TODO: remove custom Accept header assertion when the API fully launches.
   275  		testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", "))
   276  		testMethod(t, r, "POST")
   277  		if !cmp.Equal(v, input) {
   278  			t.Errorf("Request body = %+v, want %+v", v, input)
   279  		}
   280  
   281  		fmt.Fprint(w, `{"id":1}`)
   282  	})
   283  
   284  	ctx := context.Background()
   285  	comment, _, err := client.PullRequests.CreateComment(ctx, "o", "r", 1, input)
   286  	if err != nil {
   287  		t.Errorf("PullRequests.CreateComment returned error: %v", err)
   288  	}
   289  
   290  	want := &PullRequestComment{ID: Int64(1)}
   291  	if !cmp.Equal(comment, want) {
   292  		t.Errorf("PullRequests.CreateComment returned %+v, want %+v", comment, want)
   293  	}
   294  
   295  	const methodName = "CreateComment"
   296  	testBadOptions(t, methodName, func() (err error) {
   297  		_, _, err = client.PullRequests.CreateComment(ctx, "\n", "\n", -1, input)
   298  		return err
   299  	})
   300  
   301  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   302  		got, resp, err := client.PullRequests.CreateComment(ctx, "o", "r", 1, input)
   303  		if got != nil {
   304  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   305  		}
   306  		return resp, err
   307  	})
   308  }
   309  
   310  func TestPullRequestsService_CreateComment_invalidOwner(t *testing.T) {
   311  	client, _, _, teardown := setup()
   312  	defer teardown()
   313  
   314  	ctx := context.Background()
   315  	_, _, err := client.PullRequests.CreateComment(ctx, "%", "r", 1, nil)
   316  	testURLParseError(t, err)
   317  }
   318  
   319  func TestPullRequestsService_CreateCommentInReplyTo(t *testing.T) {
   320  	client, mux, _, teardown := setup()
   321  	defer teardown()
   322  
   323  	input := &PullRequestComment{Body: String("b")}
   324  
   325  	mux.HandleFunc("/repos/o/r/pulls/1/comments", func(w http.ResponseWriter, r *http.Request) {
   326  		v := new(PullRequestComment)
   327  		json.NewDecoder(r.Body).Decode(v)
   328  
   329  		testMethod(t, r, "POST")
   330  		if !cmp.Equal(v, input) {
   331  			t.Errorf("Request body = %+v, want %+v", v, input)
   332  		}
   333  
   334  		fmt.Fprint(w, `{"id":1}`)
   335  	})
   336  
   337  	ctx := context.Background()
   338  	comment, _, err := client.PullRequests.CreateCommentInReplyTo(ctx, "o", "r", 1, "b", 2)
   339  	if err != nil {
   340  		t.Errorf("PullRequests.CreateCommentInReplyTo returned error: %v", err)
   341  	}
   342  
   343  	want := &PullRequestComment{ID: Int64(1)}
   344  	if !cmp.Equal(comment, want) {
   345  		t.Errorf("PullRequests.CreateCommentInReplyTo returned %+v, want %+v", comment, want)
   346  	}
   347  
   348  	const methodName = "CreateCommentInReplyTo"
   349  	testBadOptions(t, methodName, func() (err error) {
   350  		_, _, err = client.PullRequests.CreateCommentInReplyTo(ctx, "\n", "\n", -1, "\n", -2)
   351  		return err
   352  	})
   353  
   354  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   355  		got, resp, err := client.PullRequests.CreateCommentInReplyTo(ctx, "o", "r", 1, "b", 2)
   356  		if got != nil {
   357  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   358  		}
   359  		return resp, err
   360  	})
   361  }
   362  
   363  func TestPullRequestsService_EditComment(t *testing.T) {
   364  	client, mux, _, teardown := setup()
   365  	defer teardown()
   366  
   367  	input := &PullRequestComment{Body: String("b")}
   368  
   369  	mux.HandleFunc("/repos/o/r/pulls/comments/1", func(w http.ResponseWriter, r *http.Request) {
   370  		v := new(PullRequestComment)
   371  		json.NewDecoder(r.Body).Decode(v)
   372  
   373  		testMethod(t, r, "PATCH")
   374  		if !cmp.Equal(v, input) {
   375  			t.Errorf("Request body = %+v, want %+v", v, input)
   376  		}
   377  
   378  		fmt.Fprint(w, `{"id":1}`)
   379  	})
   380  
   381  	ctx := context.Background()
   382  	comment, _, err := client.PullRequests.EditComment(ctx, "o", "r", 1, input)
   383  	if err != nil {
   384  		t.Errorf("PullRequests.EditComment returned error: %v", err)
   385  	}
   386  
   387  	want := &PullRequestComment{ID: Int64(1)}
   388  	if !cmp.Equal(comment, want) {
   389  		t.Errorf("PullRequests.EditComment returned %+v, want %+v", comment, want)
   390  	}
   391  
   392  	const methodName = "EditComment"
   393  	testBadOptions(t, methodName, func() (err error) {
   394  		_, _, err = client.PullRequests.EditComment(ctx, "\n", "\n", -1, input)
   395  		return err
   396  	})
   397  
   398  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   399  		got, resp, err := client.PullRequests.EditComment(ctx, "o", "r", 1, input)
   400  		if got != nil {
   401  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   402  		}
   403  		return resp, err
   404  	})
   405  }
   406  
   407  func TestPullRequestsService_EditComment_invalidOwner(t *testing.T) {
   408  	client, _, _, teardown := setup()
   409  	defer teardown()
   410  
   411  	ctx := context.Background()
   412  	_, _, err := client.PullRequests.EditComment(ctx, "%", "r", 1, nil)
   413  	testURLParseError(t, err)
   414  }
   415  
   416  func TestPullRequestsService_DeleteComment(t *testing.T) {
   417  	client, mux, _, teardown := setup()
   418  	defer teardown()
   419  
   420  	mux.HandleFunc("/repos/o/r/pulls/comments/1", func(w http.ResponseWriter, r *http.Request) {
   421  		testMethod(t, r, "DELETE")
   422  	})
   423  
   424  	ctx := context.Background()
   425  	_, err := client.PullRequests.DeleteComment(ctx, "o", "r", 1)
   426  	if err != nil {
   427  		t.Errorf("PullRequests.DeleteComment returned error: %v", err)
   428  	}
   429  
   430  	const methodName = "DeleteComment"
   431  	testBadOptions(t, methodName, func() (err error) {
   432  		_, err = client.PullRequests.DeleteComment(ctx, "\n", "\n", -1)
   433  		return err
   434  	})
   435  
   436  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   437  		return client.PullRequests.DeleteComment(ctx, "o", "r", 1)
   438  	})
   439  }
   440  
   441  func TestPullRequestsService_DeleteComment_invalidOwner(t *testing.T) {
   442  	client, _, _, teardown := setup()
   443  	defer teardown()
   444  
   445  	ctx := context.Background()
   446  	_, err := client.PullRequests.DeleteComment(ctx, "%", "r", 1)
   447  	testURLParseError(t, err)
   448  }
   449  

View as plain text