Admin: widacznosc konta usera - lastLoginAt + IP w logach logowania + kafel historii konta (kto edytowal dane, historia kar) ; GET /admin/users/:id/activity
This commit is contained in:
@@ -845,3 +845,89 @@ describe('admin: kary na koncie (status)', () => {
|
||||
expect(relogin.status).toBe(200);
|
||||
});
|
||||
});
|
||||
|
||||
describe('admin: historia konta (GET /admin/users/:id/activity)', () => {
|
||||
let adminToken: string;
|
||||
let adminId: string;
|
||||
let adminEmail: string;
|
||||
|
||||
beforeAll(async () => {
|
||||
const { user } = await createUser({ isAdmin: true, firstName: 'Ada', lastName: 'Admin' });
|
||||
adminId = user.id;
|
||||
adminEmail = user.email;
|
||||
adminToken = (await loginViaApi(user.email, PASSWORD)).token;
|
||||
});
|
||||
|
||||
it('nieistniejący użytkownik → 404', async () => {
|
||||
const app = buildApp();
|
||||
const res = await app
|
||||
.get('/api/v1/admin/users/nope-123/activity')
|
||||
.set('Authorization', `Bearer ${adminToken}`);
|
||||
expect(res.status).toBe(404);
|
||||
});
|
||||
|
||||
it('zwraca wpisy wykonane NA użytkowniku z aktorem i szczegółami kary', async () => {
|
||||
const app = buildApp();
|
||||
const { user } = await createUser();
|
||||
|
||||
// logowanie celu (user.logged_in z ip) + edycja tożsamości przez admina + kara
|
||||
await loginViaApi(user.email, PASSWORD);
|
||||
const edit = await app
|
||||
.patch(`/api/v1/admin/users/${user.id}`)
|
||||
.set('Authorization', `Bearer ${adminToken}`)
|
||||
.send({ firstName: 'Zmieniony' });
|
||||
expect(edit.status).toBe(200);
|
||||
const ban = await app
|
||||
.patch(`/api/v1/admin/users/${user.id}`)
|
||||
.set('Authorization', `Bearer ${adminToken}`)
|
||||
.send({ status: 'BANNED', restrictionReason: 'Dystrybucja kont' });
|
||||
expect(ban.status).toBe(200);
|
||||
|
||||
const res = await app
|
||||
.get(`/api/v1/admin/users/${user.id}/activity`)
|
||||
.set('Authorization', `Bearer ${adminToken}`);
|
||||
expect(res.status).toBe(200);
|
||||
const actions: string[] = res.body.logs.map((l: { action: string }) => l.action);
|
||||
expect(actions).toContain('user.logged_in');
|
||||
expect(actions).toContain('user.updated');
|
||||
expect(actions).toContain('user.status.changed');
|
||||
|
||||
const banLog = res.body.logs.find(
|
||||
(l: { action: string }) => l.action === 'user.status.changed',
|
||||
);
|
||||
expect(banLog.entityId).toBe(user.id);
|
||||
expect(banLog.actor).toMatchObject({ id: adminId, email: adminEmail });
|
||||
expect(banLog.details).toMatchObject({ status: 'BANNED', restrictionReason: 'Dystrybucja kont' });
|
||||
|
||||
const editLog = res.body.logs.find((l: { action: string }) => l.action === 'user.updated');
|
||||
expect(editLog.details.fields).toEqual(['firstName']);
|
||||
|
||||
const loginLog = res.body.logs.find((l: { action: string }) => l.action === 'user.logged_in');
|
||||
expect(typeof loginLog.details.ip).toBe('string');
|
||||
expect(loginLog.details.ip!.length).toBeGreaterThan(0);
|
||||
|
||||
// nowość pierwsza + paginacja działa
|
||||
expect(res.body.total).toBeGreaterThanOrEqual(3);
|
||||
const paged = await app
|
||||
.get(`/api/v1/admin/users/${user.id}/activity?limit=2&offset=0`)
|
||||
.set('Authorization', `Bearer ${adminToken}`);
|
||||
expect(paged.body.logs).toHaveLength(2);
|
||||
expect(paged.body.logs[0].createdAt >= paged.body.logs[1].createdAt).toBe(true);
|
||||
});
|
||||
|
||||
it('filtr nie wycieka logami innych użytkowników', async () => {
|
||||
const app = buildApp();
|
||||
const { user: a } = await createUser();
|
||||
const { user: b } = await createUser();
|
||||
await app
|
||||
.patch(`/api/v1/admin/users/${a.id}`)
|
||||
.set('Authorization', `Bearer ${adminToken}`)
|
||||
.send({ status: 'BANNED', restrictionReason: 'Powód A' });
|
||||
|
||||
const res = await app
|
||||
.get(`/api/v1/admin/users/${b.id}/activity`)
|
||||
.set('Authorization', `Bearer ${adminToken}`);
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.logs.every((l: { entityId: string }) => l.entityId === b.id)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -73,6 +73,45 @@ describe('auth routes', () => {
|
||||
expect((res.headers['set-cookie'] ?? [])[0]).toContain('fitos_refresh=');
|
||||
});
|
||||
|
||||
it('login: stamps lastLoginAt and logs the client IP', async () => {
|
||||
const app = buildApp();
|
||||
const { user, password } = await createUser();
|
||||
const before = await prisma.user.findUnique({ where: { id: user.id } });
|
||||
expect(before?.lastLoginAt).toBeNull();
|
||||
const res = await app
|
||||
.post('/api/v1/auth/login')
|
||||
.set('X-Forwarded-For', '203.0.113.9')
|
||||
.send({ email: user.email, password });
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.user.lastLoginAt).toBeTruthy();
|
||||
|
||||
const db = await prisma.user.findUnique({ where: { id: user.id } });
|
||||
expect(db?.lastLoginAt).toBeInstanceOf(Date);
|
||||
|
||||
const log = await prisma.activityLog.findFirst({
|
||||
where: { action: 'user.logged_in', entityId: user.id },
|
||||
orderBy: { createdAt: 'desc' },
|
||||
});
|
||||
expect(log).not.toBeNull();
|
||||
expect((log!.details as Record<string, unknown>).ip).toBe('203.0.113.9');
|
||||
expect((log!.details as Record<string, unknown>).client).toBe('app');
|
||||
});
|
||||
|
||||
it('register: logs the client IP', async () => {
|
||||
const app = buildApp();
|
||||
const email = `ipreg-${rand()}@test.local`;
|
||||
const res = await app
|
||||
.post('/api/v1/auth/register')
|
||||
.set('X-Forwarded-For', '198.51.100.7')
|
||||
.send({ email, password: PASSWORD });
|
||||
expect(res.status).toBe(201);
|
||||
const log = await prisma.activityLog.findFirst({
|
||||
where: { action: 'user.registered', entityId: res.body.user.id },
|
||||
orderBy: { createdAt: 'desc' },
|
||||
});
|
||||
expect((log!.details as Record<string, unknown>).ip).toBe('198.51.100.7');
|
||||
});
|
||||
|
||||
it('login: rejects a wrong password with 401', async () => {
|
||||
const app = buildApp();
|
||||
const { user } = await createUser();
|
||||
|
||||
Reference in New Issue
Block a user