mattermost/e2e-tests/playwright/specs/client/upload_file.spec.ts
yasser khan b8944f372d
Some checks are pending
API / build (push) Waiting to run
Server CI / Compute Go Version (push) Waiting to run
Server CI / Check mocks (push) Blocked by required conditions
Server CI / Check go mod tidy (push) Blocked by required conditions
Server CI / check-style (push) Blocked by required conditions
Server CI / Check serialization methods for hot structs (push) Blocked by required conditions
Server CI / Vet API (push) Blocked by required conditions
Server CI / Check migration files (push) Blocked by required conditions
Server CI / Generate email templates (push) Blocked by required conditions
Server CI / Check store layers (push) Blocked by required conditions
Server CI / Check mmctl docs (push) Blocked by required conditions
Server CI / Postgres with binary parameters (push) Blocked by required conditions
Server CI / Postgres (push) Blocked by required conditions
Server CI / Postgres (FIPS) (push) Blocked by required conditions
Server CI / Generate Test Coverage (push) Blocked by required conditions
Server CI / Run mmctl tests (push) Blocked by required conditions
Server CI / Run mmctl tests (FIPS) (push) Blocked by required conditions
Server CI / Build mattermost server app (push) Blocked by required conditions
Web App CI / check-lint (push) Waiting to run
Web App CI / check-i18n (push) Waiting to run
Web App CI / check-types (push) Waiting to run
Web App CI / test (push) Waiting to run
Web App CI / build (push) Waiting to run
Feat(e2e): Add tests cases for Content Flagging (#34288)
- E2E tests for Content Flagging 
- Fixes tests failing on master
2025-11-19 10:16:57 +00:00

138 lines
4.8 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {Client4} from '@mattermost/client';
import {ServerChannel} from '@mattermost/types/channels';
import {FileUploadResponse} from '@mattermost/types/files';
import {Team} from '@mattermost/types/teams';
import {UserProfile} from '@mattermost/types/users';
import {expect, test, getFileFromAsset, getBlobFromAsset} from '@mattermost/playwright-lib';
import {FileUploadResponseSchema} from './schema';
let userClient: Client4;
let user: UserProfile;
let team: Team;
let townSquareChannel: ServerChannel;
const filename = 'mattermost-icon_128x128.png';
const file = getFileFromAsset(filename);
const blob = getBlobFromAsset(filename);
test.beforeEach(async ({pw}) => {
({userClient, user, team} = await pw.initSetup());
townSquareChannel = await userClient.getChannelByName(team.id, 'town-square');
});
test('should succeed with File', async ({pw}) => {
// # Prepare data with File
const clientId = await pw.random.id();
const formData = new FormData();
formData.set('channel_id', townSquareChannel.id);
formData.set('client_ids', clientId);
formData.set('files', file, filename);
// # Do upload then validate the response
const data = await userClient.uploadFile(formData);
validateFileUploadResponse(data, clientId, user.id, townSquareChannel.id);
});
test('should succeed with Blob', async ({pw}) => {
// # Prepare data with Blob
const clientId = await pw.random.id();
const formData = new FormData();
formData.set('channel_id', townSquareChannel.id);
formData.set('client_ids', clientId);
formData.set('files', blob, filename);
// # Do upload then validate the response
const data = await userClient.uploadFile(formData);
validateFileUploadResponse(data, clientId, user.id, townSquareChannel.id);
});
test('should succeed even with channel_id only', async () => {
// # Set without channel ID
const formData = new FormData();
formData.set('channel_id', townSquareChannel.id);
// # Do upload then validate the response
const data = await userClient.uploadFile(formData);
// * Validate that it doe snot throw an error
const validate = () => FileUploadResponseSchema.parse(data);
expect(validate).not.toThrow();
// * Validate that file_infos and client_ids are as expected
expect(data.client_ids).toMatchObject([]);
expect(data.file_infos.length).toBe(0);
});
test('should fail on invalid channel ID', async ({pw}) => {
const clientId = await pw.random.id();
// # Set with invalid channel ID
let formData = new FormData();
formData.set('channel_id', 'invalid.channel.id');
formData.set('client_ids', clientId);
formData.set('files', file, filename);
await expect(userClient.uploadFile(formData)).rejects.toThrowError(
'Invalid or missing channel_id parameter in request URL.',
);
// # Set without channel ID
formData = new FormData();
formData.set('client_ids', clientId);
formData.set('files', file, filename);
await expect(userClient.uploadFile(formData)).rejects.toThrowError(
'Invalid or missing channel_id in request body.',
);
});
test('should fail on missing files', async ({pw}) => {
const clientId = await pw.random.id();
// # Set with invalid channel ID
const formData = new FormData();
formData.set('channel_id', townSquareChannel.id);
formData.set('client_ids', clientId);
await expect(userClient.uploadFile(formData)).rejects.toThrowError(
'Unable to upload file(s). Have 1 client_ids for 0 files.',
);
});
test('should fail on incorrect order setting up FormData', async ({pw}) => {
const clientId = await pw.random.id();
// # Set with files before client_ids
const formData = new FormData();
formData.set('channel_id', townSquareChannel.id);
formData.set('files', file, filename);
formData.set('client_ids', clientId);
await expect(userClient.uploadFile(formData)).rejects.toThrowError(
'Invalid or missing client_ids in request body.',
);
});
function validateFileUploadResponse(data: FileUploadResponse, clientId: string, userId: string, channelId: string) {
// * Validate the schema
const validate = () => FileUploadResponseSchema.parse(data);
expect(validate).not.toThrow();
// * Validate that file_infos and client_ids are as expected
expect(data.client_ids).toMatchObject([clientId]);
expect(data.file_infos.length).toBe(1);
// * Validate important contents of file_infos
const fileInfo = data.file_infos[0];
expect(fileInfo.user_id).toBe(userId);
expect(fileInfo.channel_id).toBe(channelId);
expect(fileInfo.delete_at).toBe(0);
expect(fileInfo.extension).toBe('png');
expect(fileInfo.mime_type).toBe('image/png');
expect(fileInfo.archived).toBe(false);
}