Skip to content

Commit

Permalink
Create test for email service and database connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Fingertips18 committed Sep 11, 2024
1 parent 9afc035 commit 0f9e08c
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 41 deletions.
38 changes: 3 additions & 35 deletions configs/email_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,9 @@ func TestConfigureEmail(t *testing.T) {
email := "EMAIL"
pass := "EMAIL_APP_PASSWORD"

resetEnv := func() {
os.Unsetenv(email)
os.Unsetenv(pass)
}

t.Run("SMTP_Envs_Found", func(t *testing.T) {
emailValue := os.Getenv(email)
passValue := os.Getenv(pass)

if emailValue != "" {
t.Logf("%s is set", email)
}
if passValue != "" {
t.Logf("%s is set", pass)
}
})

t.Run("SMTP_Envs_Missing", func(t *testing.T) {
resetEnv()

emailValue := os.Getenv(email)
passValue := os.Getenv(pass)

if emailValue != "" {
t.Errorf("Expected %s to be empty but got '%s'", email, emailValue)
}

if passValue != "" {
t.Errorf("Expected %s to be empty but got '%s'", pass, passValue)
}
})

t.Run("ConfigureEmail_Success", func(t *testing.T) {
resetEnv()
os.Setenv("EMAIL", "[email protected]")
os.Setenv("EMAIL_APP_PASSWORD", "password")
os.Setenv(email, "[email protected]")
os.Setenv(pass, "password")

var buf bytes.Buffer
log.SetOutput(&buf)
Expand All @@ -61,4 +28,5 @@ func TestConfigureEmail(t *testing.T) {
t.Error("Expected SMTPAuth to be set, but it is nil")
}
})

}
2 changes: 1 addition & 1 deletion database/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func ConnectDB() {
PreferSimpleProtocol: true,
}), &gorm.Config{})
if err != nil {
panic("Failed to connect to database")
log.Fatal("Failed to connect to database")
}

if os.Getenv("RUN_MIGRATIONS") == "true" {
Expand Down
24 changes: 24 additions & 0 deletions database/connection_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package database

import (
"bytes"
"log"
"testing"
)

func TestConnectDB(t *testing.T) {
var buf bytes.Buffer
log.SetOutput(&buf)

ConnectDB()

if buf.String() != "" {
t.Errorf("Expected no log output, but got: %s", buf.String())
}

if Instance == nil {
t.Error("Expected Instance to be set, but it is nil")
} else {
t.Logf("Instance: %v", Instance.Name())
}
}
2 changes: 1 addition & 1 deletion templates/request_reset_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const _REQUEST_RESET_PASSWORD_HTML = `
<div style="text-align: center; margin: 30px 0;">
<a href="{resetURL}" style="background-color: %s; color: #DCF9F5; padding: 12px 20px; text-decoration: none; border-radius: 5px; font-weight: bold;">Reset Password</a>
</div>
<p>This link will expire in 1 hour for security reasons.</p>
<p>This link will expire in 15 minutes for security reasons.</p>
<p>Best regards,<br>Fingertips</p>
</div>
<div style="text-align: center; margin-top: 20px; color: #888; font-size: 0.8em;">
Expand Down
2 changes: 1 addition & 1 deletion templates/welcome.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const _WELCOME_MESSAGE_HTML = `
<li>Explore our features</li>
<li>Join our community forums</li>
</ul>
<p>If you have any questions, feel free to send email at <a href="[email protected]" style="color: %s;">[email protected]</a>to contact our support team.</p>
<p>If you have any questions, feel free to send email at <a href="[email protected]" style="color: %s;">[email protected]</a> to contact our support team.</p>
<p>Best regards,<br>The Team</p>
</div>
<div style="text-align: center; margin-top: 20px; color: #888; font-size: 0.8em;">
Expand Down
6 changes: 3 additions & 3 deletions utils/email_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ func SendEmailRequestResetPassword(toEmail string, resetToken string) error {
from := configs.Email
to := toEmail

msg := fmt.Sprintf("From: %s\nTo: %s\nSubject: %s\nMIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n%s", from, to, subject, body)
msg := fmt.Sprintf("From: %s\nTo: %s\nSubject: %s\nMime-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n%s", from, to, subject, body)

err := smtp.SendMail(configs.SMTPADDRESS, configs.SMTPAuth, configs.Email, []string{toEmail}, []byte(msg))
if err != nil {
return err
}

log.Println("successful reset password request email sent")
log.Println("Successful reset password request email sent")

return nil
}
Expand All @@ -86,7 +86,7 @@ func SendEmailResetPasswordSuccess(toEmail string, username string) error {
return err
}

log.Println("reset password successful email sent")
log.Println("Reset password successful email sent")

return nil
}
58 changes: 58 additions & 0 deletions utils/email_service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package utils

import (
"os"
"testing"

"github.com/Fingertips18/go-auth/configs"
)

func TestEmailService(t *testing.T) {
toEmail := os.Getenv("TEST_EMAIL")
const username = "Test"

configs.ConfigureEmail()

t.Run("SendWelcomeEmail_Test", func(t *testing.T) {
if err := SendWelcomeEmail(toEmail, username); err != nil {
t.Errorf("Ërror sending welcome email: %v", err)
} else {
t.Log("welcome email sent")
}
})

t.Run("SendEmailVerification_Test", func(t *testing.T) {
if err := SendEmailVerification(toEmail, username, "1234"); err != nil {
t.Errorf("Ërror sending email verification: %v", err)
} else {
t.Log("Verification email sent")
}
})

t.Run("SendEmailRequestResetPassword_Test", func(t *testing.T) {
os.Setenv("CLIENT_URL", "https://example.com")

if err := SendEmailRequestResetPassword(toEmail, "reset-token"); err != nil {
t.Errorf("Ërror sending reset password request : %v", err)
} else {
t.Log("Successful reset password request email sent")
}
})

t.Run("SendEmailResetPasswordSuccess_Test", func(t *testing.T) {
if err := SendEmailResetPasswordSuccess(toEmail, username); err != nil {
t.Errorf("Ërror sending reset password success : %v", err)
} else {
t.Log("Reset password successful email sent")
}
})

t.Run("SendWelcomeEmail_Test", func(t *testing.T) {
if err := SendWelcomeEmail(toEmail, username); err != nil {
t.Errorf("Ërror sending welcome email: %v", err)
} else {
t.Log("welcome email sent")
}

})
}

0 comments on commit 0f9e08c

Please sign in to comment.