aboutsummaryrefslogtreecommitdiffstats
path: root/tests/functional/dashboard/forgot-password.spec.ts
blob: 9dcec5ac9c6bdb41ace7545118bc23645a99ff14 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { test } from '@japa/runner';
import Event from '@ioc:Adonis/Core/Event';
import UserFactory from 'Database/factories/UserFactory';

test.group('Dashboard / Forgot password page', () => {
  test('returns a 200 opening the forgot password route', async ({
    client,
  }) => {
    const response = await client.get('/user/forgot');

    response.assertStatus(200);
    response.assertTextIncludes('Forgot Password?');
  });

  test('returns `Please enter a valid email address` when providing invalid email', async ({
    client,
  }) => {
    const response = await client.post('/user/forgot').fields({
      mail: 'invalid',
    });

    response.assertStatus(200);
    response.assertTextIncludes('Please enter a valid email address');
  });

  test('returns `email send when exists` without forgot:password event', async ({
    client,
    assert,
  }) => {
    const emitter = Event.fake();

    const response = await client.post('/user/forgot').fields({
      mail: 'test@ferdium.org',
    });

    response.assertStatus(200);
    response.assertTextIncludes(
      'If your provided E-Mail address is linked to an account, we have just sent an E-Mail to that address.',
    );

    assert.isFalse(emitter.exists('forgot:password'));
  });

  test('returns `email send when exists` and trigger forgot:password event', async ({
    client,
    assert,
  }) => {
    const emitter = Event.fake();
    const user = await UserFactory.merge({
      email: 'test+forgot_password@ferdium.org',
    }).create();

    const response = await client.post('/user/forgot').fields({
      mail: 'test+forgot_password@ferdium.org',
    });

    response.assertStatus(200);
    response.assertTextIncludes(
      'If your provided E-Mail address is linked to an account, we have just sent an E-Mail to that address.',
    );

    assert.isTrue(
      emitter.exists(
        event =>
          event.name === 'forgot:password' &&
          event.data.user.email === user.email,
      ),
    );
  });
});