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
|
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package candid_test
import (
"net/http"
"testing"
qt "github.com/frankban/quicktest"
"github.com/frankban/quicktest/qtsuite"
"github.com/juju/qthttptest"
yaml "gopkg.in/yaml.v2"
"github.com/canonical/candid"
"github.com/canonical/candid/config"
"github.com/canonical/candid/idp"
_ "github.com/canonical/candid/idp/agent"
_ "github.com/canonical/candid/idp/static"
"github.com/canonical/candid/internal/candidtest"
"github.com/canonical/candid/version"
)
func TestServer(t *testing.T) {
qtsuite.Run(qt.New(t), &serverSuite{})
}
type serverSuite struct {
store *candidtest.Store
}
func (s *serverSuite) Init(c *qt.C) {
s.store = candidtest.NewStore()
}
func (s *serverSuite) TestNewServerWithNoVersions(c *qt.C) {
h, err := candid.NewServer(candid.ServerParams(s.store.ServerParams()))
c.Assert(err, qt.ErrorMatches, `identity server must serve at least one version of the API`)
c.Assert(h, qt.IsNil)
}
func (s *serverSuite) TestNewServerWithUnregisteredVersion(c *qt.C) {
h, err := candid.NewServer(
candid.ServerParams(s.store.ServerParams()),
"wrong",
)
c.Assert(err, qt.ErrorMatches, `unknown version "wrong"`)
c.Assert(h, qt.IsNil)
}
type versionResponse struct {
Version string
Path string
}
func (s *serverSuite) TestVersions(c *qt.C) {
c.Assert(candid.Versions(), qt.DeepEquals, []string{"debug", "discharger", "v1"})
}
func (s *serverSuite) TestNewServerWithVersions(c *qt.C) {
h, err := candid.NewServer(
candid.ServerParams(s.store.ServerParams()),
candid.Debug,
)
c.Assert(err, qt.IsNil)
defer h.Close()
qthttptest.AssertJSONCall(c, qthttptest.JSONCallParams{
Handler: h,
URL: "/debug/info",
ExpectStatus: http.StatusOK,
ExpectBody: version.VersionInfo,
})
assertDoesNotServeVersion(c, h, "v0")
}
func (s *serverSuite) TestNewServerRemovesAgentIDP(c *qt.C) {
var conf config.Config
err := yaml.Unmarshal([]byte(`{"identity-providers": [{"type":"agent"},{"type":"static","name":"test"}]}`), &conf)
c.Assert(err, qt.IsNil)
idps := make([]idp.IdentityProvider, len(conf.IdentityProviders))
for i, idp := range conf.IdentityProviders {
idps[i] = idp.IdentityProvider
}
sp := candid.ServerParams(s.store.ServerParams())
sp.IdentityProviders = idps
h, err := candid.NewServer(sp, candid.V1)
c.Assert(err, qt.IsNil)
h.Close()
}
func assertServesVersion(c *qt.C, h http.Handler, vers string) {
qthttptest.AssertJSONCall(c, qthttptest.JSONCallParams{
Handler: h,
URL: "/" + vers + "/some/path",
ExpectBody: versionResponse{
Version: vers,
Path: "/some/path",
},
})
}
func assertDoesNotServeVersion(c *qt.C, h http.Handler, vers string) {
rec := qthttptest.DoRequest(c, qthttptest.DoRequestParams{
Handler: h,
URL: "/" + vers + "/some/path",
})
c.Assert(rec.Code, qt.Equals, http.StatusNotFound)
}
|