-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ProjectRepository.getPersonalProjectForUser[OrFail] not taking t…
…he type or the role of the user to the project into account (#8676)
- Loading branch information
1 parent
674abd8
commit 655ada8
Showing
4 changed files
with
152 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
packages/cli/test/integration/database/repositories/project.repository.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import Container from 'typedi'; | ||
import { createOwner } from '../../shared/db/users'; | ||
import * as testDb from '../../shared/testDb'; | ||
import { ProjectRepository } from '@/databases/repositories/project.repository'; | ||
import { EntityNotFoundError } from '@n8n/typeorm'; | ||
import { createProject } from '../../shared/db/projects'; | ||
|
||
describe('ProjectRepository', () => { | ||
beforeAll(async () => { | ||
await testDb.init(); | ||
}); | ||
|
||
beforeEach(async () => { | ||
await testDb.truncate(['User', 'Workflow', 'Project']); | ||
}); | ||
|
||
afterAll(async () => { | ||
await testDb.terminate(); | ||
}); | ||
|
||
describe('getPersonalProjectForUser', () => { | ||
it('returns the personal project', async () => { | ||
// | ||
// ARRANGE | ||
// | ||
const owner = await createOwner(); | ||
const ownerPersonalProject = await Container.get(ProjectRepository).findOneByOrFail({ | ||
projectRelations: { userId: owner.id }, | ||
}); | ||
|
||
// | ||
// ACT | ||
// | ||
const personalProject = await Container.get(ProjectRepository).getPersonalProjectForUser( | ||
owner.id, | ||
); | ||
|
||
// | ||
// ASSERT | ||
// | ||
if (!personalProject) { | ||
fail('Expected personalProject to be defined.'); | ||
} | ||
expect(personalProject).toBeDefined(); | ||
expect(personalProject.id).toBe(ownerPersonalProject.id); | ||
}); | ||
|
||
it('does not return non personal projects', async () => { | ||
// | ||
// ARRANGE | ||
// | ||
const owner = await createOwner(); | ||
await Container.get(ProjectRepository).delete({}); | ||
await createProject(owner); | ||
|
||
// | ||
// ACT | ||
// | ||
const personalProject = await Container.get(ProjectRepository).getPersonalProjectForUser( | ||
owner.id, | ||
); | ||
|
||
// | ||
// ASSERT | ||
// | ||
expect(personalProject).toBeNull(); | ||
}); | ||
}); | ||
|
||
describe('getPersonalProjectForUserOrFail', () => { | ||
it('returns the personal project', async () => { | ||
// | ||
// ARRANGE | ||
// | ||
const owner = await createOwner(); | ||
const ownerPersonalProject = await Container.get(ProjectRepository).findOneByOrFail({ | ||
projectRelations: { userId: owner.id }, | ||
}); | ||
|
||
// | ||
// ACT | ||
// | ||
const personalProject = await Container.get( | ||
ProjectRepository, | ||
).getPersonalProjectForUserOrFail(owner.id); | ||
|
||
// | ||
// ASSERT | ||
// | ||
if (!personalProject) { | ||
fail('Expected personalProject to be defined.'); | ||
} | ||
expect(personalProject).toBeDefined(); | ||
expect(personalProject.id).toBe(ownerPersonalProject.id); | ||
}); | ||
|
||
it('does not return non personal projects', async () => { | ||
// | ||
// ARRANGE | ||
// | ||
const owner = await createOwner(); | ||
await Container.get(ProjectRepository).delete({}); | ||
await createProject(owner); | ||
|
||
// | ||
// ACT | ||
// | ||
const promise = Container.get(ProjectRepository).getPersonalProjectForUserOrFail(owner.id); | ||
|
||
// | ||
// ASSERT | ||
// | ||
await expect(promise).rejects.toThrowError(EntityNotFoundError); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import Container from 'typedi'; | ||
import { ProjectRepository } from '@/databases/repositories/project.repository'; | ||
import type { DeepPartial } from 'ts-essentials'; | ||
import type { Project } from '@/databases/entities/Project'; | ||
import { ProjectRelationRepository } from '@/databases/repositories/projectRelation.repository'; | ||
import type { User } from '@/databases/entities/User'; | ||
|
||
export async function createProject(user: User, project?: DeepPartial<Project>) { | ||
const projectRepository = Container.get(ProjectRepository); | ||
const projectRelationRepository = Container.get(ProjectRelationRepository); | ||
|
||
const savedProject = await projectRepository.save( | ||
projectRepository.create({ | ||
name: 'project name', | ||
type: 'team', | ||
...project, | ||
}), | ||
); | ||
await projectRelationRepository.save( | ||
projectRelationRepository.create({ | ||
userId: user.id, | ||
projectId: savedProject.id, | ||
role: 'project:personalOwner', | ||
}), | ||
); | ||
|
||
return savedProject; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters