github.com/google/go-github/v74@v74.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 "testing" 14 15 "github.com/google/go-cmp/cmp" 16 ) 17 18 func TestAdminService_UpdateUserLDAPMapping(t *testing.T) { 19 t.Parallel() 20 client, mux, _ := setup(t) 21 22 input := &UserLDAPMapping{ 23 LDAPDN: Ptr("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 assertNilError(t, json.NewDecoder(r.Body).Decode(v)) 29 30 testMethod(t, r, "PATCH") 31 if !cmp.Equal(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: Ptr(int64(1)), 45 LDAPDN: Ptr("uid=asdf,ou=users,dc=github,dc=com"), 46 } 47 if !cmp.Equal(mapping, want) { 48 t.Errorf("Admin.UpdateUserLDAPMapping returned %+v, want %+v", mapping, want) 49 } 50 51 const methodName = "UpdateUserLDAPMapping" 52 testBadOptions(t, methodName, func() (err error) { 53 _, _, err = client.Admin.UpdateUserLDAPMapping(ctx, "\n", input) 54 return err 55 }) 56 57 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 58 got, resp, err := client.Admin.UpdateUserLDAPMapping(ctx, "u", input) 59 if got != nil { 60 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 61 } 62 return resp, err 63 }) 64 } 65 66 func TestAdminService_UpdateTeamLDAPMapping(t *testing.T) { 67 t.Parallel() 68 client, mux, _ := setup(t) 69 70 input := &TeamLDAPMapping{ 71 LDAPDN: Ptr("cn=Enterprise Ops,ou=teams,dc=github,dc=com"), 72 } 73 74 mux.HandleFunc("/admin/ldap/teams/1/mapping", func(w http.ResponseWriter, r *http.Request) { 75 v := new(TeamLDAPMapping) 76 assertNilError(t, json.NewDecoder(r.Body).Decode(v)) 77 78 testMethod(t, r, "PATCH") 79 if !cmp.Equal(v, input) { 80 t.Errorf("Request body = %+v, want %+v", v, input) 81 } 82 fmt.Fprint(w, `{"id":1,"ldap_dn":"cn=Enterprise Ops,ou=teams,dc=github,dc=com"}`) 83 }) 84 85 ctx := context.Background() 86 mapping, _, err := client.Admin.UpdateTeamLDAPMapping(ctx, 1, input) 87 if err != nil { 88 t.Errorf("Admin.UpdateTeamLDAPMapping returned error: %v", err) 89 } 90 91 want := &TeamLDAPMapping{ 92 ID: Ptr(int64(1)), 93 LDAPDN: Ptr("cn=Enterprise Ops,ou=teams,dc=github,dc=com"), 94 } 95 if !cmp.Equal(mapping, want) { 96 t.Errorf("Admin.UpdateTeamLDAPMapping returned %+v, want %+v", mapping, want) 97 } 98 99 const methodName = "UpdateTeamLDAPMapping" 100 testBadOptions(t, methodName, func() (err error) { 101 _, _, err = client.Admin.UpdateTeamLDAPMapping(ctx, -1, input) 102 return err 103 }) 104 105 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 106 got, resp, err := client.Admin.UpdateTeamLDAPMapping(ctx, 1, input) 107 if got != nil { 108 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 109 } 110 return resp, err 111 }) 112 } 113 114 func TestAdminService_TeamLDAPMapping_String(t *testing.T) { 115 t.Parallel() 116 v := &TeamLDAPMapping{ 117 ID: Ptr(int64(1)), 118 LDAPDN: Ptr("a"), 119 URL: Ptr("b"), 120 Name: Ptr("c"), 121 Slug: Ptr("d"), 122 Description: Ptr("e"), 123 Privacy: Ptr("f"), 124 Permission: Ptr("g"), 125 MembersURL: Ptr("h"), 126 RepositoriesURL: Ptr("i"), 127 } 128 129 want := `github.TeamLDAPMapping{ID:1, LDAPDN:"a", URL:"b", Name:"c", Slug:"d", Description:"e", Privacy:"f", Permission:"g", MembersURL:"h", RepositoriesURL:"i"}` 130 if got := v.String(); got != want { 131 t.Errorf("TeamLDAPMapping.String = `%v`, want `%v`", got, want) 132 } 133 } 134 135 func TestAdminService_UserLDAPMapping_String(t *testing.T) { 136 t.Parallel() 137 v := &UserLDAPMapping{ 138 ID: Ptr(int64(1)), 139 LDAPDN: Ptr("a"), 140 Login: Ptr("b"), 141 AvatarURL: Ptr("c"), 142 GravatarID: Ptr("d"), 143 Type: Ptr("e"), 144 SiteAdmin: Ptr(true), 145 URL: Ptr("f"), 146 EventsURL: Ptr("g"), 147 FollowingURL: Ptr("h"), 148 FollowersURL: Ptr("i"), 149 GistsURL: Ptr("j"), 150 OrganizationsURL: Ptr("k"), 151 ReceivedEventsURL: Ptr("l"), 152 ReposURL: Ptr("m"), 153 StarredURL: Ptr("n"), 154 SubscriptionsURL: Ptr("o"), 155 } 156 157 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"}` 158 if got := v.String(); got != want { 159 t.Errorf("UserLDAPMapping.String = `%v`, want `%v`", got, want) 160 } 161 } 162 163 func TestTeamLDAPMapping_Marshal(t *testing.T) { 164 t.Parallel() 165 testJSONMarshal(t, &TeamLDAPMapping{}, "{}") 166 167 u := &TeamLDAPMapping{ 168 ID: Ptr(int64(1)), 169 LDAPDN: Ptr("ldapdn"), 170 URL: Ptr("u"), 171 Name: Ptr("n"), 172 Slug: Ptr("s"), 173 Description: Ptr("d"), 174 Privacy: Ptr("p"), 175 Permission: Ptr("per"), 176 MembersURL: Ptr("mu"), 177 RepositoriesURL: Ptr("ru"), 178 } 179 180 want := `{ 181 "id": 1, 182 "ldap_dn": "ldapdn", 183 "url": "u", 184 "name": "n", 185 "slug": "s", 186 "description": "d", 187 "privacy": "p", 188 "permission": "per", 189 "members_url": "mu", 190 "repositories_url": "ru" 191 }` 192 193 testJSONMarshal(t, u, want) 194 } 195 196 func TestUserLDAPMapping_Marshal(t *testing.T) { 197 t.Parallel() 198 testJSONMarshal(t, &UserLDAPMapping{}, "{}") 199 200 u := &UserLDAPMapping{ 201 ID: Ptr(int64(1)), 202 LDAPDN: Ptr("ldapdn"), 203 Login: Ptr("l"), 204 AvatarURL: Ptr("au"), 205 GravatarID: Ptr("gi"), 206 Type: Ptr("t"), 207 SiteAdmin: Ptr(true), 208 URL: Ptr("u"), 209 EventsURL: Ptr("eu"), 210 FollowingURL: Ptr("fu"), 211 FollowersURL: Ptr("fu"), 212 GistsURL: Ptr("gu"), 213 OrganizationsURL: Ptr("ou"), 214 ReceivedEventsURL: Ptr("reu"), 215 ReposURL: Ptr("ru"), 216 StarredURL: Ptr("su"), 217 SubscriptionsURL: Ptr("subu"), 218 } 219 220 want := `{ 221 "id": 1, 222 "ldap_dn": "ldapdn", 223 "login": "l", 224 "avatar_url": "au", 225 "gravatar_id": "gi", 226 "type": "t", 227 "site_admin": true, 228 "url": "u", 229 "events_url": "eu", 230 "following_url": "fu", 231 "followers_url": "fu", 232 "gists_url": "gu", 233 "organizations_url": "ou", 234 "received_events_url": "reu", 235 "repos_url": "ru", 236 "starred_url": "su", 237 "subscriptions_url": "subu" 238 }` 239 240 testJSONMarshal(t, u, want) 241 } 242 243 func TestEnterprise_Marshal(t *testing.T) { 244 t.Parallel() 245 testJSONMarshal(t, &Enterprise{}, "{}") 246 247 u := &Enterprise{ 248 ID: Ptr(1), 249 Slug: Ptr("s"), 250 Name: Ptr("n"), 251 NodeID: Ptr("nid"), 252 AvatarURL: Ptr("au"), 253 Description: Ptr("d"), 254 WebsiteURL: Ptr("wu"), 255 HTMLURL: Ptr("hu"), 256 CreatedAt: &Timestamp{referenceTime}, 257 UpdatedAt: &Timestamp{referenceTime}, 258 } 259 260 want := `{ 261 "id": 1, 262 "slug": "s", 263 "name": "n", 264 "node_id": "nid", 265 "avatar_url": "au", 266 "description": "d", 267 "website_url": "wu", 268 "html_url": "hu", 269 "created_at": ` + referenceTimeStr + `, 270 "updated_at": ` + referenceTimeStr + ` 271 }` 272 273 testJSONMarshal(t, u, want) 274 }