const models = require('vn-loopback/server/server').models;

describe('loopback model NotificationSubscription', () => {
    it('should fail to add a notification subscription if the worker doesnt have ACLs', async() => {
        const tx = await models.NotificationSubscription.beginTransaction({});
        let error;

        try {
            const options = {transaction: tx, accessToken: {userId: 9}};
            await models.NotificationSubscription.create({notificationFk: 1, userFk: 62}, options);

            await tx.rollback();
        } catch (e) {
            await tx.rollback();
            error = e;
        }

        expect(error.message).toEqual('The notification subscription of this worker cant be modified');
    });

    it('should fail to add a notification subscription if the user isnt editing itself or subordinate', async() => {
        const tx = await models.NotificationSubscription.beginTransaction({});
        let error;

        try {
            const options = {transaction: tx, accessToken: {userId: 1}};
            await models.NotificationSubscription.create({notificationFk: 1, userFk: 9}, options);

            await tx.rollback();
        } catch (e) {
            await tx.rollback();
            error = e;
        }

        expect(error.message).toEqual('The notification subscription of this worker cant be modified');
    });

    it('should fail to delete a notification subscription if the user isnt editing itself or subordinate', async() => {
        const tx = await models.NotificationSubscription.beginTransaction({});
        let error;

        try {
            const options = {transaction: tx, accessToken: {userId: 9}};
            await models.NotificationSubscription.destroyAll({id: 2}, options);

            await tx.rollback();
        } catch (e) {
            await tx.rollback();
            error = e;
        }

        expect(error.message).toEqual('The notification subscription of this worker cant be modified');
    });

    it('should add a notification subscription if the user is editing itself', async() => {
        const tx = await models.NotificationSubscription.beginTransaction({});
        let error;

        try {
            const options = {transaction: tx, accessToken: {userId: 9}};
            await models.NotificationSubscription.create({notificationFk: 2, userFk: 9}, options);

            await tx.rollback();
        } catch (e) {
            await tx.rollback();
            error = e;
        }

        expect(error).toBeUndefined();
    });

    it('should delete a notification subscription if the user is editing itself', async() => {
        const tx = await models.NotificationSubscription.beginTransaction({});
        let error;

        try {
            const options = {transaction: tx, accessToken: {userId: 9}};
            await models.NotificationSubscription.destroyAll({id: 6}, options);

            await tx.rollback();
        } catch (e) {
            await tx.rollback();
            error = e;
        }

        expect(error).toBeUndefined();
    });

    it('should add a notification subscription if the user is editing a subordinate', async() => {
        const tx = await models.NotificationSubscription.beginTransaction({});
        let error;

        try {
            const options = {transaction: tx, accessToken: {userId: 9}};
            await models.NotificationSubscription.create({notificationFk: 1, userFk: 5}, options);

            await tx.rollback();
        } catch (e) {
            await tx.rollback();
            error = e;
        }

        expect(error).toBeUndefined();
    });

    it('should delete a notification subscription if the user is editing a subordinate', async() => {
        const tx = await models.NotificationSubscription.beginTransaction({});
        let error;

        try {
            const options = {transaction: tx, accessToken: {userId: 19}};
            await models.NotificationSubscription.destroyAll({id: 4}, options);

            await tx.rollback();
        } catch (e) {
            await tx.rollback();
            error = e;
        }

        expect(error).toBeUndefined();
    });
});