import {
  getUserById, createUser, setUserProfile,
  ERR_EMAIL_ALREADY_EXISTS } from '../utils/userUtils';
import { setUserGroup, createGroup } from '../utils/groupUtils';
import { ERR_TASK_FAILED } from './constants';

function createTestUser() {
  return createUser({
    email: 'test@test.com',
    password: 'test',
  });
}


describe('User utils test: ', () => {

  it('should create a user', async () => {
    let testUser;
    try {
      testUser = await createTestUser();
      const theUser = await getUserById(testUser.get('id'));
      if (!theUser) throw ERR_TASK_FAILED;
    } catch (err) {
      throw err;
    } finally {
      if (testUser) await testUser.destroy();
    }
  });

  it('should throw a email exist err', async () => {
    let testUser;
    try {
      testUser = await createTestUser();
      await createUser({
        email: 'test@test.com',
        password: 'test',
      });
    } catch (err) {
      if (err !== ERR_EMAIL_ALREADY_EXISTS) throw err;
    } finally {
      if (testUser) testUser.destroy();
    }
  });

  it('should set testUser displayName to test', async () => {
    let testUser;
    try {
      testUser = await createTestUser();
      await setUserProfile({
        id: testUser.get('id'),
        displayName: 'test',
      });
      const theUser = await getUserById(testUser.get('id'));
      const userProfile = await theUser.getProfile();
      if (userProfile.get('displayName') !== 'test') throw ERR_TASK_FAILED;
    } catch (err) {
      throw err;
    } finally {
      if (testUser) await testUser.destroy();
    }
  });

  it('should set test user to test group', async () => {
    let testUser;
    let testGroup;
    try {
      testGroup = await createGroup('test');
      testUser = await createTestUser();
      await setUserGroup(testUser, testGroup);

      if (!await testGroup.hasMember(testUser)) {
        throw ERR_TASK_FAILED;
      }

    } catch (err) {
      throw err;
    } finally {
      if (testUser) await testUser.destroy();
      if (testGroup) await testGroup.destroy();
    }
  });

});

@@ -214,16 +214,98 @@
 ants';%0A%0A
+const EMAIL = 'test@test.com';%0Aconst USERNAME = 'test';%0Aconst PASSWORD = 'test';%0A%0A
 function
@@ -353,39 +353,53 @@
 %0A    email: 
-'test@test.com'
+EMAIL,%0A    username: USERNAME
 ,%0A    passwo
@@ -398,30 +398,32 @@
   password: 
-'test'
+PASSWORD
 ,%0A  %7D);%0A%7D%0A%0A%0A
@@ -421,17 +421,16 @@
  %7D);%0A%7D%0A%0A
-%0A
 describe
@@ -780,32 +780,337 @@
 );%0A    %7D%0A  %7D);%0A%0A
+  it(%60should username equal to $%7BUSERNAME%7D%60, async () =%3E %7B%0A    let testUser;%0A    try %7B%0A      testUser = await createTestUser();%0A      if (testUser.username !== USERNAME) throw ERR_TASK_FAILED;%0A    %7D catch (err) %7B%0A      throw err;%0A    %7D finally %7B%0A      if (testUser) await testUser.destroy();%0A    %7D%0A  %7D);%0A%0A
   it('should thr
@@ -1418,16 +1418,22 @@
 estUser)
+ await
  testUse

Add username test