func TestUser_SignUp_failsIfUserHasSignedUpAlready(t *testing.T) { password := ess.Password() password.UnmarshalText([]byte("password")) history := []*ess.Event{ ess.NewEvent("user.signed-up"). Add("username", "test-user"). Add("password", password.String()). Add("email", "*****@*****.**"). Add("name", "John Doe"), } user := NewUser("username") for _, event := range history { user.HandleEvent(event) } err := user.SignUp("Jane Doe", "*****@*****.**", password.String()) if err == nil { t.Fatal("Expected an error") } verr, ok := err.(*ess.ValidationError) if !ok { t.Fatalf("err.(type) = %T; want %T", err, verr) } if got, want := len(verr.Errors["username"]), 1; got != want { t.Errorf(`len(verr.Errors["username"]) = %v; want %v`, got, want) } if got, want := verr.Errors["username"][0], "not_unique"; got != want { t.Errorf(`verr.Errors["username"][0] = %v; want %v`, got, want) } }
func (self *User) SignUp(name, email, password string) error { err := ess.NewValidationError() if self.signedUp { err.Add("username", "not_unique") } if password == "" { err.Add("password", "empty") } if email == "" { err.Add("email", "empty") } if err.Ok() { self.events.PublishEvent( ess.NewEvent("user.signed-up"). For(self). Add("password", password). Add("email", email). Add("name", name), ) } return err.Return() }
func (self *Post) Write(title, body, author string) error { err := ess.NewValidationError() if self.written { err.Add("post", "not_unique") } if title == "" { err.Add("title", "empty") } if body == "" { err.Add("body", "empty") } if author == "" { err.Add("username", "empty") } if err.Ok() { self.events.PublishEvent( ess.NewEvent("post.written"). For(self). Add("title", title). Add("author", author). Add("body", body), ) } return err.Return() }
func (self *User) Logout(session string) error { err := ess.NewValidationError() if !self.signedUp { err.Add("user", "not_found") } if session == "" { err.Add("session", "empty") } if !self.HasActiveSession(session) { err.Add("session", "expired") } if err.Ok() { self.events.PublishEvent( ess.NewEvent("user.logged-out"). For(self). Add("session", session), ) } return err.Return() }
func (self *User) SignUp(params *ess.Command) error { err := ess.NewValidationError() if self.signedUp { err.Add("username", "not_unique") } if err.Ok() { self.events.PublishEvent( ess.NewEvent("user.signed-up"). For(self). Add("username", params.Get("username").String()). Add("password", params.Get("password").String()). Add("email", params.Get("email").String()), ) } return err.Return() }
func (self *User) Login(session string, password *ess.BcryptedPassword) error { err := ess.NewValidationError() if !self.signedUp { err.Add("user", "not_found") } if !password.Matches(self.password) { err.Add("password", "mismatch") } if err.Ok() { self.events.PublishEvent( ess.NewEvent("user.logged-in"). For(self). Add("session", session), ) } return err.Return() }
func (self *Post) Edit(title, body, reason, username string) error { err := ess.NewValidationError() if !self.written { err.Add("post", "not_found") } if title == "" { err.Add("title", "empty") } if body == "" { err.Add("body", "empty") } if username != self.author { err.Add("username", "mismatch") } if reason == "" { err.Add("reason", "empty") } if err.Ok() { self.events.PublishEvent( ess.NewEvent("post.edited"). For(self). Add("title", title). Add("body", body). Add("author", username). Add("reason", reason), ) } return err.Return() }