| 1 |
// Copyright 2014 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 |
|
| 6 |
package github |
| 7 |
|
| 8 |
import ( |
| 9 |
"context" |
| 10 |
"encoding/json" |
| 11 |
"net/http" |
| 12 |
"testing" |
| 13 |
|
| 14 |
"github.com/google/go-cmp/cmp" |
| 15 |
) |
| 16 |
|
| 17 |
func TestUsersService_PromoteSiteAdmin(t *testing.T) { |
| 18 |
client, mux, _, teardown := setup() |
| 19 |
defer teardown() |
| 20 |
|
| 21 |
mux.HandleFunc("/users/u/site_admin", func(w http.ResponseWriter, r *http.Request) { |
| 22 |
testMethod(t, r, "PUT") |
| 23 |
w.WriteHeader(http.StatusNoContent) |
| 24 |
}) |
| 25 |
|
| 26 |
ctx := context.Background() |
| 27 |
_, err := client.Users.PromoteSiteAdmin(ctx, "u") |
| 28 |
if err != nil { |
| 29 |
t.Errorf("Users.PromoteSiteAdmin returned error: %v", err) |
| 30 |
} |
| 31 |
|
| 32 |
const methodName = "PromoteSiteAdmin" |
| 33 |
testBadOptions(t, methodName, func() (err error) { |
| 34 |
_, err = client.Users.PromoteSiteAdmin(ctx, "\n") |
| 35 |
return err |
| 36 |
}) |
| 37 |
|
| 38 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 39 |
return client.Users.PromoteSiteAdmin(ctx, "u") |
| 40 |
}) |
| 41 |
} |
| 42 |
|
| 43 |
func TestUsersService_DemoteSiteAdmin(t *testing.T) { |
| 44 |
client, mux, _, teardown := setup() |
| 45 |
defer teardown() |
| 46 |
|
| 47 |
mux.HandleFunc("/users/u/site_admin", func(w http.ResponseWriter, r *http.Request) { |
| 48 |
testMethod(t, r, "DELETE") |
| 49 |
w.WriteHeader(http.StatusNoContent) |
| 50 |
}) |
| 51 |
|
| 52 |
ctx := context.Background() |
| 53 |
_, err := client.Users.DemoteSiteAdmin(ctx, "u") |
| 54 |
if err != nil { |
| 55 |
t.Errorf("Users.DemoteSiteAdmin returned error: %v", err) |
| 56 |
} |
| 57 |
|
| 58 |
const methodName = "DemoteSiteAdmin" |
| 59 |
testBadOptions(t, methodName, func() (err error) { |
| 60 |
_, err = client.Users.DemoteSiteAdmin(ctx, "\n") |
| 61 |
return err |
| 62 |
}) |
| 63 |
|
| 64 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 65 |
return client.Users.DemoteSiteAdmin(ctx, "u") |
| 66 |
}) |
| 67 |
} |
| 68 |
|
| 69 |
func TestUsersService_Suspend(t *testing.T) { |
| 70 |
client, mux, _, teardown := setup() |
| 71 |
defer teardown() |
| 72 |
|
| 73 |
mux.HandleFunc("/users/u/suspended", func(w http.ResponseWriter, r *http.Request) { |
| 74 |
testMethod(t, r, "PUT") |
| 75 |
w.WriteHeader(http.StatusNoContent) |
| 76 |
}) |
| 77 |
|
| 78 |
ctx := context.Background() |
| 79 |
_, err := client.Users.Suspend(ctx, "u", nil) |
| 80 |
if err != nil { |
| 81 |
t.Errorf("Users.Suspend returned error: %v", err) |
| 82 |
} |
| 83 |
|
| 84 |
const methodName = "Suspend" |
| 85 |
testBadOptions(t, methodName, func() (err error) { |
| 86 |
_, err = client.Users.Suspend(ctx, "\n", nil) |
| 87 |
return err |
| 88 |
}) |
| 89 |
|
| 90 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 91 |
return client.Users.Suspend(ctx, "u", nil) |
| 92 |
}) |
| 93 |
} |
| 94 |
|
| 95 |
func TestUsersServiceReason_Suspend(t *testing.T) { |
| 96 |
client, mux, _, teardown := setup() |
| 97 |
defer teardown() |
| 98 |
|
| 99 |
input := &UserSuspendOptions{Reason: String("test")} |
| 100 |
|
| 101 |
mux.HandleFunc("/users/u/suspended", func(w http.ResponseWriter, r *http.Request) { |
| 102 |
v := new(UserSuspendOptions) |
| 103 |
assertNilError(t, json.NewDecoder(r.Body).Decode(v)) |
| 104 |
|
| 105 |
testMethod(t, r, "PUT") |
| 106 |
if !cmp.Equal(v, input) { |
| 107 |
t.Errorf("Request body = %+v, want %+v", v, input) |
| 108 |
} |
| 109 |
|
| 110 |
w.WriteHeader(http.StatusNoContent) |
| 111 |
}) |
| 112 |
|
| 113 |
ctx := context.Background() |
| 114 |
_, err := client.Users.Suspend(ctx, "u", input) |
| 115 |
if err != nil { |
| 116 |
t.Errorf("Users.Suspend returned error: %v", err) |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
func TestUsersService_Unsuspend(t *testing.T) { |
| 121 |
client, mux, _, teardown := setup() |
| 122 |
defer teardown() |
| 123 |
|
| 124 |
mux.HandleFunc("/users/u/suspended", func(w http.ResponseWriter, r *http.Request) { |
| 125 |
testMethod(t, r, "DELETE") |
| 126 |
w.WriteHeader(http.StatusNoContent) |
| 127 |
}) |
| 128 |
|
| 129 |
ctx := context.Background() |
| 130 |
_, err := client.Users.Unsuspend(ctx, "u") |
| 131 |
if err != nil { |
| 132 |
t.Errorf("Users.Unsuspend returned error: %v", err) |
| 133 |
} |
| 134 |
|
| 135 |
const methodName = "Unsuspend" |
| 136 |
testBadOptions(t, methodName, func() (err error) { |
| 137 |
_, err = client.Users.Unsuspend(ctx, "\n") |
| 138 |
return err |
| 139 |
}) |
| 140 |
|
| 141 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 142 |
return client.Users.Unsuspend(ctx, "u") |
| 143 |
}) |
| 144 |
} |
| 145 |
|
| 146 |
func TestUserSuspendOptions_Marshal(t *testing.T) { |
| 147 |
testJSONMarshal(t, &UserSuspendOptions{}, "{}") |
| 148 |
|
| 149 |
u := &UserSuspendOptions{ |
| 150 |
Reason: String("reason"), |
| 151 |
} |
| 152 |
|
| 153 |
want := `{ |
| 154 |
"reason": "reason" |
| 155 |
}` |
| 156 |
|
| 157 |
testJSONMarshal(t, u, want) |
| 158 |
} |