forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
introspection_test.go
73 lines (62 loc) · 1.76 KB
/
introspection_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
// Copyright 2017 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package apiserver_test
import (
"io"
"net/http"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
apitesting "github.com/juju/juju/apiserver/testing"
"github.com/juju/juju/core/permission"
"github.com/juju/juju/state"
)
type introspectionSuite struct {
apiserverBaseSuite
bob *state.User
url string
}
var _ = gc.Suite(&introspectionSuite{})
func (s *introspectionSuite) SetUpTest(c *gc.C) {
s.apiserverBaseSuite.SetUpTest(c)
bob, err := s.State.AddUser("bob", "", "hunter2", "admin")
c.Assert(err, jc.ErrorIsNil)
s.bob = bob
s.url = s.server.URL + "/introspection/navel"
}
func (s *introspectionSuite) TestAccess(c *gc.C) {
s.testAccess(c, s.Owner.String(), ownerPassword)
model, err := s.State.Model()
c.Assert(err, jc.ErrorIsNil)
_, err = model.AddUser(
state.UserAccessSpec{
User: s.bob.UserTag(),
CreatedBy: s.Owner,
Access: permission.ReadAccess,
},
)
c.Assert(err, jc.ErrorIsNil)
s.testAccess(c, "user-bob", "hunter2")
}
func (s *introspectionSuite) testAccess(c *gc.C, tag, password string) {
resp := apitesting.SendHTTPRequest(c, apitesting.HTTPRequestParams{
Method: "GET",
URL: s.url,
Tag: tag,
Password: password,
})
defer resp.Body.Close()
c.Assert(resp.StatusCode, gc.Equals, http.StatusOK)
content, err := io.ReadAll(resp.Body)
c.Assert(err, jc.ErrorIsNil)
c.Assert(string(content), gc.Equals, "gazing")
}
func (s *introspectionSuite) TestAccessDenied(c *gc.C) {
resp := apitesting.SendHTTPRequest(c, apitesting.HTTPRequestParams{
Method: "GET",
URL: s.url,
Tag: "user-bob",
Password: "hunter2",
})
defer resp.Body.Close()
c.Assert(resp.StatusCode, gc.Equals, http.StatusForbidden)
}