Advertisement
Guest User

Untitled

a guest
Aug 28th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const request = require('supertest');
  2. const app = require('../../app');
  3. const userService = require('../../dao/userService');
  4. const grantService = require('../../dao/grantService');
  5. const ruleTypesService = require('../../dao/ruleTypesService');
  6. const createPrivilegeService = require('../../dao/createPrivilegeService');
  7. const Promise = require('bluebird');
  8. const faker = require('faker');
  9.  
  10. describe('Registering user', () => {
  11.   beforeEach((done) => {
  12.     Promise.props({
  13.       addEdit: ruleTypesService.add('edit'),
  14.       addView: ruleTypesService.add('view'),
  15.       addDelete: ruleTypesService.add('delete')
  16.     })
  17.             .then(result =>
  18.                 Promise.props({
  19.                   addEditUser: createPrivilegeService.add({ ruleTypeId: result.addEdit.id, entityType: 'user' }),
  20.                   addViewUser: createPrivilegeService.add({ ruleTypeId: result.addView.id, entityType: 'user' }),
  21.                   addDeleteUser: createPrivilegeService.add({ ruleTypeId: result.addDelete.id, entityType: 'user' })
  22.                 }))
  23.             .then(done);
  24.   });
  25.   afterEach((done) => {
  26.     Promise.all([
  27.       ruleTypesService.destroyAll(),
  28.       createPrivilegeService.destroyAll()
  29.     ])
  30.             .then(done);
  31.   });
  32.   it('should register user and get all his grants', (done) => {
  33.     const reqUser = {
  34.       registration: {
  35.         email: faker.internet.email(),
  36.         password: '123456',
  37.         firstName: faker.name.firstName(),
  38.         lastName: faker.name.lastName()
  39.       }
  40.     };
  41.     request(app)
  42.             .post('/api/registration')
  43.             .send(reqUser)
  44.             .expect(200)
  45.             .then(res => grantService.getUserGrantsByUserId(res.body.registration.id))
  46.             .then((res) => {
  47.               expect(res.length).toEqual(3);
  48.               done();
  49.             });
  50.   });
  51.   it('should successfully register user with status 200', (done) => {
  52.     const reqUser = {
  53.       registration: {
  54.         email: faker.internet.email(),
  55.         password: '123456',
  56.         firstName: faker.name.firstName(),
  57.         lastName: faker.name.lastName()
  58.       }
  59.     };
  60.     request(app)
  61.             .post('/api/registration')
  62.             .send(reqUser)
  63.             .expect(200, done);
  64.   });
  65.   it('should register user and check that id field not null', (done) => {
  66.     const reqUser = {
  67.       registration: {
  68.         email: faker.internet.email(),
  69.         password: '123456',
  70.         firstName: faker.name.firstName(),
  71.         lastName: faker.name.lastName()
  72.       }
  73.     };
  74.     request(app)
  75.             .post('/api/registration')
  76.             .send(reqUser)
  77.             .expect(200)
  78.             .then((res) => {
  79.               expect(res.body.registration.id).not.toBe(null);
  80.               done();
  81.             });
  82.   });
  83.   it('should successfully register and check response.data fields', (done) => {
  84.     const reqEmail = faker.internet.email();
  85.     const reqFirstName = faker.name.firstName();
  86.     const reqLastName = faker.name.lastName();
  87.     const reqUser = {
  88.       registration: {
  89.         email: reqEmail,
  90.         password: '123456',
  91.         firstName: reqFirstName,
  92.         lastName: reqLastName
  93.       }
  94.     };
  95.     request(app)
  96.             .post('/api/registration')
  97.             .send(reqUser)
  98.             .expect(200)
  99.             .then((res) => {
  100.               expect(res.body.registration).toEqual(jasmine.objectContaining({
  101.                 firstName: reqFirstName,
  102.                 lastName: reqLastName
  103.               }));
  104.               expect(Object.keys(res.body.registration)).toContain('id');
  105.               done();
  106.             });
  107.   });
  108.   it('should try to register user but get 400 with specified error msg about email', (done) => {
  109.     const reqUser = {
  110.       registration: {
  111.         email: faker.internet.email(),
  112.         password: faker.internet.userName(),
  113.         firstName: faker.name.firstName(),
  114.         lastName: faker.name.lastName()
  115.       }
  116.     };
  117.     request(app)
  118.             .post('/api/registration')
  119.             .send(reqUser)
  120.             .expect(200)
  121.             .then(() =>
  122.                 request(app)
  123.                     .post('/api/registration')
  124.                     .send(reqUser)
  125.                     .expect(400))
  126.             .then((res) => {
  127.               expect(res.body.errors).toEqual(jasmine.objectContaining({
  128.                 email: 'User with this email already registered',
  129.               }));
  130.               done();
  131.             });
  132.   });
  133. });
  134.  
  135. describe('Logging user', () => {
  136.   beforeEach((done) => {
  137.     this.reqUser = {
  138.       email: faker.internet.email(),
  139.       password: faker.internet.userName(),
  140.       firstName: faker.name.firstName(),
  141.       lastName: faker.name.lastName()
  142.     };
  143.     request(app)
  144.             .post('/api/registration')
  145.             .send({ registration: this.reqUser })
  146.             .expect(200, done);
  147.   });
  148.   afterEach((done) => {
  149.     userService.deleteUserByEmail(this.reqUser.email)
  150.             .then(done);
  151.   });
  152.   it('should successfully log user in with status 200', (done) => {
  153.     request(app)
  154.             .post('/api/login')
  155.             .send({
  156.               username: this.reqUser.email,
  157.               password: this.reqUser.password
  158.             })
  159.             .expect(200)
  160.             .then(done);
  161.   });
  162.   it('should check that token present in response body', (done) => {
  163.     request(app)
  164.             .post('/api/login')
  165.             .send({
  166.               username: this.reqUser.email,
  167.               password: this.reqUser.password
  168.             })
  169.             .expect(200)
  170.             .then((res) => {
  171.               expect(res.body.access_token).not.toBe(null);
  172.               done();
  173.             });
  174.   });
  175.   it('should check res status is 401, wrong pwd', (done) => {
  176.     request(app)
  177.             .post('/api/login')
  178.             .send({
  179.               username: this.reqUser.email,
  180.               password: faker.internet.userName()
  181.             })
  182.             .expect(401, done);
  183.   });
  184.   it('should check res status is 401, wrong email', (done) => {
  185.     request(app)
  186.             .post('/api/login')
  187.             .send({
  188.               username: faker.internet.email(),
  189.               password: this.reqUser.password
  190.             })
  191.             .expect(401, done);
  192.   });
  193. });
  194.  
  195. describe('Working with user info', () => {
  196.   beforeEach((done) => {
  197.     this.reqUser = {
  198.       email: faker.internet.email(),
  199.       password: faker.internet.userName(),
  200.       firstName: faker.name.firstName(),
  201.       lastName: faker.name.lastName()
  202.     };
  203.     Promise.props({
  204.       addEdit: ruleTypesService.add('edit'),
  205.       addView: ruleTypesService.add('view'),
  206.       addDelete: ruleTypesService.add('delete')
  207.     })
  208.             .then(result =>
  209.                 Promise.props({
  210.                   addEditUser: createPrivilegeService.add({ ruleTypeId: result.addEdit.id, entityType: 'user' }),
  211.                   addViewUser: createPrivilegeService.add({ ruleTypeId: result.addView.id, entityType: 'user' }),
  212.                   addDeleteUser: createPrivilegeService.add({ ruleTypeId: result.addDelete.id, entityType: 'user' })
  213.                 }))
  214.             .then(() => request(app)
  215.                 .post('/api/registration')
  216.                 .send({ registration: this.reqUser })
  217.                 .expect(200))
  218.             .then((res) => {
  219.               this.createdUserId = res.body.registration.id;
  220.               return request(app)
  221.                     .post('/api/login')
  222.                     .send({
  223.                       username: this.reqUser.email,
  224.                       password: this.reqUser.password
  225.                     })
  226.                     .expect(200);
  227.             })
  228.             .then((res) => {
  229.               this.userToken = res.body.access_token;
  230.               done();
  231.             });
  232.   });
  233.   afterEach((done) => {
  234.     userService.deleteAll()
  235.             .then(done);
  236.   });
  237.   it('should get user info and response with object containing necessary fields', (done) => {
  238.     request(app)
  239.             .get(`/api/users/${this.createdUserId}`)
  240.             .set({ Authorization: `Bearer ${this.userToken}` })
  241.             .expect(200)
  242.             .then((res) => {
  243.               expect(Object.keys(res.body.users[0])).toContain('firstName');
  244.               expect(Object.keys(res.body.users[0])).toContain('lastName');
  245.               done();
  246.             });
  247.   });
  248.   it('should update user info', (done) => {
  249.     const updatedUser = {
  250.       firstName: faker.name.firstName(),
  251.       lastName: faker.name.lastName(),
  252.       group: 0
  253.     };
  254.     request(app)
  255.             .put(`/api/users/${this.createdUserId}`)
  256.             .set({ Authorization: `Bearer ${this.userToken}` })
  257.             .send({ user: updatedUser })
  258.             .expect(200)
  259.             .then((res) => {
  260.               expect(res.body).toEqual(jasmine.objectContaining({
  261.                 firstName: updatedUser.firstName,
  262.                 lastName: updatedUser.lastName,
  263.                 group: updatedUser.group
  264.               }));
  265.               done();
  266.             });
  267.   });
  268.  
  269.   xit('should retrieve user info as manager').pend('This feature not yet implemented');
  270. })
  271. ;
  272.  
  273. xdescribe('Admin features', () => {
  274.   xit('should promote regular user to manager').pend('Implement admin features');
  275.   xit('should demote manager to regular user').pend('Implement admin features');
  276. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement