github.com/google/go-github/v33@v33.0.0/github/repos_collaborators_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 TestRepositoriesService_ListCollaborators(t *testing.T) {
    18  	client, mux, _, teardown := setup()
    19  	defer teardown()
    20  
    21  	mux.HandleFunc("/repos/o/r/collaborators", func(w http.ResponseWriter, r *http.Request) {
    22  		testMethod(t, r, "GET")
    23  		testFormValues(t, r, values{"page": "2"})
    24  		fmt.Fprintf(w, `[{"id":1}, {"id":2}]`)
    25  	})
    26  
    27  	opt := &ListCollaboratorsOptions{
    28  		ListOptions: ListOptions{Page: 2},
    29  	}
    30  	users, _, err := client.Repositories.ListCollaborators(context.Background(), "o", "r", opt)
    31  	if err != nil {
    32  		t.Errorf("Repositories.ListCollaborators returned error: %v", err)
    33  	}
    34  
    35  	want := []*User{{ID: Int64(1)}, {ID: Int64(2)}}
    36  	if !reflect.DeepEqual(users, want) {
    37  		t.Errorf("Repositories.ListCollaborators returned %+v, want %+v", users, want)
    38  	}
    39  }
    40  
    41  func TestRepositoriesService_ListCollaborators_withAffiliation(t *testing.T) {
    42  	client, mux, _, teardown := setup()
    43  	defer teardown()
    44  
    45  	mux.HandleFunc("/repos/o/r/collaborators", func(w http.ResponseWriter, r *http.Request) {
    46  		testMethod(t, r, "GET")
    47  		testFormValues(t, r, values{"affiliation": "all", "page": "2"})
    48  		fmt.Fprintf(w, `[{"id":1}, {"id":2}]`)
    49  	})
    50  
    51  	opt := &ListCollaboratorsOptions{
    52  		ListOptions: ListOptions{Page: 2},
    53  		Affiliation: "all",
    54  	}
    55  	users, _, err := client.Repositories.ListCollaborators(context.Background(), "o", "r", opt)
    56  	if err != nil {
    57  		t.Errorf("Repositories.ListCollaborators returned error: %v", err)
    58  	}
    59  
    60  	want := []*User{{ID: Int64(1)}, {ID: Int64(2)}}
    61  	if !reflect.DeepEqual(users, want) {
    62  		t.Errorf("Repositories.ListCollaborators returned %+v, want %+v", users, want)
    63  	}
    64  }
    65  
    66  func TestRepositoriesService_ListCollaborators_invalidOwner(t *testing.T) {
    67  	client, _, _, teardown := setup()
    68  	defer teardown()
    69  
    70  	_, _, err := client.Repositories.ListCollaborators(context.Background(), "%", "%", nil)
    71  	testURLParseError(t, err)
    72  }
    73  
    74  func TestRepositoriesService_IsCollaborator_True(t *testing.T) {
    75  	client, mux, _, teardown := setup()
    76  	defer teardown()
    77  
    78  	mux.HandleFunc("/repos/o/r/collaborators/u", func(w http.ResponseWriter, r *http.Request) {
    79  		testMethod(t, r, "GET")
    80  		w.WriteHeader(http.StatusNoContent)
    81  	})
    82  
    83  	isCollab, _, err := client.Repositories.IsCollaborator(context.Background(), "o", "r", "u")
    84  	if err != nil {
    85  		t.Errorf("Repositories.IsCollaborator returned error: %v", err)
    86  	}
    87  
    88  	if !isCollab {
    89  		t.Errorf("Repositories.IsCollaborator returned false, want true")
    90  	}
    91  }
    92  
    93  func TestRepositoriesService_IsCollaborator_False(t *testing.T) {
    94  	client, mux, _, teardown := setup()
    95  	defer teardown()
    96  
    97  	mux.HandleFunc("/repos/o/r/collaborators/u", func(w http.ResponseWriter, r *http.Request) {
    98  		testMethod(t, r, "GET")
    99  		w.WriteHeader(http.StatusNotFound)
   100  	})
   101  
   102  	isCollab, _, err := client.Repositories.IsCollaborator(context.Background(), "o", "r", "u")
   103  	if err != nil {
   104  		t.Errorf("Repositories.IsCollaborator returned error: %v", err)
   105  	}
   106  
   107  	if isCollab {
   108  		t.Errorf("Repositories.IsCollaborator returned true, want false")
   109  	}
   110  }
   111  
   112  func TestRepositoriesService_IsCollaborator_invalidUser(t *testing.T) {
   113  	client, _, _, teardown := setup()
   114  	defer teardown()
   115  
   116  	_, _, err := client.Repositories.IsCollaborator(context.Background(), "%", "%", "%")
   117  	testURLParseError(t, err)
   118  }
   119  
   120  func TestRepositoryService_GetPermissionLevel(t *testing.T) {
   121  	client, mux, _, teardown := setup()
   122  	defer teardown()
   123  
   124  	mux.HandleFunc("/repos/o/r/collaborators/u/permission", func(w http.ResponseWriter, r *http.Request) {
   125  		testMethod(t, r, "GET")
   126  		fmt.Fprintf(w, `{"permission":"admin","user":{"login":"u"}}`)
   127  	})
   128  
   129  	rpl, _, err := client.Repositories.GetPermissionLevel(context.Background(), "o", "r", "u")
   130  	if err != nil {
   131  		t.Errorf("Repositories.GetPermissionLevel returned error: %v", err)
   132  	}
   133  
   134  	want := &RepositoryPermissionLevel{
   135  		Permission: String("admin"),
   136  		User: &User{
   137  			Login: String("u"),
   138  		},
   139  	}
   140  
   141  	if !reflect.DeepEqual(rpl, want) {
   142  		t.Errorf("Repositories.GetPermissionLevel returned %+v, want %+v", rpl, want)
   143  	}
   144  }
   145  
   146  func TestRepositoriesService_AddCollaborator(t *testing.T) {
   147  	client, mux, _, teardown := setup()
   148  	defer teardown()
   149  
   150  	opt := &RepositoryAddCollaboratorOptions{Permission: "admin"}
   151  	mux.HandleFunc("/repos/o/r/collaborators/u", func(w http.ResponseWriter, r *http.Request) {
   152  		v := new(RepositoryAddCollaboratorOptions)
   153  		json.NewDecoder(r.Body).Decode(v)
   154  		testMethod(t, r, "PUT")
   155  		if !reflect.DeepEqual(v, opt) {
   156  			t.Errorf("Request body = %+v, want %+v", v, opt)
   157  		}
   158  		w.WriteHeader(http.StatusOK)
   159  		w.Write([]byte(`{"permissions": "write","url": "https://api.github.com/user/repository_invitations/1296269","html_url": "https://github.com/octocat/Hello-World/invitations","id":1,"permissions":"write","repository":{"url":"s","name":"r","id":1},"invitee":{"login":"u"},"inviter":{"login":"o"}}`))
   160  	})
   161  	collaboratorInvitation, _, err := client.Repositories.AddCollaborator(context.Background(), "o", "r", "u", opt)
   162  	if err != nil {
   163  		t.Errorf("Repositories.AddCollaborator returned error: %v", err)
   164  	}
   165  	want := &CollaboratorInvitation{
   166  		ID: Int64(1),
   167  		Repo: &Repository{
   168  			ID:   Int64(1),
   169  			URL:  String("s"),
   170  			Name: String("r"),
   171  		},
   172  		Invitee: &User{
   173  			Login: String("u"),
   174  		},
   175  		Inviter: &User{
   176  			Login: String("o"),
   177  		},
   178  		Permissions: String("write"),
   179  		URL:         String("https://api.github.com/user/repository_invitations/1296269"),
   180  		HTMLURL:     String("https://github.com/octocat/Hello-World/invitations"),
   181  	}
   182  
   183  	if !reflect.DeepEqual(collaboratorInvitation, want) {
   184  		t.Errorf("AddCollaborator returned %+v, want %+v", collaboratorInvitation, want)
   185  	}
   186  }
   187  
   188  func TestRepositoriesService_AddCollaborator_invalidUser(t *testing.T) {
   189  	client, _, _, teardown := setup()
   190  	defer teardown()
   191  
   192  	_, _, err := client.Repositories.AddCollaborator(context.Background(), "%", "%", "%", nil)
   193  	testURLParseError(t, err)
   194  }
   195  
   196  func TestRepositoriesService_RemoveCollaborator(t *testing.T) {
   197  	client, mux, _, teardown := setup()
   198  	defer teardown()
   199  
   200  	mux.HandleFunc("/repos/o/r/collaborators/u", func(w http.ResponseWriter, r *http.Request) {
   201  		testMethod(t, r, "DELETE")
   202  		w.WriteHeader(http.StatusNoContent)
   203  	})
   204  
   205  	_, err := client.Repositories.RemoveCollaborator(context.Background(), "o", "r", "u")
   206  	if err != nil {
   207  		t.Errorf("Repositories.RemoveCollaborator returned error: %v", err)
   208  	}
   209  }
   210  
   211  func TestRepositoriesService_RemoveCollaborator_invalidUser(t *testing.T) {
   212  	client, _, _, teardown := setup()
   213  	defer teardown()
   214  
   215  	_, err := client.Repositories.RemoveCollaborator(context.Background(), "%", "%", "%")
   216  	testURLParseError(t, err)
   217  }