github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/apiserver/stateauthenticator/authenticator_test.go (about) 1 // Copyright 2014-2018 Canonical Ltd. All rights reserved. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package stateauthenticator_test 5 6 import ( 7 "context" 8 9 "github.com/juju/clock" 10 "github.com/juju/errors" 11 "github.com/juju/names/v5" 12 jc "github.com/juju/testing/checkers" 13 gc "gopkg.in/check.v1" 14 15 "github.com/juju/juju/apiserver/authentication" 16 "github.com/juju/juju/apiserver/stateauthenticator" 17 "github.com/juju/juju/state" 18 statetesting "github.com/juju/juju/state/testing" 19 "github.com/juju/juju/testing/factory" 20 ) 21 22 // TODO update these tests (moved from apiserver) to test 23 // via the public interface, and then get rid of export_test.go. 24 type agentAuthenticatorSuite struct { 25 statetesting.StateSuite 26 authenticator *stateauthenticator.Authenticator 27 } 28 29 var _ = gc.Suite(&agentAuthenticatorSuite{}) 30 31 func (s *agentAuthenticatorSuite) SetUpTest(c *gc.C) { 32 s.StateSuite.SetUpTest(c) 33 authenticator, err := stateauthenticator.NewAuthenticator(s.StatePool, clock.WallClock) 34 c.Assert(err, jc.ErrorIsNil) 35 s.authenticator = authenticator 36 } 37 38 func (s *agentAuthenticatorSuite) TestAuthenticateLoginRequestHandleNotSupportedRequests(c *gc.C) { 39 _, err := s.authenticator.AuthenticateLoginRequest(context.TODO(), "", "", authentication.AuthParams{Token: "token"}) 40 c.Assert(err, jc.Satisfies, errors.IsNotSupported) 41 } 42 43 func (s *agentAuthenticatorSuite) TestAuthenticatorForTag(c *gc.C) { 44 user := s.Factory.MakeUser(c, &factory.UserParams{Password: "password"}) 45 46 authenticator, err := stateauthenticator.EntityAuthenticator(s.authenticator, user.Tag()) 47 c.Assert(err, jc.ErrorIsNil) 48 c.Assert(authenticator, gc.NotNil) 49 userFinder := userFinder{user} 50 51 entity, err := authenticator.Authenticate(context.TODO(), userFinder, authentication.AuthParams{ 52 AuthTag: user.Tag(), 53 Credentials: "password", 54 Nonce: "nonce", 55 }) 56 c.Assert(err, jc.ErrorIsNil) 57 c.Assert(entity, gc.DeepEquals, user) 58 } 59 60 func (s *agentAuthenticatorSuite) TestMachineGetsAgentAuthenticator(c *gc.C) { 61 authenticator, err := stateauthenticator.EntityAuthenticator(s.authenticator, names.NewMachineTag("0")) 62 c.Assert(err, jc.ErrorIsNil) 63 _, ok := authenticator.(*authentication.AgentAuthenticator) 64 c.Assert(ok, jc.IsTrue) 65 } 66 67 func (s *agentAuthenticatorSuite) TestModelGetsAgentAuthenticator(c *gc.C) { 68 authenticator, err := stateauthenticator.EntityAuthenticator(s.authenticator, names.NewModelTag("deadbeef-0bad-400d-8000-4b1d0d06f00d")) 69 c.Assert(err, jc.ErrorIsNil) 70 _, ok := authenticator.(*authentication.AgentAuthenticator) 71 c.Assert(ok, jc.IsTrue) 72 } 73 74 func (s *agentAuthenticatorSuite) TestUnitGetsAgentAuthenticator(c *gc.C) { 75 authenticator, err := stateauthenticator.EntityAuthenticator(s.authenticator, names.NewUnitTag("wordpress/0")) 76 c.Assert(err, jc.ErrorIsNil) 77 _, ok := authenticator.(*authentication.AgentAuthenticator) 78 c.Assert(ok, jc.IsTrue) 79 } 80 81 func (s *agentAuthenticatorSuite) TestNotSupportedTag(c *gc.C) { 82 authenticator, err := stateauthenticator.EntityAuthenticator(s.authenticator, names.NewCloudTag("not-support")) 83 c.Assert(err, gc.ErrorMatches, "unexpected login entity tag: invalid request") 84 c.Assert(authenticator, gc.IsNil) 85 } 86 87 type userFinder struct { 88 user state.Entity 89 } 90 91 func (u userFinder) FindEntity(tag names.Tag) (state.Entity, error) { 92 return u.user, nil 93 }