github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/state/apiserver/common/password_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package common_test
     5  
     6  import (
     7  	"fmt"
     8  
     9  	gc "launchpad.net/gocheck"
    10  
    11  	"launchpad.net/juju-core/errors"
    12  	"launchpad.net/juju-core/state"
    13  	"launchpad.net/juju-core/state/api/params"
    14  	"launchpad.net/juju-core/state/apiserver/common"
    15  	apiservertesting "launchpad.net/juju-core/state/apiserver/testing"
    16  	jc "launchpad.net/juju-core/testing/checkers"
    17  )
    18  
    19  type passwordSuite struct{}
    20  
    21  var _ = gc.Suite(&passwordSuite{})
    22  
    23  type entityWithError interface {
    24  	state.Entity
    25  	error() error
    26  }
    27  
    28  type fakeState struct {
    29  	entities map[string]entityWithError
    30  }
    31  
    32  func (st *fakeState) FindEntity(tag string) (state.Entity, error) {
    33  	entity, ok := st.entities[tag]
    34  	if !ok {
    35  		return nil, errors.NotFoundf("entity %q", tag)
    36  	}
    37  	if err := entity.error(); err != nil {
    38  		return nil, err
    39  	}
    40  	return entity, nil
    41  }
    42  
    43  type fetchError string
    44  
    45  func (f fetchError) error() error {
    46  	if f == "" {
    47  		return nil
    48  	}
    49  	return fmt.Errorf("%s", string(f))
    50  }
    51  
    52  type fakeAuthenticator struct {
    53  	// Any Authenticator methods we don't implement on fakeAuthenticator
    54  	// will fall back to this and panic because it's always nil.
    55  	state.Authenticator
    56  	state.Entity
    57  	err  error
    58  	pass string
    59  	fetchError
    60  }
    61  
    62  func (a *fakeAuthenticator) SetPassword(pass string) error {
    63  	if a.err != nil {
    64  		return a.err
    65  	}
    66  	a.pass = pass
    67  	return nil
    68  }
    69  
    70  // fakeUnitAuthenticator simulates a unit entity.
    71  type fakeUnitAuthenticator struct {
    72  	fakeAuthenticator
    73  	mongoPass string
    74  }
    75  
    76  func (a *fakeUnitAuthenticator) Tag() string {
    77  	return "fake"
    78  }
    79  
    80  func (a *fakeUnitAuthenticator) SetMongoPassword(pass string) error {
    81  	if a.err != nil {
    82  		return a.err
    83  	}
    84  	a.mongoPass = pass
    85  	return nil
    86  }
    87  
    88  // fakeMachineAuthenticator simulates a machine entity.
    89  type fakeMachineAuthenticator struct {
    90  	fakeUnitAuthenticator
    91  	jobs []state.MachineJob
    92  }
    93  
    94  func (a *fakeMachineAuthenticator) Jobs() []state.MachineJob {
    95  	return a.jobs
    96  }
    97  
    98  func (*passwordSuite) TestSetPasswords(c *gc.C) {
    99  	st := &fakeState{
   100  		entities: map[string]entityWithError{
   101  			"x0": &fakeAuthenticator{},
   102  			"x1": &fakeAuthenticator{},
   103  			"x2": &fakeAuthenticator{
   104  				err: fmt.Errorf("x2 error"),
   105  			},
   106  			"x3": &fakeAuthenticator{
   107  				fetchError: "x3 error",
   108  			},
   109  			"x4": &fakeUnitAuthenticator{},
   110  			"x5": &fakeMachineAuthenticator{jobs: []state.MachineJob{state.JobHostUnits}},
   111  			"x6": &fakeMachineAuthenticator{jobs: []state.MachineJob{state.JobManageEnviron}},
   112  		},
   113  	}
   114  	getCanChange := func() (common.AuthFunc, error) {
   115  		return func(tag string) bool {
   116  			return tag != "x0"
   117  		}, nil
   118  	}
   119  	pc := common.NewPasswordChanger(st, getCanChange)
   120  	var changes []params.PasswordChange
   121  	for i := 0; i < len(st.entities); i++ {
   122  		tag := fmt.Sprintf("x%d", i)
   123  		changes = append(changes, params.PasswordChange{
   124  			Tag:      tag,
   125  			Password: fmt.Sprintf("%spass", tag),
   126  		})
   127  	}
   128  	results, err := pc.SetPasswords(params.PasswordChanges{
   129  		Changes: changes,
   130  	})
   131  	c.Assert(err, gc.IsNil)
   132  	c.Assert(results, jc.DeepEquals, params.ErrorResults{
   133  		Results: []params.ErrorResult{
   134  			{apiservertesting.ErrUnauthorized},
   135  			{nil},
   136  			{&params.Error{Message: "x2 error"}},
   137  			{&params.Error{Message: "x3 error"}},
   138  			{nil},
   139  			{nil},
   140  			{nil},
   141  		},
   142  	})
   143  	c.Check(st.entities["x0"].(*fakeAuthenticator).pass, gc.Equals, "")
   144  	c.Check(st.entities["x1"].(*fakeAuthenticator).pass, gc.Equals, "x1pass")
   145  	c.Check(st.entities["x2"].(*fakeAuthenticator).pass, gc.Equals, "")
   146  	c.Check(st.entities["x4"].(*fakeUnitAuthenticator).pass, gc.Equals, "x4pass")
   147  	c.Check(st.entities["x4"].(*fakeUnitAuthenticator).mongoPass, gc.Equals, "")
   148  	c.Check(st.entities["x5"].(*fakeMachineAuthenticator).pass, gc.Equals, "x5pass")
   149  	c.Check(st.entities["x5"].(*fakeMachineAuthenticator).mongoPass, gc.Equals, "")
   150  	c.Check(st.entities["x6"].(*fakeMachineAuthenticator).pass, gc.Equals, "x6pass")
   151  	c.Check(st.entities["x6"].(*fakeMachineAuthenticator).mongoPass, gc.Equals, "x6pass")
   152  }
   153  
   154  func (*passwordSuite) TestSetPasswordsError(c *gc.C) {
   155  	getCanChange := func() (common.AuthFunc, error) {
   156  		return nil, fmt.Errorf("splat")
   157  	}
   158  	pc := common.NewPasswordChanger(&fakeState{}, getCanChange)
   159  	var changes []params.PasswordChange
   160  	for i := 0; i < 4; i++ {
   161  		tag := fmt.Sprintf("x%d", i)
   162  		changes = append(changes, params.PasswordChange{
   163  			Tag:      tag,
   164  			Password: fmt.Sprintf("%spass", tag),
   165  		})
   166  	}
   167  	_, err := pc.SetPasswords(params.PasswordChanges{Changes: changes})
   168  	c.Assert(err, gc.ErrorMatches, "splat")
   169  }
   170  
   171  func (*passwordSuite) TestSetPasswordsNoArgsNoError(c *gc.C) {
   172  	getCanChange := func() (common.AuthFunc, error) {
   173  		return nil, fmt.Errorf("splat")
   174  	}
   175  	pc := common.NewPasswordChanger(&fakeState{}, getCanChange)
   176  	result, err := pc.SetPasswords(params.PasswordChanges{})
   177  	c.Assert(err, gc.IsNil)
   178  	c.Assert(result.Results, gc.HasLen, 0)
   179  }