-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
26ee471
commit 31ec3ec
Showing
11 changed files
with
221 additions
and
9 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
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
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
73 changes: 73 additions & 0 deletions
73
test/tests/integration/adoption-code/adoption-code.test.ts
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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import nock from 'nock'; | ||
import { expect } from 'chai'; | ||
import * as sinon from 'sinon'; | ||
import type { Socket } from 'socket.io-client'; | ||
import request from 'supertest'; | ||
import { getTestServer, addFakeProbe, deleteFakeProbe } from '../../../utils/server.js'; | ||
import nockGeoIpProviders from '../../../utils/nock-geo-ip.js'; | ||
|
||
let probe: Socket; | ||
const app = await getTestServer(); | ||
const requestAgent = request(app); | ||
const adoptionCodeStub = sinon.stub(); | ||
|
||
describe('Adoption code', () => { | ||
before(async () => { | ||
nockGeoIpProviders(); | ||
|
||
probe = await addFakeProbe({ | ||
'probe:adoption:code': adoptionCodeStub, | ||
}); | ||
}); | ||
|
||
afterEach(async () => { | ||
sinon.resetHistory(); | ||
}); | ||
|
||
after(async () => { | ||
nock.cleanAll(); | ||
await deleteFakeProbe(probe); | ||
}); | ||
|
||
it('should send code to the requested probe', async () => { | ||
await requestAgent.post('/v1/adoption-code?adminkey=admin') | ||
.send({ | ||
ip: '1.2.3.4', | ||
code: '123456', | ||
}) | ||
.expect(200).expect((response) => { | ||
expect(response.body).to.deep.equal({ | ||
result: 'Code was sent to the probe', | ||
}); | ||
}); | ||
|
||
expect(adoptionCodeStub.callCount).to.equal(1); | ||
expect(adoptionCodeStub.args[0]).to.deep.equal([{ code: '123456' }]); | ||
}); | ||
|
||
it('should return 403 for non-admins', async () => { | ||
await requestAgent.post('/v1/adoption-code?adminkey=wrongkey') | ||
.send({ | ||
ip: '1.2.3.4', | ||
code: '123456', | ||
}) | ||
.expect(403).expect((response) => { | ||
expect(response.body.error.message).to.equal('Forbidden'); | ||
}); | ||
|
||
expect(adoptionCodeStub.callCount).to.equal(0); | ||
}); | ||
|
||
it('should return 422 if probe not found', async () => { | ||
await requestAgent.post('/v1/adoption-code?adminkey=admin') | ||
.send({ | ||
ip: '9.9.9.9', | ||
code: '123456', | ||
}) | ||
.expect(422).expect((response) => { | ||
expect(response.body.error.message).to.equal('No suitable probes found.'); | ||
}); | ||
|
||
expect(adoptionCodeStub.callCount).to.equal(0); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { expect } from 'chai'; | ||
import { Knex } from 'knex'; | ||
import * as sinon from 'sinon'; | ||
import { AdoptedProbes } from '../../../src/lib/adopted-probes.js'; | ||
|
||
const selectStub = sinon.stub(); | ||
const updateStub = sinon.stub(); | ||
const whereStub = sinon.stub().returns({ | ||
update: updateStub, | ||
}); | ||
const sqlStub = sinon.stub().returns({ | ||
select: selectStub, | ||
where: whereStub, | ||
}); | ||
let clock: sinon.SinonSandbox['clock']; | ||
|
||
describe('AdoptedProbes', () => { | ||
before(() => { | ||
clock = sinon.useFakeTimers(); | ||
}); | ||
|
||
beforeEach(() => { | ||
sinon.resetHistory(); | ||
}); | ||
|
||
afterEach(() => { | ||
clock.restore(); | ||
}); | ||
|
||
it('startSync method should sync the data and start regular syncs', async () => { | ||
const adoptedProbes = new AdoptedProbes(sqlStub as unknown as Knex); | ||
selectStub.resolves([{ | ||
ip: '1.1.1.1', | ||
uuid: '1-1-1-1-1', | ||
}]); | ||
|
||
await adoptedProbes.startSync(); | ||
|
||
expect(sqlStub.callCount).to.equal(1); | ||
expect(sqlStub.args[0]).deep.equal([ 'adopted_probes' ]); | ||
expect(selectStub.callCount).to.equal(1); | ||
expect(selectStub.args[0]).deep.equal([ 'ip', 'uuid' ]); | ||
|
||
await clock.tickAsync(5500); | ||
expect(sqlStub.callCount).to.equal(2); | ||
expect(selectStub.callCount).to.equal(2); | ||
}); | ||
|
||
it('syncProbeIds method should do nothing if probe was not found either by ip or uuid', async () => { | ||
const adoptedProbes = new AdoptedProbes(sqlStub as unknown as Knex); | ||
selectStub.resolves([{ | ||
ip: '1.1.1.1', | ||
uuid: '1-1-1-1-1', | ||
}]); | ||
|
||
await adoptedProbes.startSync(); | ||
await adoptedProbes.syncProbeIds('2.2.2.2', '2-2-2-2-2'); | ||
|
||
expect(whereStub.callCount).to.equal(0); | ||
expect(updateStub.callCount).to.equal(0); | ||
}); | ||
|
||
it('syncProbeIds method should do nothing if probe data is actual', async () => { | ||
const adoptedProbes = new AdoptedProbes(sqlStub as unknown as Knex); | ||
selectStub.resolves([{ | ||
ip: '1.1.1.1', | ||
uuid: '1-1-1-1-1', | ||
}]); | ||
|
||
await adoptedProbes.startSync(); | ||
await adoptedProbes.syncProbeIds('1.1.1.1', '1-1-1-1-1'); | ||
|
||
expect(whereStub.callCount).to.equal(0); | ||
expect(updateStub.callCount).to.equal(0); | ||
}); | ||
|
||
it('syncProbeIds method should update uuid if it is wrong', async () => { | ||
const adoptedProbes = new AdoptedProbes(sqlStub as unknown as Knex); | ||
selectStub.resolves([{ | ||
ip: '1.1.1.1', | ||
uuid: '1-1-1-1-1', | ||
}]); | ||
|
||
await adoptedProbes.startSync(); | ||
await adoptedProbes.syncProbeIds('1.1.1.1', '2-2-2-2-2'); | ||
|
||
expect(whereStub.callCount).to.equal(1); | ||
expect(whereStub.args[0]).to.deep.equal([{ ip: '1.1.1.1' }]); | ||
expect(updateStub.callCount).to.equal(1); | ||
expect(updateStub.args[0]).to.deep.equal([{ uuid: '2-2-2-2-2' }]); | ||
}); | ||
|
||
it('syncProbeIds method should update ip if it is wrong', async () => { | ||
const adoptedProbes = new AdoptedProbes(sqlStub as unknown as Knex); | ||
selectStub.resolves([{ | ||
ip: '1.1.1.1', | ||
uuid: '1-1-1-1-1', | ||
}]); | ||
|
||
await adoptedProbes.startSync(); | ||
await adoptedProbes.syncProbeIds('2.2.2.2', '1-1-1-1-1'); | ||
|
||
expect(whereStub.callCount).to.equal(1); | ||
expect(whereStub.args[0]).to.deep.equal([{ uuid: '1-1-1-1-1' }]); | ||
expect(updateStub.callCount).to.equal(1); | ||
expect(updateStub.args[0]).to.deep.equal([{ ip: '2.2.2.2' }]); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type { Context } from 'koa'; | ||
import * as sinon from 'sinon'; | ||
import { expect } from 'chai'; | ||
import createHttpError from 'http-errors'; | ||
import { onlyAdmin } from '../../../../src/lib/http/middleware/only-admin.js'; | ||
|
||
const next = sinon.stub(); | ||
|
||
beforeEach(() => { | ||
sinon.resetHistory(); | ||
}); | ||
|
||
describe('rate limit middleware', () => { | ||
it('should reject requests with "isAdmin" false', async () => { | ||
const ctx = { isAdmin: false } as unknown as Context; | ||
const err = await onlyAdmin()(ctx, next).catch(err => err); | ||
expect(err).to.deep.equal(createHttpError(403, 'Forbidden', { type: 'access_forbidden' })); | ||
expect(next.callCount).to.equal(0); | ||
}); | ||
|
||
it('should accept requests with "isAdmin" true', async () => { | ||
const ctx = { isAdmin: true } as unknown as Context; | ||
await onlyAdmin()(ctx, next); | ||
expect(next.callCount).to.equal(1); | ||
}); | ||
}); |
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