-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathxpack_security_change_password_test.go
119 lines (113 loc) · 2.79 KB
/
xpack_security_change_password_test.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
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
// Copyright 2012-2018 Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.
package elastic
import (
"net/url"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestXPackSecurityChangePasswordBuildURL(t *testing.T) {
client := setupTestClientForXpackSecurity(t)
tests := []struct {
Username string
Password string
Refresh string
Body interface{}
ExpectedPath string
ExpectedParams url.Values
ExpectedErr bool
}{
// #0 No username
{
Username: "",
Password: "",
Refresh: "",
Body: nil,
ExpectedPath: "",
ExpectedParams: url.Values{},
ExpectedErr: true,
},
// #1 No username (but body)
{
Username: "",
Password: "",
Refresh: "",
Body: `{}`,
ExpectedPath: "",
ExpectedParams: url.Values{},
ExpectedErr: true,
},
// #2 No body or password
{
Username: "my-user",
Password: "",
Refresh: "",
Body: nil,
ExpectedPath: "",
ExpectedParams: url.Values{},
ExpectedErr: true,
},
// #3 No password but body
{
Username: "my-user",
Password: "",
Refresh: "",
Body: `{"password":"secret"}`,
ExpectedPath: "/_xpack/security/user/my-user/_password",
ExpectedParams: url.Values{},
ExpectedErr: false,
},
// #4 No body but password
{
Username: "my-user",
Password: "secret",
Refresh: "",
Body: nil,
ExpectedPath: "/_xpack/security/user/my-user/_password",
ExpectedParams: url.Values{},
ExpectedErr: false,
},
// #5 With refresh option
{
Username: "my-user",
Password: "secret",
Refresh: "wait_for",
Body: nil,
ExpectedPath: "/_xpack/security/user/my-user/_password",
ExpectedParams: url.Values{
"refresh": []string{"wait_for"},
},
ExpectedErr: false,
},
}
for i, tt := range tests {
builder := client.XPackSecurityChangePassword(tt.Username).
Password(tt.Password).
Refresh(tt.Refresh).
Body(tt.Body)
err := builder.Validate()
if err != nil {
if !tt.ExpectedErr {
t.Errorf("case #%d: %v", i, err)
continue
}
} else {
// err == nil
if tt.ExpectedErr {
t.Errorf("case #%d: expected error", i)
continue
}
path, params, err := builder.buildURL()
if err != nil {
t.Fatalf("case #%d: %v", i, err)
}
if path != tt.ExpectedPath {
t.Errorf("case #%d: expected %q; got: %q", i, tt.ExpectedPath, path)
}
if want, have := tt.ExpectedParams, params; !cmp.Equal(want, have) {
t.Errorf("case #%d: want Params=%#v, have %#v\n\tdiff: %s", i, want, have, cmp.Diff(want, have))
}
}
}
}