github.com/google/go-github/v66@v66.0.0/github/pulls_reviewers_test.go (about)

     1  // Copyright 2017 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  	"fmt"
    11  	"net/http"
    12  	"testing"
    13  
    14  	"github.com/google/go-cmp/cmp"
    15  )
    16  
    17  func TestReviewersRequest_Marshal(t *testing.T) {
    18  	t.Parallel()
    19  	testJSONMarshal(t, &ReviewersRequest{}, "{}")
    20  
    21  	u := &ReviewersRequest{
    22  		NodeID:        String("n"),
    23  		Reviewers:     []string{"r"},
    24  		TeamReviewers: []string{"t"},
    25  	}
    26  
    27  	want := `{
    28  		"node_id": "n",
    29  		"reviewers": [
    30  			"r"
    31  		],
    32  		"team_reviewers" : [
    33  			"t"
    34  		]
    35  	}`
    36  
    37  	testJSONMarshal(t, u, want)
    38  }
    39  
    40  func TestReviewers_Marshal(t *testing.T) {
    41  	t.Parallel()
    42  	testJSONMarshal(t, &Reviewers{}, "{}")
    43  
    44  	u := &Reviewers{
    45  		Users: []*User{{
    46  			Login:       String("l"),
    47  			ID:          Int64(1),
    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  		Teams: []*Team{{
    63  			ID:              Int64(1),
    64  			NodeID:          String("node"),
    65  			Name:            String("n"),
    66  			Description:     String("d"),
    67  			URL:             String("u"),
    68  			Slug:            String("s"),
    69  			Permission:      String("p"),
    70  			Privacy:         String("priv"),
    71  			MembersCount:    Int(1),
    72  			ReposCount:      Int(1),
    73  			Organization:    nil,
    74  			MembersURL:      String("m"),
    75  			RepositoriesURL: String("r"),
    76  			Parent:          nil,
    77  			LDAPDN:          String("l"),
    78  		}},
    79  	}
    80  
    81  	want := `{
    82  		"users" : [
    83  			{
    84  				"login": "l",
    85  				"id": 1,
    86  				"avatar_url": "a",
    87  				"gravatar_id": "g",
    88  				"name": "n",
    89  				"company": "c",
    90  				"blog": "b",
    91  				"location": "l",
    92  				"email": "e",
    93  				"hireable": true,
    94  				"public_repos": 1,
    95  				"followers": 1,
    96  				"following": 1,
    97  				"created_at": ` + referenceTimeStr + `,
    98  				"url": "u"
    99  			}
   100  		],
   101  		"teams" : [
   102  			{
   103  				"id": 1,
   104  				"node_id": "node",
   105  				"name": "n",
   106  				"description": "d",
   107  				"url": "u",
   108  				"slug": "s",
   109  				"permission": "p",
   110  				"privacy": "priv",
   111  				"members_count": 1,
   112  				"repos_count": 1,
   113  				"members_url": "m",
   114  				"repositories_url": "r",
   115  				"ldap_dn": "l"
   116  			}
   117  		]
   118  	}`
   119  
   120  	testJSONMarshal(t, u, want)
   121  }
   122  
   123  func TestRequestReviewers(t *testing.T) {
   124  	t.Parallel()
   125  	client, mux, _ := setup(t)
   126  
   127  	mux.HandleFunc("/repos/o/r/pulls/1/requested_reviewers", func(w http.ResponseWriter, r *http.Request) {
   128  		testMethod(t, r, "POST")
   129  		testBody(t, r, `{"reviewers":["octocat","googlebot"],"team_reviewers":["justice-league","injustice-league"]}`+"\n")
   130  		fmt.Fprint(w, `{"number":1}`)
   131  	})
   132  
   133  	// This returns a PR, unmarshalling of which is tested elsewhere
   134  	ctx := context.Background()
   135  	got, _, err := client.PullRequests.RequestReviewers(ctx, "o", "r", 1, ReviewersRequest{Reviewers: []string{"octocat", "googlebot"}, TeamReviewers: []string{"justice-league", "injustice-league"}})
   136  	if err != nil {
   137  		t.Errorf("PullRequests.RequestReviewers returned error: %v", err)
   138  	}
   139  	want := &PullRequest{Number: Int(1)}
   140  	if !cmp.Equal(got, want) {
   141  		t.Errorf("PullRequests.RequestReviewers returned %+v, want %+v", got, want)
   142  	}
   143  
   144  	const methodName = "RequestReviewers"
   145  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   146  		got, resp, err := client.PullRequests.RequestReviewers(ctx, "o", "r", 1, ReviewersRequest{Reviewers: []string{"octocat", "googlebot"}, TeamReviewers: []string{"justice-league", "injustice-league"}})
   147  		if got != nil {
   148  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   149  		}
   150  		return resp, err
   151  	})
   152  }
   153  
   154  func TestRemoveReviewers(t *testing.T) {
   155  	t.Parallel()
   156  	client, mux, _ := setup(t)
   157  
   158  	mux.HandleFunc("/repos/o/r/pulls/1/requested_reviewers", func(w http.ResponseWriter, r *http.Request) {
   159  		testMethod(t, r, "DELETE")
   160  		testBody(t, r, `{"reviewers":["octocat","googlebot"],"team_reviewers":["justice-league"]}`+"\n")
   161  	})
   162  
   163  	ctx := context.Background()
   164  	_, err := client.PullRequests.RemoveReviewers(ctx, "o", "r", 1, ReviewersRequest{Reviewers: []string{"octocat", "googlebot"}, TeamReviewers: []string{"justice-league"}})
   165  	if err != nil {
   166  		t.Errorf("PullRequests.RemoveReviewers returned error: %v", err)
   167  	}
   168  
   169  	const methodName = "RemoveReviewers"
   170  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   171  		return client.PullRequests.RemoveReviewers(ctx, "o", "r", 1, ReviewersRequest{Reviewers: []string{"octocat", "googlebot"}, TeamReviewers: []string{"justice-league"}})
   172  	})
   173  }
   174  
   175  func TestListReviewers(t *testing.T) {
   176  	t.Parallel()
   177  	client, mux, _ := setup(t)
   178  
   179  	mux.HandleFunc("/repos/o/r/pulls/1/requested_reviewers", func(w http.ResponseWriter, r *http.Request) {
   180  		testMethod(t, r, "GET")
   181  		fmt.Fprint(w, `{"users":[{"login":"octocat","id":1}],"teams":[{"id":1,"name":"Justice League"}]}`)
   182  	})
   183  
   184  	ctx := context.Background()
   185  	got, _, err := client.PullRequests.ListReviewers(ctx, "o", "r", 1, nil)
   186  	if err != nil {
   187  		t.Errorf("PullRequests.ListReviewers returned error: %v", err)
   188  	}
   189  
   190  	want := &Reviewers{
   191  		Users: []*User{
   192  			{
   193  				Login: String("octocat"),
   194  				ID:    Int64(1),
   195  			},
   196  		},
   197  		Teams: []*Team{
   198  			{
   199  				ID:   Int64(1),
   200  				Name: String("Justice League"),
   201  			},
   202  		},
   203  	}
   204  	if !cmp.Equal(got, want) {
   205  		t.Errorf("PullRequests.ListReviewers returned %+v, want %+v", got, want)
   206  	}
   207  
   208  	const methodName = "ListReviewers"
   209  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   210  		got, resp, err := client.PullRequests.ListReviewers(ctx, "o", "r", 1, nil)
   211  		if got != nil {
   212  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   213  		}
   214  		return resp, err
   215  	})
   216  }
   217  
   218  func TestListReviewers_withOptions(t *testing.T) {
   219  	t.Parallel()
   220  	client, mux, _ := setup(t)
   221  
   222  	mux.HandleFunc("/repos/o/r/pulls/1/requested_reviewers", func(w http.ResponseWriter, r *http.Request) {
   223  		testMethod(t, r, "GET")
   224  		testFormValues(t, r, values{
   225  			"page": "2",
   226  		})
   227  		fmt.Fprint(w, `{}`)
   228  	})
   229  
   230  	ctx := context.Background()
   231  	_, _, err := client.PullRequests.ListReviewers(ctx, "o", "r", 1, &ListOptions{Page: 2})
   232  	if err != nil {
   233  		t.Errorf("PullRequests.ListReviewers returned error: %v", err)
   234  	}
   235  
   236  	const methodName = "ListReviewers"
   237  	testBadOptions(t, methodName, func() (err error) {
   238  		_, _, err = client.PullRequests.ListReviewers(ctx, "\n", "\n", 1, &ListOptions{Page: 2})
   239  		return err
   240  	})
   241  
   242  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   243  		got, resp, err := client.PullRequests.ListReviewers(ctx, "o", "r", 1, &ListOptions{Page: 2})
   244  		if got != nil {
   245  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   246  		}
   247  		return resp, err
   248  	})
   249  }