sails-hook-blacksails
Version:
A Sails Micro-app architecture framework
67 lines (60 loc) • 1.19 kB
JavaScript
import sinon from 'sinon';
let mock = null;
let mockIsAdmin = null;
module.exports = {
async mockAdmin() {
try {
if (mock) {
mock.restore();
}
if (mockIsAdmin) {
mockIsAdmin.restore();
}
const admin = await User.findOne({
where: {
id: 1,
},
include: [Role],
});
mock = sinon.stub(AuthService, 'getSessionUser', () => admin.toJSON());
mockIsAdmin = sinon.stub(AuthService, 'isAdmin', () => true);
} catch (e) {
throw e;
}
},
unMockAdmin() {
try {
// AuthService.getSessionUser.restore();
if (mock) {
mock.restore();
}
if (mockIsAdmin) {
mockIsAdmin.restore();
}
} catch (e) {
throw e;
}
},
mockLogin(user, deviceToken) {
try {
if (mock) {
mock.restore();
mock = null;
}
mock = sinon.stub(AuthService, 'getSessionUser', () => ({
id: user.id,
deviceToken,
authenticated: true,
}));
} catch (e) {
throw e;
}
},
logout() {
try {
if (mock) mock.restore();
} catch (e) {
throw e;
}
},
};