github.com/google/go-github/v33@v33.0.0/github/issues_assignees_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  )
    16  
    17  func TestIssuesService_ListAssignees(t *testing.T) {
    18  	client, mux, _, teardown := setup()
    19  	defer teardown()
    20  
    21  	mux.HandleFunc("/repos/o/r/assignees", func(w http.ResponseWriter, r *http.Request) {
    22  		testMethod(t, r, "GET")
    23  		testFormValues(t, r, values{"page": "2"})
    24  		fmt.Fprint(w, `[{"id":1}]`)
    25  	})
    26  
    27  	opt := &ListOptions{Page: 2}
    28  	assignees, _, err := client.Issues.ListAssignees(context.Background(), "o", "r", opt)
    29  	if err != nil {
    30  		t.Errorf("Issues.ListAssignees returned error: %v", err)
    31  	}
    32  
    33  	want := []*User{{ID: Int64(1)}}
    34  	if !reflect.DeepEqual(assignees, want) {
    35  		t.Errorf("Issues.ListAssignees returned %+v, want %+v", assignees, want)
    36  	}
    37  }
    38  
    39  func TestIssuesService_ListAssignees_invalidOwner(t *testing.T) {
    40  	client, _, _, teardown := setup()
    41  	defer teardown()
    42  
    43  	_, _, err := client.Issues.ListAssignees(context.Background(), "%", "r", nil)
    44  	testURLParseError(t, err)
    45  }
    46  
    47  func TestIssuesService_IsAssignee_true(t *testing.T) {
    48  	client, mux, _, teardown := setup()
    49  	defer teardown()
    50  
    51  	mux.HandleFunc("/repos/o/r/assignees/u", func(w http.ResponseWriter, r *http.Request) {
    52  		testMethod(t, r, "GET")
    53  	})
    54  
    55  	assignee, _, err := client.Issues.IsAssignee(context.Background(), "o", "r", "u")
    56  	if err != nil {
    57  		t.Errorf("Issues.IsAssignee returned error: %v", err)
    58  	}
    59  	if want := true; assignee != want {
    60  		t.Errorf("Issues.IsAssignee returned %+v, want %+v", assignee, want)
    61  	}
    62  }
    63  
    64  func TestIssuesService_IsAssignee_false(t *testing.T) {
    65  	client, mux, _, teardown := setup()
    66  	defer teardown()
    67  
    68  	mux.HandleFunc("/repos/o/r/assignees/u", func(w http.ResponseWriter, r *http.Request) {
    69  		testMethod(t, r, "GET")
    70  		w.WriteHeader(http.StatusNotFound)
    71  	})
    72  
    73  	assignee, _, err := client.Issues.IsAssignee(context.Background(), "o", "r", "u")
    74  	if err != nil {
    75  		t.Errorf("Issues.IsAssignee returned error: %v", err)
    76  	}
    77  	if want := false; assignee != want {
    78  		t.Errorf("Issues.IsAssignee returned %+v, want %+v", assignee, want)
    79  	}
    80  }
    81  
    82  func TestIssuesService_IsAssignee_error(t *testing.T) {
    83  	client, mux, _, teardown := setup()
    84  	defer teardown()
    85  
    86  	mux.HandleFunc("/repos/o/r/assignees/u", func(w http.ResponseWriter, r *http.Request) {
    87  		testMethod(t, r, "GET")
    88  		http.Error(w, "BadRequest", http.StatusBadRequest)
    89  	})
    90  
    91  	assignee, _, err := client.Issues.IsAssignee(context.Background(), "o", "r", "u")
    92  	if err == nil {
    93  		t.Errorf("Expected HTTP 400 response")
    94  	}
    95  	if want := false; assignee != want {
    96  		t.Errorf("Issues.IsAssignee returned %+v, want %+v", assignee, want)
    97  	}
    98  }
    99  
   100  func TestIssuesService_IsAssignee_invalidOwner(t *testing.T) {
   101  	client, _, _, teardown := setup()
   102  	defer teardown()
   103  
   104  	_, _, err := client.Issues.IsAssignee(context.Background(), "%", "r", "u")
   105  	testURLParseError(t, err)
   106  }
   107  
   108  func TestIssuesService_AddAssignees(t *testing.T) {
   109  	client, mux, _, teardown := setup()
   110  	defer teardown()
   111  
   112  	mux.HandleFunc("/repos/o/r/issues/1/assignees", func(w http.ResponseWriter, r *http.Request) {
   113  		var assignees struct {
   114  			Assignees []string `json:"assignees,omitempty"`
   115  		}
   116  		json.NewDecoder(r.Body).Decode(&assignees)
   117  
   118  		testMethod(t, r, "POST")
   119  		want := []string{"user1", "user2"}
   120  		if !reflect.DeepEqual(assignees.Assignees, want) {
   121  			t.Errorf("assignees = %+v, want %+v", assignees, want)
   122  		}
   123  		fmt.Fprint(w, `{"number":1,"assignees":[{"login":"user1"},{"login":"user2"}]}`)
   124  	})
   125  
   126  	got, _, err := client.Issues.AddAssignees(context.Background(), "o", "r", 1, []string{"user1", "user2"})
   127  	if err != nil {
   128  		t.Errorf("Issues.AddAssignees returned error: %v", err)
   129  	}
   130  
   131  	want := &Issue{Number: Int(1), Assignees: []*User{{Login: String("user1")}, {Login: String("user2")}}}
   132  	if !reflect.DeepEqual(got, want) {
   133  		t.Errorf("Issues.AddAssignees = %+v, want %+v", got, want)
   134  	}
   135  }
   136  
   137  func TestIssuesService_RemoveAssignees(t *testing.T) {
   138  	client, mux, _, teardown := setup()
   139  	defer teardown()
   140  
   141  	mux.HandleFunc("/repos/o/r/issues/1/assignees", func(w http.ResponseWriter, r *http.Request) {
   142  		var assignees struct {
   143  			Assignees []string `json:"assignees,omitempty"`
   144  		}
   145  		json.NewDecoder(r.Body).Decode(&assignees)
   146  
   147  		testMethod(t, r, "DELETE")
   148  		want := []string{"user1", "user2"}
   149  		if !reflect.DeepEqual(assignees.Assignees, want) {
   150  			t.Errorf("assignees = %+v, want %+v", assignees, want)
   151  		}
   152  		fmt.Fprint(w, `{"number":1,"assignees":[]}`)
   153  	})
   154  
   155  	got, _, err := client.Issues.RemoveAssignees(context.Background(), "o", "r", 1, []string{"user1", "user2"})
   156  	if err != nil {
   157  		t.Errorf("Issues.RemoveAssignees returned error: %v", err)
   158  	}
   159  
   160  	want := &Issue{Number: Int(1), Assignees: []*User{}}
   161  	if !reflect.DeepEqual(got, want) {
   162  		t.Errorf("Issues.RemoveAssignees = %+v, want %+v", got, want)
   163  	}
   164  }