Skip to content

Commit 4da4b8e

Browse files
committed
RED-16 - reworking frontend tests and user settings
1 parent 57ce6f4 commit 4da4b8e

File tree

12 files changed

+798
-6223
lines changed

12 files changed

+798
-6223
lines changed

.github/workflows/node.js.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ jobs:
4242
run: |
4343
npm ci
4444
npm run build --if-present
45-
npm run test:web
45+
npm run test:web --if-present
4646
run-tests-backend:
4747
defaults:
4848
run:
@@ -65,7 +65,7 @@ jobs:
6565
run: |
6666
npm ci
6767
npm run build --if-present
68-
npm run test:service:coverage
68+
npm run test:service:coverage --if-present
6969
publish-docker-image-frontend:
7070
if: github.ref == 'refs/heads/main'
7171
permissions: write-all

service-node-koa/app/config/security/encryption.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const decrypt = (secret) => {
2424
export const sign = (payload) => {
2525
// TODO what else to add to payload?
2626
payload.iat = new Date().getTime() / 1000;
27-
payload.exp = 86400 + new Date().getTime() / 1000;
27+
payload.exp = 86400 + payload.iat;
2828
return { token: jwt.sign(payload, process.env.SECRET) };
2929
};
3030

service-node-koa/app/config/security/middleware.mjs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ export const ifAuthenticated = async (ctx, next) => {
1818
const details = extractDetails(ctx); // id, nome, emil, admin, criacao, alteracao, iat, exp
1919
if (!details.iat || !details.exp)
2020
ctx.throw(401, { message: "Something strange with this token" });
21-
if (new Date().getTime() > new Date(details.exp * 1000))
21+
else if (new Date().getTime() > new Date(details.exp * 1000))
2222
ctx.throw(401, { message: "Token expired" });
23-
return await next();
23+
else if(ctx.params.usuario_id != details.id)
24+
ctx.throw(401, { message: "user mismatch"});
25+
else
26+
return await next();
2427
};
2528

2629
export const contaOwnedBy = async (ctx, next) => {
Lines changed: 67 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,81 @@
1-
import chai from "chai";
1+
import chai from 'chai'
22

3-
import * as middleware from "./middleware.mjs";
4-
import sinon from "sinon";
5-
import { sign } from "./encryption.mjs";
6-
import { getAdmin, resetConta } from "../../services/index.mjs";
3+
import * as middleware from './middleware.mjs'
4+
import sinon from 'sinon'
5+
import { sign } from './encryption.mjs'
6+
import { getAdmin, resetConta } from '../../services/index.mjs'
77

8-
chai.should();
8+
chai.should()
99

10-
describe("Middleware tests", () => {
11-
it("should check if it's admin", async () => {
12-
const adm = await getAdmin();
13-
const { token } = sign(adm);
14-
const authorization = `Bearer ${token}`;
15-
const ctx = { request: { header: { authorization } }, throw: sinon.fake() };
16-
const next = sinon.mock();
17-
next.once();
10+
describe('Middleware tests', () => {
11+
it('should check if it\'s admin', async () => {
12+
const adm = await getAdmin()
13+
const { token } = sign(adm)
14+
const authorization = `Bearer ${token}`
15+
const ctx = { request: { header: { authorization } }, throw: sinon.fake() }
16+
const next = sinon.mock()
17+
next.once()
1818

19-
await middleware.ifAdmin(ctx, next);
19+
await middleware.ifAdmin(ctx, next)
2020

21-
next.verify();
22-
});
21+
next.verify()
22+
})
2323

24-
it("should check if it's authenticated", async () => {
25-
const adm = await getAdmin();
26-
const { token } = sign(adm);
27-
const authorization = `Bearer ${token}`;
28-
const ctx = { request: { header: { authorization } }, throw: sinon.fake() };
29-
const next = sinon.mock();
30-
next.once();
24+
it('should check if it\'s authenticated', async () => {
25+
const adm = await getAdmin()
26+
const { token } = sign(adm)
27+
const authorization = `Bearer ${token}`
28+
const ctx = { params: { usuario_id: 1 }, request: { header: { authorization } }, throw: sinon.fake() }
29+
const next = sinon.mock()
30+
next.once()
3131

32-
await middleware.ifAuthenticated(ctx, next);
32+
await middleware.ifAuthenticated(ctx, next)
3333

34-
next.verify();
35-
});
34+
next.verify()
35+
})
3636

37-
it("should check if it owns the resource", async () => {
38-
// given
39-
const adm = await getAdmin();
40-
const contasIds = await resetConta({ usuario_id: adm.id });
41-
const { token } = sign(adm);
42-
const authorization = `Bearer ${token}`;
43-
const params = { usuario_id: adm.id, conta_id: contasIds[0].id };
44-
const ctx = {
45-
request: { header: { authorization }, params },
46-
throw: sinon.fake(),
47-
};
48-
const next = sinon.mock();
49-
next.once();
37+
it('should check if it owns the resource', async () => {
38+
// given
39+
const adm = await getAdmin()
40+
const contasIds = await resetConta({ usuario_id: adm.id })
41+
const { token } = sign(adm)
42+
const authorization = `Bearer ${token}`
43+
const params = { usuario_id: adm.id, conta_id: contasIds[0].id }
44+
const ctx = {
45+
request: { header: { authorization }, params },
46+
throw: sinon.fake()
47+
}
48+
const next = sinon.mock()
49+
next.once()
5050

51-
// when
52-
await middleware.contaOwnedBy(ctx, next);
51+
// when
52+
await middleware.contaOwnedBy(ctx, next)
5353

54-
// then
55-
next.verify();
56-
});
54+
// then
55+
next.verify()
56+
})
5757

58-
it("Should FAIL due missing auth header", async () => {
59-
// given
60-
const authorization = `Bearer`;
61-
const ctx = { request: { header: { authorization } }, throw: sinon.mock() };
62-
const next = sinon.mock();
63-
next.never();
64-
ctx.throw.never();
58+
it('Should FAIL due missing auth header', async () => {
59+
// given
60+
const authorization = `Bearer`
61+
const ctx = { request: { header: { authorization } }, throw: sinon.mock() }
62+
const next = sinon.mock()
63+
next.never()
64+
ctx.throw.never()
6565

66-
// when
67-
const spyable = { ifAuthenticated: middleware.ifAuthenticated };
68-
const spy = sinon.spy(spyable, "ifAuthenticated");
69-
try {
70-
await spyable.ifAuthenticated(ctx, next);
71-
} catch (e) {
72-
chai.expect(spy.exceptions).length(1);
73-
}
66+
// when
67+
const spyable = { ifAuthenticated: middleware.ifAuthenticated }
68+
const spy = sinon.spy(spyable, 'ifAuthenticated')
69+
try {
70+
await spyable.ifAuthenticated(ctx, next)
71+
} catch (e) {
72+
chai.expect(spy.exceptions).length(1)
73+
}
7474

75-
// then
76-
chai.expect(spy.called);
77-
chai.expect(spy.threw());
78-
ctx.throw.verify();
79-
next.verify();
80-
});
81-
});
75+
// then
76+
chai.expect(spy.called)
77+
chai.expect(spy.threw())
78+
ctx.throw.verify()
79+
next.verify()
80+
})
81+
})

service-node-koa/app/controllers/user.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
import { sign } from "../config/security/index.mjs";
1010

1111
export const userLoginRequest = async (ctx) => {
12+
// TODO captcha protection
1213
const { email, senha } = ctx.request.body;
1314
const user = await login({ email, senha });
1415
if (!user) return; // 404

web-app-vue/infrastructure/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ FROM alpine:3.19 AS build
22

33
ENV NODE_ENV=${NODE_ENV:-production}
44

5-
ADD .env.production .eslintrc.cjs .prettierrc.json index.html \
5+
ADD .env.production index.html \
66
package.json package-lock.json vite.config.js vitest.config.js ./
77
ADD src ./src/
88
ADD public ./public/

0 commit comments

Comments
 (0)