forked from stmcginnis/gofish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession_test.go
214 lines (182 loc) · 5.13 KB
/
session_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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//
// SPDX-License-Identifier: BSD-3-Clause
//
package redfish
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"
"github.com/stmcginnis/gofish/common"
)
var sessionBody = `{
"@odata.context": "/redfish/v1/$metadata#Session.Session",
"@odata.type": "#Session.v1_2_0.Session",
"@odata.id": "/redfish/v1/Session",
"Id": "Session-1",
"Name": "SessionOne",
"Description": "Session One",
"OemSessionType": "Ticket",
"SessionType": "OEM",
"UserName": "mfreeman"
}`
// TestSession tests the parsing of Session objects.
func TestSession(t *testing.T) {
var result Session
err := json.NewDecoder(strings.NewReader(sessionBody)).Decode(&result)
if err != nil {
t.Errorf("Error decoding JSON: %s", err)
}
if result.ID != "Session-1" {
t.Errorf("Received invalid ID: %s", result.ID)
}
if result.Name != "SessionOne" {
t.Errorf("Received invalid name: %s", result.Name)
}
if result.Password != "" {
t.Error("Password should be nil")
}
if result.SessionType != OEMSessionTypes {
t.Errorf("Invalid session type: %s", result.SessionType)
}
if result.UserName != "mfreeman" {
t.Errorf("Invalid user name: %s", result.UserName)
}
}
// TestCreateSession tests the CreateSession call.
func TestCreateSession(t *testing.T) {
var result Session
err := json.NewDecoder(strings.NewReader(sessionBody)).Decode(&result)
if err != nil {
t.Errorf("Error decoding JSON: %s", err)
}
// define the expected values
expectedSessionURI := "/redfish/v1/SessionService/Sessions/SessionId/"
expectedXAuthToken := "expectedXAuthToken"
// create the custom test client
testClient := &common.TestClient{
CustomReturnForActions: map[string][]interface{}{
http.MethodPost: {
// defining the custom return for the first POST operation
&http.Response{
Status: "201 Created",
StatusCode: 201,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Body: ioutil.NopCloser(bytes.NewBufferString("")),
ContentLength: int64(len("")),
Header: http.Header{
"Location": []string{
expectedSessionURI,
},
"X-Auth-Token": []string{
expectedXAuthToken,
},
},
},
},
},
}
result.SetClient(testClient)
// create the session
auth, err := CreateSession(
testClient,
"/redfish/v1/SessionService/Sessions/",
"user",
"password",
)
// validate the return values
if err != nil {
t.Errorf("Error making CreateSession call: %s", err)
}
if auth.Session != expectedSessionURI {
t.Errorf("Error CreateSession returned: %s expected: %s",
auth.Session,
expectedSessionURI)
}
if auth.Token != expectedXAuthToken {
t.Errorf("Error CreateSession returned: %s expected: %s",
auth.Token,
expectedXAuthToken)
}
// validate the payload
calls := testClient.CapturedCalls()
if !strings.Contains(calls[0].Payload, "UserName:user") {
t.Errorf("Unexpected Username CreateSession payload: %s", calls[0].Payload)
}
if !strings.Contains(calls[0].Payload, "Password:password") {
t.Errorf("Unexpected Password CreateSession payload: %s", calls[0].Payload)
}
}
// TestCreateSessionFullURIPath tests the CreateSession call
// when the vendor returns full URI path.
func TestCreateSessionFullURIPath(t *testing.T) {
var result Session
err := json.NewDecoder(strings.NewReader(sessionBody)).Decode(&result)
if err != nil {
t.Errorf("Error decoding JSON: %s", err)
}
// define the expected values
expectedSessionURI := "/redfish/v1/SessionService/Sessions/SessionId/"
fullURIPath := fmt.Sprintf("https://redfish-server%s", expectedSessionURI)
expectedXAuthToken := "expectedXAuthToken"
// create the custom test client
testClient := &common.TestClient{
CustomReturnForActions: map[string][]interface{}{
http.MethodPost: {
// defining the custom return for the first POST operation
&http.Response{
Status: "201 Created",
StatusCode: 201,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Body: ioutil.NopCloser(bytes.NewBufferString("")),
ContentLength: int64(len("")),
Header: http.Header{
"Location": []string{
fullURIPath,
},
"X-Auth-Token": []string{
expectedXAuthToken,
},
},
},
},
},
}
result.SetClient(testClient)
// create session
auth, err := CreateSession(
testClient,
"/redfish/v1/SessionService/Sessions/",
"user",
"password",
)
// validate the return values
if err != nil {
t.Errorf("Error making CreateSession call: %s", err)
}
if auth.Session != expectedSessionURI {
t.Errorf("Error CreateSession returned: %s expected: %s",
auth.Session,
expectedSessionURI)
}
if auth.Token != expectedXAuthToken {
t.Errorf("Error CreateSession returned: %s expected: %s",
auth.Token,
expectedXAuthToken)
}
// validate the payload
calls := testClient.CapturedCalls()
if !strings.Contains(calls[0].Payload, "UserName:user") {
t.Errorf("Unexpected Username CreateSession payload: %s", calls[0].Payload)
}
if !strings.Contains(calls[0].Payload, "Password:password") {
t.Errorf("Unexpected Password CreateSession payload: %s", calls[0].Payload)
}
}