-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RED-16 - reworking frontend tests and user settings
- Loading branch information
Showing
12 changed files
with
798 additions
and
6,223 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 67 additions & 67 deletions
134
service-node-koa/app/config/security/middleware.spec.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,81 @@ | ||
import chai from "chai"; | ||
import chai from 'chai' | ||
|
||
import * as middleware from "./middleware.mjs"; | ||
import sinon from "sinon"; | ||
import { sign } from "./encryption.mjs"; | ||
import { getAdmin, resetConta } from "../../services/index.mjs"; | ||
import * as middleware from './middleware.mjs' | ||
import sinon from 'sinon' | ||
import { sign } from './encryption.mjs' | ||
import { getAdmin, resetConta } from '../../services/index.mjs' | ||
|
||
chai.should(); | ||
chai.should() | ||
|
||
describe("Middleware tests", () => { | ||
it("should check if it's admin", async () => { | ||
const adm = await getAdmin(); | ||
const { token } = sign(adm); | ||
const authorization = `Bearer ${token}`; | ||
const ctx = { request: { header: { authorization } }, throw: sinon.fake() }; | ||
const next = sinon.mock(); | ||
next.once(); | ||
describe('Middleware tests', () => { | ||
it('should check if it\'s admin', async () => { | ||
const adm = await getAdmin() | ||
const { token } = sign(adm) | ||
const authorization = `Bearer ${token}` | ||
const ctx = { request: { header: { authorization } }, throw: sinon.fake() } | ||
const next = sinon.mock() | ||
next.once() | ||
|
||
await middleware.ifAdmin(ctx, next); | ||
await middleware.ifAdmin(ctx, next) | ||
|
||
next.verify(); | ||
}); | ||
next.verify() | ||
}) | ||
|
||
it("should check if it's authenticated", async () => { | ||
const adm = await getAdmin(); | ||
const { token } = sign(adm); | ||
const authorization = `Bearer ${token}`; | ||
const ctx = { request: { header: { authorization } }, throw: sinon.fake() }; | ||
const next = sinon.mock(); | ||
next.once(); | ||
it('should check if it\'s authenticated', async () => { | ||
const adm = await getAdmin() | ||
const { token } = sign(adm) | ||
const authorization = `Bearer ${token}` | ||
const ctx = { params: { usuario_id: 1 }, request: { header: { authorization } }, throw: sinon.fake() } | ||
const next = sinon.mock() | ||
next.once() | ||
|
||
await middleware.ifAuthenticated(ctx, next); | ||
await middleware.ifAuthenticated(ctx, next) | ||
|
||
next.verify(); | ||
}); | ||
next.verify() | ||
}) | ||
|
||
it("should check if it owns the resource", async () => { | ||
// given | ||
const adm = await getAdmin(); | ||
const contasIds = await resetConta({ usuario_id: adm.id }); | ||
const { token } = sign(adm); | ||
const authorization = `Bearer ${token}`; | ||
const params = { usuario_id: adm.id, conta_id: contasIds[0].id }; | ||
const ctx = { | ||
request: { header: { authorization }, params }, | ||
throw: sinon.fake(), | ||
}; | ||
const next = sinon.mock(); | ||
next.once(); | ||
it('should check if it owns the resource', async () => { | ||
// given | ||
const adm = await getAdmin() | ||
const contasIds = await resetConta({ usuario_id: adm.id }) | ||
const { token } = sign(adm) | ||
const authorization = `Bearer ${token}` | ||
const params = { usuario_id: adm.id, conta_id: contasIds[0].id } | ||
const ctx = { | ||
request: { header: { authorization }, params }, | ||
throw: sinon.fake() | ||
} | ||
const next = sinon.mock() | ||
next.once() | ||
|
||
// when | ||
await middleware.contaOwnedBy(ctx, next); | ||
// when | ||
await middleware.contaOwnedBy(ctx, next) | ||
|
||
// then | ||
next.verify(); | ||
}); | ||
// then | ||
next.verify() | ||
}) | ||
|
||
it("Should FAIL due missing auth header", async () => { | ||
// given | ||
const authorization = `Bearer`; | ||
const ctx = { request: { header: { authorization } }, throw: sinon.mock() }; | ||
const next = sinon.mock(); | ||
next.never(); | ||
ctx.throw.never(); | ||
it('Should FAIL due missing auth header', async () => { | ||
// given | ||
const authorization = `Bearer` | ||
const ctx = { request: { header: { authorization } }, throw: sinon.mock() } | ||
const next = sinon.mock() | ||
next.never() | ||
ctx.throw.never() | ||
|
||
// when | ||
const spyable = { ifAuthenticated: middleware.ifAuthenticated }; | ||
const spy = sinon.spy(spyable, "ifAuthenticated"); | ||
try { | ||
await spyable.ifAuthenticated(ctx, next); | ||
} catch (e) { | ||
chai.expect(spy.exceptions).length(1); | ||
} | ||
// when | ||
const spyable = { ifAuthenticated: middleware.ifAuthenticated } | ||
const spy = sinon.spy(spyable, 'ifAuthenticated') | ||
try { | ||
await spyable.ifAuthenticated(ctx, next) | ||
} catch (e) { | ||
chai.expect(spy.exceptions).length(1) | ||
} | ||
|
||
// then | ||
chai.expect(spy.called); | ||
chai.expect(spy.threw()); | ||
ctx.throw.verify(); | ||
next.verify(); | ||
}); | ||
}); | ||
// then | ||
chai.expect(spy.called) | ||
chai.expect(spy.threw()) | ||
ctx.throw.verify() | ||
next.verify() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.