Skip to content

Commit bc3a632

Browse files
Add more login test cases
1 parent 9e3e894 commit bc3a632

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

tests/backend/admin/auth/login.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22
* Tests for POST /api/admin/auth/login
33
*
44
* Allows users to log into the site, enabling editing of the data.
5+
*
6+
* # Errors
7+
* - Invalid username
8+
* - Invalid password
9+
* - After repeated failed logins, all requests are rejected
10+
*
11+
* # Success
12+
* - Lets user log in if they give the correct credentials
13+
*
14+
* # Edge cases
15+
* - Empty form fields
516
*/
617
import { it, expect, beforeEach } from 'vitest';
718
import { setup } from '../../helpers';
@@ -30,11 +41,27 @@ it('Blocks logins with non-existent usernames', async () => {
3041
.rejects.toMatchObject({ code: 401 });
3142
});
3243

44+
it('Errors if fields are empty', async () => {
45+
await expect(api().admin.auth.login('', ''))
46+
.rejects.toMatchObject({ code: 401 });
47+
});
48+
3349
it('Blocks logins with incorrect passwords', async () => {
3450
await expect(api().admin.auth.login(credentials.username, credentials.password + 'hi'))
3551
.rejects.toMatchObject({ code: 401 });
3652
});
3753

54+
it('Blocks all logins after 25 failed login requests', { fails: true }, async () => {
55+
for (let i = 0; i < 25; i++) {
56+
await api().admin.auth.login(credentials.username + 'hi', credentials.password)
57+
// Discard error
58+
.catch(() => {});
59+
}
60+
// User has been banned because of login failure happening too many times
61+
await expect(api().admin.auth.login(credentials.username, credentials.password))
62+
.rejects.toMatchObject({ code: 403 });
63+
});
64+
3865
/**
3966
* Run many failed login attempts, and ensure that there is a significant
4067
* difference between the times on average.

0 commit comments

Comments
 (0)