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

     1  // Copyright 2018 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  // "Team Discussion Comments" endpoint, when using a teamID.
    19  func tdcEndpointByID(orgID, teamID, discussionNumber, commentNumber string) string {
    20  	out := fmt.Sprintf("/organizations/%v/team/%v/discussions/%v/comments", orgID, teamID, discussionNumber)
    21  	if commentNumber != "" {
    22  		return fmt.Sprintf("%v/%v", out, commentNumber)
    23  	}
    24  	return out
    25  }
    26  
    27  // "Team Discussion Comments" endpoint, when using a team slug.
    28  func tdcEndpointBySlug(org, slug, dicsuccionsNumber, commentNumber string) string {
    29  	out := fmt.Sprintf("/orgs/%v/teams/%v/discussions/%v/comments", org, slug, dicsuccionsNumber)
    30  	if commentNumber != "" {
    31  		return fmt.Sprintf("%v/%v", out, commentNumber)
    32  	}
    33  	return out
    34  }
    35  
    36  func TestTeamsService_ListComments(t *testing.T) {
    37  	client, mux, _, teardown := setup()
    38  	defer teardown()
    39  
    40  	handleFunc := func(w http.ResponseWriter, r *http.Request) {
    41  		testMethod(t, r, "GET")
    42  		testFormValues(t, r, values{
    43  			"direction": "desc",
    44  		})
    45  		fmt.Fprintf(w,
    46  			`[
    47  				{
    48  					"author": {
    49  						"login": "author",
    50  						"id": 0,
    51  						"avatar_url": "https://avatars1.githubusercontent.com/u/0?v=4",
    52  						"gravatar_id": "",
    53  						"url": "https://api.github.com/users/author",
    54  						"html_url": "https://github.com/author",
    55  						"followers_url": "https://api.github.com/users/author/followers",
    56  						"following_url": "https://api.github.com/users/author/following{/other_user}",
    57  						"gists_url": "https://api.github.com/users/author/gists{/gist_id}",
    58  						"starred_url": "https://api.github.com/users/author/starred{/owner}{/repo}",
    59  						"subscriptions_url": "https://api.github.com/users/author/subscriptions",
    60  						"organizations_url": "https://api.github.com/users/author/orgs",
    61  						"repos_url": "https://api.github.com/users/author/repos",
    62  						"events_url": "https://api.github.com/users/author/events{/privacy}",
    63  						"received_events_url": "https://api.github.com/users/author/received_events",
    64  						"type": "User",
    65  						"site_admin": false
    66  					},
    67  					"body": "comment",
    68  					"body_html": "<p>comment</p>",
    69  					"body_version": "version",
    70  					"created_at": "2018-01-01T00:00:00Z",
    71  					"last_edited_at": null,
    72  					"discussion_url": "https://api.github.com/teams/2/discussions/3",
    73  					"html_url": "https://github.com/orgs/1/teams/2/discussions/3/comments/4",
    74  					"node_id": "node",
    75  					"number": 4,
    76  					"updated_at": "2018-01-01T00:00:00Z",
    77  					"url": "https://api.github.com/teams/2/discussions/3/comments/4"
    78  				}
    79  			]`)
    80  	}
    81  
    82  	want := []*DiscussionComment{
    83  		{
    84  			Author: &User{
    85  				Login:             String("author"),
    86  				ID:                Int64(0),
    87  				AvatarURL:         String("https://avatars1.githubusercontent.com/u/0?v=4"),
    88  				GravatarID:        String(""),
    89  				URL:               String("https://api.github.com/users/author"),
    90  				HTMLURL:           String("https://github.com/author"),
    91  				FollowersURL:      String("https://api.github.com/users/author/followers"),
    92  				FollowingURL:      String("https://api.github.com/users/author/following{/other_user}"),
    93  				GistsURL:          String("https://api.github.com/users/author/gists{/gist_id}"),
    94  				StarredURL:        String("https://api.github.com/users/author/starred{/owner}{/repo}"),
    95  				SubscriptionsURL:  String("https://api.github.com/users/author/subscriptions"),
    96  				OrganizationsURL:  String("https://api.github.com/users/author/orgs"),
    97  				ReposURL:          String("https://api.github.com/users/author/repos"),
    98  				EventsURL:         String("https://api.github.com/users/author/events{/privacy}"),
    99  				ReceivedEventsURL: String("https://api.github.com/users/author/received_events"),
   100  				Type:              String("User"),
   101  				SiteAdmin:         Bool(false),
   102  			},
   103  			Body:          String("comment"),
   104  			BodyHTML:      String("<p>comment</p>"),
   105  			BodyVersion:   String("version"),
   106  			CreatedAt:     &Timestamp{time.Date(2018, time.January, 1, 0, 0, 0, 0, time.UTC)},
   107  			LastEditedAt:  nil,
   108  			DiscussionURL: String("https://api.github.com/teams/2/discussions/3"),
   109  			HTMLURL:       String("https://github.com/orgs/1/teams/2/discussions/3/comments/4"),
   110  			NodeID:        String("node"),
   111  			Number:        Int(4),
   112  			UpdatedAt:     &Timestamp{time.Date(2018, time.January, 1, 0, 0, 0, 0, time.UTC)},
   113  			URL:           String("https://api.github.com/teams/2/discussions/3/comments/4"),
   114  		},
   115  	}
   116  
   117  	e := tdcEndpointByID("1", "2", "3", "")
   118  	mux.HandleFunc(e, handleFunc)
   119  
   120  	commentsByID, _, err := client.Teams.ListCommentsByID(context.Background(), 1, 2, 3,
   121  		&DiscussionCommentListOptions{Direction: "desc"})
   122  	if err != nil {
   123  		t.Errorf("Teams.ListCommentsByID returned error: %v", err)
   124  	}
   125  
   126  	if !reflect.DeepEqual(commentsByID, want) {
   127  		t.Errorf("Teams.ListCommentsByID returned %+v, want %+v", commentsByID, want)
   128  	}
   129  
   130  	e = tdcEndpointBySlug("a", "b", "3", "")
   131  	mux.HandleFunc(e, handleFunc)
   132  
   133  	commentsBySlug, _, err := client.Teams.ListCommentsBySlug(context.Background(), "a", "b", 3,
   134  		&DiscussionCommentListOptions{Direction: "desc"})
   135  	if err != nil {
   136  		t.Errorf("Teams.ListCommentsBySlug returned error: %v", err)
   137  	}
   138  
   139  	if !reflect.DeepEqual(commentsBySlug, want) {
   140  		t.Errorf("Teams.ListCommentsBySlug returned %+v, want %+v", commentsBySlug, want)
   141  	}
   142  }
   143  
   144  func TestTeamsService_GetComment(t *testing.T) {
   145  	client, mux, _, teardown := setup()
   146  	defer teardown()
   147  
   148  	handlerFunc := func(w http.ResponseWriter, r *http.Request) {
   149  		testMethod(t, r, "GET")
   150  		fmt.Fprint(w, `{"number":4}`)
   151  	}
   152  	want := &DiscussionComment{Number: Int(4)}
   153  
   154  	e := tdcEndpointByID("1", "2", "3", "4")
   155  	mux.HandleFunc(e, handlerFunc)
   156  
   157  	commentByID, _, err := client.Teams.GetCommentByID(context.Background(), 1, 2, 3, 4)
   158  	if err != nil {
   159  		t.Errorf("Teams.GetCommentByID returned error: %v", err)
   160  	}
   161  
   162  	if !reflect.DeepEqual(commentByID, want) {
   163  		t.Errorf("Teams.GetCommentByID returned %+v, want %+v", commentByID, want)
   164  	}
   165  
   166  	e = tdcEndpointBySlug("a", "b", "3", "4")
   167  	mux.HandleFunc(e, handlerFunc)
   168  
   169  	commentBySlug, _, err := client.Teams.GetCommentBySlug(context.Background(), "a", "b", 3, 4)
   170  	if err != nil {
   171  		t.Errorf("Teams.GetCommentBySlug returned error: %v", err)
   172  	}
   173  
   174  	if !reflect.DeepEqual(commentBySlug, want) {
   175  		t.Errorf("Teams.GetCommentBySlug returned %+v, want %+v", commentBySlug, want)
   176  	}
   177  
   178  }
   179  
   180  func TestTeamsService_CreateComment(t *testing.T) {
   181  	client, mux, _, teardown := setup()
   182  	defer teardown()
   183  
   184  	input := DiscussionComment{Body: String("c")}
   185  
   186  	handlerFunc := func(w http.ResponseWriter, r *http.Request) {
   187  		v := new(DiscussionComment)
   188  		json.NewDecoder(r.Body).Decode(v)
   189  
   190  		testMethod(t, r, "POST")
   191  		if !reflect.DeepEqual(v, &input) {
   192  			t.Errorf("Request body = %+v, want %+v", v, input)
   193  		}
   194  
   195  		fmt.Fprint(w, `{"number":4}`)
   196  	}
   197  	want := &DiscussionComment{Number: Int(4)}
   198  
   199  	e := tdcEndpointByID("1", "2", "3", "")
   200  	mux.HandleFunc(e, handlerFunc)
   201  
   202  	commentByID, _, err := client.Teams.CreateCommentByID(context.Background(), 1, 2, 3, input)
   203  	if err != nil {
   204  		t.Errorf("Teams.CreateCommentByID returned error: %v", err)
   205  	}
   206  
   207  	if !reflect.DeepEqual(commentByID, want) {
   208  		t.Errorf("Teams.CreateCommentByID returned %+v, want %+v", commentByID, want)
   209  	}
   210  
   211  	e = tdcEndpointBySlug("a", "b", "3", "")
   212  	mux.HandleFunc(e, handlerFunc)
   213  
   214  	commentBySlug, _, err := client.Teams.CreateCommentBySlug(context.Background(), "a", "b", 3, input)
   215  	if err != nil {
   216  		t.Errorf("Teams.CreateCommentBySlug returned error: %v", err)
   217  	}
   218  
   219  	if !reflect.DeepEqual(commentBySlug, want) {
   220  		t.Errorf("Teams.CreateCommentBySlug returned %+v, want %+v", commentBySlug, want)
   221  	}
   222  }
   223  
   224  func TestTeamsService_EditComment(t *testing.T) {
   225  	client, mux, _, teardown := setup()
   226  	defer teardown()
   227  
   228  	input := DiscussionComment{Body: String("e")}
   229  	handlerFunc := func(w http.ResponseWriter, r *http.Request) {
   230  		v := new(DiscussionComment)
   231  		json.NewDecoder(r.Body).Decode(v)
   232  
   233  		testMethod(t, r, "PATCH")
   234  		if !reflect.DeepEqual(v, &input) {
   235  			t.Errorf("Request body = %+v, want %+v", v, input)
   236  		}
   237  
   238  		fmt.Fprint(w, `{"number":4}`)
   239  	}
   240  	want := &DiscussionComment{Number: Int(4)}
   241  
   242  	e := tdcEndpointByID("1", "2", "3", "4")
   243  	mux.HandleFunc(e, handlerFunc)
   244  
   245  	commentByID, _, err := client.Teams.EditCommentByID(context.Background(), 1, 2, 3, 4, input)
   246  	if err != nil {
   247  		t.Errorf("Teams.EditCommentByID returned error: %v", err)
   248  	}
   249  
   250  	if !reflect.DeepEqual(commentByID, want) {
   251  		t.Errorf("Teams.EditCommentByID returned %+v, want %+v", commentByID, want)
   252  	}
   253  
   254  	e = tdcEndpointBySlug("a", "b", "3", "4")
   255  	mux.HandleFunc(e, handlerFunc)
   256  
   257  	commentBySlug, _, err := client.Teams.EditCommentBySlug(context.Background(), "a", "b", 3, 4, input)
   258  	if err != nil {
   259  		t.Errorf("Teams.EditCommentBySlug returned error: %v", err)
   260  	}
   261  
   262  	if !reflect.DeepEqual(commentBySlug, want) {
   263  		t.Errorf("Teams.EditCommentBySlug returned %+v, want %+v", commentBySlug, want)
   264  	}
   265  }
   266  
   267  func TestTeamsService_DeleteComment(t *testing.T) {
   268  	client, mux, _, teardown := setup()
   269  	defer teardown()
   270  
   271  	handlerFunc := func(w http.ResponseWriter, r *http.Request) {
   272  		testMethod(t, r, "DELETE")
   273  	}
   274  
   275  	e := tdcEndpointByID("1", "2", "3", "4")
   276  	mux.HandleFunc(e, handlerFunc)
   277  
   278  	_, err := client.Teams.DeleteCommentByID(context.Background(), 1, 2, 3, 4)
   279  	if err != nil {
   280  		t.Errorf("Teams.DeleteCommentByID returned error: %v", err)
   281  	}
   282  
   283  	e = tdcEndpointBySlug("a", "b", "3", "4")
   284  	mux.HandleFunc(e, handlerFunc)
   285  
   286  	_, err = client.Teams.DeleteCommentBySlug(context.Background(), "a", "b", 3, 4)
   287  	if err != nil {
   288  		t.Errorf("Teams.DeleteCommentBySlug returned error: %v", err)
   289  	}
   290  }