Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(server): user e2e: wait for user delete event #7799

Merged
merged 3 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions e2e/src/api/specs/asset.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ describe('/asset', () => {
},
});

await utils.waitForWebsocketEvent({ event: 'upload', assetId: locationAsset.id });
await utils.waitForWebsocketEvent({ event: 'assetUpload', id: locationAsset.id });

user1Assets = await Promise.all([
utils.createAsset(user1.accessToken),
Expand Down Expand Up @@ -693,7 +693,7 @@ describe('/asset', () => {

expect(duplicate).toBe(false);

await utils.waitForWebsocketEvent({ event: 'upload', assetId: id });
await utils.waitForWebsocketEvent({ event: 'assetUpload', id: id });

const asset = await utils.getAssetInfo(admin.accessToken, id);

Expand Down Expand Up @@ -795,7 +795,7 @@ describe('/asset', () => {
},
});

await utils.waitForWebsocketEvent({ event: 'upload', assetId: response.id });
await utils.waitForWebsocketEvent({ event: 'assetUpload', id: response.id });

expect(response.duplicate).toBe(false);

Expand All @@ -822,8 +822,8 @@ describe('/asset', () => {
.set('Authorization', `Bearer ${admin.accessToken}`);

await utils.waitForWebsocketEvent({
event: 'upload',
assetId: locationAsset.id,
event: 'assetUpload',
id: locationAsset.id,
});

expect(status).toBe(200);
Expand Down
4 changes: 1 addition & 3 deletions e2e/src/api/specs/search.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe('/search', () => {
}

for (const asset of assets) {
await utils.waitForWebsocketEvent({ event: 'upload', assetId: asset.id });
await utils.waitForWebsocketEvent({ event: 'assetUpload', id: asset.id });
}

[
Expand Down Expand Up @@ -325,11 +325,9 @@ describe('/search', () => {
.post('/search/metadata')
.send(dto)
.set('Authorization', `Bearer ${admin.accessToken}`);
console.dir({ status, body }, { depth: 10 });
expect(status).toBe(200);
expect(body.assets).toBeDefined();
expect(Array.isArray(body.assets.items)).toBe(true);
console.log({ assets: body.assets.items });
for (const [i, asset] of assets.entries()) {
expect(body.assets.items[i]).toEqual(expect.objectContaining({ id: asset.id }));
}
Expand Down
2 changes: 1 addition & 1 deletion e2e/src/api/specs/trash.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('/trash', () => {
const { status } = await request(app).post('/trash/empty').set('Authorization', `Bearer ${admin.accessToken}`);
expect(status).toBe(204);

await utils.waitForWebsocketEvent({ event: 'delete', assetId });
await utils.waitForWebsocketEvent({ event: 'assetDelete', id: assetId });

const after = await getAllAssets({}, { headers: asBearerAuth(admin.accessToken) });
expect(after.length).toBe(0);
Expand Down
47 changes: 38 additions & 9 deletions e2e/src/api/specs/user.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,37 @@
import { LoginResponseDto, deleteUser, getUserById } from '@immich/sdk';
import { Socket } from 'socket.io-client';
import { createUserDto, userDto } from 'src/fixtures';
import { errorDto } from 'src/responses';
import { app, asBearerAuth, utils } from 'src/utils';
import request from 'supertest';
import { beforeAll, describe, expect, it } from 'vitest';
import { afterAll, beforeAll, describe, expect, it } from 'vitest';

describe('/user', () => {
let websocket: Socket;

describe('/server-info', () => {
let admin: LoginResponseDto;
let deletedUser: LoginResponseDto;
let userToDelete: LoginResponseDto;
let userToHardDelete: LoginResponseDto;
let nonAdmin: LoginResponseDto;

beforeAll(async () => {
await utils.resetDatabase();
admin = await utils.adminSetup({ onboarding: false });

[deletedUser, nonAdmin, userToDelete] = await Promise.all([
[websocket, deletedUser, nonAdmin, userToDelete, userToHardDelete] = await Promise.all([
utils.connectWebsocket(admin.accessToken),
utils.userSetup(admin.accessToken, createUserDto.user1),
utils.userSetup(admin.accessToken, createUserDto.user2),
utils.userSetup(admin.accessToken, createUserDto.user3),
utils.userSetup(admin.accessToken, createUserDto.user4),
]);

await deleteUser({ id: deletedUser.userId }, { headers: asBearerAuth(admin.accessToken) });
await deleteUser({ id: deletedUser.userId, deleteUserDto: {} }, { headers: asBearerAuth(admin.accessToken) });
});

afterAll(() => {
utils.disconnectWebsocket(websocket);
});

describe('GET /user', () => {
Expand All @@ -34,13 +44,14 @@ describe('/server-info', () => {
it('should get users', async () => {
const { status, body } = await request(app).get('/user').set('Authorization', `Bearer ${admin.accessToken}`);
expect(status).toEqual(200);
expect(body).toHaveLength(4);
expect(body).toHaveLength(5);
expect(body).toEqual(
expect.arrayContaining([
expect.objectContaining({ email: 'admin@immich.cloud' }),
expect.objectContaining({ email: 'user1@immich.cloud' }),
expect.objectContaining({ email: 'user2@immich.cloud' }),
expect.objectContaining({ email: 'user3@immich.cloud' }),
expect.objectContaining({ email: 'user4@immich.cloud' }),
]),
);
});
Expand All @@ -51,12 +62,13 @@ describe('/server-info', () => {
.query({ isAll: true })
.set('Authorization', `Bearer ${admin.accessToken}`);
expect(status).toBe(200);
expect(body).toHaveLength(3);
expect(body).toHaveLength(4);
expect(body).toEqual(
expect.arrayContaining([
expect.objectContaining({ email: 'admin@immich.cloud' }),
expect.objectContaining({ email: 'user2@immich.cloud' }),
expect.objectContaining({ email: 'user3@immich.cloud' }),
expect.objectContaining({ email: 'user4@immich.cloud' }),
]),
);
});
Expand All @@ -68,13 +80,14 @@ describe('/server-info', () => {
.set('Authorization', `Bearer ${admin.accessToken}`);

expect(status).toBe(200);
expect(body).toHaveLength(4);
expect(body).toHaveLength(5);
expect(body).toEqual(
expect.arrayContaining([
expect.objectContaining({ email: 'admin@immich.cloud' }),
expect.objectContaining({ email: 'user1@immich.cloud' }),
expect.objectContaining({ email: 'user2@immich.cloud' }),
expect.objectContaining({ email: 'user3@immich.cloud' }),
expect.objectContaining({ email: 'user4@immich.cloud' }),
]),
);
});
Expand Down Expand Up @@ -138,13 +151,13 @@ describe('/server-info', () => {
.post(`/user`)
.send({
isAdmin: true,
email: 'user4@immich.cloud',
email: 'user5@immich.cloud',
password: 'password123',
name: 'Immich',
})
.set('Authorization', `Bearer ${admin.accessToken}`);
expect(body).toMatchObject({
email: 'user4@immich.cloud',
email: 'user5@immich.cloud',
isAdmin: false,
shouldChangePassword: true,
});
Expand Down Expand Up @@ -188,6 +201,22 @@ describe('/server-info', () => {
deletedAt: expect.any(String),
});
});

it('should hard delete user', async () => {
const { status, body } = await request(app)
.delete(`/user/${userToHardDelete.userId}`)
.send({ force: true })
.set('Authorization', `Bearer ${admin.accessToken}`);

expect(status).toBe(200);
expect(body).toMatchObject({
id: userToHardDelete.userId,
updatedAt: expect.any(String),
deletedAt: expect.any(String),
});

await utils.waitForWebsocketEvent({ event: 'userDelete', id: userToHardDelete.userId, timeout: 5000 });
});
});

describe('PUT /user', () => {
Expand Down
7 changes: 6 additions & 1 deletion e2e/src/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const createUserDto = {
create(key: string) {
return {
email: `${key}@immich.cloud`,
name: `User ${key}`,
name: `Generated User ${key}`,
password: `password-${key}`,
};
},
Expand All @@ -43,6 +43,11 @@ export const createUserDto = {
name: 'User 3',
password: 'password123',
},
user4: {
email: 'user4@immich.cloud',
name: 'User 4',
password: 'password123',
},
userQuota: {
email: 'user-quota@immich.cloud',
name: 'User Quota',
Expand Down
30 changes: 16 additions & 14 deletions e2e/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ import { makeRandomImage } from 'src/generators';
import request from 'supertest';

type CliResponse = { stdout: string; stderr: string; exitCode: number | null };
type EventType = 'upload' | 'delete';
type WaitOptions = { event: EventType; assetId: string; timeout?: number };
type EventType = 'assetUpload' | 'assetDelete' | 'userDelete';
type WaitOptions = { event: EventType; id: string; timeout?: number };
type AdminSetupOptions = { onboarding?: boolean };
type AssetData = { bytes?: Buffer; filename: string };

Expand Down Expand Up @@ -78,20 +78,21 @@ export const immichCli = async (args: string[]) => {
let client: pg.Client | null = null;

const events: Record<EventType, Set<string>> = {
upload: new Set<string>(),
delete: new Set<string>(),
assetUpload: new Set<string>(),
assetDelete: new Set<string>(),
userDelete: new Set<string>(),
};

const callbacks: Record<string, () => void> = {};

const execPromise = promisify(exec);

const onEvent = ({ event, assetId }: { event: EventType; assetId: string }) => {
events[event].add(assetId);
const callback = callbacks[assetId];
const onEvent = ({ event, id }: { event: EventType; id: string }) => {
events[event].add(id);
const callback = callbacks[id];
if (callback) {
callback();
delete callbacks[assetId];
delete callbacks[id];
}
};

Expand Down Expand Up @@ -166,8 +167,9 @@ export const utils = {
return new Promise<Socket>((resolve) => {
websocket
.on('connect', () => resolve(websocket))
.on('on_upload_success', (data: AssetResponseDto) => onEvent({ event: 'upload', assetId: data.id }))
.on('on_asset_delete', (assetId: string) => onEvent({ event: 'delete', assetId }))
.on('on_upload_success', (data: AssetResponseDto) => onEvent({ event: 'assetUpload', id: data.id }))
.on('on_asset_delete', (assetId: string) => onEvent({ event: 'assetDelete', id: assetId }))
.on('on_user_delete', (userId: string) => onEvent({ event: 'userDelete', id: userId }))
.connect();
});
},
Expand All @@ -182,17 +184,17 @@ export const utils = {
}
},

waitForWebsocketEvent: async ({ event, assetId, timeout: ms }: WaitOptions): Promise<void> => {
console.log(`Waiting for ${event} [${assetId}]`);
waitForWebsocketEvent: async ({ event, id, timeout: ms }: WaitOptions): Promise<void> => {
console.log(`Waiting for ${event} [${id}]`);
const set = events[event];
if (set.has(assetId)) {
if (set.has(id)) {
return;
}

return new Promise<void>((resolve, reject) => {
const timeout = setTimeout(() => reject(new Error(`Timed out waiting for ${event} event`)), ms || 10_000);

callbacks[assetId] = () => {
callbacks[id] = () => {
clearTimeout(timeout);
resolve();
};
Expand Down
Loading