From fe7178ea034c2bc1dc941851aaa591e7bd7daf31 Mon Sep 17 00:00:00 2001 From: Tim Wundenberg Date: Fri, 27 Sep 2024 23:21:37 +0200 Subject: [PATCH] chore: extract mail to it's own service #181 --- handler/auth.go | 2 +- handler/auth_test.go | 11 +++++++++++ service/auth.go | 2 ++ service/auth_test.go | 8 ++++---- 4 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 handler/auth_test.go diff --git a/handler/auth.go b/handler/auth.go index 0c3f79c..d48a589 100644 --- a/handler/auth.go +++ b/handler/auth.go @@ -76,7 +76,7 @@ func (handler HandlerAuthImpl) handleSignIn() http.HandlerFunc { http.Error(w, "Invalid email or password", http.StatusUnauthorized) } else { utils.LogError("Error signing in", err) - http.Error(w, "An error occurred", http.StatusInternalServerError) + http.Error(w, "InternalServerErr", http.StatusInternalServerError) } return } diff --git a/handler/auth_test.go b/handler/auth_test.go new file mode 100644 index 0000000..921902a --- /dev/null +++ b/handler/auth_test.go @@ -0,0 +1,11 @@ +package handler + +import ( + "testing" +) + +func TestHandleSignIn(t *testing.T) { + t.Parallel() + t.Run("should signIn and return session cookie", func(t *testing.T) { + }) +} diff --git a/service/auth.go b/service/auth.go index 24edf01..32a2e20 100644 --- a/service/auth.go +++ b/service/auth.go @@ -400,6 +400,7 @@ func HandleDeleteAccountComp(db *sql.DB, serverSettings *types.ServerSettings) h return } + mailService := NewMailService(serverSettings) go mailService.SendMail(user.Email, "Account deleted", "Your account has been deleted") utils.DoRedirect(w, r, "/") @@ -579,6 +580,7 @@ func HandleResetPasswordComp(db *sql.DB, serverSettings *types.ServerSettings) h utils.TriggerToast(w, r, "error", "Internal Server Error") return } + mailService := NewMailService(serverSettings) mailService.SendMail(email, "Reset Password", mail.String()) } diff --git a/service/auth_test.go b/service/auth_test.go index 295ada2..61930db 100644 --- a/service/auth_test.go +++ b/service/auth_test.go @@ -38,7 +38,7 @@ func TestSignIn(t *testing.T) { ), err: nil, } - underTest := NewServiceAuthImpl(stub) + underTest := NewServiceAuthImpl(stub, NewServiceMailStub()) actualUser, err := underTest.SignIn("test@test.de", "password") if err != nil { @@ -71,7 +71,7 @@ func TestSignIn(t *testing.T) { ), err: nil, } - underTest := NewServiceAuthImpl(stub) + underTest := NewServiceAuthImpl(stub, NewServiceMailStub()) _, err := underTest.SignIn("test@test.de", "wrong password") if err != ErrInvaidCredentials { @@ -84,7 +84,7 @@ func TestSignIn(t *testing.T) { user: nil, err: db.ErrUserNotFound, } - underTest := NewServiceAuthImpl(stub) + underTest := NewServiceAuthImpl(stub, NewServiceMailStub()) _, err := underTest.SignIn("test", "test") if err != ErrInvaidCredentials { @@ -97,7 +97,7 @@ func TestSignIn(t *testing.T) { user: nil, err: errors.New("Some error"), } - underTest := NewServiceAuthImpl(stub) + underTest := NewServiceAuthImpl(stub, NewServiceMailStub()) _, err := underTest.SignIn("test", "test") if err != types.ErrInternal {