Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Salvar nos eventos o histórico de alterações de username #1727

Merged
merged 2 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions models/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,7 @@ function validateObject(object) {
return cleanObject;
}

async function findAll() {
const results = await database.query('SELECT * FROM events;');
return results.rows;
}

export default Object.freeze({
create,
updateMetadata,
findAll,
});
2 changes: 1 addition & 1 deletion pages/api/v1/contents/[username]/[slug]/index.public.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ async function patchHandler(request, response) {

const currentEvent = await event.create(
{
type: filteredBodyValues.parent_id ? 'update:content:text_child' : 'update:content:text_root',
type: contentToBeUpdated.parent_id ? 'update:content:text_child' : 'update:content:text_root',
originatorUserId: request.context.user.id,
originatorIp: request.context.clientIp,
},
Expand Down
7 changes: 7 additions & 0 deletions pages/api/v1/users/[username]/index.public.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ async function patchHandler(request, response) {
for (const field of updatableFields) {
if (originalUser[field] !== updatedUser[field]) {
metadata.updatedFields.push(field);

if (field === 'username') {
metadata.username = {
old: originalUser.username,
new: updatedUser.username,
};
}
}
}

Expand Down
61 changes: 34 additions & 27 deletions tests/integration/api/v1/contents/firewall.post.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { version as uuidVersion } from 'uuid';

import content from 'models/content.js';
import event from 'models/event.js';
import orchestrator from 'tests/orchestrator.js';

beforeEach(async () => {
Expand Down Expand Up @@ -61,9 +60,9 @@ describe('POST /api/v1/contents [FIREWALL]', () => {
const request2Body = await request2.json();
const request3Body = await request3.json();

expect(request1.status).toBe(201);
expect(request2.status).toBe(201);
expect(request3.status).toBe(429);
expect.soft(request1.status).toBe(201);
expect.soft(request2.status).toBe(201);
expect.soft(request3.status).toBe(429);

expect(request3Body).toStrictEqual({
name: 'TooManyRequestsError',
Expand Down Expand Up @@ -96,18 +95,22 @@ describe('POST /api/v1/contents [FIREWALL]', () => {
expect(content2.status).toStrictEqual('draft');
expect(content3).toStrictEqual(undefined);

const events = await event.findAll();
expect(events.length).toEqual(3);
const lastEvent = await orchestrator.getLastEvent();

expect(uuidVersion(events[2].id)).toEqual(4);
expect(events[2].type).toEqual('firewall:block_contents:text_root');
expect(events[2].originator_user_id).toEqual(defaultUser.id);
expect(events[2].originator_ip).toEqual('127.0.0.1');
expect(events[2].metadata).toEqual({
from_rule: 'create:content:text_root',
contents: [content1.id, content2.id],
expect(lastEvent).toStrictEqual({
id: lastEvent.id,
type: 'firewall:block_contents:text_root',
originator_user_id: defaultUser.id,
originator_ip: '127.0.0.1',
metadata: {
from_rule: 'create:content:text_root',
contents: [content1.id, content2.id],
},
created_at: lastEvent.created_at,
});
expect(Date.parse(events[2].created_at)).not.toEqual(NaN);

expect(uuidVersion(lastEvent.id)).toBe(4);
expect(Date.parse(lastEvent.created_at)).not.toBe(NaN);
});

test('Spamming valid "child" contents', async () => {
Expand Down Expand Up @@ -176,9 +179,9 @@ describe('POST /api/v1/contents [FIREWALL]', () => {
const request2Body = await request2.json();
const request3Body = await request3.json();

expect(request1.status).toBe(201);
expect(request2.status).toBe(201);
expect(request3.status).toBe(429);
expect.soft(request1.status).toBe(201);
expect.soft(request2.status).toBe(201);
expect.soft(request3.status).toBe(429);

expect(request3Body).toStrictEqual({
name: 'TooManyRequestsError',
Expand Down Expand Up @@ -211,18 +214,22 @@ describe('POST /api/v1/contents [FIREWALL]', () => {
expect(content2.status).toStrictEqual('draft');
expect(content3).toStrictEqual(undefined);

const events = await event.findAll();
expect(events.length).toEqual(4);
const lastEvent = await orchestrator.getLastEvent();

expect(uuidVersion(events[3].id)).toEqual(4);
expect(events[3].type).toEqual('firewall:block_contents:text_child');
expect(events[3].originator_user_id).toEqual(defaultUser.id);
expect(events[3].originator_ip).toEqual('127.0.0.1');
expect(events[3].metadata).toEqual({
from_rule: 'create:content:text_child',
contents: [content1.id, content2.id],
expect(lastEvent).toStrictEqual({
id: lastEvent.id,
type: 'firewall:block_contents:text_child',
originator_user_id: defaultUser.id,
originator_ip: '127.0.0.1',
metadata: {
from_rule: 'create:content:text_child',
contents: [content1.id, content2.id],
},
created_at: lastEvent.created_at,
});
expect(Date.parse(events[3].created_at)).not.toEqual(NaN);

expect(uuidVersion(lastEvent.id)).toBe(4);
expect(Date.parse(lastEvent.created_at)).not.toBe(NaN);
});
});
});
95 changes: 0 additions & 95 deletions tests/integration/api/v1/events/get.test.js

This file was deleted.

31 changes: 17 additions & 14 deletions tests/integration/api/v1/users/firewall.post.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { version as uuidVersion } from 'uuid';

import event from 'models/event.js';
import user from 'models/user.js';
import orchestrator from 'tests/orchestrator.js';

Expand Down Expand Up @@ -54,9 +53,9 @@ describe('POST /api/v1/users [FIREWALL]', () => {
const request2Body = await request2.json();
const request3Body = await request3.json();

expect(request1.status).toBe(201);
expect(request2.status).toBe(201);
expect(request3.status).toBe(429);
expect.soft(request1.status).toBe(201);
expect.soft(request2.status).toBe(201);
expect.soft(request3.status).toBe(429);

expect(request3Body).toStrictEqual({
name: 'TooManyRequestsError',
Expand All @@ -76,18 +75,22 @@ describe('POST /api/v1/users [FIREWALL]', () => {
expect(user1.features).toStrictEqual([]);
expect(user2.features).toStrictEqual([]);

const events = await event.findAll();
expect(events.length).toEqual(3);
const lastEvent = await orchestrator.getLastEvent();

expect(uuidVersion(events[2].id)).toEqual(4);
expect(events[2].type).toEqual('firewall:block_users');
expect(events[2].originator_user_id).toEqual(null);
expect(events[2].originator_ip).toEqual('127.0.0.1');
expect(events[2].metadata).toEqual({
from_rule: 'create:user',
users: [user1.id, user2.id],
expect(lastEvent).toStrictEqual({
id: lastEvent.id,
type: 'firewall:block_users',
originator_user_id: null,
originator_ip: '127.0.0.1',
metadata: {
from_rule: 'create:user',
users: [user1.id, user2.id],
},
created_at: lastEvent.created_at,
});
expect(Date.parse(events[2].created_at)).not.toEqual(NaN);

expect(uuidVersion(lastEvent.id)).toBe(4);
expect(Date.parse(lastEvent.created_at)).not.toBe(NaN);
});
});
});
Loading
Loading