|
| 1 | +/* eslint-env node, mocha */ |
| 2 | + |
| 3 | +'use strict' |
| 4 | + |
| 5 | +const sinon = require('sinon') |
| 6 | +require('sinon-as-promised') |
| 7 | +const proxyquire = require('proxyquire').noCallThru() |
| 8 | +const assert = require('assert') |
| 9 | + |
| 10 | +describe('Dynamic DNS Client', function () { |
| 11 | + const client = { |
| 12 | + domains: { |
| 13 | + listRecords: function () { throw new Error('not stubbed') }, |
| 14 | + updateRecord: function () { throw new Error('not stubbed') } |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + const publicIp = { |
| 19 | + v4: function () { throw new Error('not stubbed') } |
| 20 | + } |
| 21 | + |
| 22 | + let sandbox, dnsClient |
| 23 | + |
| 24 | + before(function () { |
| 25 | + dnsClient = proxyquire('../lib', { |
| 26 | + 'public-ip': publicIp, |
| 27 | + 'digitalocean': { |
| 28 | + client: function () { |
| 29 | + return client |
| 30 | + } |
| 31 | + } |
| 32 | + }) |
| 33 | + }) |
| 34 | + |
| 35 | + beforeEach(function () { |
| 36 | + sandbox = sinon.sandbox.create() |
| 37 | + }) |
| 38 | + |
| 39 | + afterEach(function () { |
| 40 | + sandbox.restore() |
| 41 | + process.env = {} |
| 42 | + }) |
| 43 | + |
| 44 | + it('should do nothing if IP does not change', function () { |
| 45 | + process.env = { |
| 46 | + DIGITAL_OCEAN_TOKEN: 'xxxx', |
| 47 | + SERVER_NAME: 'server.example.com' |
| 48 | + } |
| 49 | + |
| 50 | + sandbox.stub(publicIp, 'v4').resolves('10.0.1.23') |
| 51 | + sandbox.stub(client.domains, 'listRecords').resolves([ |
| 52 | + { |
| 53 | + id: 123, |
| 54 | + type: 'A', |
| 55 | + name: 'server', |
| 56 | + data: '10.0.1.23', |
| 57 | + priority: null, |
| 58 | + port: null, |
| 59 | + weight: null |
| 60 | + }, |
| 61 | + { |
| 62 | + id: 124, |
| 63 | + type: 'NS', |
| 64 | + name: '@', |
| 65 | + data: 'ns1.digitalocean.com', |
| 66 | + priority: null, |
| 67 | + port: null, |
| 68 | + weight: null |
| 69 | + } |
| 70 | + ]) |
| 71 | + |
| 72 | + sandbox.stub(client.domains, 'updateRecord') |
| 73 | + |
| 74 | + return dnsClient().then(function () { |
| 75 | + sinon.assert.calledOnce(publicIp.v4) |
| 76 | + sinon.assert.calledWithExactly(client.domains.listRecords, 'example.com') |
| 77 | + sinon.assert.notCalled(client.domains.updateRecord) |
| 78 | + }) |
| 79 | + }) |
| 80 | + |
| 81 | + it('should update domain record if IP does change', function () { |
| 82 | + process.env = { |
| 83 | + DIGITAL_OCEAN_TOKEN: 'xxxx', |
| 84 | + SERVER_NAME: 'server.example.com' |
| 85 | + } |
| 86 | + |
| 87 | + sandbox.stub(publicIp, 'v4').resolves('10.0.1.15') |
| 88 | + sandbox.stub(client.domains, 'listRecords').resolves([ |
| 89 | + { |
| 90 | + id: 123, |
| 91 | + type: 'A', |
| 92 | + name: 'server', |
| 93 | + data: '10.0.1.23', |
| 94 | + priority: null, |
| 95 | + port: null, |
| 96 | + weight: null |
| 97 | + }, |
| 98 | + { |
| 99 | + id: 124, |
| 100 | + type: 'NS', |
| 101 | + name: '@', |
| 102 | + data: 'ns1.digitalocean.com', |
| 103 | + priority: null, |
| 104 | + port: null, |
| 105 | + weight: null |
| 106 | + } |
| 107 | + ]) |
| 108 | + |
| 109 | + sandbox.stub(client.domains, 'updateRecord') |
| 110 | + |
| 111 | + return dnsClient().then(function () { |
| 112 | + sinon.assert.calledOnce(publicIp.v4) |
| 113 | + sinon.assert.calledWithExactly(client.domains.listRecords, 'example.com') |
| 114 | + sinon.assert.calledWithExactly(client.domains.updateRecord, 'example.com', 123, { |
| 115 | + name: 'server', |
| 116 | + type: 'A', |
| 117 | + data: '10.0.1.15' |
| 118 | + }) |
| 119 | + }) |
| 120 | + }) |
| 121 | + |
| 122 | + it('should fail if unable to find domain record', function () { |
| 123 | + process.env = { |
| 124 | + DIGITAL_OCEAN_TOKEN: 'xxxx', |
| 125 | + SERVER_NAME: 'server.example.com' |
| 126 | + } |
| 127 | + |
| 128 | + sandbox.stub(publicIp, 'v4').resolves('10.0.1.15') |
| 129 | + sandbox.stub(client.domains, 'listRecords').resolves([ |
| 130 | + { |
| 131 | + id: 124, |
| 132 | + type: 'NS', |
| 133 | + name: '@', |
| 134 | + data: 'ns1.digitalocean.com', |
| 135 | + priority: null, |
| 136 | + port: null, |
| 137 | + weight: null |
| 138 | + } |
| 139 | + ]) |
| 140 | + |
| 141 | + sandbox.stub(client.domains, 'updateRecord') |
| 142 | + |
| 143 | + return dnsClient() |
| 144 | + .then(function () { |
| 145 | + throw new Error('Expected promise to fail!') |
| 146 | + }) |
| 147 | + .catch(function (error) { |
| 148 | + assert.equal(error.message, 'Unable to find domain A record for subdomain "server" and domain "example.com"') |
| 149 | + sinon.assert.calledOnce(publicIp.v4) |
| 150 | + sinon.assert.calledWithExactly(client.domains.listRecords, 'example.com') |
| 151 | + sinon.assert.notCalled(client.domains.updateRecord) |
| 152 | + }) |
| 153 | + }) |
| 154 | + |
| 155 | + it('should exit if required environment variables are not set', function () { |
| 156 | + sandbox.stub(console, 'error') |
| 157 | + require('../') |
| 158 | + sinon.assert.calledWithExactly(console.error, 'Missing required environment variables: DIGITAL_OCEAN_TOKEN or SERVER_NAME') |
| 159 | + }) |
| 160 | +}) |
0 commit comments