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
6package github
7
8import (
9	"context"
10	"encoding/json"
11	"fmt"
12	"net/http"
13	"reflect"
14	"testing"
15)
16
17func TestAdminService_UpdateUserLDAPMapping(t *testing.T) {
18	client, mux, _, teardown := setup()
19	defer teardown()
20
21	input := &UserLDAPMapping{
22		LDAPDN: String("uid=asdf,ou=users,dc=github,dc=com"),
23	}
24
25	mux.HandleFunc("/admin/ldap/users/u/mapping", func(w http.ResponseWriter, r *http.Request) {
26		v := new(UserLDAPMapping)
27		json.NewDecoder(r.Body).Decode(v)
28
29		testMethod(t, r, "PATCH")
30		if !reflect.DeepEqual(v, input) {
31			t.Errorf("Request body = %+v, want %+v", v, input)
32		}
33		fmt.Fprint(w, `{"id":1,"ldap_dn":"uid=asdf,ou=users,dc=github,dc=com"}`)
34	})
35
36	ctx := context.Background()
37	mapping, _, err := client.Admin.UpdateUserLDAPMapping(ctx, "u", input)
38	if err != nil {
39		t.Errorf("Admin.UpdateUserLDAPMapping returned error: %v", err)
40	}
41
42	want := &UserLDAPMapping{
43		ID:     Int64(1),
44		LDAPDN: String("uid=asdf,ou=users,dc=github,dc=com"),
45	}
46	if !reflect.DeepEqual(mapping, want) {
47		t.Errorf("Admin.UpdateUserLDAPMapping returned %+v, want %+v", mapping, want)
48	}
49
50	const methodName = "UpdateUserLDAPMapping"
51	testBadOptions(t, methodName, func() (err error) {
52		_, _, err = client.Admin.UpdateUserLDAPMapping(ctx, "\n", input)
53		return err
54	})
55
56	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
57		got, resp, err := client.Admin.UpdateUserLDAPMapping(ctx, "u", input)
58		if got != nil {
59			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
60		}
61		return resp, err
62	})
63}
64
65func TestAdminService_UpdateTeamLDAPMapping(t *testing.T) {
66	client, mux, _, teardown := setup()
67	defer teardown()
68
69	input := &TeamLDAPMapping{
70		LDAPDN: String("cn=Enterprise Ops,ou=teams,dc=github,dc=com"),
71	}
72
73	mux.HandleFunc("/admin/ldap/teams/1/mapping", func(w http.ResponseWriter, r *http.Request) {
74		v := new(TeamLDAPMapping)
75		json.NewDecoder(r.Body).Decode(v)
76
77		testMethod(t, r, "PATCH")
78		if !reflect.DeepEqual(v, input) {
79			t.Errorf("Request body = %+v, want %+v", v, input)
80		}
81		fmt.Fprint(w, `{"id":1,"ldap_dn":"cn=Enterprise Ops,ou=teams,dc=github,dc=com"}`)
82	})
83
84	ctx := context.Background()
85	mapping, _, err := client.Admin.UpdateTeamLDAPMapping(ctx, 1, input)
86	if err != nil {
87		t.Errorf("Admin.UpdateTeamLDAPMapping returned error: %v", err)
88	}
89
90	want := &TeamLDAPMapping{
91		ID:     Int64(1),
92		LDAPDN: String("cn=Enterprise Ops,ou=teams,dc=github,dc=com"),
93	}
94	if !reflect.DeepEqual(mapping, want) {
95		t.Errorf("Admin.UpdateTeamLDAPMapping returned %+v, want %+v", mapping, want)
96	}
97
98	const methodName = "UpdateTeamLDAPMapping"
99	testBadOptions(t, methodName, func() (err error) {
100		_, _, err = client.Admin.UpdateTeamLDAPMapping(ctx, -1, input)
101		return err
102	})
103
104	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
105		got, resp, err := client.Admin.UpdateTeamLDAPMapping(ctx, 1, input)
106		if got != nil {
107			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
108		}
109		return resp, err
110	})
111}
112
113func TestAdminService_TeamLDAPMapping_String(t *testing.T) {
114	v := &TeamLDAPMapping{
115		ID:              Int64(1),
116		LDAPDN:          String("a"),
117		URL:             String("b"),
118		Name:            String("c"),
119		Slug:            String("d"),
120		Description:     String("e"),
121		Privacy:         String("f"),
122		Permission:      String("g"),
123		MembersURL:      String("h"),
124		RepositoriesURL: String("i"),
125	}
126
127	want := `github.TeamLDAPMapping{ID:1, LDAPDN:"a", URL:"b", Name:"c", Slug:"d", Description:"e", Privacy:"f", Permission:"g", MembersURL:"h", RepositoriesURL:"i"}`
128	if got := v.String(); got != want {
129		t.Errorf("TeamLDAPMapping.String = `%v`, want `%v`", got, want)
130	}
131}
132
133func TestAdminService_UserLDAPMapping_String(t *testing.T) {
134	v := &UserLDAPMapping{
135		ID:                Int64(1),
136		LDAPDN:            String("a"),
137		Login:             String("b"),
138		AvatarURL:         String("c"),
139		GravatarID:        String("d"),
140		Type:              String("e"),
141		SiteAdmin:         Bool(true),
142		URL:               String("f"),
143		EventsURL:         String("g"),
144		FollowingURL:      String("h"),
145		FollowersURL:      String("i"),
146		GistsURL:          String("j"),
147		OrganizationsURL:  String("k"),
148		ReceivedEventsURL: String("l"),
149		ReposURL:          String("m"),
150		StarredURL:        String("n"),
151		SubscriptionsURL:  String("o"),
152	}
153
154	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"}`
155	if got := v.String(); got != want {
156		t.Errorf("UserLDAPMapping.String = `%v`, want `%v`", got, want)
157	}
158}
159