2019-11-02 14:32:58 +00:00
|
|
|
package suites
|
|
|
|
|
|
|
|
import (
|
2020-12-29 02:30:00 +00:00
|
|
|
"strings"
|
2019-11-24 20:27:59 +00:00
|
|
|
"testing"
|
2019-11-02 14:32:58 +00:00
|
|
|
"time"
|
|
|
|
|
2021-11-05 13:14:42 +00:00
|
|
|
"github.com/go-rod/rod"
|
2022-12-07 09:22:03 +00:00
|
|
|
"github.com/go-rod/rod/lib/input"
|
2019-11-02 14:32:58 +00:00
|
|
|
"github.com/pquerna/otp/totp"
|
2019-11-24 20:27:59 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-12-16 01:47:31 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-11-02 14:32:58 +00:00
|
|
|
)
|
|
|
|
|
2021-11-05 13:14:42 +00:00
|
|
|
func (rs *RodSession) doRegisterTOTP(t *testing.T, page *rod.Page) string {
|
2022-09-26 03:10:37 +00:00
|
|
|
err := rs.WaitElementLocatedByID(t, page, "register-link").Click("left", 1)
|
2020-12-16 01:47:31 +00:00
|
|
|
require.NoError(t, err)
|
2021-11-05 13:14:42 +00:00
|
|
|
rs.verifyMailNotificationDisplayed(t, page)
|
2019-11-24 20:27:59 +00:00
|
|
|
link := doGetLinkFromLastMail(t)
|
2021-11-05 13:14:42 +00:00
|
|
|
rs.doVisit(t, page, link)
|
|
|
|
secretURL, err := page.MustElement("#secret-url").Attribute("value")
|
2019-11-24 20:27:59 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-29 02:30:00 +00:00
|
|
|
|
2021-11-05 13:14:42 +00:00
|
|
|
secret := (*secretURL)[strings.LastIndex(*secretURL, "=")+1:]
|
2019-11-24 20:27:59 +00:00
|
|
|
assert.NotEqual(t, "", secret)
|
|
|
|
assert.NotNil(t, secret)
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
return secret
|
|
|
|
}
|
|
|
|
|
2021-11-05 13:14:42 +00:00
|
|
|
func (rs *RodSession) doEnterOTP(t *testing.T, page *rod.Page, code string) {
|
2022-04-07 05:33:53 +00:00
|
|
|
inputs := rs.WaitElementsLocatedByID(t, page, "otp-input input")
|
2019-11-24 20:27:59 +00:00
|
|
|
|
2021-12-01 12:11:29 +00:00
|
|
|
for i := 0; i < len(code); i++ {
|
2022-12-07 09:22:03 +00:00
|
|
|
err := inputs[i].Type(input.Key(code[i]))
|
|
|
|
require.NoError(t, err)
|
2019-11-24 20:27:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-05 13:14:42 +00:00
|
|
|
func (rs *RodSession) doValidateTOTP(t *testing.T, page *rod.Page, secret string) {
|
2019-11-02 14:32:58 +00:00
|
|
|
code, err := totp.GenerateCode(secret, time.Now())
|
2019-11-24 20:27:59 +00:00
|
|
|
assert.NoError(t, err)
|
2021-11-05 13:14:42 +00:00
|
|
|
rs.doEnterOTP(t, page, code)
|
2019-11-02 14:32:58 +00:00
|
|
|
}
|