...

Source file src/github.com/google/go-github/v45/github/orgs_outside_collaborators_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 TestOrganizationsService_ListOutsideCollaborators(t *testing.T) {
    18  	client, mux, _, teardown := setup()
    19  	defer teardown()
    20  
    21  	mux.HandleFunc("/orgs/o/outside_collaborators", func(w http.ResponseWriter, r *http.Request) {
    22  		testMethod(t, r, "GET")
    23  		testFormValues(t, r, values{
    24  			"filter": "2fa_disabled",
    25  			"page":   "2",
    26  		})
    27  		fmt.Fprint(w, `[{"id":1}]`)
    28  	})
    29  
    30  	opt := &ListOutsideCollaboratorsOptions{
    31  		Filter:      "2fa_disabled",
    32  		ListOptions: ListOptions{Page: 2},
    33  	}
    34  	ctx := context.Background()
    35  	members, _, err := client.Organizations.ListOutsideCollaborators(ctx, "o", opt)
    36  	if err != nil {
    37  		t.Errorf("Organizations.ListOutsideCollaborators returned error: %v", err)
    38  	}
    39  
    40  	want := []*User{{ID: Int64(1)}}
    41  	if !cmp.Equal(members, want) {
    42  		t.Errorf("Organizations.ListOutsideCollaborators returned %+v, want %+v", members, want)
    43  	}
    44  
    45  	const methodName = "ListOutsideCollaborators"
    46  	testBadOptions(t, methodName, func() (err error) {
    47  		_, _, err = client.Organizations.ListOutsideCollaborators(ctx, "\n", opt)
    48  		return err
    49  	})
    50  
    51  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    52  		got, resp, err := client.Organizations.ListOutsideCollaborators(ctx, "o", opt)
    53  		if got != nil {
    54  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
    55  		}
    56  		return resp, err
    57  	})
    58  }
    59  
    60  func TestOrganizationsService_ListOutsideCollaborators_invalidOrg(t *testing.T) {
    61  	client, _, _, teardown := setup()
    62  	defer teardown()
    63  
    64  	ctx := context.Background()
    65  	_, _, err := client.Organizations.ListOutsideCollaborators(ctx, "%", nil)
    66  	testURLParseError(t, err)
    67  }
    68  
    69  func TestOrganizationsService_RemoveOutsideCollaborator(t *testing.T) {
    70  	client, mux, _, teardown := setup()
    71  	defer teardown()
    72  
    73  	handler := func(w http.ResponseWriter, r *http.Request) {
    74  		testMethod(t, r, "DELETE")
    75  	}
    76  	mux.HandleFunc("/orgs/o/outside_collaborators/u", handler)
    77  
    78  	ctx := context.Background()
    79  	_, err := client.Organizations.RemoveOutsideCollaborator(ctx, "o", "u")
    80  	if err != nil {
    81  		t.Errorf("Organizations.RemoveOutsideCollaborator returned error: %v", err)
    82  	}
    83  
    84  	const methodName = "RemoveOutsideCollaborator"
    85  	testBadOptions(t, methodName, func() (err error) {
    86  		_, err = client.Organizations.RemoveOutsideCollaborator(ctx, "\n", "\n")
    87  		return err
    88  	})
    89  
    90  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    91  		return client.Organizations.RemoveOutsideCollaborator(ctx, "o", "u")
    92  	})
    93  }
    94  
    95  func TestOrganizationsService_RemoveOutsideCollaborator_NonMember(t *testing.T) {
    96  	client, mux, _, teardown := setup()
    97  	defer teardown()
    98  
    99  	handler := func(w http.ResponseWriter, r *http.Request) {
   100  		testMethod(t, r, "DELETE")
   101  		w.WriteHeader(http.StatusNotFound)
   102  	}
   103  	mux.HandleFunc("/orgs/o/outside_collaborators/u", handler)
   104  
   105  	ctx := context.Background()
   106  	_, err := client.Organizations.RemoveOutsideCollaborator(ctx, "o", "u")
   107  	if err, ok := err.(*ErrorResponse); !ok {
   108  		t.Errorf("Organizations.RemoveOutsideCollaborator did not return an error")
   109  	} else if err.Response.StatusCode != http.StatusNotFound {
   110  		t.Errorf("Organizations.RemoveOutsideCollaborator did not return 404 status code")
   111  	}
   112  }
   113  
   114  func TestOrganizationsService_RemoveOutsideCollaborator_Member(t *testing.T) {
   115  	client, mux, _, teardown := setup()
   116  	defer teardown()
   117  
   118  	handler := func(w http.ResponseWriter, r *http.Request) {
   119  		testMethod(t, r, "DELETE")
   120  		w.WriteHeader(http.StatusUnprocessableEntity)
   121  	}
   122  	mux.HandleFunc("/orgs/o/outside_collaborators/u", handler)
   123  
   124  	ctx := context.Background()
   125  	_, err := client.Organizations.RemoveOutsideCollaborator(ctx, "o", "u")
   126  	if err, ok := err.(*ErrorResponse); !ok {
   127  		t.Errorf("Organizations.RemoveOutsideCollaborator did not return an error")
   128  	} else if err.Response.StatusCode != http.StatusUnprocessableEntity {
   129  		t.Errorf("Organizations.RemoveOutsideCollaborator did not return 422 status code")
   130  	}
   131  }
   132  
   133  func TestOrganizationsService_ConvertMemberToOutsideCollaborator(t *testing.T) {
   134  	client, mux, _, teardown := setup()
   135  	defer teardown()
   136  
   137  	handler := func(w http.ResponseWriter, r *http.Request) {
   138  		testMethod(t, r, "PUT")
   139  	}
   140  	mux.HandleFunc("/orgs/o/outside_collaborators/u", handler)
   141  
   142  	ctx := context.Background()
   143  	_, err := client.Organizations.ConvertMemberToOutsideCollaborator(ctx, "o", "u")
   144  	if err != nil {
   145  		t.Errorf("Organizations.ConvertMemberToOutsideCollaborator returned error: %v", err)
   146  	}
   147  
   148  	const methodName = "ConvertMemberToOutsideCollaborator"
   149  	testBadOptions(t, methodName, func() (err error) {
   150  		_, err = client.Organizations.ConvertMemberToOutsideCollaborator(ctx, "\n", "\n")
   151  		return err
   152  	})
   153  
   154  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   155  		return client.Organizations.ConvertMemberToOutsideCollaborator(ctx, "o", "u")
   156  	})
   157  }
   158  
   159  func TestOrganizationsService_ConvertMemberToOutsideCollaborator_NonMemberOrLastOwner(t *testing.T) {
   160  	client, mux, _, teardown := setup()
   161  	defer teardown()
   162  
   163  	handler := func(w http.ResponseWriter, r *http.Request) {
   164  		testMethod(t, r, "PUT")
   165  		w.WriteHeader(http.StatusForbidden)
   166  	}
   167  	mux.HandleFunc("/orgs/o/outside_collaborators/u", handler)
   168  
   169  	ctx := context.Background()
   170  	_, err := client.Organizations.ConvertMemberToOutsideCollaborator(ctx, "o", "u")
   171  	if err, ok := err.(*ErrorResponse); !ok {
   172  		t.Errorf("Organizations.ConvertMemberToOutsideCollaborator did not return an error")
   173  	} else if err.Response.StatusCode != http.StatusForbidden {
   174  		t.Errorf("Organizations.ConvertMemberToOutsideCollaborator did not return 403 status code")
   175  	}
   176  }
   177  

View as plain text