Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const mongoose = require('mongoose');
- const request = require('supertest');
- const app = require('../../config/app');
- const User = mongoose.model('User');
- const Device = mongoose.model('Device');
- process.env.TEST_SUITE = 'temtum-api-test';
- const auth = {
- username: 'user',
- password: 'zzzzzZ!1',
- email: 'dragoncoininfo@gmail.com',
- newUsername: 'test1',
- newPassword: 'xxxxxX!1',
- newEmail: 'temtumcoin@gmail.com',
- passwordChanged: 'changed',
- passwordForgot: 'Link for changing password was send to your email.',
- emailChanged: 'Link for changing email was send to your email.',
- emailConfirm: 'Email was confirmed.',
- emailUpdate: 'Email was changed successfully!'
- };
- /*
- let deviceResponces = {
- ipAddress: '127.0.0.1',
- geo: 'Warsaw',
- type: 'mobile',
- name: 'meizu m5',
- os: 'android os',
- status: 'login success',
- deleted: 'Successfully deleted device',
- rejected: 'Cannot find the device you want to delete'
- };*/
- Object.defineProperty(auth, 'header', {
- get() {
- return { Authorization: `Bearer ${this.token}` };
- },
- set(token) {
- this.token = token;
- }
- });
- const addUser = async () =>
- await new User({
- username: auth.username,
- password: auth.password,
- email: auth.email,
- address: 'address',
- privateKey: 'privateKey',
- isDeactivated: false
- }).save();
- const addDevice = async (user) => {
- await new Device({
- ipAddress: '127.0.0.1',
- geo: 'Warsaw',
- type: 'mobile',
- name: 'meizu m5',
- os: 'android os',
- status: 'login success',
- user: user._id
- }).save();
- };
- const login = async () => {
- const response = await request(app)
- .post('/api/login')
- .send({
- username: auth.username,
- password: auth.password
- });
- auth.token = response.body.token;
- //console.log("auth.token", auth.token);
- return response;
- };
- /*
- const emailChange = async () =>
- await request(app)
- .post('/api/user/change/email')
- .send({
- email: auth.newEmail
- })
- .set(auth.header);*/
- /*
- const emailConfirm = async (token) =>
- await request(app)
- .post('/api/user/change/email/confirm')
- .send({
- token
- })
- .set(auth.header);*/
- describe('POST /password/forgot', () => {
- test('should return 200 status', async () => {
- await addUser();
- const response = await request(app)
- .post('/api/password/forgot')
- .send({
- email: auth.email
- });
- expect(response.text).toBe(auth.passwordForgot);
- });
- });
- describe('POST /login', () => {
- test('should login', async () => {
- await addUser();
- const response = await login();
- expect(response.status).toBe(200);
- });
- });
- describe('POST /user/change/name', () => {
- it('should rename existing user successfully', async () => {
- await addUser();
- await login();
- const response = await request(app)
- .post('/api/user/change/name')
- .send({
- name: auth.newUsername
- })
- .set(auth.header);
- expect(response.body.username).toBe(auth.newUsername);
- });
- });
- describe('POST /user/change/password', () => {
- it('should change existing user\'s password', async () => {
- await addUser();
- await login();
- const response = await request(app)
- .post('/api/user/change/password')
- .send({
- password: auth.newPassword
- })
- .set(auth.header);
- expect(response.text).toBe(auth.passwordChanged);
- });
- });
- /*
- describe('POST /user/change/email/confirm', () => {
- it('should change send confirm link with the token to user\'s new email', async () => {
- await addUser();
- await login();
- await emailChange();
- const user = await User.findOne({ username: auth.username });
- const response = await emailConfirm(user.changeEmailToken);
- expect(response.text).toBe(auth.emailConfirm);
- });
- });
- describe('POST /user/change/email', () => {
- it('should change send link with the token to old email', async () => {
- await addUser();
- await login();
- const response = await request(app)
- .post('/api/user/change/email')
- .send({
- email: auth.newEmail
- })
- .set(auth.header);
- expect(response.text).toBe('Link for changing email was send to your email.').then(res => {console.log("@@", res)}).catch(err => {console.log("@@", err)});
- });
- });
- describe('POST /user/change/email/update', () => {
- it('should change send update link with the token to user\'s new email', async () => {
- await addUser();
- await login();
- await emailChange();
- const user = await User.findOne({
- username: auth.username
- });
- await emailConfirm(user.changeEmailToken);
- const response = await request(app)
- .post('/api/user/change/email/update')
- .send({
- token: user.changeEmailToken
- })
- .set(auth.header);
- expect(response.text).toBe(auth.emailUpdate);
- });
- });*/
- describe('Device add', () => {
- it('should add device', async () => {
- try {
- const user = await User.findOne({ username: 'piy' });
- const addedDevice = await addDevice(user);
- expect(addedDevice.ip).toBe('127.0.0.1');
- } catch (errr) {
- // console.log("errr", errr)
- }
- // await Device.findOne({ ip: addedDevice.ipAddress });
- /* const response = await request(app)
- .post('/api/user/device/delete')
- .send({ id: addedDevice.id });
- console.log('response.text', response.text);
- console.log('deviceResponces.deleted', deviceResponces.deleted);
- expect(response.text).toBe(deviceResponces.deleted); */
- });
- });
- describe('POST /device/delete', () => {
- it('should delete device', async() => {
- // let device = await Device.findOne({ name: "Other"});
- // console.log("Device", Device)
- /* console.log("device", device)
- const response = await request(app)
- .post('/api/user/device/delete')
- .send({ id: device._id }); */
- //expect(device.type).toBe("phone");
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement