end-to-end testing setup

This commit is contained in:
Hayden 2022-09-03 18:42:03 -08:00
parent b4eb7d8ddc
commit ad4c8c9ab4
41 changed files with 544 additions and 313 deletions

View file

@ -33,7 +33,7 @@ type SwaggerConf struct {
type WebConfig struct {
Port string `yaml:"port" conf:"default:7745"`
Host string `yaml:"host" conf:"default:127.0.0.1"`
Host string `yaml:"host"`
}
// NewConfig parses the CLI/Config file and returns a Config struct. If the file argument is an empty string, the

View file

@ -14,23 +14,17 @@ type EntUserRepository struct {
}
func (e *EntUserRepository) GetOneId(ctx context.Context, id uuid.UUID) (*ent.User, error) {
usr, err := e.db.User.Query().Where(user.ID(id)).Only(ctx)
if err != nil {
return usr, err
}
return usr, nil
return e.db.User.Query().
Where(user.ID(id)).
WithGroup().
Only(ctx)
}
func (e *EntUserRepository) GetOneEmail(ctx context.Context, email string) (*ent.User, error) {
usr, err := e.db.User.Query().Where(user.Email(email)).Only(ctx)
if err != nil {
return usr, err
}
return usr, nil
return e.db.User.Query().
Where(user.Email(email)).
WithGroup().
Only(ctx)
}
func (e *EntUserRepository) GetAll(ctx context.Context) ([]*ent.User, error) {
@ -59,7 +53,11 @@ func (e *EntUserRepository) Create(ctx context.Context, usr types.UserCreate) (*
SetGroupID(usr.GroupID).
Save(ctx)
return entUser, err
if err != nil {
return entUser, err
}
return e.GetOneId(ctx, entUser.ID)
}
func (e *EntUserRepository) Update(ctx context.Context, ID uuid.UUID, data types.UserUpdate) error {

View file

@ -0,0 +1,20 @@
package mappers
import (
"github.com/hay-kot/content/backend/ent"
"github.com/hay-kot/content/backend/internal/types"
)
func ToOutUser(user *ent.User, err error) (*types.UserOut, error) {
if err != nil {
return &types.UserOut{}, err
}
return &types.UserOut{
ID: user.ID,
Name: user.Name,
Email: user.Email,
IsSuperuser: user.IsSuperuser,
GroupName: user.Edges.Group.Name,
GroupID: user.Edges.Group.ID,
}, nil
}

View file

@ -6,8 +6,8 @@ import (
"time"
"github.com/google/uuid"
"github.com/hay-kot/content/backend/ent"
"github.com/hay-kot/content/backend/internal/repo"
"github.com/hay-kot/content/backend/internal/services/mappers"
"github.com/hay-kot/content/backend/internal/types"
"github.com/hay-kot/content/backend/pkgs/hasher"
)
@ -23,24 +23,6 @@ type UserService struct {
repos *repo.AllRepos
}
func ToOutUser(user *ent.User, err error) (*types.UserOut, error) {
if err != nil {
return &types.UserOut{}, err
}
return &types.UserOut{
ID: user.ID,
Name: user.Name,
Email: user.Email,
IsSuperuser: user.IsSuperuser,
GroupName: user.Edges.Group.Name,
GroupID: user.Edges.Group.ID,
}, nil
}
func (UserService) toOutUser(user *ent.User, err error) (*types.UserOut, error) {
return ToOutUser(user, err)
}
func (svc *UserService) RegisterUser(ctx context.Context, data types.UserRegistration) (*types.UserOut, error) {
group, err := svc.repos.Groups.Create(ctx, data.GroupName)
if err != nil {
@ -48,7 +30,6 @@ func (svc *UserService) RegisterUser(ctx context.Context, data types.UserRegistr
}
hashed, _ := hasher.HashPassword(data.User.Password)
usrCreate := types.UserCreate{
Name: data.User.Name,
Email: data.User.Email,
@ -57,23 +38,22 @@ func (svc *UserService) RegisterUser(ctx context.Context, data types.UserRegistr
GroupID: group.ID,
}
return svc.toOutUser(svc.repos.Users.Create(ctx, usrCreate))
return mappers.ToOutUser(svc.repos.Users.Create(ctx, usrCreate))
}
// GetSelf returns the user that is currently logged in based of the token provided within
func (svc *UserService) GetSelf(ctx context.Context, requestToken string) (*types.UserOut, error) {
hash := hasher.HashToken(requestToken)
return svc.toOutUser(svc.repos.AuthTokens.GetUserFromToken(ctx, hash))
return mappers.ToOutUser(svc.repos.AuthTokens.GetUserFromToken(ctx, hash))
}
func (svc *UserService) UpdateSelf(ctx context.Context, ID uuid.UUID, data types.UserUpdate) (*types.UserOut, error) {
err := svc.repos.Users.Update(ctx, ID, data)
if err != nil {
return &types.UserOut{}, err
}
return svc.toOutUser(svc.repos.Users.GetOneId(ctx, ID))
return mappers.ToOutUser(svc.repos.Users.GetOneId(ctx, ID))
}
// ============================================================================
@ -120,3 +100,10 @@ func (svc *UserService) RenewToken(ctx context.Context, token string) (types.Use
return newToken, nil
}
// DeleteSelf deletes the user that is currently logged based of the provided UUID
// There is _NO_ protection against deleting the wrong user, as such this should only
// be used when the identify of the user has been confirmed.
func (svc *UserService) DeleteSelf(ctx context.Context, ID uuid.UUID) error {
return svc.repos.Users.Delete(ctx, ID)
}