github.com/google/go-github/v33@v33.0.0/github/orgs_outside_collaborators_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  	"reflect"
    13  	"testing"
    14  )
    15  
    16  func TestOrganizationsService_ListOutsideCollaborators(t *testing.T) {
    17  	client, mux, _, teardown := setup()
    18  	defer teardown()
    19  
    20  	mux.HandleFunc("/orgs/o/outside_collaborators", func(w http.ResponseWriter, r *http.Request) {
    21  		testMethod(t, r, "GET")
    22  		testFormValues(t, r, values{
    23  			"filter": "2fa_disabled",
    24  			"page":   "2",
    25  		})
    26  		fmt.Fprint(w, `[{"id":1}]`)
    27  	})
    28  
    29  	opt := &ListOutsideCollaboratorsOptions{
    30  		Filter:      "2fa_disabled",
    31  		ListOptions: ListOptions{Page: 2},
    32  	}
    33  	members, _, err := client.Organizations.ListOutsideCollaborators(context.Background(), "o", opt)
    34  	if err != nil {
    35  		t.Errorf("Organizations.ListOutsideCollaborators returned error: %v", err)
    36  	}
    37  
    38  	want := []*User{{ID: Int64(1)}}
    39  	if !reflect.DeepEqual(members, want) {
    40  		t.Errorf("Organizations.ListOutsideCollaborators returned %+v, want %+v", members, want)
    41  	}
    42  }
    43  
    44  func TestOrganizationsService_ListOutsideCollaborators_invalidOrg(t *testing.T) {
    45  	client, _, _, teardown := setup()
    46  	defer teardown()
    47  
    48  	_, _, err := client.Organizations.ListOutsideCollaborators(context.Background(), "%", nil)
    49  	testURLParseError(t, err)
    50  }
    51  
    52  func TestOrganizationsService_RemoveOutsideCollaborator(t *testing.T) {
    53  	client, mux, _, teardown := setup()
    54  	defer teardown()
    55  
    56  	handler := func(w http.ResponseWriter, r *http.Request) {
    57  		testMethod(t, r, "DELETE")
    58  	}
    59  	mux.HandleFunc("/orgs/o/outside_collaborators/u", handler)
    60  
    61  	_, err := client.Organizations.RemoveOutsideCollaborator(context.Background(), "o", "u")
    62  	if err != nil {
    63  		t.Errorf("Organizations.RemoveOutsideCollaborator returned error: %v", err)
    64  	}
    65  }
    66  
    67  func TestOrganizationsService_RemoveOutsideCollaborator_NonMember(t *testing.T) {
    68  	client, mux, _, teardown := setup()
    69  	defer teardown()
    70  
    71  	handler := func(w http.ResponseWriter, r *http.Request) {
    72  		testMethod(t, r, "DELETE")
    73  		w.WriteHeader(http.StatusNotFound)
    74  	}
    75  	mux.HandleFunc("/orgs/o/outside_collaborators/u", handler)
    76  
    77  	_, err := client.Organizations.RemoveOutsideCollaborator(context.Background(), "o", "u")
    78  	if err, ok := err.(*ErrorResponse); !ok {
    79  		t.Errorf("Organizations.RemoveOutsideCollaborator did not return an error")
    80  	} else if err.Response.StatusCode != http.StatusNotFound {
    81  		t.Errorf("Organizations.RemoveOutsideCollaborator did not return 404 status code")
    82  	}
    83  }
    84  
    85  func TestOrganizationsService_RemoveOutsideCollaborator_Member(t *testing.T) {
    86  	client, mux, _, teardown := setup()
    87  	defer teardown()
    88  
    89  	handler := func(w http.ResponseWriter, r *http.Request) {
    90  		testMethod(t, r, "DELETE")
    91  		w.WriteHeader(http.StatusUnprocessableEntity)
    92  	}
    93  	mux.HandleFunc("/orgs/o/outside_collaborators/u", handler)
    94  
    95  	_, err := client.Organizations.RemoveOutsideCollaborator(context.Background(), "o", "u")
    96  	if err, ok := err.(*ErrorResponse); !ok {
    97  		t.Errorf("Organizations.RemoveOutsideCollaborator did not return an error")
    98  	} else if err.Response.StatusCode != http.StatusUnprocessableEntity {
    99  		t.Errorf("Organizations.RemoveOutsideCollaborator did not return 422 status code")
   100  	}
   101  }
   102  
   103  func TestOrganizationsService_ConvertMemberToOutsideCollaborator(t *testing.T) {
   104  	client, mux, _, teardown := setup()
   105  	defer teardown()
   106  
   107  	handler := func(w http.ResponseWriter, r *http.Request) {
   108  		testMethod(t, r, "PUT")
   109  	}
   110  	mux.HandleFunc("/orgs/o/outside_collaborators/u", handler)
   111  
   112  	_, err := client.Organizations.ConvertMemberToOutsideCollaborator(context.Background(), "o", "u")
   113  	if err != nil {
   114  		t.Errorf("Organizations.ConvertMemberToOutsideCollaborator returned error: %v", err)
   115  	}
   116  }
   117  
   118  func TestOrganizationsService_ConvertMemberToOutsideCollaborator_NonMemberOrLastOwner(t *testing.T) {
   119  	client, mux, _, teardown := setup()
   120  	defer teardown()
   121  
   122  	handler := func(w http.ResponseWriter, r *http.Request) {
   123  		testMethod(t, r, "PUT")
   124  		w.WriteHeader(http.StatusForbidden)
   125  	}
   126  	mux.HandleFunc("/orgs/o/outside_collaborators/u", handler)
   127  
   128  	_, err := client.Organizations.ConvertMemberToOutsideCollaborator(context.Background(), "o", "u")
   129  	if err, ok := err.(*ErrorResponse); !ok {
   130  		t.Errorf("Organizations.ConvertMemberToOutsideCollaborator did not return an error")
   131  	} else if err.Response.StatusCode != http.StatusForbidden {
   132  		t.Errorf("Organizations.ConvertMemberToOutsideCollaborator did not return 403 status code")
   133  	}
   134  }