github.com/google/go-github/v33@v33.0.0/github/issues_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  	"testing"
    15  	"time"
    16  )
    17  
    18  func TestIssuesService_ListComments_allIssues(t *testing.T) {
    19  	client, mux, _, teardown := setup()
    20  	defer teardown()
    21  
    22  	mux.HandleFunc("/repos/o/r/issues/comments", func(w http.ResponseWriter, r *http.Request) {
    23  		testMethod(t, r, "GET")
    24  		testHeader(t, r, "Accept", mediaTypeReactionsPreview)
    25  		testFormValues(t, r, values{
    26  			"since": "2002-02-10T15:30:00Z",
    27  			"page":  "2",
    28  		})
    29  		fmt.Fprint(w, `[{"id":1}]`)
    30  	})
    31  
    32  	since := time.Date(2002, time.February, 10, 15, 30, 0, 0, time.UTC)
    33  	opt := &IssueListCommentsOptions{
    34  		Since:       &since,
    35  		ListOptions: ListOptions{Page: 2},
    36  	}
    37  	comments, _, err := client.Issues.ListComments(context.Background(), "o", "r", 0, opt)
    38  	if err != nil {
    39  		t.Errorf("Issues.ListComments returned error: %v", err)
    40  	}
    41  
    42  	want := []*IssueComment{{ID: Int64(1)}}
    43  	if !reflect.DeepEqual(comments, want) {
    44  		t.Errorf("Issues.ListComments returned %+v, want %+v", comments, want)
    45  	}
    46  }
    47  
    48  func TestIssuesService_ListComments_specificIssue(t *testing.T) {
    49  	client, mux, _, teardown := setup()
    50  	defer teardown()
    51  
    52  	mux.HandleFunc("/repos/o/r/issues/1/comments", func(w http.ResponseWriter, r *http.Request) {
    53  		testMethod(t, r, "GET")
    54  		testHeader(t, r, "Accept", mediaTypeReactionsPreview)
    55  		fmt.Fprint(w, `[{"id":1}]`)
    56  	})
    57  
    58  	comments, _, err := client.Issues.ListComments(context.Background(), "o", "r", 1, nil)
    59  	if err != nil {
    60  		t.Errorf("Issues.ListComments returned error: %v", err)
    61  	}
    62  
    63  	want := []*IssueComment{{ID: Int64(1)}}
    64  	if !reflect.DeepEqual(comments, want) {
    65  		t.Errorf("Issues.ListComments returned %+v, want %+v", comments, want)
    66  	}
    67  }
    68  
    69  func TestIssuesService_ListComments_invalidOwner(t *testing.T) {
    70  	client, _, _, teardown := setup()
    71  	defer teardown()
    72  
    73  	_, _, err := client.Issues.ListComments(context.Background(), "%", "r", 1, nil)
    74  	testURLParseError(t, err)
    75  }
    76  
    77  func TestIssuesService_GetComment(t *testing.T) {
    78  	client, mux, _, teardown := setup()
    79  	defer teardown()
    80  
    81  	mux.HandleFunc("/repos/o/r/issues/comments/1", func(w http.ResponseWriter, r *http.Request) {
    82  		testMethod(t, r, "GET")
    83  		testHeader(t, r, "Accept", mediaTypeReactionsPreview)
    84  		fmt.Fprint(w, `{"id":1}`)
    85  	})
    86  
    87  	comment, _, err := client.Issues.GetComment(context.Background(), "o", "r", 1)
    88  	if err != nil {
    89  		t.Errorf("Issues.GetComment returned error: %v", err)
    90  	}
    91  
    92  	want := &IssueComment{ID: Int64(1)}
    93  	if !reflect.DeepEqual(comment, want) {
    94  		t.Errorf("Issues.GetComment returned %+v, want %+v", comment, want)
    95  	}
    96  }
    97  
    98  func TestIssuesService_GetComment_invalidOrg(t *testing.T) {
    99  	client, _, _, teardown := setup()
   100  	defer teardown()
   101  
   102  	_, _, err := client.Issues.GetComment(context.Background(), "%", "r", 1)
   103  	testURLParseError(t, err)
   104  }
   105  
   106  func TestIssuesService_CreateComment(t *testing.T) {
   107  	client, mux, _, teardown := setup()
   108  	defer teardown()
   109  
   110  	input := &IssueComment{Body: String("b")}
   111  
   112  	mux.HandleFunc("/repos/o/r/issues/1/comments", func(w http.ResponseWriter, r *http.Request) {
   113  		v := new(IssueComment)
   114  		json.NewDecoder(r.Body).Decode(v)
   115  
   116  		testMethod(t, r, "POST")
   117  		if !reflect.DeepEqual(v, input) {
   118  			t.Errorf("Request body = %+v, want %+v", v, input)
   119  		}
   120  
   121  		fmt.Fprint(w, `{"id":1}`)
   122  	})
   123  
   124  	comment, _, err := client.Issues.CreateComment(context.Background(), "o", "r", 1, input)
   125  	if err != nil {
   126  		t.Errorf("Issues.CreateComment returned error: %v", err)
   127  	}
   128  
   129  	want := &IssueComment{ID: Int64(1)}
   130  	if !reflect.DeepEqual(comment, want) {
   131  		t.Errorf("Issues.CreateComment returned %+v, want %+v", comment, want)
   132  	}
   133  }
   134  
   135  func TestIssuesService_CreateComment_invalidOrg(t *testing.T) {
   136  	client, _, _, teardown := setup()
   137  	defer teardown()
   138  
   139  	_, _, err := client.Issues.CreateComment(context.Background(), "%", "r", 1, nil)
   140  	testURLParseError(t, err)
   141  }
   142  
   143  func TestIssuesService_EditComment(t *testing.T) {
   144  	client, mux, _, teardown := setup()
   145  	defer teardown()
   146  
   147  	input := &IssueComment{Body: String("b")}
   148  
   149  	mux.HandleFunc("/repos/o/r/issues/comments/1", func(w http.ResponseWriter, r *http.Request) {
   150  		v := new(IssueComment)
   151  		json.NewDecoder(r.Body).Decode(v)
   152  
   153  		testMethod(t, r, "PATCH")
   154  		if !reflect.DeepEqual(v, input) {
   155  			t.Errorf("Request body = %+v, want %+v", v, input)
   156  		}
   157  
   158  		fmt.Fprint(w, `{"id":1}`)
   159  	})
   160  
   161  	comment, _, err := client.Issues.EditComment(context.Background(), "o", "r", 1, input)
   162  	if err != nil {
   163  		t.Errorf("Issues.EditComment returned error: %v", err)
   164  	}
   165  
   166  	want := &IssueComment{ID: Int64(1)}
   167  	if !reflect.DeepEqual(comment, want) {
   168  		t.Errorf("Issues.EditComment returned %+v, want %+v", comment, want)
   169  	}
   170  }
   171  
   172  func TestIssuesService_EditComment_invalidOwner(t *testing.T) {
   173  	client, _, _, teardown := setup()
   174  	defer teardown()
   175  
   176  	_, _, err := client.Issues.EditComment(context.Background(), "%", "r", 1, nil)
   177  	testURLParseError(t, err)
   178  }
   179  
   180  func TestIssuesService_DeleteComment(t *testing.T) {
   181  	client, mux, _, teardown := setup()
   182  	defer teardown()
   183  
   184  	mux.HandleFunc("/repos/o/r/issues/comments/1", func(w http.ResponseWriter, r *http.Request) {
   185  		testMethod(t, r, "DELETE")
   186  	})
   187  
   188  	_, err := client.Issues.DeleteComment(context.Background(), "o", "r", 1)
   189  	if err != nil {
   190  		t.Errorf("Issues.DeleteComments returned error: %v", err)
   191  	}
   192  }
   193  
   194  func TestIssuesService_DeleteComment_invalidOwner(t *testing.T) {
   195  	client, _, _, teardown := setup()
   196  	defer teardown()
   197  
   198  	_, err := client.Issues.DeleteComment(context.Background(), "%", "r", 1)
   199  	testURLParseError(t, err)
   200  }