Skip to content

Commit

Permalink
feat : add new case to run legacy project import handler test
Browse files Browse the repository at this point in the history
  • Loading branch information
angelhigueraacid committed May 17, 2022
1 parent 4be44ef commit 76c0d5b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { UsersProjectsApiEntity } from '@marxan-api/modules/access-control/projects-acl/entity/users-projects.api.entity';
import { ArchiveLocation, ResourceId } from '@marxan/cloning/domain';
import { UserId } from '@marxan/domain-ids';
import {
Expand All @@ -7,15 +8,19 @@ import {
import { FixtureType } from '@marxan/utils/tests/fixture-type';
import { CqrsModule, EventBus, IEvent } from '@nestjs/cqrs';
import { Test } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
import { isLeft, isRight } from 'fp-ts/Either';
import { v4 } from 'uuid';
import { LegacyProjectImportPieceRequested } from '../domain/events/legacy-project-import-piece-requested.event';
import { LegacyProjectImportRequested } from '../domain/events/legacy-project-import-requested.event';
import {
LegacyProjectImport,
legacyProjectImportMissingRequiredFile,
} from '../domain/legacy-project-import/legacy-project-import';
import {
legacyProjectImportNotFound,
LegacyProjectImportRepository,
legacyProjectImportSaveError,
} from '../domain/legacy-project-import/legacy-project-import.repository';
import { LegacyProjectImportMemoryRepository } from '../infra/legacy-project-import-memory.repository';
import {
Expand All @@ -32,23 +37,36 @@ beforeEach(async () => {

it(`runs a legacy project import`, async () => {
await fixtures.GivenAnExistingLegacyProjectImport();
await fixtures.GivenFilesAreUploaded();
await fixtures.GivenAllFilesAreUploaded();
const result = await fixtures.WhenRunningALegacyProjectImport();
await fixtures.ThenStartingLegacyProjectImportIsUpdated(result);
fixtures.ThenLegacyProjectImportRequestedEventIsEmitted();
fixtures.ThenLegacyProjectImportPieceRequestedAreRequested();
});

it(`fails to run when missing uploaded files`, async () => {
fixtures.GivenAnExistingLegacyProjectImport();
fixtures.GivenNotAllFilesAreUploaded();
const result = await fixtures.WhenRunningALegacyProjectImport();
fixtures.ThenNoEventsAreEmitted();
fixtures.ThenMissingUploadedError(result);
await fixtures.ThenLegacyProjectImportIsNotUpdated();
fixtures.ThenMissingUploadedErrorIsReturned(result);
});

it(`fails to run when there is not an existing legacy project import `, async () => {
it(`fails to run when there is not an existing legacy project import`, async () => {
fixtures.GivenNoExistingLegacyProjectImport();
const result = await fixtures.WhenRunningALegacyProjectImport();
fixtures.ThenNoEventsAreEmitted();
fixtures.ThenMissingLegacyProjectImportError(result);
fixtures.ThenMissingLegacyProjectImportErrorIsReturned(result);
});

it(`fails to run when updating legacy project import fails`, async () => {
await fixtures.GivenAnExistingLegacyProjectImport();
await fixtures.GivenAllFilesAreUploaded();
fixtures.GivenUpdatingALegacyProjectImportFails();
const result = await fixtures.WhenRunningALegacyProjectImport();
fixtures.ThenNoEventsAreEmitted();
fixtures.ThenUpdateErrorIsReturned(result);
});

const getFixtures = async () => {
Expand All @@ -59,6 +77,10 @@ const getFixtures = async () => {
provide: LegacyProjectImportRepository,
useClass: LegacyProjectImportMemoryRepository,
},
{
provide: getRepositoryToken(UsersProjectsApiEntity),
useValue: { save: () => {} },
},
RunLegacyProjectImportHandler,
],
}).compile();
Expand Down Expand Up @@ -98,7 +120,8 @@ const getFixtures = async () => {
GivenNoExistingLegacyProjectImport: () => {},
GivenAnExistingLegacyProjectImport: () =>
repo.save(existingLegacyProjectImport),
GivenFilesAreUploaded: () => {
GivenNotAllFilesAreUploaded: () => {},
GivenAllFilesAreUploaded: () => {
allRequiredFiles.forEach((importFile) =>
existingLegacyProjectImport.addFile(importFile),
);
Expand All @@ -113,22 +136,47 @@ const getFixtures = async () => {
ThenNoEventsAreEmitted: () => {
expect(events).toHaveLength(0);
},
ThenMissingLegacyProjectImportError: async (
ThenLegacyProjectImportRequestedEventIsEmitted: () => {
expect(events[0] as LegacyProjectImportRequested).toEqual(
new LegacyProjectImportRequested(
existingLegacyProjectImport.id,
new ResourceId(projectId),
),
);
},
ThenLegacyProjectImportPieceRequestedAreRequested: () => {
expect(
events.slice(1).every((event) => {
return (
event instanceof LegacyProjectImportPieceRequested &&
event.legacyProjectImportId === existingLegacyProjectImport.id
);
}),
);
},
ThenMissingLegacyProjectImportErrorIsReturned: (
result: RunLegacyProjectImportResponse,
) => {
expect(result).toBeDefined();
if (isRight(result)) throw new Error('the handler should have failed');

expect(result.left).toEqual(legacyProjectImportNotFound);
},
ThenMissingUploadedError: async (
ThenMissingUploadedErrorIsReturned: (
result: RunLegacyProjectImportResponse,
) => {
expect(result).toBeDefined();
if (isRight(result)) throw new Error('the handler should have failed');

expect(result.left).toEqual(legacyProjectImportMissingRequiredFile);
},
ThenUpdateErrorIsReturned: (result: RunLegacyProjectImportResponse) => {
expect(result).toBeDefined();
if (isRight(result)) throw new Error('the handler should have failed');

expect(result.left).toEqual(legacyProjectImportSaveError);
},
ThenLegacyProjectImportIsNotUpdated: async () => {
const savedLegacyProjectImport = await repo.find(
new ResourceId(projectId),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { ProjectRoles } from '@marxan-api/modules/access-control/projects-acl/dto/user-role-project.dto';
import { UsersProjectsApiEntity } from '@marxan-api/modules/access-control/projects-acl/entity/users-projects.api.entity';
import { ResourceId } from '@marxan/cloning/domain';
import {
CommandHandler,
EventPublisher,
IInferredCommandHandler,
} from '@nestjs/cqrs';
import { InjectRepository } from '@nestjs/typeorm';
import { isLeft, right } from 'fp-ts/Either';
import { Repository } from 'typeorm';
import { LegacyProjectImportRepository } from '../domain/legacy-project-import/legacy-project-import.repository';
import {
RunLegacyProjectImport,
Expand All @@ -16,6 +20,8 @@ export class RunLegacyProjectImportHandler
implements IInferredCommandHandler<RunLegacyProjectImport> {
constructor(
private readonly legacyProjectImportRepository: LegacyProjectImportRepository,
@InjectRepository(UsersProjectsApiEntity)
private readonly usersRepo: Repository<UsersProjectsApiEntity>,
private readonly eventPublisher: EventPublisher,
) {}

Expand All @@ -32,19 +38,24 @@ export class RunLegacyProjectImportHandler
legacyProjectImportOrError.right,
);

const { ownerId: userId } = legacyProjectImport.toSnapshot();

const result = legacyProjectImport.start();

if (isLeft(result)) return result;

// TODO start transaction for saving the and adding permissions
const legacyProjectImportSaveError = await this.legacyProjectImportRepository.save(
legacyProjectImport,
);

if (isLeft(legacyProjectImportSaveError))
return legacyProjectImportSaveError;

// TODO add permissions
await this.usersRepo.save({
userId,
projectId,
roleName: ProjectRoles.project_owner,
});

legacyProjectImport.commit();

Expand Down

0 comments on commit 76c0d5b

Please sign in to comment.