Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const mongoose = require('mongoose');
  2. const request = require('supertest');
  3. const app = require('../../config/app');
  4. const User = mongoose.model('User');
  5. const Device = mongoose.model('Device');
  6.  
  7. process.env.TEST_SUITE = 'temtum-api-test';
  8.  
  9. const auth = {
  10.   username: 'user',
  11.   password: 'zzzzzZ!1',
  12.   email: 'dragoncoininfo@gmail.com',
  13.   newUsername: 'test1',
  14.   newPassword: 'xxxxxX!1',
  15.   newEmail: 'temtumcoin@gmail.com',
  16.   passwordChanged: 'changed',
  17.   passwordForgot: 'Link for changing password was send to your email.',
  18.   emailChanged: 'Link for changing email was send to your email.',
  19.   emailConfirm: 'Email was confirmed.',
  20.   emailUpdate: 'Email was changed successfully!'
  21. };
  22. /*
  23. let deviceResponces = {
  24.   ipAddress: '127.0.0.1',
  25.   geo: 'Warsaw',
  26.   type: 'mobile',
  27.   name: 'meizu m5',
  28.   os: 'android os',
  29.   status: 'login success',
  30.   deleted: 'Successfully deleted device',
  31.   rejected: 'Cannot find the device you want to delete'
  32. };*/
  33.  
  34. Object.defineProperty(auth, 'header', {
  35.   get() {
  36.     return { Authorization: `Bearer ${this.token}` };
  37.   },
  38.   set(token) {
  39.     this.token = token;
  40.   }
  41. });
  42.  
  43. const addUser = async () =>
  44.   await new User({
  45.     username: auth.username,
  46.     password: auth.password,
  47.     email: auth.email,
  48.     address: 'address',
  49.     privateKey: 'privateKey',
  50.     isDeactivated: false
  51.   }).save();
  52.  
  53. const addDevice = async (user) => {
  54.   await new Device({
  55.     ipAddress: '127.0.0.1',
  56.     geo: 'Warsaw',
  57.     type: 'mobile',
  58.     name: 'meizu m5',
  59.     os: 'android os',
  60.     status: 'login success',
  61.     user: user._id
  62.   }).save();
  63. };
  64.  
  65. const login = async () => {
  66.   const response = await request(app)
  67.     .post('/api/login')
  68.     .send({
  69.       username: auth.username,
  70.       password: auth.password
  71.     });
  72.  
  73.   auth.token = response.body.token;
  74.  
  75.   //console.log("auth.token", auth.token);
  76.  
  77.   return response;
  78. };
  79. /*
  80. const emailChange = async () =>
  81.   await request(app)
  82.     .post('/api/user/change/email')
  83.     .send({
  84.       email: auth.newEmail
  85.     })
  86.     .set(auth.header);*/
  87. /*
  88. const emailConfirm = async (token) =>
  89.   await request(app)
  90.     .post('/api/user/change/email/confirm')
  91.     .send({
  92.       token
  93.     })
  94.     .set(auth.header);*/
  95.  
  96. describe('POST /password/forgot', () => {
  97.   test('should return 200 status', async () => {
  98.     await addUser();
  99.  
  100.     const response = await request(app)
  101.       .post('/api/password/forgot')
  102.       .send({
  103.         email: auth.email
  104.       });
  105.  
  106.     expect(response.text).toBe(auth.passwordForgot);
  107.   });
  108. });
  109.  
  110. describe('POST /login', () => {
  111.   test('should login', async () => {
  112.     await addUser();
  113.     const response = await login();
  114.  
  115.     expect(response.status).toBe(200);
  116.   });
  117. });
  118.  
  119. describe('POST /user/change/name', () => {
  120.   it('should rename existing user successfully', async () => {
  121.     await addUser();
  122.     await login();
  123.  
  124.  
  125.     const response = await request(app)
  126.       .post('/api/user/change/name')
  127.       .send({
  128.         name: auth.newUsername
  129.       })
  130.       .set(auth.header);
  131.  
  132.     expect(response.body.username).toBe(auth.newUsername);
  133.   });
  134. });
  135.  
  136. describe('POST /user/change/password', () => {
  137.   it('should change existing user\'s password', async () => {
  138.     await addUser();
  139.     await login();
  140.  
  141.  
  142.  
  143.  
  144.     const response = await request(app)
  145.       .post('/api/user/change/password')
  146.       .send({
  147.         password: auth.newPassword
  148.       })
  149.       .set(auth.header);
  150.  
  151.     expect(response.text).toBe(auth.passwordChanged);
  152.   });
  153. });
  154. /*
  155. describe('POST /user/change/email/confirm', () => {
  156.   it('should change send confirm link with the token to user\'s new email', async () => {
  157.     await addUser();
  158.     await login();
  159.     await emailChange();
  160.  
  161.     const user = await User.findOne({ username: auth.username });
  162.     const response = await emailConfirm(user.changeEmailToken);
  163.  
  164.     expect(response.text).toBe(auth.emailConfirm);
  165.   });
  166. });
  167.  
  168.  describe('POST /user/change/email', () => {
  169.   it('should change send link with the token to old email', async () => {
  170.    
  171.  
  172.     await addUser();
  173.     await login();
  174.  
  175.    
  176.     const response = await request(app)
  177.       .post('/api/user/change/email')
  178.       .send({
  179.         email: auth.newEmail
  180.       })
  181.       .set(auth.header);
  182.  
  183.    
  184.     expect(response.text).toBe('Link for changing email was send to your email.').then(res => {console.log("@@", res)}).catch(err => {console.log("@@", err)});
  185.  
  186.  
  187.  
  188.   });
  189. });
  190. describe('POST /user/change/email/update', () => {
  191.   it('should change send update link with the token to user\'s new email', async () => {
  192.     await addUser();
  193.     await login();
  194.     await emailChange();
  195.     const user = await User.findOne({
  196.       username: auth.username
  197.     });
  198.     await emailConfirm(user.changeEmailToken);
  199.  
  200.     const response = await request(app)
  201.       .post('/api/user/change/email/update')
  202.       .send({
  203.         token: user.changeEmailToken
  204.       })
  205.       .set(auth.header);
  206.  
  207.     expect(response.text).toBe(auth.emailUpdate);
  208.   });
  209. });*/
  210.  
  211.  
  212. describe('Device add', () => {
  213.   it('should add device', async () => {
  214.  
  215.    
  216.     try {
  217.       const user = await User.findOne({ username: 'piy' });
  218.  
  219.       const addedDevice = await addDevice(user);
  220.      
  221.       expect(addedDevice.ip).toBe('127.0.0.1');
  222.  
  223.     } catch (errr) {
  224.       // console.log("errr", errr)
  225.     }
  226.    
  227.     // await Device.findOne({ ip: addedDevice.ipAddress });
  228.   /*     const response = await request(app)
  229.       .post('/api/user/device/delete')
  230.       .send({ id: addedDevice.id });
  231.  
  232.     console.log('response.text', response.text);
  233.     console.log('deviceResponces.deleted', deviceResponces.deleted);
  234.     expect(response.text).toBe(deviceResponces.deleted); */
  235.   });
  236. });
  237.  
  238. describe('POST /device/delete', () => {
  239.   it('should delete device', async() => {
  240.     // let device = await Device.findOne({ name: "Other"});
  241.     // console.log("Device", Device)
  242.    
  243.     /*   console.log("device", device)
  244.     const response = await request(app)
  245.       .post('/api/user/device/delete')
  246.       .send({ id: device._id }); */
  247.  
  248.     //expect(device.type).toBe("phone");
  249.   });
  250. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement