|
2 | 2 | const expect = require('chai').expect;
|
3 | 3 | const sinon = require('sinon');
|
4 | 4 | const proxyquire = require('proxyquire');
|
| 5 | +const configStub = require('../../../test/utils/config-stub'); |
5 | 6 |
|
6 | 7 | const modulePath = '../index';
|
7 | 8 | const errors = require('../../../lib/errors');
|
8 | 9 |
|
9 | 10 | describe('Unit: Mysql extension', function () {
|
10 |
| - describe('setup hook', function () { |
| 11 | + it('setup hook works', function () { |
11 | 12 | const MysqlExtension = require(modulePath);
|
12 |
| - |
13 |
| - it('does not add stage if --local is true', function () { |
14 |
| - const instance = new MysqlExtension({}, {}, {}, '/some/dir'); |
15 |
| - const addStageStub = sinon.stub(); |
16 |
| - |
17 |
| - instance.setup({addStage: addStageStub}, {local: true}); |
18 |
| - expect(addStageStub.called).to.be.false; |
19 |
| - }); |
20 |
| - |
21 |
| - it('does not add stage if db is sqlite3', function () { |
22 |
| - const instance = new MysqlExtension({}, {}, {}, '/some/dir'); |
23 |
| - const addStageStub = sinon.stub(); |
24 |
| - |
25 |
| - instance.setup({addStage: addStageStub}, {local: false, db: 'sqlite3'}); |
26 |
| - expect(addStageStub.called).to.be.false; |
27 |
| - }); |
28 |
| - |
29 |
| - it('adds stage if not local and db is not sqlite3', function () { |
30 |
| - const instance = new MysqlExtension({}, {}, {}, '/some/dir'); |
31 |
| - const addStageStub = sinon.stub(); |
32 |
| - |
33 |
| - instance.setup({addStage: addStageStub}, {local: false, db: 'mysql'}); |
34 |
| - expect(addStageStub.calledOnce).to.be.true; |
35 |
| - expect(addStageStub.calledWith('mysql')).to.be.true; |
36 |
| - }); |
| 13 | + const inst = new MysqlExtension({}, {}, {}, '/some/dir'); |
| 14 | + const result = inst.setup(); |
| 15 | + |
| 16 | + expect(result).to.have.length(1); |
| 17 | + const [task] = result; |
| 18 | + |
| 19 | + // Check static properties |
| 20 | + expect(task.id).to.equal('mysql'); |
| 21 | + expect(task.name).to.equal('"ghost" mysql user'); |
| 22 | + |
| 23 | + // Check task functions |
| 24 | + expect(task.task).to.be.a('function'); |
| 25 | + expect(task.enabled).to.be.a('function'); |
| 26 | + expect(task.skip).to.be.a('function'); |
| 27 | + |
| 28 | + // Check task.task |
| 29 | + const stub = sinon.stub(inst, 'setupMySQL'); |
| 30 | + task.task('a', 'set', 'of', 'args', true); |
| 31 | + expect(stub.calledOnce).to.be.true; |
| 32 | + expect(stub.calledWithExactly('a', 'set', 'of', 'args', true)).to.be.true; |
| 33 | + |
| 34 | + // Check task.enabled |
| 35 | + expect(task.enabled({argv: {local: true}})).to.be.false; |
| 36 | + expect(task.enabled({argv: {local: false, db: 'sqlite3'}})).to.be.false; |
| 37 | + expect(task.enabled({argv: {local: false}})).to.be.true; |
| 38 | + |
| 39 | + // Check task.skip |
| 40 | + const get = sinon.stub(); |
| 41 | + get.onFirstCall().returns('not-root'); |
| 42 | + get.onSecondCall().returns('root'); |
| 43 | + |
| 44 | + expect(task.skip({instance: {config: {get}}})).to.be.true; |
| 45 | + expect(get.calledOnce).to.be.true; |
| 46 | + expect(task.skip({instance: {config: {get}}})).to.be.false; |
| 47 | + expect(get.calledTwice).to.be.true; |
37 | 48 | });
|
38 | 49 |
|
39 |
| - describe('setupMySQL', function () { |
| 50 | + it('setupMySQL works', function () { |
40 | 51 | const MysqlExtension = require(modulePath);
|
41 |
| - |
42 |
| - it('skips if db user is not root', function () { |
43 |
| - const logStub = sinon.stub(); |
44 |
| - const instance = new MysqlExtension({log: logStub}, {}, {}, '/some/dir'); |
45 |
| - const getStub = sinon.stub().returns({user: 'notroot'}); |
46 |
| - const skipStub = sinon.stub().resolves(); |
47 |
| - |
48 |
| - return instance.setupMySQL({}, {instance: {config: {get: getStub}}}, {skip: skipStub}).then(() => { |
49 |
| - expect(getStub.calledOnce).to.be.true; |
50 |
| - expect(getStub.calledWithExactly('database.connection')).to.be.true; |
51 |
| - expect(logStub.calledOnce).to.be.true; |
52 |
| - expect(logStub.args[0][0]).to.match(/user is not "root"/); |
53 |
| - expect(skipStub.calledOnce).to.be.true; |
54 |
| - }); |
55 |
| - }); |
56 |
| - |
57 |
| - it('returns tasks that call the helpers and cleanup', function () { |
58 |
| - const listrStub = sinon.stub().callsFake(function (tasks) { |
59 |
| - expect(tasks).to.be.an.instanceof(Array); |
60 |
| - expect(tasks).to.have.length(4); |
61 |
| - |
62 |
| - // Run each task step |
63 |
| - tasks.forEach(task => task.task()); |
64 |
| - return Promise.resolve(); |
65 |
| - }); |
66 |
| - const instance = new MysqlExtension({listr: listrStub}, {}, {}, '/some/dir'); |
67 |
| - const getStub = sinon.stub().returns({user: 'root'}); |
68 |
| - const saveStub = sinon.stub(); |
69 |
| - const setStub = sinon.stub(); |
70 |
| - setStub.returns({set: setStub, save: saveStub}); |
71 |
| - const endStub = sinon.stub(); |
72 |
| - const skipStub = sinon.stub().resolves(); |
73 |
| - const canConnectStub = sinon.stub(instance, 'canConnect'); |
74 |
| - const createUserStub = sinon.stub(instance, 'createUser').callsFake((ctx) => { |
75 |
| - ctx.mysql = {username: 'testuser', password: 'testpass'}; |
76 |
| - }); |
77 |
| - const grantPermissionsStub = sinon.stub(instance, 'grantPermissions'); |
78 |
| - |
79 |
| - instance.connection = {end: endStub}; |
80 |
| - |
81 |
| - return instance.setupMySQL({}, { |
82 |
| - instance: {config: {get: getStub, set: setStub, save: saveStub}} |
83 |
| - }, {skip: skipStub}).then(() => { |
84 |
| - expect(skipStub.called).to.be.false; |
85 |
| - expect(listrStub.calledOnce).to.be.true; |
86 |
| - expect(listrStub.args[0][1]).to.be.false; |
87 |
| - |
88 |
| - expect(getStub.calledOnce).to.be.true; |
89 |
| - expect(canConnectStub.calledOnce).to.be.true; |
90 |
| - expect(createUserStub.calledOnce).to.be.true; |
91 |
| - expect(grantPermissionsStub.calledOnce).to.be.true; |
92 |
| - expect(setStub.calledTwice).to.be.true; |
93 |
| - expect(setStub.calledWithExactly('database.connection.user', 'testuser')).to.be.true; |
94 |
| - expect(setStub.calledWithExactly('database.connection.password', 'testpass')).to.be.true; |
95 |
| - expect(saveStub.calledOnce).to.be.true; |
96 |
| - expect(endStub.calledOnce).to.be.true; |
97 |
| - }); |
98 |
| - }); |
| 52 | + const listr = sinon.stub(); |
| 53 | + const config = configStub(); |
| 54 | + const dbConfig = {host: 'localhost', user: 'root', password: 'password'}; |
| 55 | + config.get.returns(dbConfig); |
| 56 | + |
| 57 | + const inst = new MysqlExtension({listr}, {}, {}, '/some/dir'); |
| 58 | + const context = {instance: {config}}; |
| 59 | + inst.setupMySQL(context); |
| 60 | + |
| 61 | + expect(config.get.calledOnce).to.be.true; |
| 62 | + expect(config.get.calledWithExactly('database.connection')).to.be.true; |
| 63 | + expect(listr.calledOnce).to.be.true; |
| 64 | + const [tasks, ctx] = listr.args[0]; |
| 65 | + expect(tasks).to.have.length(4); |
| 66 | + expect(ctx).to.be.false; |
| 67 | + |
| 68 | + const canConnect = sinon.stub(inst, 'canConnect'); |
| 69 | + expect(tasks[0].title).to.equal('Connecting to database'); |
| 70 | + tasks[0].task(); |
| 71 | + expect(canConnect.calledOnce).to.be.true; |
| 72 | + expect(canConnect.calledWithExactly(context, dbConfig)).to.be.true; |
| 73 | + |
| 74 | + const createUser = sinon.stub(inst, 'createUser'); |
| 75 | + expect(tasks[1].title).to.equal('Creating new MySQL user'); |
| 76 | + tasks[1].task(); |
| 77 | + expect(createUser.calledOnce).to.be.true; |
| 78 | + expect(createUser.calledWithExactly(context, dbConfig)).to.be.true; |
| 79 | + |
| 80 | + const grantPermissions = sinon.stub(inst, 'grantPermissions'); |
| 81 | + expect(tasks[2].title).to.equal('Granting new user permissions'); |
| 82 | + tasks[2].task(); |
| 83 | + expect(grantPermissions.calledOnce).to.be.true; |
| 84 | + expect(grantPermissions.calledWithExactly(context, dbConfig)).to.be.true; |
| 85 | + |
| 86 | + const end = sinon.stub(); |
| 87 | + inst.connection = {end}; |
| 88 | + context.mysql = {username: 'new', password: 'new'}; |
| 89 | + expect(tasks[3].title).to.equal('Saving new config'); |
| 90 | + tasks[3].task(); |
| 91 | + expect(config.set.calledTwice).to.be.true; |
| 92 | + expect(config.set.calledWithExactly('database.connection.user', 'new')).to.be.true; |
| 93 | + expect(config.set.calledWithExactly('database.connection.password', 'new')).to.be.true; |
| 94 | + expect(config.save.calledOnce).to.be.true; |
| 95 | + expect(end.calledOnce).to.be.true; |
99 | 96 | });
|
100 | 97 |
|
101 | 98 | describe('canConnect', function () {
|
|
0 commit comments