github.com/google/go-github/v33@v33.0.0/github/issues_labels_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_ListLabels(t *testing.T) {
    18  	client, mux, _, teardown := setup()
    19  	defer teardown()
    20  
    21  	mux.HandleFunc("/repos/o/r/labels", func(w http.ResponseWriter, r *http.Request) {
    22  		testMethod(t, r, "GET")
    23  		testFormValues(t, r, values{"page": "2"})
    24  		fmt.Fprint(w, `[{"name": "a"},{"name": "b"}]`)
    25  	})
    26  
    27  	opt := &ListOptions{Page: 2}
    28  	labels, _, err := client.Issues.ListLabels(context.Background(), "o", "r", opt)
    29  	if err != nil {
    30  		t.Errorf("Issues.ListLabels returned error: %v", err)
    31  	}
    32  
    33  	want := []*Label{{Name: String("a")}, {Name: String("b")}}
    34  	if !reflect.DeepEqual(labels, want) {
    35  		t.Errorf("Issues.ListLabels returned %+v, want %+v", labels, want)
    36  	}
    37  }
    38  
    39  func TestIssuesService_ListLabels_invalidOwner(t *testing.T) {
    40  	client, _, _, teardown := setup()
    41  	defer teardown()
    42  
    43  	_, _, err := client.Issues.ListLabels(context.Background(), "%", "%", nil)
    44  	testURLParseError(t, err)
    45  }
    46  
    47  func TestIssuesService_GetLabel(t *testing.T) {
    48  	client, mux, _, teardown := setup()
    49  	defer teardown()
    50  
    51  	mux.HandleFunc("/repos/o/r/labels/n", func(w http.ResponseWriter, r *http.Request) {
    52  		testMethod(t, r, "GET")
    53  		fmt.Fprint(w, `{"url":"u", "name": "n", "color": "c", "description": "d"}`)
    54  	})
    55  
    56  	label, _, err := client.Issues.GetLabel(context.Background(), "o", "r", "n")
    57  	if err != nil {
    58  		t.Errorf("Issues.GetLabel returned error: %v", err)
    59  	}
    60  
    61  	want := &Label{URL: String("u"), Name: String("n"), Color: String("c"), Description: String("d")}
    62  	if !reflect.DeepEqual(label, want) {
    63  		t.Errorf("Issues.GetLabel returned %+v, want %+v", label, want)
    64  	}
    65  }
    66  
    67  func TestIssuesService_GetLabel_invalidOwner(t *testing.T) {
    68  	client, _, _, teardown := setup()
    69  	defer teardown()
    70  
    71  	_, _, err := client.Issues.GetLabel(context.Background(), "%", "%", "%")
    72  	testURLParseError(t, err)
    73  }
    74  
    75  func TestIssuesService_CreateLabel(t *testing.T) {
    76  	client, mux, _, teardown := setup()
    77  	defer teardown()
    78  
    79  	input := &Label{Name: String("n")}
    80  
    81  	mux.HandleFunc("/repos/o/r/labels", func(w http.ResponseWriter, r *http.Request) {
    82  		v := new(Label)
    83  		json.NewDecoder(r.Body).Decode(v)
    84  
    85  		testMethod(t, r, "POST")
    86  		if !reflect.DeepEqual(v, input) {
    87  			t.Errorf("Request body = %+v, want %+v", v, input)
    88  		}
    89  
    90  		fmt.Fprint(w, `{"url":"u"}`)
    91  	})
    92  
    93  	label, _, err := client.Issues.CreateLabel(context.Background(), "o", "r", input)
    94  	if err != nil {
    95  		t.Errorf("Issues.CreateLabel returned error: %v", err)
    96  	}
    97  
    98  	want := &Label{URL: String("u")}
    99  	if !reflect.DeepEqual(label, want) {
   100  		t.Errorf("Issues.CreateLabel returned %+v, want %+v", label, want)
   101  	}
   102  }
   103  
   104  func TestIssuesService_CreateLabel_invalidOwner(t *testing.T) {
   105  	client, _, _, teardown := setup()
   106  	defer teardown()
   107  
   108  	_, _, err := client.Issues.CreateLabel(context.Background(), "%", "%", nil)
   109  	testURLParseError(t, err)
   110  }
   111  
   112  func TestIssuesService_EditLabel(t *testing.T) {
   113  	client, mux, _, teardown := setup()
   114  	defer teardown()
   115  
   116  	input := &Label{Name: String("z")}
   117  
   118  	mux.HandleFunc("/repos/o/r/labels/n", func(w http.ResponseWriter, r *http.Request) {
   119  		v := new(Label)
   120  		json.NewDecoder(r.Body).Decode(v)
   121  
   122  		testMethod(t, r, "PATCH")
   123  		if !reflect.DeepEqual(v, input) {
   124  			t.Errorf("Request body = %+v, want %+v", v, input)
   125  		}
   126  
   127  		fmt.Fprint(w, `{"url":"u"}`)
   128  	})
   129  
   130  	label, _, err := client.Issues.EditLabel(context.Background(), "o", "r", "n", input)
   131  	if err != nil {
   132  		t.Errorf("Issues.EditLabel returned error: %v", err)
   133  	}
   134  
   135  	want := &Label{URL: String("u")}
   136  	if !reflect.DeepEqual(label, want) {
   137  		t.Errorf("Issues.EditLabel returned %+v, want %+v", label, want)
   138  	}
   139  }
   140  
   141  func TestIssuesService_EditLabel_invalidOwner(t *testing.T) {
   142  	client, _, _, teardown := setup()
   143  	defer teardown()
   144  
   145  	_, _, err := client.Issues.EditLabel(context.Background(), "%", "%", "%", nil)
   146  	testURLParseError(t, err)
   147  }
   148  
   149  func TestIssuesService_DeleteLabel(t *testing.T) {
   150  	client, mux, _, teardown := setup()
   151  	defer teardown()
   152  
   153  	mux.HandleFunc("/repos/o/r/labels/n", func(w http.ResponseWriter, r *http.Request) {
   154  		testMethod(t, r, "DELETE")
   155  	})
   156  
   157  	_, err := client.Issues.DeleteLabel(context.Background(), "o", "r", "n")
   158  	if err != nil {
   159  		t.Errorf("Issues.DeleteLabel returned error: %v", err)
   160  	}
   161  }
   162  
   163  func TestIssuesService_DeleteLabel_invalidOwner(t *testing.T) {
   164  	client, _, _, teardown := setup()
   165  	defer teardown()
   166  
   167  	_, err := client.Issues.DeleteLabel(context.Background(), "%", "%", "%")
   168  	testURLParseError(t, err)
   169  }
   170  
   171  func TestIssuesService_ListLabelsByIssue(t *testing.T) {
   172  	client, mux, _, teardown := setup()
   173  	defer teardown()
   174  
   175  	mux.HandleFunc("/repos/o/r/issues/1/labels", func(w http.ResponseWriter, r *http.Request) {
   176  		testMethod(t, r, "GET")
   177  		testFormValues(t, r, values{"page": "2"})
   178  		fmt.Fprint(w, `[{"name":"a","id":1},{"name":"b","id":2}]`)
   179  	})
   180  
   181  	opt := &ListOptions{Page: 2}
   182  	labels, _, err := client.Issues.ListLabelsByIssue(context.Background(), "o", "r", 1, opt)
   183  	if err != nil {
   184  		t.Errorf("Issues.ListLabelsByIssue returned error: %v", err)
   185  	}
   186  
   187  	want := []*Label{
   188  		{Name: String("a"), ID: Int64(1)},
   189  		{Name: String("b"), ID: Int64(2)},
   190  	}
   191  	if !reflect.DeepEqual(labels, want) {
   192  		t.Errorf("Issues.ListLabelsByIssue returned %+v, want %+v", labels, want)
   193  	}
   194  }
   195  
   196  func TestIssuesService_ListLabelsByIssue_invalidOwner(t *testing.T) {
   197  	client, _, _, teardown := setup()
   198  	defer teardown()
   199  
   200  	_, _, err := client.Issues.ListLabelsByIssue(context.Background(), "%", "%", 1, nil)
   201  	testURLParseError(t, err)
   202  }
   203  
   204  func TestIssuesService_AddLabelsToIssue(t *testing.T) {
   205  	client, mux, _, teardown := setup()
   206  	defer teardown()
   207  
   208  	input := []string{"a", "b"}
   209  
   210  	mux.HandleFunc("/repos/o/r/issues/1/labels", func(w http.ResponseWriter, r *http.Request) {
   211  		var v []string
   212  		json.NewDecoder(r.Body).Decode(&v)
   213  
   214  		testMethod(t, r, "POST")
   215  		if !reflect.DeepEqual(v, input) {
   216  			t.Errorf("Request body = %+v, want %+v", v, input)
   217  		}
   218  
   219  		fmt.Fprint(w, `[{"url":"u"}]`)
   220  	})
   221  
   222  	labels, _, err := client.Issues.AddLabelsToIssue(context.Background(), "o", "r", 1, input)
   223  	if err != nil {
   224  		t.Errorf("Issues.AddLabelsToIssue returned error: %v", err)
   225  	}
   226  
   227  	want := []*Label{{URL: String("u")}}
   228  	if !reflect.DeepEqual(labels, want) {
   229  		t.Errorf("Issues.AddLabelsToIssue returned %+v, want %+v", labels, want)
   230  	}
   231  }
   232  
   233  func TestIssuesService_AddLabelsToIssue_invalidOwner(t *testing.T) {
   234  	client, _, _, teardown := setup()
   235  	defer teardown()
   236  
   237  	_, _, err := client.Issues.AddLabelsToIssue(context.Background(), "%", "%", 1, nil)
   238  	testURLParseError(t, err)
   239  }
   240  
   241  func TestIssuesService_RemoveLabelForIssue(t *testing.T) {
   242  	client, mux, _, teardown := setup()
   243  	defer teardown()
   244  
   245  	mux.HandleFunc("/repos/o/r/issues/1/labels/l", func(w http.ResponseWriter, r *http.Request) {
   246  		testMethod(t, r, "DELETE")
   247  	})
   248  
   249  	_, err := client.Issues.RemoveLabelForIssue(context.Background(), "o", "r", 1, "l")
   250  	if err != nil {
   251  		t.Errorf("Issues.RemoveLabelForIssue returned error: %v", err)
   252  	}
   253  }
   254  
   255  func TestIssuesService_RemoveLabelForIssue_invalidOwner(t *testing.T) {
   256  	client, _, _, teardown := setup()
   257  	defer teardown()
   258  
   259  	_, err := client.Issues.RemoveLabelForIssue(context.Background(), "%", "%", 1, "%")
   260  	testURLParseError(t, err)
   261  }
   262  
   263  func TestIssuesService_ReplaceLabelsForIssue(t *testing.T) {
   264  	client, mux, _, teardown := setup()
   265  	defer teardown()
   266  
   267  	input := []string{"a", "b"}
   268  
   269  	mux.HandleFunc("/repos/o/r/issues/1/labels", func(w http.ResponseWriter, r *http.Request) {
   270  		var v []string
   271  		json.NewDecoder(r.Body).Decode(&v)
   272  
   273  		testMethod(t, r, "PUT")
   274  		if !reflect.DeepEqual(v, input) {
   275  			t.Errorf("Request body = %+v, want %+v", v, input)
   276  		}
   277  
   278  		fmt.Fprint(w, `[{"url":"u"}]`)
   279  	})
   280  
   281  	labels, _, err := client.Issues.ReplaceLabelsForIssue(context.Background(), "o", "r", 1, input)
   282  	if err != nil {
   283  		t.Errorf("Issues.ReplaceLabelsForIssue returned error: %v", err)
   284  	}
   285  
   286  	want := []*Label{{URL: String("u")}}
   287  	if !reflect.DeepEqual(labels, want) {
   288  		t.Errorf("Issues.ReplaceLabelsForIssue returned %+v, want %+v", labels, want)
   289  	}
   290  }
   291  
   292  func TestIssuesService_ReplaceLabelsForIssue_invalidOwner(t *testing.T) {
   293  	client, _, _, teardown := setup()
   294  	defer teardown()
   295  
   296  	_, _, err := client.Issues.ReplaceLabelsForIssue(context.Background(), "%", "%", 1, nil)
   297  	testURLParseError(t, err)
   298  }
   299  
   300  func TestIssuesService_RemoveLabelsForIssue(t *testing.T) {
   301  	client, mux, _, teardown := setup()
   302  	defer teardown()
   303  
   304  	mux.HandleFunc("/repos/o/r/issues/1/labels", func(w http.ResponseWriter, r *http.Request) {
   305  		testMethod(t, r, "DELETE")
   306  	})
   307  
   308  	_, err := client.Issues.RemoveLabelsForIssue(context.Background(), "o", "r", 1)
   309  	if err != nil {
   310  		t.Errorf("Issues.RemoveLabelsForIssue returned error: %v", err)
   311  	}
   312  }
   313  
   314  func TestIssuesService_RemoveLabelsForIssue_invalidOwner(t *testing.T) {
   315  	client, _, _, teardown := setup()
   316  	defer teardown()
   317  
   318  	_, err := client.Issues.RemoveLabelsForIssue(context.Background(), "%", "%", 1)
   319  	testURLParseError(t, err)
   320  }
   321  
   322  func TestIssuesService_ListLabelsForMilestone(t *testing.T) {
   323  	client, mux, _, teardown := setup()
   324  	defer teardown()
   325  
   326  	mux.HandleFunc("/repos/o/r/milestones/1/labels", func(w http.ResponseWriter, r *http.Request) {
   327  		testMethod(t, r, "GET")
   328  		testFormValues(t, r, values{"page": "2"})
   329  		fmt.Fprint(w, `[{"name": "a"},{"name": "b"}]`)
   330  	})
   331  
   332  	opt := &ListOptions{Page: 2}
   333  	labels, _, err := client.Issues.ListLabelsForMilestone(context.Background(), "o", "r", 1, opt)
   334  	if err != nil {
   335  		t.Errorf("Issues.ListLabelsForMilestone returned error: %v", err)
   336  	}
   337  
   338  	want := []*Label{{Name: String("a")}, {Name: String("b")}}
   339  	if !reflect.DeepEqual(labels, want) {
   340  		t.Errorf("Issues.ListLabelsForMilestone returned %+v, want %+v", labels, want)
   341  	}
   342  }
   343  
   344  func TestIssuesService_ListLabelsForMilestone_invalidOwner(t *testing.T) {
   345  	client, _, _, teardown := setup()
   346  	defer teardown()
   347  
   348  	_, _, err := client.Issues.ListLabelsForMilestone(context.Background(), "%", "%", 1, nil)
   349  	testURLParseError(t, err)
   350  }