diff --git a/error.go b/error.go index 4645d41..42daca0 100644 --- a/error.go +++ b/error.go @@ -11,6 +11,7 @@ const INTERNAL_ERROR_MESSAGE = "internal error" // // Note: these are generic codes but map well to HTTP status codes. const ( + EINVALID = "invalid" EINTERNAL = "internal" ENOTFOUND = "not_found" ) diff --git a/user.go b/user.go index 940704c..aa726f1 100644 --- a/user.go +++ b/user.go @@ -25,9 +25,49 @@ type User struct { // Auths []*Auth `json:"auths"` } +func (u *User) Validate() error { + if u.Name == "" { + return Errorf(EINVALID, "User name required.") + } + return nil +} + // UserService represents a service for managing users. type UserService interface { // Retrieves a user by ID along with their associated auth objects // Returns ENOTFOUND if user does not exist. FindUserByID(ctx context.Context, id int) (*User, error) + + // Retrieves a list of users by filter. Also returns total count of matching users + // which may differ from retruned results if filter.Limit is specified. + FindUsers(ctc context.Context, filter UserFilter) ([]*User, int, error) + + // Creates a new use. This is only used for testing since users are typically + // cretaed during the OAuth creation process in the AuthService.CreateAuth(). + CreateUser(ctx context.Context, user *User) error + + // Updates a user object. Returns EUNAUTHORIZED if current user is not + // the user that is being updated. Returns ENOTFOUND if the user does not exist. + UpdateUser(ctx context.Context, id int, upd UserUpdate) (*User, error) + + // Permanently deletes a user and all owned application resources. Returns EUNAUTHORIZED + // if current user is not the user being deleted. Returns ENOTFOUND if the user + // does not exist. + DeleteUser(ctx context.Context, id int) error +} + +// UserFilter respresents a filter passed to FindUsers(). +type UserFilter struct { + ID *int `json:"id"` + Email *string `json:"email"` + APIKey *string `json:"apiKey"` + + // Restrict to subset of results + Offset int `json:"offset"` + Limit int `json:"limit"` +} + +type UserUpdate struct { + Name *string `json:"name"` + Email *string `json:"email"` } diff --git a/user_test.go b/user_test.go new file mode 100644 index 0000000..f124f67 --- /dev/null +++ b/user_test.go @@ -0,0 +1,20 @@ +package ratchet + +import "testing" + +func TestUserValidation(t *testing.T) { + t.Run("user should return invalid", func(t *testing.T) { + u := &User{} + if ErrorCode(u.Validate()) != EINVALID { + t.Errorf("User validation should have failed but passed instead.") + } + }) + + t.Run("user validation should pass", func(t *testing.T) { + u := &User{Name: "Drew"} + if u.Validate() != nil { + t.Errorf("User validation failed") + } + }) + +}