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