Skip to content

Commit

Permalink
Refactors user registration service
Browse files Browse the repository at this point in the history
Updates the user registration service to use injected dependencies for caching, mailing, and code generation. This improves modularity and testability by decoupling the service from specific implementations.  Adds welcome email sending upon successful registration.
  • Loading branch information
Ayobami6 committed Nov 26, 2024
1 parent fc26ab2 commit 95bee3c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 9 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Go CI

on:
pull_request:
branches: [main]

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.22'

- name: Install dependencies
run: go mod download

- name: Run tests
run: go test -v ./test

- name: Run linter
uses: golangci/golangci-lint-action@v3
with:
version: latest
48 changes: 39 additions & 9 deletions tests/user_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"testing"
"time"

"github.com/LoginX/SprayDash/internal/model"
"github.com/LoginX/SprayDash/internal/service/dto"
Expand All @@ -15,8 +16,10 @@ import (
func TestRegisterUser(t *testing.T) {
// dependencies setup
mockRepo := &MockUserRepository{}
userService := impls.NewUserServiceImpl(mockRepo)
mockUtils := &MockUtils{}
mockCache := &MockRedisCacheService{}
mockMailer := &mailerService{}
mockCodeGenerator := &codeGeneratorService{}
userService := impls.NewUserServiceImpl(mockRepo, mockCache, mockMailer, mockCodeGenerator)
// arrange
createUserDto := dto.CreateUserDTO{
Name: "Test User",
Expand All @@ -31,7 +34,9 @@ func TestRegisterUser(t *testing.T) {
// expect
mockRepo.On("GetUserByEmail", mock.Anything, createUserDto.Email).Return(newUser, errors.New("user not found")).Once()
mockRepo.On("CreateUser", mock.Anything, mock.AnythingOfType("*model.User")).Return(newUser, nil)
mockUtils.On("GenerateAndCacheCode", createUserDto.Email).Return(1234, nil)
mockCache.On("Set", mock.Anything, createUserDto.Email, mock.AnythingOfType("int"), mock.AnythingOfType("time.Duration")).Return(1234, nil)
mockCodeGenerator.On("GenerateCode").Return(1234)
mockMailer.On("SendMail", createUserDto.Email, "Welcome to SprayDash", createUserDto.Name, "Your account has been created successfully.", "welcome_email").Return(nil)

// act
message, err := userService.Register(createUserDto)
Expand All @@ -49,8 +54,6 @@ type MockUserRepository struct {
mock.Mock
}



func (m *MockUserRepository) CreateUser(ctx context.Context, user *model.User) (*model.User, error) {
args := m.Called(ctx, user)
return args.Get(0).(*model.User), args.Error(1)
Expand Down Expand Up @@ -91,11 +94,38 @@ func (m *MockUserRepository) UpdateUser(ctx context.Context, updateMap map[strin
return nil, nil
}

type MockUtils struct {
type MockRedisCacheService struct {
mock.Mock
}

func (m *MockRedisCacheService) Set(ctx context.Context, email string, code int, expiration time.Duration) error {
return nil
}

func (m *MockRedisCacheService) Get(ctx context.Context, email string) (int, error) {
return 0, nil
}

type mailerService struct {
mock.Mock
}

func (m *MockUtils) GenerateAndCacheCode(email string) (int, error) {
args := m.Called(email)
return args.Int(0), args.Error(1)
func (m *mailerService) SendMail(recipient string, subject string, username string, message string, template_name string) error {
return nil
}

type codeGeneratorService struct {
mock.Mock
}

func (c *codeGeneratorService) GenerateCode() int {
return 1234
}

func (c *codeGeneratorService) GenerateInviteCode() string {
return "1234"
}

func (c *codeGeneratorService) GenerateReferenceCode() string {
return "1234"
}

0 comments on commit 95bee3c

Please sign in to comment.