summaryrefslogtreecommitdiff
path: root/internal/suites/action_reset_password.go
blob: 85b0624f654527ad0aaaa0d1fc069526b681ab04 (plain)
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
71
72
package suites

import (
	"testing"
	"time"

	"github.com/go-rod/rod"
	"github.com/stretchr/testify/require"
)

func (rs *RodSession) doInitiatePasswordReset(t *testing.T, page *rod.Page, username string) {
	err := rs.WaitElementLocatedByID(t, page, "reset-password-button").Click("left", 1)
	require.NoError(t, err)

	require.NoError(t, page.WaitStable(time.Millisecond*100))

	// Fill in username.
	err = rs.WaitElementLocatedByID(t, page, "username-textfield").Input(username)
	require.NoError(t, err)
	// And click on the reset button.
	err = rs.WaitElementLocatedByID(t, page, "reset-button").Click("left", 1)
	require.NoError(t, err)
}

func (rs *RodSession) doCompletePasswordReset(t *testing.T, page *rod.Page, newPassword1, newPassword2 string) {
	link := doGetResetPasswordJWTLinkFromLastEmail(t)
	rs.doVisit(t, page, link)

	password1 := rs.WaitElementLocatedByID(t, page, "password1-textfield")
	password2 := rs.WaitElementLocatedByID(t, page, "password2-textfield")

password1:
	err := password1.MustSelectAllText().Input(newPassword1)
	require.NoError(t, err)

	if password1.MustText() != newPassword1 {
		goto password1
	}

password2:
	err = password2.MustSelectAllText().Input(newPassword2)
	require.NoError(t, err)

	if password2.MustText() != newPassword2 {
		goto password2
	}

	err = rs.WaitElementLocatedByID(t, page, "reset-button").Click("left", 1)
	require.NoError(t, err)
}

func (rs *RodSession) doSuccessfullyCompletePasswordReset(t *testing.T, page *rod.Page, newPassword1, newPassword2 string) {
	rs.doCompletePasswordReset(t, page, newPassword1, newPassword2)
	rs.verifyIsFirstFactorPage(t, page)
}

func (rs *RodSession) doUnsuccessfulPasswordReset(t *testing.T, page *rod.Page, newPassword1, newPassword2 string) {
	rs.doCompletePasswordReset(t, page, newPassword1, newPassword2)
	rs.verifyNotificationDisplayed(t, page, "Your supplied password does not meet the password policy requirements")
}

func (rs *RodSession) doResetPassword(t *testing.T, page *rod.Page, username, newPassword1, newPassword2 string, unsuccessful bool) {
	rs.doInitiatePasswordReset(t, page, username)
	// then wait for the "email sent notification".
	rs.verifyMailNotificationDisplayed(t, page)

	if unsuccessful {
		rs.doUnsuccessfulPasswordReset(t, page, newPassword1, newPassword2)
	} else {
		rs.doSuccessfullyCompletePasswordReset(t, page, newPassword1, newPassword2)
	}
}