github.com/google/go-github/v33@v33.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 "reflect" 14 "testing" 15 ) 16 17 func TestAdminOrgs_Create(t *testing.T) { 18 client, mux, _, teardown := setup() 19 defer teardown() 20 21 input := &Organization{ 22 Login: String("github"), 23 } 24 25 mux.HandleFunc("/admin/organizations", func(w http.ResponseWriter, r *http.Request) { 26 v := new(createOrgRequest) 27 json.NewDecoder(r.Body).Decode(v) 28 29 testMethod(t, r, "POST") 30 want := &createOrgRequest{Login: String("github"), Admin: String("ghAdmin")} 31 if !reflect.DeepEqual(v, want) { 32 t.Errorf("Request body = %+v, want %+v", v, want) 33 } 34 35 fmt.Fprint(w, `{"login":"github","id":1}`) 36 }) 37 38 org, _, err := client.Admin.CreateOrg(context.Background(), input, "ghAdmin") 39 if err != nil { 40 t.Errorf("Admin.CreateOrg returned error: %v", err) 41 } 42 43 want := &Organization{ID: Int64(1), Login: String("github")} 44 if !reflect.DeepEqual(org, want) { 45 t.Errorf("Admin.CreateOrg returned %+v, want %+v", org, want) 46 } 47 } 48 49 func TestAdminOrgs_Rename(t *testing.T) { 50 client, mux, _, teardown := setup() 51 defer teardown() 52 53 input := &Organization{ 54 Login: String("o"), 55 } 56 57 mux.HandleFunc("/admin/organizations/o", func(w http.ResponseWriter, r *http.Request) { 58 v := new(renameOrgRequest) 59 json.NewDecoder(r.Body).Decode(v) 60 61 testMethod(t, r, "PATCH") 62 want := &renameOrgRequest{Login: String("the-new-octocats")} 63 if !reflect.DeepEqual(v, want) { 64 t.Errorf("Request body = %+v, want %+v", v, want) 65 } 66 67 fmt.Fprint(w, `{"message":"Job queued to rename organization. It may take a few minutes to complete.","url":"https://<hostname>/api/v3/organizations/1"}`) 68 }) 69 70 resp, _, err := client.Admin.RenameOrg(context.Background(), input, "the-new-octocats") 71 if err != nil { 72 t.Errorf("Admin.RenameOrg returned error: %v", err) 73 } 74 75 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")} 76 if !reflect.DeepEqual(resp, want) { 77 t.Errorf("Admin.RenameOrg returned %+v, want %+v", resp, want) 78 } 79 } 80 81 func TestAdminOrgs_RenameByName(t *testing.T) { 82 client, mux, _, teardown := setup() 83 defer teardown() 84 85 mux.HandleFunc("/admin/organizations/o", func(w http.ResponseWriter, r *http.Request) { 86 v := new(renameOrgRequest) 87 json.NewDecoder(r.Body).Decode(v) 88 89 testMethod(t, r, "PATCH") 90 want := &renameOrgRequest{Login: String("the-new-octocats")} 91 if !reflect.DeepEqual(v, want) { 92 t.Errorf("Request body = %+v, want %+v", v, want) 93 } 94 95 fmt.Fprint(w, `{"message":"Job queued to rename organization. It may take a few minutes to complete.","url":"https://<hostname>/api/v3/organizations/1"}`) 96 }) 97 98 resp, _, err := client.Admin.RenameOrgByName(context.Background(), "o", "the-new-octocats") 99 if err != nil { 100 t.Errorf("Admin.RenameOrg returned error: %v", err) 101 } 102 103 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")} 104 if !reflect.DeepEqual(resp, want) { 105 t.Errorf("Admin.RenameOrg returned %+v, want %+v", resp, want) 106 } 107 }