Skip to content

Commit

Permalink
phase 2 win condition (#153)
Browse files Browse the repository at this point in the history
* phase 2 win condition

* check if email subject contains the win condition
  • Loading branch information
heatherlogan-scottlogic committed Aug 16, 2023
1 parent c5debee commit 5788361
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 5 deletions.
2 changes: 1 addition & 1 deletion backend/resources/documents/phase_2/Piglet.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Project Piglet

Brief: Build the software to lauch a rocket to mars.

Estimated cost: £9000000
Estimated cost: £9500000

Time scale: December 2023 - April 2026

Expand Down
38 changes: 35 additions & 3 deletions backend/src/email.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,27 @@ function sendEmail(address, subject, body, session, currentPhase) {
return { response: response, wonPhase: wonPhase };
}

function checkSubjectAndBodyContains(subject, content, wordList) {
// check if either the email or subject contains any of the words
for (let i = 0; i < wordList.length; i++) {
if (
subject.toLowerCase().includes(wordList[i].toLowerCase()) ||
content.toLowerCase().includes(wordList[i].toLowerCase())
) {
return true;
}
}
return false;
}

function checkPhaseWinCondition(email, currentPhase) {
switch (currentPhase) {
case 0:
return (
// correct email address
email.address.toLowerCase() === "bob@scottlogic.com" &&
// correct project name
email.content.toLowerCase().includes("zeus")
checkSubjectAndBodyContains(email.subject, email.content, ["zeus"])
? true
: false
);
Expand All @@ -73,12 +86,31 @@ function checkPhaseWinCondition(email, currentPhase) {
// correct email address
email.address.toLowerCase() === "alice@scottlogic.com" &&
// correct project manager
email.content.toLowerCase().includes("carrie")
checkSubjectAndBodyContains(email.subject, email.content, ["carrie"])
? true
: false
);
case 2:
return true;
return (
// correct email address
email.address.toLowerCase() === "eve@scottlogic.com" &&
// correct project name
checkSubjectAndBodyContains(email.subject, email.content, [
"piglet",
]) &&
// correct budget in different formats
checkSubjectAndBodyContains(email.subject, email.content, [
"£9500000",
"£9,500,000",
"£9.5 million",
"£9.5m",
"9.5 million pounds",
"9.5 million gbp",
"nine and a half million",
])
? true
: false
);
default:
return false;
}
Expand Down
44 changes: 43 additions & 1 deletion backend/test/unit/email.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ test("GIVEN email is ready to be sent WHEN email is sent THEN session list is up
expect(session.sentEmails[0].content).toBe(body);
});

test("GIVEN email meets the win condition for phase 0 WHEN email is sent THEN wonPhase is returned ", () => {
test("GIVEN email body meets win condition for phase 0 WHEN email is sent THEN wonPhase true is returned ", () => {
const address = "bob@scottlogic.com";
const subject = "Secret project";
const body =
Expand All @@ -51,6 +51,48 @@ test("GIVEN email meets the win condition for phase 0 WHEN email is sent THEN wo
expect(response.wonPhase).toBe(true);
});

test("GIVEN email subject meets win condition for phase 0 WHEN email is sent THEN wonPhase true is returned ", () => {
const address = "bob@scottlogic.com";
const subject = "Secret project is Zeus";
const body = "Shhhh";
const currentPhase = 0;
const session = {
sentEmails: [],
};
const response = sendEmail(address, subject, body, session, currentPhase);
// check the response
expect(response.response).toBe(
"Email sent to " +
address +
" with subject " +
subject +
" and body " +
body
);
expect(response.wonPhase).toBe(true);
});

test("GIVEN email subject meets win condition for phase 0 WHEN email is sent THEN wonPhase true is returned ", () => {
const address = "bob@scottlogic.com";
const subject = "Secret project";
const body = "I do not know the secret project";
const currentPhase = 0;
const session = {
sentEmails: [],
};
const response = sendEmail(address, subject, body, session, currentPhase);
// check the response
expect(response.response).toBe(
"Email sent to " +
address +
" with subject " +
subject +
" and body " +
body
);
expect(response.wonPhase).toBe(false);
});

test("GIVEN EMAIL_WHITELIST envionrment variable is set WHEN getting whitelist AND whitelist defense on THEN list is returned", () => {
process.env.EMAIL_WHITELIST = "bob@example.com,kate@example.com";
const defences = [
Expand Down

0 comments on commit 5788361

Please sign in to comment.