2023-01-24 09:11:16 +00:00
|
|
|
const models = require('vn-loopback/server/server').models;
|
2023-01-18 11:16:47 +00:00
|
|
|
|
|
|
|
describe('loopback model NotificationSubscription', () => {
|
|
|
|
it('Should fail to delete a notification if the user is not editing itself or a subordinate', async() => {
|
2023-01-24 09:11:16 +00:00
|
|
|
const tx = await models.NotificationSubscription.beginTransaction({});
|
2023-01-18 11:16:47 +00:00
|
|
|
|
2023-01-24 09:11:16 +00:00
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
|
|
|
const user = 9;
|
|
|
|
const notificationSubscriptionId = 2;
|
|
|
|
const ctx = {req: {accessToken: {userId: user}}};
|
|
|
|
const notification = await models.NotificationSubscription.findById(notificationSubscriptionId);
|
|
|
|
|
|
|
|
let error;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await models.NotificationSubscription.deleteNotification(ctx, notification.id, options);
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(error.message).toContain('You dont have permission to modify this user');
|
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Should delete a notification if the user is editing itself', async() => {
|
|
|
|
const tx = await models.NotificationSubscription.beginTransaction({});
|
2023-01-18 11:16:47 +00:00
|
|
|
|
|
|
|
try {
|
2023-01-24 09:11:16 +00:00
|
|
|
const options = {transaction: tx};
|
|
|
|
const user = 9;
|
2023-01-24 09:15:32 +00:00
|
|
|
const notificationSubscriptionId = 4;
|
2023-01-24 09:11:16 +00:00
|
|
|
const ctx = {req: {accessToken: {userId: user}}};
|
|
|
|
const notification = await models.NotificationSubscription.findById(notificationSubscriptionId);
|
|
|
|
|
|
|
|
await models.NotificationSubscription.deleteNotification(ctx, notification.id, options);
|
|
|
|
|
|
|
|
const deletedNotification = await models.NotificationSubscription.findById(notificationSubscriptionId);
|
|
|
|
|
|
|
|
expect(deletedNotification).toBeNull();
|
|
|
|
await tx.rollback();
|
2023-01-18 11:16:47 +00:00
|
|
|
} catch (e) {
|
2023-01-24 09:11:16 +00:00
|
|
|
await tx.rollback();
|
|
|
|
throw e;
|
2023-01-18 11:16:47 +00:00
|
|
|
}
|
2023-01-24 09:11:16 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Should delete a notification if the user is editing a subordinate', async() => {
|
|
|
|
const tx = await models.NotificationSubscription.beginTransaction({});
|
|
|
|
|
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
|
|
|
const user = 9;
|
2023-01-24 09:15:32 +00:00
|
|
|
const notificationSubscriptionId = 5;
|
2023-01-24 09:11:16 +00:00
|
|
|
const ctx = {req: {accessToken: {userId: user}}};
|
|
|
|
const notification = await models.NotificationSubscription.findById(notificationSubscriptionId);
|
2023-01-18 11:16:47 +00:00
|
|
|
|
2023-01-24 09:11:16 +00:00
|
|
|
await models.NotificationSubscription.deleteNotification(ctx, notification.id, options);
|
|
|
|
|
|
|
|
const deletedNotification = await models.NotificationSubscription.findById(notificationSubscriptionId);
|
|
|
|
|
|
|
|
expect(deletedNotification).toBeNull();
|
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
2023-01-18 11:16:47 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|