Commit b8ae9157 authored by Rémi PAILHAREY's avatar Rémi PAILHAREY
Browse files

feat: EmailRecorder can record more than 1 email

parent 4497ef31
......@@ -37,14 +37,18 @@ func NewMockSender() (EmailSender, *EmailRecorder) {
}
func mockSend(errToReturn error) (func(string, smtp.Auth, string, []string, []byte) error, *EmailRecorder) {
r := new(EmailRecorder)
r := &EmailRecorder{}
return func(addr string, a smtp.Auth, from string, to []string, msg []byte) error {
*r = EmailRecorder{addr, a, from, to, msg}
r.emails = append(r.emails, EmailData{addr, a, from, to, msg})
return errToReturn
}, r
}
type EmailRecorder struct {
emails []EmailData
}
type EmailData struct {
addr string
auth smtp.Auth
from string
......@@ -52,6 +56,10 @@ type EmailRecorder struct {
msg []byte
}
func (r *EmailRecorder) Msg() string {
return string(r.msg)
func (r *EmailRecorder) Msg(i int) string {
if i < 0 || i >= len(r.emails) {
return ""
} else {
return string(r.emails[i].msg)
}
}
......@@ -12,7 +12,7 @@ func TestEmail_SendSuccessful(t *testing.T) {
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if string(recorder.msg) != body {
t.Errorf("wrong message body.\n\nexpected: %v\n got: %s", body, recorder.msg)
if string(recorder.Msg(0)) != body {
t.Errorf("wrong message body.\n\nexpected: %v\n got: %s", body, recorder.Msg(0))
}
}
......@@ -63,17 +63,17 @@ func TestUseCase(t *testing.T) {
// Try to upload a file without the authorization token (must fail)
do("POST", "/upload", noH, "", http.StatusUnauthorized, "error invalid authorization token")
// // Check that no mails were sent
tester.AssertEqual(t, recorder.Msg(), "")
tester.AssertEqual(t, recorder.Msg(0), "")
// Try to upload an empty file (must fail)
do("POST", "/upload", authorizationHeader, "", http.StatusBadRequest, "error body is empty")
// // Check that no mails were sent
tester.AssertEqual(t, recorder.Msg(), "")
tester.AssertEqual(t, recorder.Msg(0), "")
// Try to upload a file without the authorization token (must fail)
do("POST", "/upload", authorizationHeader, "invalid body content", http.StatusBadRequest, "error invalid file type")
// // Check that no mails were sent
tester.AssertEqual(t, recorder.Msg(), "")
tester.AssertEqual(t, recorder.Msg(0), "")
fileBytes, err := ioutil.ReadFile("../../testmail.msg")
if err != nil {
......@@ -82,20 +82,21 @@ func TestUseCase(t *testing.T) {
// Try to upload a msg file with the correct authorization token (must pass)
do("POST", "/upload", authorizationHeader, string(fileBytes), http.StatusOK, "")
// Wait to receive the acknowledgment email
time.Sleep(1 * time.Millisecond)
// Wait to receive all emails
time.Sleep(1 * time.Second)
fmt.Printf("Sent mail : %v\n", recorder.Msg())
if !strings.Contains(recorder.Msg(), "Accusé de réception") {
fmt.Printf("Sent mail : %v\n", recorder.Msg(0))
if !strings.Contains(recorder.Msg(0), "Accusé de réception") {
t.Errorf("received body is not what is expected")
}
// Wait to receive SSI email
recorder = cybersignal.SetTestModeAndReturnRecorder()
time.Sleep(40 * time.Second)
fmt.Printf("Sent mail : %v\n", recorder.Msg(1))
if !strings.Contains(recorder.Msg(1), "Résultat de l'analyse") {
t.Errorf("received body is not what is expected")
}
fmt.Printf("Sent mail : %v\n", recorder.Msg())
if !strings.Contains(recorder.Msg(), "Nouvel incident créé") {
fmt.Printf("Sent mail : %v\n", recorder.Msg(2))
if !strings.Contains(recorder.Msg(2), "Nouvel incident créé") {
t.Errorf("received body is not what is expected")
}
......
// Imports
import { HandleError } from "/services/common/errors.js";
import { IsEmpty } from "/services/common/common.js"
import { IsEmpty } from "/services/common/common.js";
// Local variables
let user = {};
......@@ -14,7 +14,18 @@ export async function GetUser() {
if (response.status !== 200) {
throw new Error(`Not authenticated (status ${response.status})`);
}
user = await response.json();
Object.assign(user, await response.json());
// Redirect to original subdomain if login was displayed after an authentication error on the original subdomain
try {
const redirectAfterLogin = document.cookie
.split("; ")
.find((row) => row.startsWith("redirectAfterLogin="))
.split(/=(.+)/)[1];
if (redirectAfterLogin != "" && redirectAfterLogin != null) {
window.location.replace("https://" + redirectAfterLogin);
}
} catch (e) {}
} catch (e) {
HandleError(e);
}
......@@ -22,5 +33,7 @@ export async function GetUser() {
}
export function DeleteUser() {
user = {};
Object.keys(user).forEach((key) => {
delete user[key];
});
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment