Archived
1
0

5 Commits

3 changed files with 260 additions and 0 deletions

View File

@ -0,0 +1,95 @@
import { describe, expect, test, } from 'bun:test';
describe('E2E: Authentication', () => {
const testUser = {
name: 'Test User',
email: `test-${Date.now()}@example.com`,
password: 'TestPassword123!',
};
let authCookie: string;
let userId: string;
test('should register new user', async () => {
const response = await fetch('http://localhost:3000/auth/api/sign-up/email', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(testUser),
});
expect(response.status).toBe(200);
const data = await response.json() as unknown as any;
expect(data.user).toBeDefined();
expect(data.user.email).toBe(testUser.email);
expect(data.user.name).toBe(testUser.name);
userId = data.user.id;
// Получаем cookies из ответа
const setCookieHeader = response.headers.get('set-cookie');
expect(setCookieHeader).toBeDefined();
authCookie = setCookieHeader!;
});
test('should get current session with cookies', async () => {
const response = await fetch('http://localhost:3000/auth/api/get-session', {
headers: {
'Cookie': authCookie,
},
});
expect(response.status).toBe(200);
const data = await response.json() as any;
expect(data.user).toBeDefined();
expect(data.user.email).toBe(testUser.email);
});
test('should sign in with credentials', async () => {
const response = await fetch('http://localhost:3000/auth/api/sign-in/email', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
email: testUser.email,
password: testUser.password,
}),
});
expect(response.status).toBe(200);
const data = await response.json() as any;
expect(data.user).toBeDefined();
expect(data.user.email).toBe(testUser.email);
const setCookieHeader = response.headers.get('set-cookie');
expect(setCookieHeader).toBeDefined();
authCookie = setCookieHeader!;
});
test('should fail with wrong password', async () => {
const response = await fetch('http://localhost:3000/auth/api/sign-in/email', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
email: testUser.email,
password: 'WrongPassword',
}),
});
expect(response.status).toBeGreaterThanOrEqual(400);
});
test('should fail without cookies', async () => {
const response = await fetch('http://localhost:3000/auth/api/session');
// Должен вернуть 401 или отсутствующую сессию
expect(response.status).toBeGreaterThanOrEqual(400);
});
});

View 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);
});
});

View File

@ -0,0 +1,19 @@
import { beforeAll, afterAll } from 'bun:test';
import { redis } from '@/services/redis.service';
beforeAll(async () => {
console.log('Setting up test environment...');
try {
await redis.connect();
} catch (error) {
console.warn('Redis not available in tests, continuing without cache');
}
});
afterAll(async () => {
console.log('Cleaning up test environment...');
await redis.disconnect();
});