Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
martabal committed Apr 30, 2024
1 parent 7fa0866 commit 1f7dc5b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 16 deletions.
1 change: 0 additions & 1 deletion server/src/dtos/album.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export class CreateAlbumDto {

export class UpdateAlbumOrderDto {
@IsEnum(AssetOrder)
@Optional()
@ApiProperty({ enum: AssetOrder, enumName: 'AssetOrder' })
order!: AssetOrder;
}
Expand Down
2 changes: 1 addition & 1 deletion server/src/interfaces/album-user.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ export type AlbumPermissionId = {

export interface IAlbumUserRepository {
create(albumUser: Partial<AlbumUserEntity>): Promise<AlbumUserEntity>;
update({ userId, albumId }: AlbumPermissionId, albumPermission: Partial<AlbumUserEntity>): Promise<AlbumUserEntity>;
update({ userId, albumId }: AlbumPermissionId, dto: Partial<AlbumUserEntity>): Promise<AlbumUserEntity>;
delete({ userId, albumId }: AlbumPermissionId): Promise<void>;
}
51 changes: 50 additions & 1 deletion server/src/services/album.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BadRequestException } from '@nestjs/common';
import { BadRequestException, ForbiddenException } from '@nestjs/common';
import _ from 'lodash';
import { BulkIdErrorReason } from 'src/dtos/asset-ids.response.dto';
import { AlbumUserRole, AssetOrder } from 'src/entities/album-user.entity';
Expand Down Expand Up @@ -241,6 +241,55 @@ describe(AlbumService.name, () => {
});
});

describe('updateAlbumOrder', () => {
it('should prevent updating if the user is not a logged in user', async () => {
albumMock.getById.mockResolvedValue(null);

await expect(
sut.updateAlbumOrder(authStub.user1, 'invalid-id', {
order: AssetOrder.DESC,
}),
).rejects.toBeInstanceOf(BadRequestException);

expect(albumMock.update).not.toHaveBeenCalled();
});
it('should change the album order', async () => {
accessMock.album.checkOwnerAccess.mockResolvedValue(new Set(['album-3']));
albumMock.getById.mockResolvedValue(albumStub.sharedWithAdmin);
albumMock.update.mockResolvedValue(albumStub.sharedWithAdmin);

await sut.updateAlbumOrder(authStub.user1, albumStub.sharedWithAdmin.id, {
order: AssetOrder.DESC,
});

expect(albumMock.update).toHaveBeenCalledTimes(1);
expect(albumMock.update).toHaveBeenCalledWith({
id: 'album-3',
order: AssetOrder.DESC,
});
});
it('should allow another user to update its album order', async () => {
accessMock.album.checkOwnerAccess.mockResolvedValue(new Set(['album-3']));
albumMock.getById.mockResolvedValue(albumStub.sharedWithAdmin);
albumUserMock.update.mockResolvedValue(albumStub.sharedWithAdmin.albumUsers[0]);

await sut.updateAlbumOrder(authStub.admin, albumStub.sharedWithAdmin.id, {
order: AssetOrder.DESC,
});

expect(albumUserMock.update).toHaveBeenCalledTimes(1);
expect(albumUserMock.update).toHaveBeenCalledWith(
{
albumId: albumStub.sharedWithAdmin.id,
userId: authStub.admin.user.id,
},
{
order: AssetOrder.DESC,
},
);
});
});

describe('update', () => {
it('should prevent updating an album that does not exist', async () => {
albumMock.getById.mockResolvedValue(null);
Expand Down
16 changes: 3 additions & 13 deletions server/src/services/album.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,26 +140,16 @@ export class AlbumService {
async updateAlbumOrder(auth: AuthDto, id: string, dto: UpdateAlbumOrderDto): Promise<UpdateAlbumOrderDto> {
await this.access.requirePermission(auth, Permission.ALBUM_READ, id);

if (auth.sharedLink) {
throw new ForbiddenException("You can't update the album order if you're not an authenticated user");
}

const album = await this.findOrFail(id, { withAssets: false });
let order: AssetOrder | undefined = undefined;
const order = dto.order;
if (album.ownerId === auth.user.id) {
const updatedAlbum = await this.albumRepository.update({
await this.albumRepository.update({
id: album.id,
order: dto.order,
});
order = updatedAlbum.order;
} else {
const updatedAlbum = await this.albumUserRepository.update(
{ userId: auth.user.id, albumId: id },
{ order: dto.order },
);
order = updatedAlbum.order;
await this.albumUserRepository.update({ userId: auth.user.id, albumId: id }, { order: dto.order });
}

return { order };
}

Expand Down

0 comments on commit 1f7dc5b

Please sign in to comment.