2019-11-02 14:32:58 +00:00
|
|
|
package suites
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-11-24 20:27:59 +00:00
|
|
|
"testing"
|
2019-11-02 14:32:58 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
2019-11-24 20:27:59 +00:00
|
|
|
func (wds *WebDriverSession) doRegisterTOTP(ctx context.Context, t *testing.T) string {
|
2020-12-16 01:47:31 +00:00
|
|
|
err := wds.WaitElementLocatedByID(ctx, t, "register-link").Click()
|
|
|
|
require.NoError(t, err)
|
2019-11-24 20:27:59 +00:00
|
|
|
wds.verifyMailNotificationDisplayed(ctx, t)
|
|
|
|
link := doGetLinkFromLastMail(t)
|
|
|
|
wds.doVisit(t, link)
|
|
|
|
secret, err := wds.WaitElementLocatedByID(ctx, t, "base32-secret").GetAttribute("value")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-11-24 20:27:59 +00:00
|
|
|
func (wds *WebDriverSession) doEnterOTP(ctx context.Context, t *testing.T, code string) {
|
|
|
|
inputs := wds.WaitElementsLocatedByCSSSelector(ctx, t, "#otp-input input")
|
|
|
|
|
|
|
|
for i := 0; i < 6; i++ {
|
2020-12-16 01:47:31 +00:00
|
|
|
err := inputs[i].SendKeys(string(code[i]))
|
|
|
|
require.NoError(t, err)
|
2019-11-24 20:27:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wds *WebDriverSession) doValidateTOTP(ctx context.Context, t *testing.T, 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)
|
|
|
|
wds.doEnterOTP(ctx, t, code)
|
2019-11-02 14:32:58 +00:00
|
|
|
}
|