Added user validation check

main
Drew Bednar 7 days ago
parent eb2f42d50a
commit 103e1d5b4e

@ -11,6 +11,7 @@ const INTERNAL_ERROR_MESSAGE = "internal error"
// //
// Note: these are generic codes but map well to HTTP status codes. // Note: these are generic codes but map well to HTTP status codes.
const ( const (
EINVALID = "invalid"
EINTERNAL = "internal" EINTERNAL = "internal"
ENOTFOUND = "not_found" ENOTFOUND = "not_found"
) )

@ -25,9 +25,49 @@ type User struct {
// Auths []*Auth `json:"auths"` // 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. // UserService represents a service for managing users.
type UserService interface { type UserService interface {
// Retrieves a user by ID along with their associated auth objects // Retrieves a user by ID along with their associated auth objects
// Returns ENOTFOUND if user does not exist. // Returns ENOTFOUND if user does not exist.
FindUserByID(ctx context.Context, id int) (*User, error) 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"`
} }

@ -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")
}
})
}
Loading…
Cancel
Save