github.com/google/go-github/v33@v33.0.0/github/admin_test.go (about) 1 // Copyright 2016 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 "time" 16 ) 17 18 func TestAdminService_UpdateUserLDAPMapping(t *testing.T) { 19 client, mux, _, teardown := setup() 20 defer teardown() 21 22 input := &UserLDAPMapping{ 23 LDAPDN: String("uid=asdf,ou=users,dc=github,dc=com"), 24 } 25 26 mux.HandleFunc("/admin/ldap/users/u/mapping", func(w http.ResponseWriter, r *http.Request) { 27 v := new(UserLDAPMapping) 28 json.NewDecoder(r.Body).Decode(v) 29 30 testMethod(t, r, "PATCH") 31 if !reflect.DeepEqual(v, input) { 32 t.Errorf("Request body = %+v, want %+v", v, input) 33 } 34 fmt.Fprint(w, `{"id":1,"ldap_dn":"uid=asdf,ou=users,dc=github,dc=com"}`) 35 }) 36 37 ctx := context.Background() 38 mapping, _, err := client.Admin.UpdateUserLDAPMapping(ctx, "u", input) 39 if err != nil { 40 t.Errorf("Admin.UpdateUserLDAPMapping returned error: %v", err) 41 } 42 43 want := &UserLDAPMapping{ 44 ID: Int64(1), 45 LDAPDN: String("uid=asdf,ou=users,dc=github,dc=com"), 46 } 47 if !reflect.DeepEqual(mapping, want) { 48 t.Errorf("Admin.UpdateUserLDAPMapping returned %+v, want %+v", mapping, want) 49 } 50 51 // Test s.client.NewRequest failure 52 client.BaseURL.Path = "" 53 got, resp, err := client.Admin.UpdateUserLDAPMapping(ctx, "u", input) 54 if got != nil { 55 t.Errorf("client.BaseURL.Path='' UpdateUserLDAPMapping = %#v, want nil", got) 56 } 57 if resp != nil { 58 t.Errorf("client.BaseURL.Path='' UpdateUserLDAPMapping resp = %#v, want nil", resp) 59 } 60 if err == nil { 61 t.Error("client.BaseURL.Path='' UpdateUserLDAPMapping err = nil, want error") 62 } 63 64 // Test s.client.Do failure 65 client.BaseURL.Path = "/api-v3/" 66 client.rateLimits[0].Reset.Time = time.Now().Add(10 * time.Minute) 67 got, resp, err = client.Admin.UpdateUserLDAPMapping(ctx, "u", input) 68 if got != nil { 69 t.Errorf("rate.Reset.Time > now UpdateUserLDAPMapping = %#v, want nil", got) 70 } 71 if want := http.StatusForbidden; resp == nil || resp.Response.StatusCode != want { 72 t.Errorf("rate.Reset.Time > now UpdateUserLDAPMapping resp = %#v, want StatusCode=%v", resp.Response, want) 73 } 74 if err == nil { 75 t.Error("rate.Reset.Time > now UpdateUserLDAPMapping err = nil, want error") 76 } 77 } 78 79 func TestAdminService_UpdateTeamLDAPMapping(t *testing.T) { 80 client, mux, _, teardown := setup() 81 defer teardown() 82 83 input := &TeamLDAPMapping{ 84 LDAPDN: String("cn=Enterprise Ops,ou=teams,dc=github,dc=com"), 85 } 86 87 mux.HandleFunc("/admin/ldap/teams/1/mapping", func(w http.ResponseWriter, r *http.Request) { 88 v := new(TeamLDAPMapping) 89 json.NewDecoder(r.Body).Decode(v) 90 91 testMethod(t, r, "PATCH") 92 if !reflect.DeepEqual(v, input) { 93 t.Errorf("Request body = %+v, want %+v", v, input) 94 } 95 fmt.Fprint(w, `{"id":1,"ldap_dn":"cn=Enterprise Ops,ou=teams,dc=github,dc=com"}`) 96 }) 97 98 ctx := context.Background() 99 mapping, _, err := client.Admin.UpdateTeamLDAPMapping(ctx, 1, input) 100 if err != nil { 101 t.Errorf("Admin.UpdateTeamLDAPMapping returned error: %v", err) 102 } 103 104 want := &TeamLDAPMapping{ 105 ID: Int64(1), 106 LDAPDN: String("cn=Enterprise Ops,ou=teams,dc=github,dc=com"), 107 } 108 if !reflect.DeepEqual(mapping, want) { 109 t.Errorf("Admin.UpdateTeamLDAPMapping returned %+v, want %+v", mapping, want) 110 } 111 112 // Test s.client.NewRequest failure 113 client.BaseURL.Path = "" 114 got, resp, err := client.Admin.UpdateTeamLDAPMapping(ctx, 1, input) 115 if got != nil { 116 t.Errorf("client.BaseURL.Path='' UpdateTeamLDAPMapping = %#v, want nil", got) 117 } 118 if resp != nil { 119 t.Errorf("client.BaseURL.Path='' UpdateTeamLDAPMapping resp = %#v, want nil", resp) 120 } 121 if err == nil { 122 t.Error("client.BaseURL.Path='' UpdateTeamLDAPMapping err = nil, want error") 123 } 124 125 // Test s.client.Do failure 126 client.BaseURL.Path = "/api-v3/" 127 client.rateLimits[0].Reset.Time = time.Now().Add(10 * time.Minute) 128 got, resp, err = client.Admin.UpdateTeamLDAPMapping(ctx, 1, input) 129 if got != nil { 130 t.Errorf("rate.Reset.Time > now UpdateTeamLDAPMapping = %#v, want nil", got) 131 } 132 if want := http.StatusForbidden; resp == nil || resp.Response.StatusCode != want { 133 t.Errorf("rate.Reset.Time > now UpdateTeamLDAPMapping resp = %#v, want StatusCode=%v", resp.Response, want) 134 } 135 if err == nil { 136 t.Error("rate.Reset.Time > now UpdateTeamLDAPMapping err = nil, want error") 137 } 138 } 139 140 func TestAdminService_TeamLDAPMapping_String(t *testing.T) { 141 v := &TeamLDAPMapping{ 142 ID: Int64(1), 143 LDAPDN: String("a"), 144 URL: String("b"), 145 Name: String("c"), 146 Slug: String("d"), 147 Description: String("e"), 148 Privacy: String("f"), 149 Permission: String("g"), 150 MembersURL: String("h"), 151 RepositoriesURL: String("i"), 152 } 153 154 want := `github.TeamLDAPMapping{ID:1, LDAPDN:"a", URL:"b", Name:"c", Slug:"d", Description:"e", Privacy:"f", Permission:"g", MembersURL:"h", RepositoriesURL:"i"}` 155 if got := v.String(); got != want { 156 t.Errorf("TeamLDAPMapping.String = `%v`, want `%v`", got, want) 157 } 158 } 159 160 func TestAdminService_UserLDAPMapping_String(t *testing.T) { 161 v := &UserLDAPMapping{ 162 ID: Int64(1), 163 LDAPDN: String("a"), 164 Login: String("b"), 165 AvatarURL: String("c"), 166 GravatarID: String("d"), 167 Type: String("e"), 168 SiteAdmin: Bool(true), 169 URL: String("f"), 170 EventsURL: String("g"), 171 FollowingURL: String("h"), 172 FollowersURL: String("i"), 173 GistsURL: String("j"), 174 OrganizationsURL: String("k"), 175 ReceivedEventsURL: String("l"), 176 ReposURL: String("m"), 177 StarredURL: String("n"), 178 SubscriptionsURL: String("o"), 179 } 180 181 want := `github.UserLDAPMapping{ID:1, LDAPDN:"a", Login:"b", AvatarURL:"c", GravatarID:"d", Type:"e", SiteAdmin:true, URL:"f", EventsURL:"g", FollowingURL:"h", FollowersURL:"i", GistsURL:"j", OrganizationsURL:"k", ReceivedEventsURL:"l", ReposURL:"m", StarredURL:"n", SubscriptionsURL:"o"}` 182 if got := v.String(); got != want { 183 t.Errorf("UserLDAPMapping.String = `%v`, want `%v`", got, want) 184 } 185 }