File: users_administration_test.go

package info (click to toggle)
golang-github-google-go-github 60.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,700 kB
  • sloc: sh: 111; makefile: 5
file content (158 lines) | stat: -rw-r--r-- 3,969 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright 2014 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package github

import (
	"context"
	"encoding/json"
	"net/http"
	"testing"

	"github.com/google/go-cmp/cmp"
)

func TestUsersService_PromoteSiteAdmin(t *testing.T) {
	client, mux, _, teardown := setup()
	defer teardown()

	mux.HandleFunc("/users/u/site_admin", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, "PUT")
		w.WriteHeader(http.StatusNoContent)
	})

	ctx := context.Background()
	_, err := client.Users.PromoteSiteAdmin(ctx, "u")
	if err != nil {
		t.Errorf("Users.PromoteSiteAdmin returned error: %v", err)
	}

	const methodName = "PromoteSiteAdmin"
	testBadOptions(t, methodName, func() (err error) {
		_, err = client.Users.PromoteSiteAdmin(ctx, "\n")
		return err
	})

	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
		return client.Users.PromoteSiteAdmin(ctx, "u")
	})
}

func TestUsersService_DemoteSiteAdmin(t *testing.T) {
	client, mux, _, teardown := setup()
	defer teardown()

	mux.HandleFunc("/users/u/site_admin", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, "DELETE")
		w.WriteHeader(http.StatusNoContent)
	})

	ctx := context.Background()
	_, err := client.Users.DemoteSiteAdmin(ctx, "u")
	if err != nil {
		t.Errorf("Users.DemoteSiteAdmin returned error: %v", err)
	}

	const methodName = "DemoteSiteAdmin"
	testBadOptions(t, methodName, func() (err error) {
		_, err = client.Users.DemoteSiteAdmin(ctx, "\n")
		return err
	})

	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
		return client.Users.DemoteSiteAdmin(ctx, "u")
	})
}

func TestUsersService_Suspend(t *testing.T) {
	client, mux, _, teardown := setup()
	defer teardown()

	mux.HandleFunc("/users/u/suspended", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, "PUT")
		w.WriteHeader(http.StatusNoContent)
	})

	ctx := context.Background()
	_, err := client.Users.Suspend(ctx, "u", nil)
	if err != nil {
		t.Errorf("Users.Suspend returned error: %v", err)
	}

	const methodName = "Suspend"
	testBadOptions(t, methodName, func() (err error) {
		_, err = client.Users.Suspend(ctx, "\n", nil)
		return err
	})

	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
		return client.Users.Suspend(ctx, "u", nil)
	})
}

func TestUsersServiceReason_Suspend(t *testing.T) {
	client, mux, _, teardown := setup()
	defer teardown()

	input := &UserSuspendOptions{Reason: String("test")}

	mux.HandleFunc("/users/u/suspended", func(w http.ResponseWriter, r *http.Request) {
		v := new(UserSuspendOptions)
		assertNilError(t, json.NewDecoder(r.Body).Decode(v))

		testMethod(t, r, "PUT")
		if !cmp.Equal(v, input) {
			t.Errorf("Request body = %+v, want %+v", v, input)
		}

		w.WriteHeader(http.StatusNoContent)
	})

	ctx := context.Background()
	_, err := client.Users.Suspend(ctx, "u", input)
	if err != nil {
		t.Errorf("Users.Suspend returned error: %v", err)
	}
}

func TestUsersService_Unsuspend(t *testing.T) {
	client, mux, _, teardown := setup()
	defer teardown()

	mux.HandleFunc("/users/u/suspended", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, "DELETE")
		w.WriteHeader(http.StatusNoContent)
	})

	ctx := context.Background()
	_, err := client.Users.Unsuspend(ctx, "u")
	if err != nil {
		t.Errorf("Users.Unsuspend returned error: %v", err)
	}

	const methodName = "Unsuspend"
	testBadOptions(t, methodName, func() (err error) {
		_, err = client.Users.Unsuspend(ctx, "\n")
		return err
	})

	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
		return client.Users.Unsuspend(ctx, "u")
	})
}

func TestUserSuspendOptions_Marshal(t *testing.T) {
	testJSONMarshal(t, &UserSuspendOptions{}, "{}")

	u := &UserSuspendOptions{
		Reason: String("reason"),
	}

	want := `{
		"reason": "reason"
	}`

	testJSONMarshal(t, u, want)
}