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