feat: добавил тесты для сохранения картинок
This commit is contained in:
146
apps/backend/src/tests/e2e/saves.test.ts
Normal file
146
apps/backend/src/tests/e2e/saves.test.ts
Normal file
@ -0,0 +1,146 @@
|
||||
// apps/backend/src/tests/e2e/saves.test.ts
|
||||
// Path: apps/backend/src/tests/e2e/saves.test.ts
|
||||
|
||||
import { describe, expect, test, beforeAll } from 'bun:test';
|
||||
|
||||
describe('E2E: Saves Management', () => {
|
||||
let user1Cookie: string;
|
||||
let user1Id: string;
|
||||
let saveId: number;
|
||||
|
||||
beforeAll(async () => {
|
||||
// Создаем тестового пользователя
|
||||
const signUpResponse = await fetch('http://localhost:3000/auth/api/sign-up/email', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
name: 'Save Test User',
|
||||
email: `save-test-${Date.now()}@example.com`,
|
||||
password: 'TestPassword123!',
|
||||
}),
|
||||
});
|
||||
|
||||
const signUpData = await signUpResponse.json();
|
||||
user1Id = signUpData.user.id;
|
||||
|
||||
// Получаем cookies
|
||||
const setCookieHeader = signUpResponse.headers.get('set-cookie');
|
||||
if (setCookieHeader) {
|
||||
user1Cookie = setCookieHeader;
|
||||
}
|
||||
});
|
||||
|
||||
test('should create save from external URL', async () => {
|
||||
const response = await fetch('http://localhost:3000/saves/external', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Cookie': user1Cookie,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
url: 'https://www.pinterest.com/pin/40391727901690267/',
|
||||
name: 'Test Image',
|
||||
description: 'A test image from httpbin',
|
||||
tags: ['test', 'e2e'],
|
||||
visibility: 'link',
|
||||
}),
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
expect(response.status).toBe(200);
|
||||
|
||||
expect(data.id).toBeDefined();
|
||||
expect(data.name).toBe('Test Image');
|
||||
expect(data.type).toBe('image');
|
||||
expect(data.visibility).toBe('link');
|
||||
expect(data.shareUrl).toBeDefined();
|
||||
|
||||
saveId = data.id;
|
||||
});
|
||||
|
||||
test('should get my saves', async () => {
|
||||
const response = await fetch('http://localhost:3000/saves/my', {
|
||||
headers: {
|
||||
'Cookie': user1Cookie,
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
|
||||
const data = await response.json();
|
||||
expect(Array.isArray(data)).toBe(true);
|
||||
expect(data.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
test('should get save by ID', async () => {
|
||||
const response = await fetch(`http://localhost:3000/saves/${saveId}`, {
|
||||
headers: {
|
||||
'Cookie': user1Cookie,
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
|
||||
const data = await response.json();
|
||||
expect(data.id).toBe(saveId);
|
||||
expect(data.name).toBe('Test Image');
|
||||
});
|
||||
|
||||
test('should update save', async () => {
|
||||
const response = await fetch(`http://localhost:3000/saves/${saveId}`, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Cookie': user1Cookie,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
name: 'Updated Test Image',
|
||||
description: 'Updated description',
|
||||
tags: ['updated', 'test'],
|
||||
visibility: 'public',
|
||||
}),
|
||||
});
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
|
||||
const data = await response.json();
|
||||
expect(data.name).toBe('Updated Test Image');
|
||||
expect(data.visibility).toBe('public');
|
||||
expect(data.tags).toContain('updated');
|
||||
});
|
||||
|
||||
test('should access public save without auth', async () => {
|
||||
const response = await fetch(`http://localhost:3000/saves/${saveId}`);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
|
||||
const data = await response.json();
|
||||
expect(data.id).toBe(saveId);
|
||||
});
|
||||
|
||||
test('should delete save', async () => {
|
||||
const response = await fetch(`http://localhost:3000/saves/${saveId}`, {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'Cookie': user1Cookie,
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
|
||||
const data = await response.json();
|
||||
expect(data.success).toBe(true);
|
||||
});
|
||||
|
||||
test('should return 404 for deleted save', async () => {
|
||||
const response = await fetch(`http://localhost:3000/saves/${saveId}`, {
|
||||
headers: {
|
||||
'Cookie': user1Cookie,
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.status).toBe(404);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user