-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathutilities_testing_user.go
81 lines (65 loc) · 2.7 KB
/
utilities_testing_user.go
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
// Copyright 2022-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.
package rest
import (
"encoding/base64"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"testing"
"github.com/couchbase/sync_gateway/base"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func MakeUser(t *testing.T, httpClient *http.Client, serverURL, username, password string, roles []string) {
form := url.Values{}
form.Add("password", password)
form.Add("roles", strings.Join(roles, ","))
retryWorker := func() (shouldRetry bool, err error, value interface{}) {
req, err := http.NewRequest("PUT", fmt.Sprintf("%s/settings/rbac/users/local/%s", serverURL, username), strings.NewReader(form.Encode()))
require.NoError(t, err)
req.SetBasicAuth(base.TestClusterUsername(), base.TestClusterPassword())
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := httpClient.Do(req)
if err != nil {
return true, err, nil
}
defer func() { assert.NoError(t, resp.Body.Close()) }()
var bodyResp []byte
if resp.StatusCode != http.StatusOK {
bodyResp, err = io.ReadAll(resp.Body)
require.NoError(t, err, "Failed to create user: %s", bodyResp)
}
require.Equalf(t, http.StatusOK, resp.StatusCode, "Failed to create user: %s", bodyResp)
return false, err, nil
}
err, _ := base.RetryLoop(base.TestCtx(t), "Admin Auth testing MakeUser", retryWorker, base.CreateSleeperFunc(10, 100))
require.NoError(t, err)
}
func DeleteUser(t *testing.T, httpClient *http.Client, serverURL, username string) {
retryWorker := func() (shouldRetry bool, err error, value interface{}) {
req, err := http.NewRequest("DELETE", fmt.Sprintf("%s/settings/rbac/users/local/%s", serverURL, username), nil)
require.NoError(t, err)
req.SetBasicAuth(base.TestClusterUsername(), base.TestClusterPassword())
resp, err := httpClient.Do(req)
if err != nil {
return true, err, resp
}
assert.NoError(t, resp.Body.Close())
return false, err, resp
}
err, resp := base.RetryLoop(base.TestCtx(t), "Admin Auth testing DeleteUser", retryWorker, base.CreateSleeperFunc(10, 100))
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.(*http.Response).StatusCode)
require.NoError(t, resp.(*http.Response).Body.Close(), "Error closing response body")
}
func getBasicAuthHeader(username, password string) string {
return "Basic " + base64.StdEncoding.EncodeToString([]byte(username+":"+password))
}