github.com/google/go-github/v66@v66.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: 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 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: Int64(1), 45 LDAPDN: String("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: String("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: Int64(1), 93 LDAPDN: String("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: Int64(1), 118 LDAPDN: String("a"), 119 URL: String("b"), 120 Name: String("c"), 121 Slug: String("d"), 122 Description: String("e"), 123 Privacy: String("f"), 124 Permission: String("g"), 125 MembersURL: String("h"), 126 RepositoriesURL: String("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: Int64(1), 139 LDAPDN: String("a"), 140 Login: String("b"), 141 AvatarURL: String("c"), 142 GravatarID: String("d"), 143 Type: String("e"), 144 SiteAdmin: Bool(true), 145 URL: String("f"), 146 EventsURL: String("g"), 147 FollowingURL: String("h"), 148 FollowersURL: String("i"), 149 GistsURL: String("j"), 150 OrganizationsURL: String("k"), 151 ReceivedEventsURL: String("l"), 152 ReposURL: String("m"), 153 StarredURL: String("n"), 154 SubscriptionsURL: String("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: Int64(1), 169 LDAPDN: String("ldapdn"), 170 URL: String("u"), 171 Name: String("n"), 172 Slug: String("s"), 173 Description: String("d"), 174 Privacy: String("p"), 175 Permission: String("per"), 176 MembersURL: String("mu"), 177 RepositoriesURL: String("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: Int64(1), 202 LDAPDN: String("ldapdn"), 203 Login: String("l"), 204 AvatarURL: String("au"), 205 GravatarID: String("gi"), 206 Type: String("t"), 207 SiteAdmin: Bool(true), 208 URL: String("u"), 209 EventsURL: String("eu"), 210 FollowingURL: String("fu"), 211 FollowersURL: String("fu"), 212 GistsURL: String("gu"), 213 OrganizationsURL: String("ou"), 214 ReceivedEventsURL: String("reu"), 215 ReposURL: String("ru"), 216 StarredURL: String("su"), 217 SubscriptionsURL: String("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: Int(1), 249 Slug: String("s"), 250 Name: String("n"), 251 NodeID: String("nid"), 252 AvatarURL: String("au"), 253 Description: String("d"), 254 WebsiteURL: String("wu"), 255 HTMLURL: String("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 }