|
1 | | -// This test needs to run first |
| 1 | +/* |
| 2 | + Template test file. Demonstrates how to: |
| 3 | + - Use chai-http to test the server |
| 4 | + - Initialize the server |
| 5 | + - Stub dependencies with sinon sandbox |
| 6 | + - Reset stubs after each test |
| 7 | + - Use proxyquire to replace modules |
| 8 | + - Clear module cache after a test |
| 9 | +*/ |
| 10 | + |
2 | 11 | const chai = require('chai'); |
3 | 12 | const chaiHttp = require('chai-http'); |
| 13 | +const sinon = require('sinon'); |
| 14 | +const proxyquire = require('proxyquire'); |
| 15 | + |
4 | 16 | const service = require('../src/service'); |
| 17 | +const db = require('../src/db'); |
| 18 | + |
| 19 | +const expect = chai.expect; |
5 | 20 |
|
6 | 21 | chai.use(chaiHttp); |
7 | | -chai.should(); |
8 | 22 |
|
9 | | -// Use this test as a template |
10 | | -describe('init', async () => { |
| 23 | +const TEST_REPO = { |
| 24 | + project: 'finos', |
| 25 | + name: 'db-test-repo', |
| 26 | + url: 'https://github.com/finos/db-test-repo.git', |
| 27 | +}; |
| 28 | + |
| 29 | +describe('init', () => { |
11 | 30 | let app; |
| 31 | + let sandbox; |
| 32 | + |
| 33 | + // Runs before all tests |
12 | 34 | before(async function () { |
13 | | - app = await service.start(); // pass in proxy if testing config loading or administration of proxy routes |
| 35 | + // Start the service (can also pass config if testing proxy routes) |
| 36 | + app = await service.start(); |
| 37 | + }); |
| 38 | + |
| 39 | + // Runs before each test |
| 40 | + beforeEach(function () { |
| 41 | + // Create a sandbox for stubbing |
| 42 | + sandbox = sinon.createSandbox(); |
| 43 | + |
| 44 | + // Example: stub a DB method |
| 45 | + sandbox.stub(db, 'getRepo').resolves(TEST_REPO); |
14 | 46 | }); |
15 | 47 |
|
16 | | - it('should not be logged in', async function () { |
| 48 | + // Example test: check server is running |
| 49 | + it('should return 401 if not logged in', async function () { |
17 | 50 | const res = await chai.request(app).get('/api/auth/profile'); |
| 51 | + expect(res).to.have.status(401); |
| 52 | + }); |
| 53 | + |
| 54 | + // Example test: check db stub is working |
| 55 | + it('should get the repo from stubbed db', async function () { |
| 56 | + const repo = await db.getRepo('finos/db-test-repo'); |
| 57 | + expect(repo).to.deep.equal(TEST_REPO); |
| 58 | + }); |
| 59 | + |
| 60 | + // Example test: use proxyquire to override the config module |
| 61 | + it('should return an array of enabled auth methods when overridden', async function () { |
| 62 | + const fsStub = { |
| 63 | + readFileSync: sandbox.stub().returns( |
| 64 | + JSON.stringify({ |
| 65 | + authentication: [ |
| 66 | + { type: 'local', enabled: true }, |
| 67 | + { type: 'ActiveDirectory', enabled: true }, |
| 68 | + { type: 'openidconnect', enabled: true }, |
| 69 | + ], |
| 70 | + }), |
| 71 | + ), |
| 72 | + }; |
| 73 | + |
| 74 | + const config = proxyquire('../src/config', { |
| 75 | + fs: fsStub, |
| 76 | + }); |
| 77 | + config.initUserConfig(); |
| 78 | + const authMethods = config.getAuthMethods(); |
| 79 | + expect(authMethods).to.have.lengthOf(3); |
| 80 | + expect(authMethods[0].type).to.equal('local'); |
| 81 | + expect(authMethods[1].type).to.equal('ActiveDirectory'); |
| 82 | + expect(authMethods[2].type).to.equal('openidconnect'); |
| 83 | + |
| 84 | + // Clear config module cache so other tests don't use the stubbed config |
| 85 | + delete require.cache[require.resolve('../src/config')]; |
| 86 | + }); |
18 | 87 |
|
19 | | - res.should.have.status(401); |
| 88 | + // Runs after each test |
| 89 | + afterEach(function () { |
| 90 | + // Restore all stubs in this sandbox |
| 91 | + sandbox.restore(); |
20 | 92 | }); |
21 | 93 |
|
| 94 | + // Runs after all tests |
22 | 95 | after(async function () { |
23 | 96 | await service.httpServer.close(); |
24 | 97 | }); |
|
0 commit comments