Skip to content

Commit

Permalink
Add unit tests for middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenWeathers committed Sep 23, 2024
1 parent b928e32 commit 185a43b
Show file tree
Hide file tree
Showing 9 changed files with 603 additions and 89 deletions.
8 changes: 6 additions & 2 deletions internal/http/checkin/checkin.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,18 @@ type TeamDataSvc interface {
TeamGet(ctx context.Context, TeamID string) (*thunderdome.Team, error)
}

type UserDataSvc interface {
GetGuestUser(ctx context.Context, UserID string) (*thunderdome.User, error)
}

// Service provides retro service
type Service struct {
config Config
logger *otelzap.Logger
validateSessionCookie func(w http.ResponseWriter, r *http.Request) (string, error)
validateUserCookie func(w http.ResponseWriter, r *http.Request) (string, error)
eventHandlers map[string]func(context.Context, string, string, string) ([]byte, error, bool)
UserService thunderdome.UserDataSvc
UserService UserDataSvc
AuthService AuthDataSvc
CheckinService CheckinDataSvc
TeamService TeamDataSvc
Expand All @@ -77,7 +81,7 @@ func New(
logger *otelzap.Logger,
validateSessionCookie func(w http.ResponseWriter, r *http.Request) (string, error),
validateUserCookie func(w http.ResponseWriter, r *http.Request) (string, error),
userService thunderdome.UserDataSvc, authService AuthDataSvc,
userService UserDataSvc, authService AuthDataSvc,
checkinService CheckinDataSvc, teamService TeamDataSvc,
) *Service {
c := &Service{
Expand Down
582 changes: 538 additions & 44 deletions internal/http/middleware_test.go

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions internal/http/poker/poker.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,18 @@ type AuthDataSvc interface {
GetSessionUser(ctx context.Context, SessionId string) (*thunderdome.User, error)
}

type UserDataSvc interface {
GetGuestUser(ctx context.Context, UserID string) (*thunderdome.User, error)
}

// Service provides battle service
type Service struct {
config Config
logger *otelzap.Logger
validateSessionCookie func(w http.ResponseWriter, r *http.Request) (string, error)
validateUserCookie func(w http.ResponseWriter, r *http.Request) (string, error)
eventHandlers map[string]func(context.Context, string, string, string) ([]byte, error, bool)
UserService thunderdome.UserDataSvc
UserService UserDataSvc
AuthService AuthDataSvc
BattleService thunderdome.PokerDataSvc
}
Expand All @@ -60,7 +64,7 @@ func New(
config Config, logger *otelzap.Logger,
validateSessionCookie func(w http.ResponseWriter, r *http.Request) (string, error),
validateUserCookie func(w http.ResponseWriter, r *http.Request) (string, error),
userService thunderdome.UserDataSvc, authService AuthDataSvc,
userService UserDataSvc, authService AuthDataSvc,
battleService thunderdome.PokerDataSvc,
) *Service {
b := &Service{
Expand Down
8 changes: 6 additions & 2 deletions internal/http/retro/retro.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,18 @@ type AuthDataSvc interface {
GetSessionUser(ctx context.Context, SessionId string) (*thunderdome.User, error)
}

type UserDataSvc interface {
GetGuestUser(ctx context.Context, UserID string) (*thunderdome.User, error)
}

// Service provides retro service
type Service struct {
config Config
logger *otelzap.Logger
validateSessionCookie func(w http.ResponseWriter, r *http.Request) (string, error)
validateUserCookie func(w http.ResponseWriter, r *http.Request) (string, error)
eventHandlers map[string]func(context.Context, string, string, string) ([]byte, error, bool)
UserService thunderdome.UserDataSvc
UserService UserDataSvc
AuthService AuthDataSvc
RetroService thunderdome.RetroDataSvc
TemplateService thunderdome.RetroTemplateDataSvc
Expand All @@ -62,7 +66,7 @@ func New(
logger *otelzap.Logger,
validateSessionCookie func(w http.ResponseWriter, r *http.Request) (string, error),
validateUserCookie func(w http.ResponseWriter, r *http.Request) (string, error),
userService thunderdome.UserDataSvc, authService AuthDataSvc,
userService UserDataSvc, authService AuthDataSvc,
retroService thunderdome.RetroDataSvc, templateService thunderdome.RetroTemplateDataSvc,
emailService thunderdome.EmailService,
) *Service {
Expand Down
8 changes: 6 additions & 2 deletions internal/http/storyboard/storyboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,18 @@ type AuthDataSvc interface {
GetSessionUser(ctx context.Context, SessionId string) (*thunderdome.User, error)
}

type UserDataSvc interface {
GetGuestUser(ctx context.Context, UserID string) (*thunderdome.User, error)
}

// Service provides storyboard service
type Service struct {
config Config
Logger *otelzap.Logger
ValidateSessionCookie func(w http.ResponseWriter, r *http.Request) (string, error)
ValidateUserCookie func(w http.ResponseWriter, r *http.Request) (string, error)
EventHandlers map[string]func(context.Context, string, string, string) ([]byte, error, bool)
UserService thunderdome.UserDataSvc
UserService UserDataSvc
AuthService AuthDataSvc
StoryboardService thunderdome.StoryboardDataSvc
}
Expand All @@ -60,7 +64,7 @@ func New(
logger *otelzap.Logger,
validateSessionCookie func(w http.ResponseWriter, r *http.Request) (string, error),
validateUserCookie func(w http.ResponseWriter, r *http.Request) (string, error),
userService thunderdome.UserDataSvc, authService AuthDataSvc,
userService UserDataSvc, authService AuthDataSvc,
storyboardService thunderdome.StoryboardDataSvc,
) *Service {
sb := &Service{
Expand Down
24 changes: 23 additions & 1 deletion internal/http/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ type Service struct {
Router *mux.Router
Email thunderdome.EmailService
Logger *otelzap.Logger
UserDataSvc thunderdome.UserDataSvc
UserDataSvc UserDataSvc
ApiKeyDataSvc APIKeyDataSvc
AlertDataSvc AlertDataSvc
AuthDataSvc AuthDataSvc
Expand Down Expand Up @@ -302,3 +302,25 @@ type SubscriptionDataSvc interface {
GetSubscriptions(ctx context.Context, Limit int, Offset int) ([]thunderdome.Subscription, int, error)
DeleteSubscription(ctx context.Context, id string) error
}

type UserDataSvc interface {
GetUser(ctx context.Context, UserID string) (*thunderdome.User, error)
GetGuestUser(ctx context.Context, UserID string) (*thunderdome.User, error)
GetUserByEmail(ctx context.Context, UserEmail string) (*thunderdome.User, error)
GetRegisteredUsers(ctx context.Context, Limit int, Offset int) ([]*thunderdome.User, int, error)
SearchRegisteredUsersByEmail(ctx context.Context, Email string, Limit int, Offset int) ([]*thunderdome.User, int, error)
CreateUser(ctx context.Context, UserName string, UserEmail string, UserPassword string) (NewUser *thunderdome.User, VerifyID string, RegisterErr error)
CreateUserGuest(ctx context.Context, UserName string) (*thunderdome.User, error)
CreateUserRegistered(ctx context.Context, UserName string, UserEmail string, UserPassword string, ActiveUserID string) (NewUser *thunderdome.User, VerifyID string, RegisterErr error)
UpdateUserAccount(ctx context.Context, UserID string, UserName string, UserEmail string, UserAvatar string, NotificationsEnabled bool, Country string, Locale string, Company string, JobTitle string, Theme string) error
UpdateUserProfile(ctx context.Context, UserID string, UserName string, UserAvatar string, NotificationsEnabled bool, Country string, Locale string, Company string, JobTitle string, Theme string) error
UpdateUserProfileLdap(ctx context.Context, UserID string, UserAvatar string, NotificationsEnabled bool, Country string, Locale string, Company string, JobTitle string, Theme string) error
PromoteUser(ctx context.Context, UserID string) error
DemoteUser(ctx context.Context, UserID string) error
DisableUser(ctx context.Context, UserID string) error
EnableUser(ctx context.Context, UserID string) error
DeleteUser(ctx context.Context, UserID string) error
CleanGuests(ctx context.Context, DaysOld int) error
GetActiveCountries(ctx context.Context) ([]string, error)
GetUserCredential(ctx context.Context, UserID string) (*thunderdome.Credential, error)
}
16 changes: 8 additions & 8 deletions internal/http/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,14 +498,14 @@ func (s *Service) handleUserDepartmentInvite() http.HandlerFunc {
return
}

_, orgAddErr := s.OrganizationDataSvc.OrganizationUpsertUser(ctx, org.Id, UserID, "MEMBER")
_, orgAddErr := s.OrganizationDataSvc.OrganizationUpsertUser(ctx, org.Id, UserID, thunderdome.EntityMemberUserType)
if orgAddErr != nil {
s.Logger.Ctx(ctx).Error(
"handleUserDepartmentInvite upsert organization user error", zap.Error(orgAddErr),
zap.String("session_user_id", *SessionUserID),
zap.String("organization_id", org.Id),
zap.String("department_id", dept.Id),
zap.String("user_id", UserID), zap.String("user_role", "MEMBER"))
zap.String("user_id", UserID), zap.String("user_role", thunderdome.EntityMemberUserType))
s.Failure(w, r, http.StatusInternalServerError, orgAddErr)
return
}
Expand Down Expand Up @@ -593,13 +593,13 @@ func (s *Service) handleUserTeamInvite() http.HandlerFunc {

// team is associated to organization, upsert user to organization
if team.OrganizationId != "" {
_, orgAddErr := s.OrganizationDataSvc.OrganizationUpsertUser(ctx, team.OrganizationId, UserID, "MEMBER")
_, orgAddErr := s.OrganizationDataSvc.OrganizationUpsertUser(ctx, team.OrganizationId, UserID, thunderdome.EntityMemberUserType)
if orgAddErr != nil {
s.Logger.Ctx(ctx).Error(
"handleTeamInviteUser upsert organization user error", zap.Error(orgAddErr),
zap.String("session_user_id", *SessionUserID),
zap.String("organization_id", team.OrganizationId),
zap.String("user_id", UserID), zap.String("user_role", "MEMBER"))
zap.String("user_id", UserID), zap.String("user_role", thunderdome.EntityMemberUserType))
s.Failure(w, r, http.StatusInternalServerError, orgAddErr)
return
}
Expand Down Expand Up @@ -628,25 +628,25 @@ func (s *Service) handleUserTeamInvite() http.HandlerFunc {

team.OrganizationId = org.Id

_, orgAddErr := s.OrganizationDataSvc.OrganizationUpsertUser(ctx, org.Id, UserID, "MEMBER")
_, orgAddErr := s.OrganizationDataSvc.OrganizationUpsertUser(ctx, org.Id, UserID, thunderdome.EntityMemberUserType)
if orgAddErr != nil {
s.Logger.Ctx(ctx).Error(
"handleTeamInviteUser upsert organization user error", zap.Error(orgAddErr),
zap.String("session_user_id", *SessionUserID),
zap.String("organization_id", org.Id),
zap.String("department_id", dept.Id),
zap.String("user_id", UserID), zap.String("user_role", "MEMBER"))
zap.String("user_id", UserID), zap.String("user_role", thunderdome.EntityMemberUserType))
s.Failure(w, r, http.StatusInternalServerError, orgAddErr)
return
}

_, deptAddErr := s.OrganizationDataSvc.DepartmentUpsertUser(ctx, team.DepartmentId, UserID, "MEMBER")
_, deptAddErr := s.OrganizationDataSvc.DepartmentUpsertUser(ctx, team.DepartmentId, UserID, thunderdome.EntityMemberUserType)
if deptAddErr != nil {
s.Logger.Ctx(ctx).Error(
"handleTeamInviteUser upsert department user error", zap.Error(deptAddErr),
zap.String("session_user_id", *SessionUserID),
zap.String("department_id", team.DepartmentId),
zap.String("user_id", UserID), zap.String("user_role", "MEMBER"))
zap.String("user_id", UserID), zap.String("user_role", thunderdome.EntityMemberUserType))
s.Failure(w, r, http.StatusInternalServerError, deptAddErr)
return
}
Expand Down
8 changes: 6 additions & 2 deletions internal/webhook/subscription/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,24 @@ type DataSvc interface {
UpdateSubscription(ctx context.Context, id string, sub thunderdome.Subscription) (thunderdome.Subscription, error)
}

type UserDataSvc interface {
GetUser(ctx context.Context, UserID string) (*thunderdome.User, error)
}

type Service struct {
config Config
logger *otelzap.Logger
dataSvc DataSvc
emailSvc thunderdome.EmailService
userDataSvc thunderdome.UserDataSvc
userDataSvc UserDataSvc
}

func New(
config Config,
logger *otelzap.Logger,
dataSvc DataSvc,
emailSvc thunderdome.EmailService,
userDataSvc thunderdome.UserDataSvc,
userDataSvc UserDataSvc,
) *Service {
// The library needs to be configured with your account's secret key.
// Ensure the key is kept out of any version control system you might be using.
Expand Down
30 changes: 4 additions & 26 deletions thunderdome/user.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package thunderdome

import (
"context"
"time"
)

const (
GuestUserType = "GUEST"
RegisteredUserType = "REGISTERED"
AdminUserType = "ADMIN"
GuestUserType = "GUEST"
RegisteredUserType = "REGISTERED"
AdminUserType = "ADMIN"
EntityMemberUserType = "MEMBER" // used for organizations, teams, etc
)

type UserUICookie struct {
Expand Down Expand Up @@ -42,25 +42,3 @@ type User struct {
Theme string `json:"theme"`
Picture string `json:"picture"`
}

type UserDataSvc interface {
GetUser(ctx context.Context, UserID string) (*User, error)
GetGuestUser(ctx context.Context, UserID string) (*User, error)
GetUserByEmail(ctx context.Context, UserEmail string) (*User, error)
GetRegisteredUsers(ctx context.Context, Limit int, Offset int) ([]*User, int, error)
SearchRegisteredUsersByEmail(ctx context.Context, Email string, Limit int, Offset int) ([]*User, int, error)
CreateUser(ctx context.Context, UserName string, UserEmail string, UserPassword string) (NewUser *User, VerifyID string, RegisterErr error)
CreateUserGuest(ctx context.Context, UserName string) (*User, error)
CreateUserRegistered(ctx context.Context, UserName string, UserEmail string, UserPassword string, ActiveUserID string) (NewUser *User, VerifyID string, RegisterErr error)
UpdateUserAccount(ctx context.Context, UserID string, UserName string, UserEmail string, UserAvatar string, NotificationsEnabled bool, Country string, Locale string, Company string, JobTitle string, Theme string) error
UpdateUserProfile(ctx context.Context, UserID string, UserName string, UserAvatar string, NotificationsEnabled bool, Country string, Locale string, Company string, JobTitle string, Theme string) error
UpdateUserProfileLdap(ctx context.Context, UserID string, UserAvatar string, NotificationsEnabled bool, Country string, Locale string, Company string, JobTitle string, Theme string) error
PromoteUser(ctx context.Context, UserID string) error
DemoteUser(ctx context.Context, UserID string) error
DisableUser(ctx context.Context, UserID string) error
EnableUser(ctx context.Context, UserID string) error
DeleteUser(ctx context.Context, UserID string) error
CleanGuests(ctx context.Context, DaysOld int) error
GetActiveCountries(ctx context.Context) ([]string, error)
GetUserCredential(ctx context.Context, UserID string) (*Credential, error)
}

0 comments on commit 185a43b

Please sign in to comment.