...

Source file src/github.com/google/go-github/v45/github/pulls_reviewers_test.go

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

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

View as plain text