|
1 |
| -import chai from "chai"; |
| 1 | +import chai from 'chai' |
2 | 2 |
|
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' |
7 | 7 |
|
8 |
| -chai.should(); |
| 8 | +chai.should() |
9 | 9 |
|
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() |
18 | 18 |
|
19 |
| - await middleware.ifAdmin(ctx, next); |
| 19 | + await middleware.ifAdmin(ctx, next) |
20 | 20 |
|
21 |
| - next.verify(); |
22 |
| - }); |
| 21 | + next.verify() |
| 22 | + }) |
23 | 23 |
|
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() |
31 | 31 |
|
32 |
| - await middleware.ifAuthenticated(ctx, next); |
| 32 | + await middleware.ifAuthenticated(ctx, next) |
33 | 33 |
|
34 |
| - next.verify(); |
35 |
| - }); |
| 34 | + next.verify() |
| 35 | + }) |
36 | 36 |
|
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() |
50 | 50 |
|
51 |
| - // when |
52 |
| - await middleware.contaOwnedBy(ctx, next); |
| 51 | + // when |
| 52 | + await middleware.contaOwnedBy(ctx, next) |
53 | 53 |
|
54 |
| - // then |
55 |
| - next.verify(); |
56 |
| - }); |
| 54 | + // then |
| 55 | + next.verify() |
| 56 | + }) |
57 | 57 |
|
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() |
65 | 65 |
|
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 | + } |
74 | 74 |
|
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 | +}) |
0 commit comments