-
Notifications
You must be signed in to change notification settings - Fork 4
/
salt_test.go
136 lines (120 loc) · 3.04 KB
/
salt_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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
var (
mux *http.ServeMux
client *Salt
server *httptest.Server
)
func setup() {
// test server
mux = http.NewServeMux()
server = httptest.NewServer(mux)
client = NewSalt(server.URL)
}
// teardown closes the test HTTP server.
func teardown() {
server.Close()
}
func testMethod(t *testing.T, r *http.Request, want string) {
if want != r.Method {
t.Errorf("Request method = %v, want %v", r.Method, want)
}
}
type values map[string]string
func testFormValues(t *testing.T, r *http.Request, values values) {
for key, want := range values {
if v := r.FormValue(key); v != want {
t.Errorf("Request parameter %v = %v, want %v", key, v, want)
}
}
}
// TestLogin ensures that we call the correct URL with the correct arguments
func TestLogin(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/login",
func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
v := values{
"username": "user.name",
"password": "Password123",
}
testFormValues(t, r, v)
fmt.Fprint(w, `{"return": [{"perms": [".*"], "token": "c8960686c8ab40dd5d1bd9e72f2c550f654dd323"}]}`)
},
)
err := client.Login("user.name", "Password123", "ldap")
if err != nil {
t.Error(err)
}
}
// TestLoginWithInvalidCredentials ensures we handle invalid logins correctly
func TestLoginWithInvalidCredentials(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/login",
func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
v := values{
"username": "user.name",
"password": "Password123",
}
testFormValues(t, r, v)
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprint(w, "Could not authenticate using provided credentials")
},
)
err := client.Login("user.name", "Password123", "ldap")
if err == nil {
t.Errorf("Error is nil, want not nil.")
}
}
// TestLoginWithParseError ensures we handle responses from the server that have an error
func TestLoginWithParseError(t *testing.T) {
setup()
defer teardown()
client.Hostname = "somethingbad"
err := client.Login("user.name", "Password123", "ldap")
if err == nil {
t.Errorf("Error is %v, want not nil.", err)
}
}
func TestRun(t *testing.T) {
setup()
defer teardown()
// client.Login("user.name", "Password123", "ldap")
mux.HandleFunc("/",
func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
v := values{
"arg": "",
"tgt": "*",
}
testFormValues(t, r, v)
fmt.Fprint(w, `{"return": []}`)
},
)
resp, err := client.Run("*", "test.ping", "")
if err != nil {
t.Errorf("Error is %v, want nil", err)
}
if !strings.Contains(resp, "return") {
t.Errorf("Reponse does not contain return, want return")
}
}
// TestRunWithParseError ensures we handle responses from the server that have an error
func TestRunWithParseError(t *testing.T) {
setup()
defer teardown()
client.Hostname = "somethingbad"
_, err := client.Run("user.name", "Password123", "ldap")
if err == nil {
t.Errorf("Error is %v, want not nil.", err)
}
}