github.com/rogpeppe/juju@v0.0.0-20140613142852-6337964b789e/testing/factory/factory.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package factory 5 6 import ( 7 "fmt" 8 9 gc "launchpad.net/gocheck" 10 11 "github.com/juju/juju/state" 12 ) 13 14 type Factory struct { 15 st *state.State 16 c *gc.C 17 index int 18 } 19 20 func NewFactory(st *state.State, c *gc.C) *Factory { 21 return &Factory{st: st, c: c} 22 } 23 24 type UserParams struct { 25 Username string 26 DisplayName string 27 Password string 28 Creator string 29 } 30 31 func (factory *Factory) UniqueInteger() int { 32 factory.index++ 33 return factory.index 34 } 35 36 func (factory *Factory) UniqueString(prefix string) string { 37 if prefix == "" { 38 prefix = "no-prefix" 39 } 40 return fmt.Sprintf("%s-%d", prefix, factory.UniqueInteger()) 41 } 42 43 // MakeAnyUser will create a user with no specified values. 44 func (factory *Factory) MakeAnyUser() *state.User { 45 return factory.MakeUser(UserParams{}) 46 } 47 48 // MakeUser will create a user with values defined by the params. 49 // For attributes of UserParams that are the default empty values, 50 // some meaningful valid values are used instead. 51 func (factory *Factory) MakeUser(params UserParams) *state.User { 52 if params.Username == "" { 53 params.Username = factory.UniqueString("username") 54 } 55 if params.DisplayName == "" { 56 params.DisplayName = factory.UniqueString("display name") 57 } 58 if params.Password == "" { 59 params.Password = "password" 60 } 61 if params.Creator == "" { 62 params.Creator = "admin" 63 } 64 user, err := factory.st.AddUser( 65 params.Username, params.DisplayName, params.Password, params.Creator) 66 factory.c.Assert(err, gc.IsNil) 67 return user 68 }