Verified Commit 85bc1202 authored by Frauke's avatar Frauke
Browse files

Updated magic web factory endpoints and mocking

parent 755cb573
......@@ -24,6 +24,7 @@
"scripts": {
"test": "npm run eslint && npm run mocha",
"eslint": "eslint src/. test/. --config .eslintrc.json",
"dev": "nodemon src/",
"start": "node src/",
"mocha": "mocha test/ --recursive --exit"
},
......
// Application hooks that run for every service
const logger = require('./hooks/logger');
const errors = require('@feathersjs/errors');
module.exports = {
before: {
all: [ logger() ],
all: [ logger(), context => {
if((!context.params.query.secret && (context.data && !context.data.secret)) || (context.data && context.data.secret && context.data.secret !== 'ethical-design') || (context.params.query.secret && context.params.query.secret !== 'ethical-design')) {
throw new errors.NotAuthenticated('Secret is not correct.')
}
} ],
find: [],
get: [],
create: [],
......
const axios = require('axios')
const axiosConfig = {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
}
const errors = require('@feathersjs/errors');
class Service {
constructor (options) {
this.options = options || {}
}
async find (params) {
return []
}
async get (id, params) {
const re = new RegExp(/^((?:(?:(?:[A-Za-z0-9][-]?)*)\w)+)((?:(?:(?:\w[\-]?){1,62})\w)+)\.(\w{2,6})$/) //eslint-disable-line
const valid = id.match(re) !== null
const states = ['available', 'not_available', 'unknown']
return {
domain: id,
valid: valid,
state: valid ? states[Math.floor(Math.random() * states.length)] : valid
switch(id) {
case 'available.gent':
return {
status: true
}
break;
case 'unavailable.gent':
return {
status: false
}
break;
case 'error.gent':
return new errors.Unavailable('Service unavailable. The domain check/registration service is unavailable.')
break;
default:
return new errors.BadRequest('Domain not valid, choose between available.gent, unavailable.gent and error.gent')
}
}
async create (data, params) {
setTimeout(() => {
axios.post(data.callbackUrl, {domain: data.domain}, axiosConfig)
.then(() => {
console.log('done')
})
.catch((err) => {
console.log(err)
console.log(data)
})
}, data.delay)
return data
}
async update (id, data, params) {
return data
}
async patch (id, data, params) {
return data
}
async remove (id, params) {
return {id}
}
}
module.exports = function (options) {
......
......@@ -3,7 +3,7 @@ const createService = require('./domain.class.js');
const hooks = require('./domain.hooks');
module.exports = function (app) {
const paginate = app.get('paginate');
const options = {
......@@ -12,10 +12,10 @@ module.exports = function (app) {
};
// Initialize our service with any options it requires
app.use('/domain', createService(options));
app.use('/v1/domain', createService(options));
// Get our initialized service so that we can register hooks and filters
const service = app.service('domain');
const service = app.service('v1/domain');
service.hooks(hooks);
};
const domain = require('./domain/domain.service.js');
const site = require('./site/site.service.js');
module.exports = function (app) {
app.configure(domain);
app.configure(site);
};
/* eslint-disable no-unused-vars */
const errors = require('@feathersjs/errors');
const axios = require('axios')
const axiosConfig = {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
}
class Service {
constructor (options) {
this.options = options || {};
}
async create (data, params) {
setTimeout(() => {
axios.post(data.callbackUrl, {domain: data.domain}, axiosConfig)
.then(() => {
console.log('done')
})
.catch((err) => {
console.log(err)
console.log(data)
return new errors.Unavailable('Service unavailable. The domain check/registration service is unavailable.')
})
}, data.delay)
return data
}
}
module.exports = function (options) {
return new Service(options);
};
module.exports.Service = Service;
module.exports = {
before: {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
},
after: {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
},
error: {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
}
};
// Initializes the `site` service on path `/site`
const createService = require('./site.class.js');
const hooks = require('./site.hooks');
module.exports = function (app) {
const paginate = app.get('paginate');
const options = {
name: 'site',
paginate
};
// Initialize our service with any options it requires
app.use('/v1/site', createService(options));
// Get our initialized service so that we can register hooks and filters
const service = app.service('v1/site');
service.hooks(hooks);
};
const assert = require('assert');
const app = require('../../src/app');
describe('\'site\' service', () => {
it('registered the service', () => {
const service = app.service('site');
assert.ok(service, 'Registered the service');
});
});
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment