-
Notifications
You must be signed in to change notification settings - Fork 0
/
authhttp_test.go
169 lines (147 loc) · 4.93 KB
/
authhttp_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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package apiserver_test
import (
"bufio"
"crypto/tls"
"crypto/x509"
"encoding/json"
"io"
"net/http"
"net/url"
"os"
"github.com/juju/names"
jc "github.com/juju/testing/checkers"
"github.com/juju/utils"
"golang.org/x/net/websocket"
gc "gopkg.in/check.v1"
apihttp "github.com/juju/juju/apiserver/http"
"github.com/juju/juju/apiserver/params"
jujutesting "github.com/juju/juju/juju/testing"
"github.com/juju/juju/state"
"github.com/juju/juju/testing"
"github.com/juju/juju/testing/factory"
)
// authHttpSuite provides helpers for testing HTTP "streaming" style APIs.
type authHttpSuite struct {
jujutesting.JujuConnSuite
envUUID string
}
func (s *authHttpSuite) SetUpTest(c *gc.C) {
s.JujuConnSuite.SetUpTest(c)
s.envUUID = s.State.EnvironUUID()
}
func (s *authHttpSuite) baseURL(c *gc.C) *url.URL {
info := s.APIInfo(c)
return &url.URL{
Scheme: "https",
Host: info.Addrs[0],
Path: "",
}
}
func (s *authHttpSuite) assertErrorResponse(c *gc.C, resp *http.Response, expCode int, expError string) {
body := assertResponse(c, resp, expCode, apihttp.CTypeJSON)
c.Check(jsonResponse(c, body).Error, gc.Matches, expError)
}
func (s *authHttpSuite) dialWebsocketFromURL(c *gc.C, server string, header http.Header) *websocket.Conn {
config := s.makeWebsocketConfigFromURL(c, server, header)
c.Logf("dialing %v", server)
conn, err := websocket.DialConfig(config)
c.Assert(err, jc.ErrorIsNil)
return conn
}
func (s *authHttpSuite) makeWebsocketConfigFromURL(c *gc.C, server string, header http.Header) *websocket.Config {
config, err := websocket.NewConfig(server, "http://localhost/")
c.Assert(err, jc.ErrorIsNil)
config.Header = header
caCerts := x509.NewCertPool()
c.Assert(caCerts.AppendCertsFromPEM([]byte(testing.CACert)), jc.IsTrue)
config.TlsConfig = &tls.Config{RootCAs: caCerts, ServerName: "anything"}
return config
}
func (s *authHttpSuite) assertWebsocketClosed(c *gc.C, reader *bufio.Reader) {
_, err := reader.ReadByte()
c.Assert(err, gc.Equals, io.EOF)
}
func (s *authHttpSuite) makeURL(c *gc.C, scheme, path string, queryParams url.Values) *url.URL {
url := s.baseURL(c)
query := ""
if queryParams != nil {
query = queryParams.Encode()
}
url.Scheme = scheme
url.Path += path
url.RawQuery = query
return url
}
// userAuthHttpSuite extends authHttpSuite with helpers for testing
// HTTP "streaming" style APIs which only accept user logins (not
// agents).
type userAuthHttpSuite struct {
authHttpSuite
userTag names.UserTag
password string
archiveContentType string
}
func (s *userAuthHttpSuite) SetUpTest(c *gc.C) {
s.authHttpSuite.SetUpTest(c)
s.password = "password"
user := s.Factory.MakeUser(c, &factory.UserParams{Password: s.password})
s.userTag = user.UserTag()
}
func (s *userAuthHttpSuite) sendRequest(c *gc.C, tag, password, method, uri, contentType string, body io.Reader) (*http.Response, error) {
c.Logf("sendRequest: %s", uri)
req, err := http.NewRequest(method, uri, body)
c.Assert(err, jc.ErrorIsNil)
if tag != "" && password != "" {
req.SetBasicAuth(tag, password)
}
if contentType != "" {
req.Header.Set("Content-Type", contentType)
}
return utils.GetNonValidatingHTTPClient().Do(req)
}
func (s *userAuthHttpSuite) setupOtherEnvironment(c *gc.C) *state.State {
envState := s.Factory.MakeEnvironment(c, nil)
s.AddCleanup(func(*gc.C) { envState.Close() })
user := s.Factory.MakeUser(c, nil)
_, err := envState.AddEnvironmentUser(user.UserTag(), s.userTag, "")
c.Assert(err, jc.ErrorIsNil)
s.userTag = user.UserTag()
s.password = "password"
s.envUUID = envState.EnvironUUID()
return envState
}
func (s *userAuthHttpSuite) authRequest(c *gc.C, method, uri, contentType string, body io.Reader) (*http.Response, error) {
return s.sendRequest(c, s.userTag.String(), s.password, method, uri, contentType, body)
}
func (s *userAuthHttpSuite) uploadRequest(c *gc.C, uri string, asZip bool, path string) (*http.Response, error) {
contentType := apihttp.CTypeRaw
if asZip {
contentType = s.archiveContentType
}
if path == "" {
return s.authRequest(c, "POST", uri, contentType, nil)
}
file, err := os.Open(path)
c.Assert(err, jc.ErrorIsNil)
defer file.Close()
return s.authRequest(c, "POST", uri, contentType, file)
}
// assertJSONError checks the JSON encoded error returned by the log
// and logsink APIs matches the expected value.
func assertJSONError(c *gc.C, reader *bufio.Reader, expected string) {
errResult := readJSONErrorLine(c, reader)
c.Assert(errResult.Error, gc.NotNil)
c.Assert(errResult.Error.Message, gc.Matches, expected)
}
// readJSONErrorLine returns the error line returned by the log and
// logsink APIS.
func readJSONErrorLine(c *gc.C, reader *bufio.Reader) params.ErrorResult {
line, err := reader.ReadSlice('\n')
c.Assert(err, jc.ErrorIsNil)
var errResult params.ErrorResult
err = json.Unmarshal(line, &errResult)
c.Assert(err, jc.ErrorIsNil)
return errResult
}