github.com/google/go-github/v71@v71.0.0/github/admin_orgs_test.go (about)

     1  // Copyright 2019 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  	"testing"
    14  
    15  	"github.com/google/go-cmp/cmp"
    16  )
    17  
    18  func TestAdminOrgs_Create(t *testing.T) {
    19  	t.Parallel()
    20  	client, mux, _ := setup(t)
    21  
    22  	input := &Organization{
    23  		Login: Ptr("github"),
    24  	}
    25  
    26  	mux.HandleFunc("/admin/organizations", func(w http.ResponseWriter, r *http.Request) {
    27  		v := new(createOrgRequest)
    28  		assertNilError(t, json.NewDecoder(r.Body).Decode(v))
    29  
    30  		testMethod(t, r, "POST")
    31  		want := &createOrgRequest{Login: Ptr("github"), Admin: Ptr("ghAdmin")}
    32  		if !cmp.Equal(v, want) {
    33  			t.Errorf("Request body = %+v, want %+v", v, want)
    34  		}
    35  
    36  		fmt.Fprint(w, `{"login":"github","id":1}`)
    37  	})
    38  
    39  	ctx := context.Background()
    40  	org, _, err := client.Admin.CreateOrg(ctx, input, "ghAdmin")
    41  	if err != nil {
    42  		t.Errorf("Admin.CreateOrg returned error: %v", err)
    43  	}
    44  
    45  	want := &Organization{ID: Ptr(int64(1)), Login: Ptr("github")}
    46  	if !cmp.Equal(org, want) {
    47  		t.Errorf("Admin.CreateOrg returned %+v, want %+v", org, want)
    48  	}
    49  
    50  	const methodName = "CreateOrg"
    51  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    52  		got, resp, err := client.Admin.CreateOrg(ctx, input, "ghAdmin")
    53  		if got != nil {
    54  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
    55  		}
    56  		return resp, err
    57  	})
    58  }
    59  
    60  func TestAdminOrgs_Rename(t *testing.T) {
    61  	t.Parallel()
    62  	client, mux, _ := setup(t)
    63  
    64  	input := &Organization{
    65  		Login: Ptr("o"),
    66  	}
    67  
    68  	mux.HandleFunc("/admin/organizations/o", func(w http.ResponseWriter, r *http.Request) {
    69  		v := new(renameOrgRequest)
    70  		assertNilError(t, json.NewDecoder(r.Body).Decode(v))
    71  
    72  		testMethod(t, r, "PATCH")
    73  		want := &renameOrgRequest{Login: Ptr("the-new-octocats")}
    74  		if !cmp.Equal(v, want) {
    75  			t.Errorf("Request body = %+v, want %+v", v, want)
    76  		}
    77  
    78  		fmt.Fprint(w, `{"message":"Job queued to rename organization. It may take a few minutes to complete.","url":"https://<hostname>/api/v3/organizations/1"}`)
    79  	})
    80  
    81  	ctx := context.Background()
    82  	resp, _, err := client.Admin.RenameOrg(ctx, input, "the-new-octocats")
    83  	if err != nil {
    84  		t.Errorf("Admin.RenameOrg returned error: %v", err)
    85  	}
    86  
    87  	want := &RenameOrgResponse{Message: Ptr("Job queued to rename organization. It may take a few minutes to complete."), URL: Ptr("https://<hostname>/api/v3/organizations/1")}
    88  	if !cmp.Equal(resp, want) {
    89  		t.Errorf("Admin.RenameOrg returned %+v, want %+v", resp, want)
    90  	}
    91  
    92  	const methodName = "RenameOrg"
    93  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    94  		got, resp, err := client.Admin.RenameOrg(ctx, input, "the-new-octocats")
    95  		if got != nil {
    96  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
    97  		}
    98  		return resp, err
    99  	})
   100  }
   101  
   102  func TestAdminOrgs_RenameByName(t *testing.T) {
   103  	t.Parallel()
   104  	client, mux, _ := setup(t)
   105  
   106  	mux.HandleFunc("/admin/organizations/o", func(w http.ResponseWriter, r *http.Request) {
   107  		v := new(renameOrgRequest)
   108  		assertNilError(t, json.NewDecoder(r.Body).Decode(v))
   109  
   110  		testMethod(t, r, "PATCH")
   111  		want := &renameOrgRequest{Login: Ptr("the-new-octocats")}
   112  		if !cmp.Equal(v, want) {
   113  			t.Errorf("Request body = %+v, want %+v", v, want)
   114  		}
   115  
   116  		fmt.Fprint(w, `{"message":"Job queued to rename organization. It may take a few minutes to complete.","url":"https://<hostname>/api/v3/organizations/1"}`)
   117  	})
   118  
   119  	ctx := context.Background()
   120  	resp, _, err := client.Admin.RenameOrgByName(ctx, "o", "the-new-octocats")
   121  	if err != nil {
   122  		t.Errorf("Admin.RenameOrg returned error: %v", err)
   123  	}
   124  
   125  	want := &RenameOrgResponse{Message: Ptr("Job queued to rename organization. It may take a few minutes to complete."), URL: Ptr("https://<hostname>/api/v3/organizations/1")}
   126  	if !cmp.Equal(resp, want) {
   127  		t.Errorf("Admin.RenameOrg returned %+v, want %+v", resp, want)
   128  	}
   129  
   130  	const methodName = "RenameOrgByName"
   131  	testBadOptions(t, methodName, func() (err error) {
   132  		_, _, err = client.Admin.RenameOrgByName(ctx, "\n", "\n")
   133  		return err
   134  	})
   135  
   136  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   137  		got, resp, err := client.Admin.RenameOrgByName(ctx, "o", "the-new-octocats")
   138  		if got != nil {
   139  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   140  		}
   141  		return resp, err
   142  	})
   143  }
   144  
   145  func TestCreateOrgRequest_Marshal(t *testing.T) {
   146  	t.Parallel()
   147  	testJSONMarshal(t, &createOrgRequest{}, "{}")
   148  
   149  	u := &createOrgRequest{
   150  		Login: Ptr("l"),
   151  		Admin: Ptr("a"),
   152  	}
   153  
   154  	want := `{
   155  		"login": "l",
   156  		"admin": "a"
   157  	}`
   158  
   159  	testJSONMarshal(t, u, want)
   160  }
   161  
   162  func TestRenameOrgRequest_Marshal(t *testing.T) {
   163  	t.Parallel()
   164  	testJSONMarshal(t, &renameOrgRequest{}, "{}")
   165  
   166  	u := &renameOrgRequest{
   167  		Login: Ptr("l"),
   168  	}
   169  
   170  	want := `{
   171  		"login": "l"
   172  	}`
   173  
   174  	testJSONMarshal(t, u, want)
   175  }
   176  
   177  func TestRenameOrgResponse_Marshal(t *testing.T) {
   178  	t.Parallel()
   179  	testJSONMarshal(t, &renameOrgRequest{}, "{}")
   180  
   181  	u := &RenameOrgResponse{
   182  		Message: Ptr("m"),
   183  		URL:     Ptr("u"),
   184  	}
   185  
   186  	want := `{
   187  		"message": "m",
   188  		"url": "u"
   189  	}`
   190  
   191  	testJSONMarshal(t, u, want)
   192  }