forked from openshift/cloud-provider-openstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken_getter_test.go
More file actions
111 lines (99 loc) · 2.88 KB
/
Copy pathtoken_getter_test.go
File metadata and controls
111 lines (99 loc) · 2.88 KB
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
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package keystone
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"testing"
"github.com/gophercloud/gophercloud/v2"
th "github.com/gophercloud/gophercloud/v2/testhelper"
)
func TestTokenGetter(t *testing.T) {
fakeServer := th.SetupHTTP()
defer fakeServer.Teardown()
const ID = "0123456789"
fakeServer.Mux.HandleFunc("/v3/auth/tokens", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("X-Subject-Token", ID)
type AuthRequest struct {
Auth struct {
Identity struct {
Password struct {
User struct {
Domain struct{ Name string }
Name string
Password string
}
}
}
}
}
var x AuthRequest
body, _ := io.ReadAll(r.Body)
_ = json.Unmarshal(body, &x)
domainName := x.Auth.Identity.Password.User.Domain.Name
userName := x.Auth.Identity.Password.User.Name
password := x.Auth.Identity.Password.User.Password
if domainName == "default" && userName == "testuser" && password == "testpw" {
w.WriteHeader(http.StatusCreated)
resp := `{"token": {
"methods": [
"password"
],
"expires_at": "2015-11-09T01:42:57.527363Z",
"user": {
"domain": {
"id": "default",
"name": "Default"
},
"id": "some_id",
"name": "admin",
"password_expires_at": null
},
"audit_ids": [
"lC2Wj1jbQe-dLjLyOx4qPQ"
],
"issued_at": "2015-11-09T00:42:57.527404Z"
}
}`
fmt.Fprint(w, resp)
} else {
w.WriteHeader(http.StatusUnauthorized)
}
})
// Correct password
options := Options{
AuthOptions: gophercloud.AuthOptions{
IdentityEndpoint: fakeServer.Endpoint(),
Username: "testuser",
Password: "testpw",
DomainName: "default",
},
}
token, err := GetToken(context.TODO(), options)
th.AssertNoErr(t, err)
th.AssertEquals(t, "0123456789", token.ID)
th.AssertEquals(t, "2015-11-09 01:42:57.527363 +0000 UTC", token.ExpiresAt.String())
// Incorrect password
options.AuthOptions.Password = "wrongpw"
_, err = GetToken(context.TODO(), options)
if !gophercloud.ResponseCodeIs(err, http.StatusUnauthorized) {
t.FailNow()
}
// Invalid auth data
options.AuthOptions.Password = ""
_, err = GetToken(context.TODO(), options)
th.AssertEquals(t, "You must provide a password to authenticate", err.Error())
}