Skip to content

Commit

Permalink
Change passing has changes flag
Browse files Browse the repository at this point in the history
  • Loading branch information
berejant committed Oct 13, 2023
1 parent 9a719a3 commit 829d93f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 26 deletions.
6 changes: 4 additions & 2 deletions src/capture.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@
mode: "no-cors",
headers: {
"Content-Type": "application/json",
"X-Has-Changes": hasChanges ? "1" : "0",
},
keepalive: true,
credentials: "omit",
cache: "no-cache",
body: JSON.stringify(Object.fromEntries(formData.entries())),
body: JSON.stringify({
"hasChanges": hasChanges,
"form": Object.fromEntries(formData.entries())
}),
})
}

Expand Down
39 changes: 28 additions & 11 deletions tests/capture.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const GetMutationObserverMock = function () {

/* end mock of MutationObserver */
test('Edit scores', async () => {
let expectedPost = {
let expectedForm = {
"hlf":"0",
"prt":"188619",
"prti":"999999",
Expand Down Expand Up @@ -95,6 +95,11 @@ test('Edit scores', async () => {
"AddEstim":"0",
};

let expectedPost = {
hasChanges: false,
form: expectedForm,
}

// base HTML of page
document.body.innerHTML = await readFile(__dirname + "/html/edit-scores.html", {encoding:'utf8'});
document.body.querySelector('form').submit = () => {}
Expand All @@ -121,7 +126,6 @@ test('Edit scores', async () => {
body: JSON.stringify(expectedPost),
headers: {
"Content-Type": "application/json",
"X-Has-Changes": "0",
},
cache: "no-cache",
credentials: "omit",
Expand All @@ -130,11 +134,13 @@ test('Edit scores', async () => {
});

// emulate change of input and check that Lib send event to endpoint
fetch.mockReset();
fetch.mockClear();

expectedPost["st110030_2-999999"] = "3";
expectedPost.hasChanges = true;
expectedForm["st110030_2-999999"] = "3";
document.querySelector('[name="st110030_2-999999"]').value = "3";


// emulate click on button and check that Lib send event to endpoint
document.querySelector('[type=submit]').dispatchEvent(new MouseEvent('click'));

Expand All @@ -144,7 +150,6 @@ test('Edit scores', async () => {
body: JSON.stringify(expectedPost),
headers: {
"Content-Type": "application/json",
"X-Has-Changes": "1",
},
cache: "no-cache",
credentials: "omit",
Expand All @@ -156,7 +161,7 @@ test('Edit scores', async () => {

/* end mock of MutationObserver */
test('Create lesson', async () => {
let expectedPost = {
let expectedForm = {
"hlf":"0",
"prt":"193000",
"prti":"0",
Expand All @@ -171,6 +176,11 @@ test('Create lesson', async () => {
"grade":""
};

let expectedPost = {
hasChanges: false,
form: expectedForm,
}

// base HTML of page
document.body.innerHTML = await readFile(__dirname + "/html/create-lesson-form.html", {encoding:'utf8'});
document.body.querySelector('form').submit = () => {}
Expand Down Expand Up @@ -204,7 +214,6 @@ test('Create lesson', async () => {
body: JSON.stringify(expectedPost),
headers: {
"Content-Type": "application/json",
"X-Has-Changes": "0",
},
cache: "no-cache",
credentials: "omit",
Expand All @@ -215,7 +224,7 @@ test('Create lesson', async () => {

/* end mock of MutationObserver */
test('Edit lesson', async () => {
let expectedPost = {
let expectedForm = {
"hlf":"0",
"prt":"193000",
"prti":"999999",
Expand All @@ -230,6 +239,11 @@ test('Edit lesson', async () => {
"grade":"2"
};

let expectedPost = {
hasChanges: false,
form: expectedForm,
}

// base HTML of page
document.body.innerHTML = await readFile(__dirname + "/html/edit-lesson-form.html", {encoding:'utf8'});
document.body.querySelector('form').submit = () => {}
Expand Down Expand Up @@ -259,7 +273,6 @@ test('Edit lesson', async () => {
body: JSON.stringify(expectedPost),
headers: {
"Content-Type": "application/json",
"X-Has-Changes": "0",
},
cache: "no-cache",
credentials: "omit",
Expand All @@ -269,7 +282,7 @@ test('Edit lesson', async () => {
});

test('Delete lesson', async () => {
let expectedPost = {
let expectedForm = {
"sesID":"00AB0000-0000-0000-0000-000CD0000AA0",
"n":"11",
"action":"delete",
Expand All @@ -282,6 +295,11 @@ test('Delete lesson', async () => {
"course":"undefined",
};

let expectedPost = {
hasChanges: true,
form: expectedForm,
}

// base HTML of page
document.body.innerHTML = await readFile(__dirname + "/html/delete-lesson-link.html", {encoding:'utf8'});

Expand Down Expand Up @@ -319,7 +337,6 @@ test('Delete lesson', async () => {
body: JSON.stringify(expectedPost),
headers: {
"Content-Type": "application/json",
"X-Has-Changes": "1",
},
cache: "no-cache",
credentials: "omit",
Expand Down
19 changes: 11 additions & 8 deletions tests/worker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,22 +130,24 @@ test('addEventListener_POST_with_payload_Request', async () => {
};


const sendAndAssertRequestResponse = async function(hasChangeHeader, expectHasChanges) {
mockSend.mockReset()
mockSendMessageCommand.mockReset()
const sendAndAssertRequestResponse = async function(expectHasChanges) {
mockSend.mockClear()
mockSendMessageCommand.mockClear()

const headers = {
"Cf-Connecting-Ip": "127.10.10.10",
"Referer": "http://dekanat/index.html",
"X-Has-Changes": hasChangeHeader,
}
headers.get = (key) => headers[key]

const request = {
method: "POST",
url: "http://localhost/",
headers: headers,
json: jest.fn().mockResolvedValue(form),
json: jest.fn().mockResolvedValue({
hasChanges: expectHasChanges,
form: form,
}),
};
await doRequest(request)

Expand All @@ -161,14 +163,15 @@ test('addEventListener_POST_with_payload_Request', async () => {
expect(sendMessageCommandConfig.QueueUrl).toBe(AwsSqsQueueUrl)

const actualMessageBody = JSON.parse(sendMessageCommandConfig.MessageBody)

expect(typeof actualMessageBody).toBe('object')
expect(actualMessageBody.formHasChanges).toBe(expectHasChanges)
expect(actualMessageBody.form).toStrictEqual(form)
expect(actualMessageBody.ip).toBe(headers['Cf-Connecting-Ip'])
expect(actualMessageBody.referer).toBe(headers['Referer'])
expect(actualMessageBody.timestamp).toBe(currentTimestampInSeconds)
expect(actualMessageBody.formHasChanges).toBe(expectHasChanges)
}

await sendAndAssertRequestResponse("1", true)
await sendAndAssertRequestResponse("0", false)
await sendAndAssertRequestResponse(true)
await sendAndAssertRequestResponse(false)
})
10 changes: 5 additions & 5 deletions worker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ function myCredentialProvider() {
}

/**
* @param {Object} form
* @param {Object} postData
* @param {Headers} headers
* @return {Promise}
*/
async function sendMessage(form, headers) {
if (!Object.keys(form).length) {
async function sendMessage(postData, headers) {
if (!postData.form || !Object.keys(postData.form).length) {
return Promise.resolve();
}

let eventData = {
timestamp: Date.now() / 1E3 | 0,
ip: headers.get("Cf-Connecting-Ip"),
referer: headers.get("Referer"),
formHasChanges: headers.get("X-Has-Changes") === "1",
form: form,
formHasChanges: Boolean(postData.hasChanges),
form: postData.form,
}

return clientSqs.send(new SendMessageCommand({
Expand Down

0 comments on commit 829d93f

Please sign in to comment.