func (as *authSuite) TestNewUserAddsToExistent(c *C) { as.state.Lock() firstUser, err := auth.NewUser(as.state, "username", "*****@*****.**", "macaroon", []string{"discharge"}) as.state.Unlock() c.Check(err, IsNil) // adding a new one as.state.Lock() user, err := auth.NewUser(as.state, "new_username", "*****@*****.**", "new_macaroon", []string{"new_discharge"}) as.state.Unlock() c.Check(err, IsNil) c.Check(user.ID, Equals, 2) c.Check(user.Username, Equals, "new_username") c.Check(user.Email, Equals, "*****@*****.**") as.state.Lock() userFromState, err := auth.User(as.state, 2) as.state.Unlock() c.Check(err, IsNil) c.Check(userFromState.ID, Equals, 2) c.Check(userFromState.Username, Equals, "new_username") c.Check(userFromState.Email, Equals, "*****@*****.**") // first user is still in the state as.state.Lock() userFromState, err = auth.User(as.state, 1) as.state.Unlock() c.Check(err, IsNil) c.Check(userFromState, DeepEquals, firstUser) }
func (as *authSuite) TestNewUserAddsToExistent(c *C) { as.state.Lock() firstUser, err := auth.NewUser(as.state, "username", "macaroon", []string{"discharge"}) as.state.Unlock() c.Check(err, IsNil) // adding a new one as.state.Lock() user, err := auth.NewUser(as.state, "new_username", "new_macaroon", []string{"new_discharge"}) as.state.Unlock() expected := &auth.UserState{ ID: 2, Username: "******", Macaroon: "new_macaroon", Discharges: []string{"new_discharge"}, StoreMacaroon: "new_macaroon", StoreDischarges: []string{"new_discharge"}, } c.Check(err, IsNil) c.Check(user, DeepEquals, expected) as.state.Lock() userFromState, err := auth.User(as.state, 2) as.state.Unlock() c.Check(err, IsNil) c.Check(userFromState, DeepEquals, expected) // first user is still in the state as.state.Lock() userFromState, err = auth.User(as.state, 1) as.state.Unlock() c.Check(err, IsNil) c.Check(userFromState, DeepEquals, firstUser) }
func (as *authSuite) TestRemove(c *C) { as.state.Lock() user, err := auth.NewUser(as.state, "username", "macaroon", []string{"discharge"}) as.state.Unlock() c.Check(err, IsNil) as.state.Lock() _, err = auth.User(as.state, user.ID) as.state.Unlock() c.Check(err, IsNil) as.state.Lock() err = auth.RemoveUser(as.state, user.ID) as.state.Unlock() c.Assert(err, IsNil) as.state.Lock() _, err = auth.User(as.state, user.ID) as.state.Unlock() c.Check(err, ErrorMatches, "invalid user") as.state.Lock() err = auth.RemoveUser(as.state, user.ID) as.state.Unlock() c.Assert(err, ErrorMatches, "invalid user") }
func (as *authSuite) TestAuthContextUpdateUserAuthOtherUpdate(c *C) { as.state.Lock() user, _ := auth.NewUser(as.state, "username", "macaroon", []string{"discharge"}) otherUpdateUser := *user otherUpdateUser.Macaroon = "macaroon2" otherUpdateUser.StoreDischarges = []string{"other-discharges"} err := auth.UpdateUser(as.state, &otherUpdateUser) as.state.Unlock() c.Assert(err, IsNil) newDischarges := []string{"updated-discharge"} authContext := auth.NewAuthContext(as.state, nil) // last discharges win curUser, err := authContext.UpdateUserAuth(user, newDischarges) c.Assert(err, IsNil) as.state.Lock() userFromState, err := auth.User(as.state, user.ID) as.state.Unlock() c.Check(err, IsNil) c.Check(userFromState, DeepEquals, curUser) c.Check(curUser, DeepEquals, &auth.UserState{ ID: user.ID, Username: "******", Macaroon: "macaroon2", Discharges: []string{"discharge"}, StoreMacaroon: "macaroon", StoreDischarges: newDischarges, }) }
func (as *authSuite) TestUserForNoAuthInState(c *C) { as.state.Lock() userFromState, err := auth.User(as.state, 42) as.state.Unlock() c.Check(err, NotNil) c.Check(userFromState, IsNil) }
func (as *authSuite) TestUserForNonExistent(c *C) { as.state.Lock() _, err := auth.NewUser(as.state, "username", "macaroon", []string{"discharge"}) as.state.Unlock() c.Check(err, IsNil) as.state.Lock() userFromState, err := auth.User(as.state, 42) c.Check(err, ErrorMatches, "invalid user") c.Check(userFromState, IsNil) }
func (as *authSuite) TestUser(c *C) { as.state.Lock() user, err := auth.NewUser(as.state, "username", "macaroon", []string{"discharge"}) as.state.Unlock() c.Check(err, IsNil) as.state.Lock() userFromState, err := auth.User(as.state, 1) as.state.Unlock() c.Check(err, IsNil) c.Check(userFromState, DeepEquals, user) }
func (as *authSuite) TestNewUserSortsDischarges(c *C) { as.state.Lock() user, err := auth.NewUser(as.state, "", "*****@*****.**", "macaroon", []string{"discharge2", "discharge1"}) as.state.Unlock() expected := []string{"discharge1", "discharge2"} c.Check(user.StoreDischarges, DeepEquals, expected) as.state.Lock() userFromState, err := auth.User(as.state, 1) as.state.Unlock() c.Check(err, IsNil) c.Check(userFromState.StoreDischarges, DeepEquals, expected) }
func (as *authSuite) TestAuthContextUpdateUserAuth(c *C) { as.state.Lock() user, _ := auth.NewUser(as.state, "username", "macaroon", []string{"discharge"}) as.state.Unlock() newDischarges := []string{"updated-discharge"} authContext := auth.NewAuthContext(as.state, nil) user, err := authContext.UpdateUserAuth(user, newDischarges) c.Check(err, IsNil) as.state.Lock() userFromState, err := auth.User(as.state, user.ID) as.state.Unlock() c.Check(err, IsNil) c.Check(userFromState, DeepEquals, user) c.Check(userFromState.Discharges, DeepEquals, []string{"discharge"}) c.Check(user.StoreDischarges, DeepEquals, newDischarges) }
func (as *authSuite) TestUpdateUser(c *C) { as.state.Lock() user, _ := auth.NewUser(as.state, "username", "macaroon", []string{"discharge"}) as.state.Unlock() user.Username = "******" user.StoreDischarges = []string{"updated-discharge"} as.state.Lock() err := auth.UpdateUser(as.state, user) as.state.Unlock() c.Check(err, IsNil) as.state.Lock() userFromState, err := auth.User(as.state, user.ID) as.state.Unlock() c.Check(err, IsNil) c.Check(userFromState, DeepEquals, user) }
func (as *authSuite) TestNewUserSortsDischarges(c *C) { as.state.Lock() user, err := auth.NewUser(as.state, "username", "macaroon", []string{"discharge2", "discharge1"}) as.state.Unlock() expected := &auth.UserState{ ID: 1, Username: "******", Macaroon: "macaroon", Discharges: []string{"discharge1", "discharge2"}, StoreMacaroon: "macaroon", StoreDischarges: []string{"discharge1", "discharge2"}, } c.Check(err, IsNil) c.Check(user, DeepEquals, expected) as.state.Lock() userFromState, err := auth.User(as.state, 1) as.state.Unlock() c.Check(err, IsNil) c.Check(userFromState, DeepEquals, expected) }
func userFromUserID(st *state.State, userID int) (*auth.UserState, error) { if userID == 0 { return nil, nil } return auth.User(st, userID) }