1// Copyright 2013 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 TestRepositoriesService_ListCollaborators(t *testing.T) {
18	client, mux, _, teardown := setup()
19	defer teardown()
20
21	mux.HandleFunc("/repos/o/r/collaborators", func(w http.ResponseWriter, r *http.Request) {
22		testMethod(t, r, "GET")
23		testHeader(t, r, "Accept", mediaTypeNestedTeamsPreview)
24		testFormValues(t, r, values{"page": "2"})
25		fmt.Fprintf(w, `[{"id":1}, {"id":2}]`)
26	})
27
28	opt := &ListCollaboratorsOptions{
29		ListOptions: ListOptions{Page: 2},
30	}
31	users, _, err := client.Repositories.ListCollaborators(context.Background(), "o", "r", opt)
32	if err != nil {
33		t.Errorf("Repositories.ListCollaborators returned error: %v", err)
34	}
35
36	want := []*User{{ID: Int64(1)}, {ID: Int64(2)}}
37	if !reflect.DeepEqual(users, want) {
38		t.Errorf("Repositori es.ListCollaborators returned %+v, want %+v", users, want)
39	}
40}
41
42func TestRepositoriesService_ListCollaborators_withAffiliation(t *testing.T) {
43	client, mux, _, teardown := setup()
44	defer teardown()
45
46	mux.HandleFunc("/repos/o/r/collaborators", func(w http.ResponseWriter, r *http.Request) {
47		testMethod(t, r, "GET")
48		testFormValues(t, r, values{"affiliation": "all", "page": "2"})
49		fmt.Fprintf(w, `[{"id":1}, {"id":2}]`)
50	})
51
52	opt := &ListCollaboratorsOptions{
53		ListOptions: ListOptions{Page: 2},
54		Affiliation: "all",
55	}
56	users, _, err := client.Repositories.ListCollaborators(context.Background(), "o", "r", opt)
57	if err != nil {
58		t.Errorf("Repositories.ListCollaborators returned error: %v", err)
59	}
60
61	want := []*User{{ID: Int64(1)}, {ID: Int64(2)}}
62	if !reflect.DeepEqual(users, want) {
63		t.Errorf("Repositories.ListCollaborators returned %+v, want %+v", users, want)
64	}
65}
66
67func TestRepositoriesService_ListCollaborators_invalidOwner(t *testing.T) {
68	client, _, _, teardown := setup()
69	defer teardown()
70
71	_, _, err := client.Repositories.ListCollaborators(context.Background(), "%", "%", nil)
72	testURLParseError(t, err)
73}
74
75func TestRepositoriesService_IsCollaborator_True(t *testing.T) {
76	client, mux, _, teardown := setup()
77	defer teardown()
78
79	mux.HandleFunc("/repos/o/r/collaborators/u", func(w http.ResponseWriter, r *http.Request) {
80		testMethod(t, r, "GET")
81		w.WriteHeader(http.StatusNoContent)
82	})
83
84	isCollab, _, err := client.Repositories.IsCollaborator(context.Background(), "o", "r", "u")
85	if err != nil {
86		t.Errorf("Repositories.IsCollaborator returned error: %v", err)
87	}
88
89	if !isCollab {
90		t.Errorf("Repositories.IsCollaborator returned false, want true")
91	}
92}
93
94func TestRepositoriesService_IsCollaborator_False(t *testing.T) {
95	client, mux, _, teardown := setup()
96	defer teardown()
97
98	mux.HandleFunc("/repos/o/r/collaborators/u", func(w http.ResponseWriter, r *http.Request) {
99		testMethod(t, r, "GET")
100		w.WriteHeader(http.StatusNotFound)
101	})
102
103	isCollab, _, err := client.Repositories.IsCollaborator(context.Background(), "o", "r", "u")
104	if err != nil {
105		t.Errorf("Repositories.IsCollaborator returned error: %v", err)
106	}
107
108	if isCollab {
109		t.Errorf("Repositories.IsCollaborator returned true, want false")
110	}
111}
112
113func TestRepositoriesService_IsCollaborator_invalidUser(t *testing.T) {
114	client, _, _, teardown := setup()
115	defer teardown()
116
117	_, _, err := client.Repositories.IsCollaborator(context.Background(), "%", "%", "%")
118	testURLParseError(t, err)
119}
120
121func TestRepositoryService_GetPermissionLevel(t *testing.T) {
122	client, mux, _, teardown := setup()
123	defer teardown()
124
125	mux.HandleFunc("/repos/o/r/collaborators/u/permission", func(w http.ResponseWriter, r *http.Request) {
126		testMethod(t, r, "GET")
127		fmt.Fprintf(w, `{"permission":"admin","user":{"login":"u"}}`)
128	})
129
130	rpl, _, err := client.Repositories.GetPermissionLevel(context.Background(), "o", "r", "u")
131	if err != nil {
132		t.Errorf("Repositories.GetPermissionLevel returned error: %v", err)
133	}
134
135	want := &RepositoryPermissionLevel{
136		Permission: String("admin"),
137		User: &User{
138			Login: String("u"),
139		},
140	}
141
142	if !reflect.DeepEqual(rpl, want) {
143		t.Errorf("Repositories.GetPermissionLevel returned %+v, want %+v", rpl, want)
144	}
145}
146
147func TestRepositoriesService_AddCollaborator(t *testing.T) {
148	client, mux, _, teardown := setup()
149	defer teardown()
150
151	opt := &RepositoryAddCollaboratorOptions{Permission: "admin"}
152
153	mux.HandleFunc("/repos/o/r/collaborators/u", func(w http.ResponseWriter, r *http.Request) {
154		v := new(RepositoryAddCollaboratorOptions)
155		json.NewDecoder(r.Body).Decode(v)
156
157		testMethod(t, r, "PUT")
158		testHeader(t, r, "Accept", mediaTypeRepositoryInvitationsPreview)
159		if !reflect.DeepEqual(v, opt) {
160			t.Errorf("Request body = %+v, want %+v", v, opt)
161		}
162
163		w.WriteHeader(http.StatusNoContent)
164	})
165
166	_, err := client.Repositories.AddCollaborator(context.Background(), "o", "r", "u", opt)
167	if err != nil {
168		t.Errorf("Repositories.AddCollaborator returned error: %v", err)
169	}
170}
171
172func TestRepositoriesService_AddCollaborator_invalidUser(t *testing.T) {
173	client, _, _, teardown := setup()
174	defer teardown()
175
176	_, err := client.Repositories.AddCollaborator(context.Background(), "%", "%", "%", nil)
177	testURLParseError(t, err)
178}
179
180func TestRepositoriesService_RemoveCollaborator(t *testing.T) {
181	client, mux, _, teardown := setup()
182	defer teardown()
183
184	mux.HandleFunc("/repos/o/r/collaborators/u", func(w http.ResponseWriter, r *http.Request) {
185		testMethod(t, r, "DELETE")
186		w.WriteHeader(http.StatusNoContent)
187	})
188
189	_, err := client.Repositories.RemoveCollaborator(context.Background(), "o", "r", "u")
190	if err != nil {
191		t.Errorf("Repositories.RemoveCollaborator returned error: %v", err)
192	}
193}
194
195func TestRepositoriesService_RemoveCollaborator_invalidUser(t *testing.T) {
196	client, _, _, teardown := setup()
197	defer teardown()
198
199	_, err := client.Repositories.RemoveCollaborator(context.Background(), "%", "%", "%")
200	testURLParseError(t, err)
201}
202