-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create test for email service and database connection
- Loading branch information
1 parent
9afc035
commit 0f9e08c
Showing
7 changed files
with
91 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
@@ -61,4 +28,5 @@ func TestConfigureEmail(t *testing.T) { | |
t.Error("Expected SMTPAuth to be set, but it is nil") | ||
} | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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;"> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
|
||
}) | ||
} |