62 lines
1.7 KiB
JavaScript
62 lines
1.7 KiB
JavaScript
const models = require('vn-loopback/server/server').models;
|
|
const LoopBackContext = require('loopback-context');
|
|
|
|
describe('loopback model Account', () => {
|
|
const userId = 1105;
|
|
const activeCtx = {
|
|
accessToken: {userId: userId},
|
|
http: {
|
|
req: {
|
|
headers: {origin: 'http://localhost'},
|
|
[`__`]: value => {
|
|
return value;
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
beforeEach(() => {
|
|
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
|
|
active: activeCtx
|
|
});
|
|
});
|
|
|
|
it('should return true if the user has the given role', async() => {
|
|
let result = await models.Account.hasRole(1, 'employee');
|
|
|
|
expect(result).toBeTruthy();
|
|
});
|
|
|
|
it('should return false if the user doesnt have the given role', async() => {
|
|
let result = await models.Account.hasRole(1, 'administrator');
|
|
|
|
expect(result).toBeFalsy();
|
|
});
|
|
|
|
it('should send email when change email', async() => {
|
|
const tx = await models.Account.beginTransaction({});
|
|
const newEmail = 'emailNotVerified@mydomain.com';
|
|
|
|
let lastEmail;
|
|
try {
|
|
const options = {transaction: tx};
|
|
|
|
const account = await models.Account.findById(userId, null, options);
|
|
await account.updateAttribute('email', newEmail, options);
|
|
|
|
[lastEmail] = await models.Mail.find({
|
|
where: {
|
|
receiver: newEmail
|
|
}
|
|
}, options);
|
|
|
|
await tx.rollback();
|
|
} catch (e) {
|
|
await tx.rollback();
|
|
throw e;
|
|
}
|
|
|
|
expect(lastEmail.receiver).toEqual(newEmail);
|
|
});
|
|
});
|