integration tests passed

This commit is contained in:
Hitesh 2019-07-25 08:56:01 -07:00
parent 948e6510ba
commit 16db087c19
4 changed files with 106 additions and 16 deletions

View File

@ -1 +1,2 @@
AJ ONeal <coolaj86@gmail.com> (https://coolaj86.com/) AJ ONeal <coolaj86@gmail.com> (https://coolaj86.com/)
Hitesh Walia <hiteshwar.walia@gmail.com>

View File

@ -1,3 +0,0 @@
ZONE=example.co.uk
USERNAME=johndoe
PASSWORD=xxxxxxxx

View File

@ -1,9 +1,16 @@
'use strict'; 'use strict';
var request; var request;
var defaults = {}; var defaults = {
baseUrl: 'https://api.dnsimple.com/v2/'
};
module.exports.create = function(config) { module.exports.create = function(config) {
var baseUrl = (config.baseUrl || defaults.baseUrl).replace(/\/$/, '');
var token = config.token;
var account = config.account;
return { return {
init: function(opts) { init: function(opts) {
request = opts.request; request = opts.request;
@ -11,19 +18,103 @@ module.exports.create = function(config) {
}, },
zones: function(data) { zones: function(data) {
// console.info('List Zones', data); // console.info('List Zones', data);
throw Error('listing zones not implemented'); return api('GET', '/' + account + '/zones')
.then(function(resp) {
return resp['body']['data'].map(function(elem) {
// console.log('DEBUG >>> elem.name: ' + elem.name);
return elem.name;
});
});
}, },
set: function(data) { set: function(data) {
// console.info('Add TXT', data); // console.info('Add TXT', data);
throw Error('setting TXT not implemented'); var ch = data.challenge;
}, //console.log('DEBUG >>> ch: ' + JSON.stringify(ch, null, 2));
remove: function(data) { var txt = ch.dnsAuthorization;
// console.info('Remove TXT', data); //console.log('DEBUG >>> txt: ' + txt);
throw Error('removing TXT not implemented');
return api('POST', '/' + account + '/zones/' + ch.dnsZone + '/records', {
name: '',
type: 'TXT',
content: txt
}).then(function(resp) {
//console.log('DEBUG >>> resp: ' + JSON.stringify(resp, null, 2));
if (resp.statusCode === 201) {
return true;
}
throw new Error('record did not set. check subdomain, api key, etc');
});
}, },
get: function(data) { get: function(data) {
// console.info('List TXT', data); // console.info('List TXT', data);
throw Error('listing TXTs not implemented');
return api('GET', '/' + account + '/zones/' + data.challenge.dnsZone + '/records')
.then(function(resp) {
var txtRecord = resp.body.data.filter(function (record) {
return data.challenge.dnsAuthorization === record.content;
})[0];
//console.log('DEBUG >>> txtRecord: ' + JSON.stringify(txtRecord,null,2));
if(txtRecord) return { dnsAuthorization: txtRecord.content };
else return null;
});
},
remove: function(data) {
return api('GET', '/' + account + '/zones/' + data.challenge.dnsZone + '/records')
.then(function(resp) {
var record = resp.body.data.filter(function (record) {
return data.challenge.dnsAuthorization === record.content;
})[0];
if(record) {
return api('DELETE', '/' + account + '/zones/' + data.challenge.dnsZone + '/records/' + record.id)
.then(function(resp) {
// console.info('DEBUG >>> resp: ', JSON.stringify(resp, null, 2));
return true;
});
} }
else {
throw new Error('Txt Record not found for removal');
}
});
}
}
// Authentication and Error handling here
function api(method, path, form) {
var req = {
method: method,
url: baseUrl + path,
headers: {
Authorization: 'Bearer ' + token,
'Content-Type': 'application/json'
},
json: true,
form: form
}; };
return request(req).then(function(resp) {
if (2 !== Math.floor(resp.statusCode / 100)) {
console.error(resp.statusCode, req.url);
console.error();
console.error('Request:');
console.error(req);
console.error();
console.error('Response:');
console.error(resp.body);
console.error();
throw new Error('Error response. Check token, baseUrl, domains, etc.');
}
return resp;
});
}
}; };

View File

@ -5,11 +5,12 @@
var tester = require('acme-challenge-test'); var tester = require('acme-challenge-test');
require('dotenv').config(); require('dotenv').config();
// Usage: node ./test.js example.com username xxxxxxxxx // Usage: node ./test.js example.com token account
var zone = process.argv[2] || process.env.ZONE; var zone = process.argv[2] || process.env.ZONE;
var challenger = require('./index.js').create({ var challenger = require('./index.js').create({
username: process.argv[3] || process.env.USERNAME, token: process.argv[3] || process.env.TOKEN,
password: process.argv[4] || process.env.PASSWORD account: process.argv[4] || process.env.ACCOUNT
}); });
// The dry-run tests can pass on, literally, 'example.com' // The dry-run tests can pass on, literally, 'example.com'