1// Copyright 2017 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	"fmt"
11	"net/http"
12	"testing"
13
14	"github.com/google/go-cmp/cmp"
15)
16
17func TestOrganizationsService_ListBlockedUsers(t *testing.T) {
18	client, mux, _, teardown := setup()
19	defer teardown()
20
21	mux.HandleFunc("/orgs/o/blocks", func(w http.ResponseWriter, r *http.Request) {
22		testMethod(t, r, "GET")
23		testHeader(t, r, "Accept", mediaTypeBlockUsersPreview)
24		testFormValues(t, r, values{"page": "2"})
25		fmt.Fprint(w, `[{
26			"login": "octocat"
27		}]`)
28	})
29
30	opt := &ListOptions{Page: 2}
31	ctx := context.Background()
32	blockedUsers, _, err := client.Organizations.ListBlockedUsers(ctx, "o", opt)
33	if err != nil {
34		t.Errorf("Organizations.ListBlockedUsers returned error: %v", err)
35	}
36
37	want := []*User{{Login: String("octocat")}}
38	if !cmp.Equal(blockedUsers, want) {
39		t.Errorf("Organizations.ListBlockedUsers returned %+v, want %+v", blockedUsers, want)
40	}
41
42	const methodName = "ListBlockedUsers"
43	testBadOptions(t, methodName, func() (err error) {
44		_, _, err = client.Organizations.ListBlockedUsers(ctx, "\n", opt)
45		return err
46	})
47
48	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
49		got, resp, err := client.Organizations.ListBlockedUsers(ctx, "o", opt)
50		if got != nil {
51			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
52		}
53		return resp, err
54	})
55}
56
57func TestOrganizationsService_IsBlocked(t *testing.T) {
58	client, mux, _, teardown := setup()
59	defer teardown()
60
61	mux.HandleFunc("/orgs/o/blocks/u", func(w http.ResponseWriter, r *http.Request) {
62		testMethod(t, r, "GET")
63		testHeader(t, r, "Accept", mediaTypeBlockUsersPreview)
64		w.WriteHeader(http.StatusNoContent)
65	})
66
67	ctx := context.Background()
68	isBlocked, _, err := client.Organizations.IsBlocked(ctx, "o", "u")
69	if err != nil {
70		t.Errorf("Organizations.IsBlocked returned error: %v", err)
71	}
72	if want := true; isBlocked != want {
73		t.Errorf("Organizations.IsBlocked returned %+v, want %+v", isBlocked, want)
74	}
75
76	const methodName = "IsBlocked"
77	testBadOptions(t, methodName, func() (err error) {
78		_, _, err = client.Organizations.IsBlocked(ctx, "\n", "\n")
79		return err
80	})
81
82	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
83		got, resp, err := client.Organizations.IsBlocked(ctx, "o", "u")
84		if got {
85			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
86		}
87		return resp, err
88	})
89}
90
91func TestOrganizationsService_BlockUser(t *testing.T) {
92	client, mux, _, teardown := setup()
93	defer teardown()
94
95	mux.HandleFunc("/orgs/o/blocks/u", func(w http.ResponseWriter, r *http.Request) {
96		testMethod(t, r, "PUT")
97		testHeader(t, r, "Accept", mediaTypeBlockUsersPreview)
98		w.WriteHeader(http.StatusNoContent)
99	})
100
101	ctx := context.Background()
102	_, err := client.Organizations.BlockUser(ctx, "o", "u")
103	if err != nil {
104		t.Errorf("Organizations.BlockUser returned error: %v", err)
105	}
106
107	const methodName = "BlockUser"
108	testBadOptions(t, methodName, func() (err error) {
109		_, err = client.Organizations.BlockUser(ctx, "\n", "\n")
110		return err
111	})
112
113	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
114		return client.Organizations.BlockUser(ctx, "o", "u")
115	})
116}
117
118func TestOrganizationsService_UnblockUser(t *testing.T) {
119	client, mux, _, teardown := setup()
120	defer teardown()
121
122	mux.HandleFunc("/orgs/o/blocks/u", func(w http.ResponseWriter, r *http.Request) {
123		testMethod(t, r, "DELETE")
124		testHeader(t, r, "Accept", mediaTypeBlockUsersPreview)
125		w.WriteHeader(http.StatusNoContent)
126	})
127
128	ctx := context.Background()
129	_, err := client.Organizations.UnblockUser(ctx, "o", "u")
130	if err != nil {
131		t.Errorf("Organizations.UnblockUser returned error: %v", err)
132	}
133
134	const methodName = "UnblockUser"
135	testBadOptions(t, methodName, func() (err error) {
136		_, err = client.Organizations.UnblockUser(ctx, "\n", "\n")
137		return err
138	})
139
140	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
141		return client.Organizations.UnblockUser(ctx, "o", "u")
142	})
143}
144