Skip to content
Snippets Groups Projects
Commit b8ae9157 authored by Rémi PAILHAREY's avatar Rémi PAILHAREY :fork_knife_plate:
Browse files

feat: EmailRecorder can record more than 1 email

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