2016-11-14 16:16:51 +03:00
|
|
|
/* eslint-disable camelcase */
|
2017-01-02 21:49:44 +03:00
|
|
|
import {Response} from 'ember-cli-mirage';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {isBlank} from '@ember/utils';
|
2016-08-17 18:14:33 +03:00
|
|
|
|
|
|
|
export default function mockAuthentication(server) {
|
2017-09-04 22:17:04 +03:00
|
|
|
server.post('/authentication/token', function () {
|
|
|
|
// Password sign-in
|
|
|
|
return {
|
|
|
|
access_token: 'MirageAccessToken',
|
|
|
|
expires_in: 172800,
|
|
|
|
refresh_token: 'MirageRefreshToken',
|
|
|
|
token_type: 'Bearer'
|
|
|
|
};
|
2016-08-17 18:14:33 +03:00
|
|
|
});
|
|
|
|
|
2017-01-02 21:49:44 +03:00
|
|
|
server.post('/authentication/passwordreset', function (schema, request) {
|
2016-09-26 19:59:04 +03:00
|
|
|
let {passwordreset} = JSON.parse(request.requestBody);
|
2016-11-14 16:16:51 +03:00
|
|
|
// eslint-disable-next-line ember-suave/prefer-destructuring
|
2016-08-17 18:14:33 +03:00
|
|
|
let email = passwordreset[0].email;
|
|
|
|
|
|
|
|
if (email === 'unknown@example.com') {
|
2017-01-02 21:49:44 +03:00
|
|
|
return new Response(404, {}, {
|
2016-08-17 18:14:33 +03:00
|
|
|
errors: [
|
|
|
|
{
|
|
|
|
message: 'There is no user with that email address.',
|
|
|
|
errorType: 'NotFoundError'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
passwordreset: [
|
|
|
|
{message: 'Check your email for further instructions.'}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-01-02 21:49:44 +03:00
|
|
|
server.get('/authentication/invitation/', function (schema, request) {
|
2016-09-30 14:43:40 +03:00
|
|
|
let {email} = request.queryParams;
|
2017-01-02 21:49:44 +03:00
|
|
|
let invite = schema.invites.findBy({email});
|
|
|
|
let user = schema.users.find(invite.createdBy);
|
2016-09-30 14:43:40 +03:00
|
|
|
let valid = !!invite;
|
|
|
|
let invitedBy = user && user.name;
|
|
|
|
|
|
|
|
return {
|
|
|
|
invitation: [{
|
|
|
|
valid,
|
|
|
|
invitedBy
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2016-08-17 18:14:33 +03:00
|
|
|
/* Setup ---------------------------------------------------------------- */
|
|
|
|
|
2017-09-28 05:33:00 +03:00
|
|
|
server.post('/authentication/setup', function ({roles, users}) {
|
|
|
|
let attrs = this.normalizedRequestAttrs();
|
2017-01-02 21:49:44 +03:00
|
|
|
let role = roles.findBy({name: 'Owner'});
|
2016-08-17 18:14:33 +03:00
|
|
|
|
|
|
|
// create owner role unless already exists
|
|
|
|
if (!role) {
|
2017-01-02 21:49:44 +03:00
|
|
|
role = roles.create({name: 'Owner'});
|
2016-08-17 18:14:33 +03:00
|
|
|
}
|
|
|
|
attrs.roles = [role];
|
|
|
|
|
|
|
|
if (!isBlank(attrs.email)) {
|
|
|
|
attrs.slug = attrs.email.split('@')[0].dasherize();
|
|
|
|
}
|
|
|
|
|
2017-01-02 21:49:44 +03:00
|
|
|
// NOTE: server does not use the user factory to fill in blank fields
|
|
|
|
return users.create(attrs);
|
2016-08-17 18:14:33 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
server.get('/authentication/setup/', function () {
|
|
|
|
return {
|
|
|
|
setup: [
|
|
|
|
{status: true}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|