feat: добавил тесты авторизации
This commit is contained in:
@ -33,4 +33,63 @@ describe('E2E: Authentication', () => {
|
|||||||
expect(setCookieHeader).toBeDefined();
|
expect(setCookieHeader).toBeDefined();
|
||||||
authCookie = setCookieHeader!;
|
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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user