const app = require('vn-loopback/server/server'); describe('image upload()', () => { afterEach(() => { // RESTORE NODE ENV delete process.env.NODE_ENV; }); describe('as buyer', () => { const buyerId = 35; const workerId = 106; 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 ImageContainer upload method for the collection "catalog"', async() => { const containerModel = app.models.ImageContainer; 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(); }); it('should throw an error to upload a photo on test environment', async() => { process.env.NODE_ENV = 'test'; const ctx = {req: {accessToken: {userId: buyerId}}, args: { id: itemId, collection: 'catalog' } }; let error; try { await app.models.Image.upload(ctx); } catch (err) { error = err; } expect(error.message).toEqual(`You can't upload images on the test instance`); }); }); describe('as marketing', () => { const marketingId = 51; const workerId = 106; const itemId = 4; it('should be able to call to the ImageContainer upload method for the collection "user"', async() => { const containerModel = app.models.ImageContainer; 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 ImageContainer upload method for the collection "catalog"', async() => { const containerModel = app.models.ImageContainer; 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 = 106; const itemId = 4; it('should upload a file for the collection "user" and call to the ImageContainer upload method', async() => { const containerModel = app.models.ImageContainer; 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`); }); }); });