<commit_before>import { Test, TestingModule } from '@nestjs/testing';
import { AccountService } from './account.service';
import { ConfigModule } from '@nestjs/config';
import { startContainer, stopContainer } from '../../test/docker-ldap';
import * as winston from 'winston';
import { setupLogger } from '../../test/setup-logger';
import { AliasDoesNotExistError } from '../errors/alias-does-not-exist.error';
import { AliasAlreadyExistsError } from '../errors/alias-already-exists.error';
import { AccountInvalidError } from '../errors/account-invalid.error';

describe('AccountService', () => {
  let service: AccountService;

  beforeEach(async () => {
    setupLogger();
    const stats = await startContainer();
    winston.info('Creating Testmodule');
    const module: TestingModule = await Test.createTestingModule({
      imports: [
        ConfigModule.forRoot({
          load: [
            () => {
              return {
                AM_LDAP_URL: `ldap://localhost:${stats.NetworkSettings.Ports['389/tcp'][0].HostPort}/`,
                AM_LDAP_BIND_DN: 'cn=admin,dc=example,dc=com',
                AM_LDAP_BIND_PW: 'admin',
                AM_LDAP_USER_DN: 'dc=example,dc=com',
                AM_LDAP_USER_ATTR: 'uid',
                AM_LDAP_ALIAS_ATTR: 'registeredAddress',
              };
            },
          ],
        }),
      ],
      providers: [AccountService],
    }).compile();

    service = module.get<AccountService>(AccountService);
  });

  afterEach(async () => {
    await stopContainer();
  });

  it('should be defined', () => {
    expect(service).toBeDefined();
  });

  it('should return all aliases', async () => {
    const aliases = await service.getAliases('user');
    expect(aliases).toHaveLength(1);
    expect(aliases[0]).toStrictEqual({
      address: '<EMAIL>',
    });
  });

  it('should throw when giving a non existent user', async () => {
    try {
      const aliases = await service.getAliases('unknown');
    } catch (e) {
      expect(e.name).toEqual(AccountInvalidError.NAME);
    }
  });

  it('should create a new alias', async () => {
    let aliases = await service.getAliases('user');
    expect(aliases).toHaveLength(1);
    const newAlias = await service.createAlias('user', {
      address: '<EMAIL>',
    });
    expect(newAlias).not.toBeNull();
    expect(newAlias).toStrictEqual({<commit_msg>feat: Completed account service<commit_after>import { Test, TestingModule } from '@nestjs/testing';
import { AccountService } from './account.service';
import { ConfigModule } from '@nestjs/config';
import { startContainer, stopContainer } from '../../test/docker-ldap';
import * as winston from 'winston';
import { setupLogger } from '../../test/setup-logger';
import { AliasDoesNotExistError } from '../errors/alias-does-not-exist.error';
import { AliasAlreadyExistsError } from '../errors/alias-already-exists.error';
import { AccountInvalidError } from '../errors/account-invalid.error';

describe('AccountService', () => {
  let service: AccountService;

  beforeEach(async () => {
    setupLogger();
    const stats = await startContainer();
    winston.info('Creating Testmodule');
    const module: TestingModule = await Test.createTestingModule({
      imports: [
        ConfigModule.forRoot({
          load: [
            () => {
              return {
                AM_LDAP_URL: `ldap://localhost:${stats.NetworkSettings.Ports['389/tcp'][0].HostPort}/`,
                AM_LDAP_BIND_DN: 'cn=admin,dc=example,dc=com',
                AM_LDAP_BIND_PW: '<PASSWORD>',
                AM_LDAP_USER_DN: 'dc=example,dc=com',
                AM_LDAP_USER_ATTR: 'uid',
                AM_LDAP_ALIAS_ATTR: 'registeredAddress',
              };
            },
          ],
        }),
      ],
      providers: [AccountService],
    }).compile();

    service = module.get<AccountService>(AccountService);
  });

  afterEach(async () => {
    await stopContainer();
  });

  it('should be defined', () => {
    expect(service).toBeDefined();
  });

  it('should return all aliases', async () => {
    const aliases = await service.getAliases('user');
    expect(aliases).toHaveLength(1);
    expect(aliases[0]).toStrictEqual({
      address: '<EMAIL>',
    });
  });

  it('should throw when giving a non existent user', async () => {
    try {
      await service.getAliases('unknown');
    } catch (e) {
      expect(e.name).toEqual(AccountInvalidError.NAME);
    }
  });

  it('should create a new alias', async () => {
    let aliases = await service.getAliases('user');
    expect(aliases).toHaveLength(1);
    const newAlias = await service.createAlias('user', {
      address: '<EMAIL>',
    });
    expect(newAlias).not.toBeNull();
    expect(newAlias).toStrictEqual({
{'fraction_non_alphanumeric': 0.15863665053650325, 'fraction_numerical': 0.0029455081001472753, 'mean_word_length': 2.8935298935298936, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1866564', 'n_tokens_mistral': 1433, 'n_tokens_neox': 1398, 'n_words': 266}