|
2 | 2 | * Tests for POST /api/admin/auth/login |
3 | 3 | * |
4 | 4 | * 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 |
5 | 16 | */ |
6 | 17 | import { it, expect, beforeEach } from 'vitest'; |
7 | 18 | import { setup } from '../../helpers'; |
@@ -30,11 +41,27 @@ it('Blocks logins with non-existent usernames', async () => { |
30 | 41 | .rejects.toMatchObject({ code: 401 }); |
31 | 42 | }); |
32 | 43 |
|
| 44 | +it('Errors if fields are empty', async () => { |
| 45 | + await expect(api().admin.auth.login('', '')) |
| 46 | + .rejects.toMatchObject({ code: 401 }); |
| 47 | +}); |
| 48 | + |
33 | 49 | it('Blocks logins with incorrect passwords', async () => { |
34 | 50 | await expect(api().admin.auth.login(credentials.username, credentials.password + 'hi')) |
35 | 51 | .rejects.toMatchObject({ code: 401 }); |
36 | 52 | }); |
37 | 53 |
|
| 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 | + |
38 | 65 | /** |
39 | 66 | * Run many failed login attempts, and ensure that there is a significant |
40 | 67 | * difference between the times on average. |
|
0 commit comments