aboutsummaryrefslogtreecommitdiffstats
path: root/tests/functional/dashboard/forgot-password.spec.ts
blob: 7c356c8d856184763fc2b054cd1a73a2c79f8569 (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
import { test } from '@japa/runner'
import emitter from '@adonisjs/core/services/emitter'
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 emitterService = emitter.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(emitterService.exists('forgot:password'))
  })

  test('returns `email send when exists` and trigger forgot:password event', async ({
    client,
    assert,
  }) => {
    const emitterService = emitter.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(
      emitterService.exists(
        (event) => event.name === 'forgot:password' && event.data.user.email === user.email
      )
    )
  })
})