...

Source file src/github.com/google/go-github/v45/github/admin_orgs_test.go

Documentation: github.com/google/go-github/v45/github

     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  	client, mux, _, teardown := setup()
    20  	defer teardown()
    21  
    22  	input := &Organization{
    23  		Login: String("github"),
    24  	}
    25  
    26  	mux.HandleFunc("/admin/organizations", func(w http.ResponseWriter, r *http.Request) {
    27  		v := new(createOrgRequest)
    28  		json.NewDecoder(r.Body).Decode(v)
    29  
    30  		testMethod(t, r, "POST")
    31  		want := &createOrgRequest{Login: String("github"), Admin: String("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: Int64(1), Login: String("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  	client, mux, _, teardown := setup()
    62  	defer teardown()
    63  
    64  	input := &Organization{
    65  		Login: String("o"),
    66  	}
    67  
    68  	mux.HandleFunc("/admin/organizations/o", func(w http.ResponseWriter, r *http.Request) {
    69  		v := new(renameOrgRequest)
    70  		json.NewDecoder(r.Body).Decode(v)
    71  
    72  		testMethod(t, r, "PATCH")
    73  		want := &renameOrgRequest{Login: String("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: String("Job queued to rename organization. It may take a few minutes to complete."), URL: String("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  	client, mux, _, teardown := setup()
   104  	defer teardown()
   105  
   106  	mux.HandleFunc("/admin/organizations/o", func(w http.ResponseWriter, r *http.Request) {
   107  		v := new(renameOrgRequest)
   108  		json.NewDecoder(r.Body).Decode(v)
   109  
   110  		testMethod(t, r, "PATCH")
   111  		want := &renameOrgRequest{Login: String("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: String("Job queued to rename organization. It may take a few minutes to complete."), URL: String("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  	testJSONMarshal(t, &createOrgRequest{}, "{}")
   147  
   148  	u := &createOrgRequest{
   149  		Login: String("l"),
   150  		Admin: String("a"),
   151  	}
   152  
   153  	want := `{
   154  		"login": "l",
   155  		"admin": "a"
   156  	}`
   157  
   158  	testJSONMarshal(t, u, want)
   159  }
   160  
   161  func TestRenameOrgRequest_Marshal(t *testing.T) {
   162  	testJSONMarshal(t, &renameOrgRequest{}, "{}")
   163  
   164  	u := &renameOrgRequest{
   165  		Login: String("l"),
   166  	}
   167  
   168  	want := `{
   169  		"login": "l"
   170  	}`
   171  
   172  	testJSONMarshal(t, u, want)
   173  }
   174  
   175  func TestRenameOrgResponse_Marshal(t *testing.T) {
   176  	testJSONMarshal(t, &renameOrgRequest{}, "{}")
   177  
   178  	u := &RenameOrgResponse{
   179  		Message: String("m"),
   180  		URL:     String("u"),
   181  	}
   182  
   183  	want := `{
   184  		"message": "m",
   185  		"url": "u"
   186  	}`
   187  
   188  	testJSONMarshal(t, u, want)
   189  }
   190  

View as plain text