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

describe('image upload()', () => {
    describe('as buyer', () => {
        const buyerId = 35;
        const workerId = 1106;
        const itemId = 4;

        it('should try to upload a file for the collection "catalog" and throw a privileges error', async() => {
            const ctx = {req: {accessToken: {userId: buyerId}},
                args: {
                    id: workerId,
                    collection: 'user'
                }
            };

            let error;
            try {
                await app.models.Image.upload(ctx);
            } catch (err) {
                error = err;
            }

            expect(error.message).toEqual(`You don't have enough privileges`);
        });

        it('should call to the TempContainer upload method for the collection "catalog"', async() => {
            const containerModel = app.models.TempContainer;
            spyOn(containerModel, 'upload');

            const ctx = {req: {accessToken: {userId: buyerId}},
                args: {
                    id: itemId,
                    collection: 'catalog'
                }
            };

            try {
                await app.models.Image.upload(ctx);
            } catch (err) { }

            expect(containerModel.upload).toHaveBeenCalled();
        });
    });

    describe('as marketing', () => {
        const marketingId = 51;
        const workerId = 1106;
        const itemId = 4;

        it('should be able to call to the TempContainer upload method for the collection "user"', async() => {
            const containerModel = app.models.TempContainer;
            spyOn(containerModel, 'upload');

            const ctx = {req: {accessToken: {userId: marketingId}},
                args: {
                    id: workerId,
                    collection: 'user'
                }
            };

            try {
                await app.models.Image.upload(ctx);
            } catch (err) { }

            expect(containerModel.upload).toHaveBeenCalled();
        });

        it('should be able to call to the TempContainer upload method for the collection "catalog"', async() => {
            const containerModel = app.models.TempContainer;
            spyOn(containerModel, 'upload');

            const ctx = {req: {accessToken: {userId: marketingId}},
                args: {
                    id: itemId,
                    collection: 'catalog'
                }
            };

            try {
                await app.models.Image.upload(ctx);
            } catch (err) { }

            expect(containerModel.upload).toHaveBeenCalled();
        });
    });

    describe('as hhrr', () => {
        const hhrrId = 37;
        const workerId = 1106;
        const itemId = 4;

        it('should upload a file for the collection "user" and call to the TempContainer upload method', async() => {
            const containerModel = app.models.TempContainer;
            spyOn(containerModel, 'upload');

            const ctx = {req: {accessToken: {userId: hhrrId}},
                args: {
                    id: itemId,
                    collection: 'user'
                }
            };

            try {
                await app.models.Image.upload(ctx);
            } catch (err) { }

            expect(containerModel.upload).toHaveBeenCalled();
        });

        it('should try to upload a file for the collection "catalog" and throw a privilege error', async() => {
            const ctx = {req: {accessToken: {userId: hhrrId}},
                args: {
                    id: workerId,
                    collection: 'catalog'
                }
            };

            let error;
            try {
                await app.models.Image.upload(ctx);
            } catch (err) {
                error = err;
            }

            expect(error.message).toEqual(`You don't have enough privileges`);
        });
    });
});