import 'mocha';
import { expect } from 'chai';
import { genCode, generateNum } from '../../app/pubUtils/util';
const request = require('request');
const crypto = require('crypto');

describe('注册登录测试', function() {
  const ENCRYPT_IV = 'f7182j5f04e377ux';
  const ENCRYPT_KEY = 'fiqaxijabbantusmprc234fj';

  it('批量注册账号1', function(done) {
    const accountNum = 10;
    let sendNum = 0;
    let resultNum = 0;
    const monTimer = setInterval(() => {
      if (resultNum === accountNum) {
        done();
        clearInterval(monTimer);
      }
    }, 20);
    const params = [];
    for (let i = 0; i < accountNum; i++) {
      const tel = `13${generateNum(9)}`;
      const param = {tel, deviceId: genCode(10), code: '', platform: 'ios', pkgName: 'com.bantu.zyz', serverType: 'dev'};
      const paramStr = aesEncrypt(JSON.stringify(param), ENCRYPT_KEY, ENCRYPT_IV);
      params.push(paramStr);
    }
    console.log(params.length);
    while(sendNum < accountNum && params[sendNum]) {
      console.log(`sendNum2: ${sendNum}`);
      request.post('http://zyz_monitor_web.trgame.cn/user/smslogin', {
        json: {
          data: params[sendNum]
        }
      }, (err, res, body) => {
        expect(err).to.be.equal(null);
        expect(body.result).to.be.a('string');
        const result = JSON.parse(aesDecrypt(body.result, ENCRYPT_KEY, ENCRYPT_IV));
        expect(result.code).to.be.equal(0);

        request.post('http://zyz_monitor_web.trgame.cn/user/createrole', {
          json: {
            data: aesEncrypt(JSON.stringify({token: result.data.token, serverId: 1, roleName: genCode(8)}), ENCRYPT_KEY, ENCRYPT_IV)
          }
        }, (err, res, body) => {
          console.log(`resultNum2: ${resultNum}`);
          expect(err).to.be.equal(null);
          expect(body.result).to.be.a('string');
          const result = JSON.parse(aesDecrypt(body.result, ENCRYPT_KEY, ENCRYPT_IV));
          expect(result.code).to.be.equal(0);
          resultNum++;
        });
      });
      sendNum++;
    }
  });
});

function aesEncrypt(data, key, iv) {
  const cipher = crypto.createCipheriv('aes-192-cbc', key, iv);
  var crypted = cipher.update(data, 'utf8', 'hex');
  crypted += cipher.final('hex');
  return crypted;
}

function aesDecrypt(data, key, iv) {
  const decipher = crypto.createDecipheriv('aes-192-cbc', key, iv);
  var decrypted = decipher.update(data, 'hex', 'utf8');
  decrypted += decipher.final('utf8');
  return decrypted;
}