github.com/google/go-github/v33@v33.0.0/github/pulls_comments_test.go (about)

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