2019-11-02 21:32:58 +07:00
|
|
|
package suites
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TwoFactorSuite struct {
|
2021-11-05 20:14:42 +07:00
|
|
|
*RodSuite
|
2019-11-02 21:32:58 +07:00
|
|
|
}
|
|
|
|
|
2022-05-04 08:01:36 +07:00
|
|
|
func New2FAScenario() *TwoFactorSuite {
|
2019-11-02 21:32:58 +07:00
|
|
|
return &TwoFactorSuite{
|
2021-11-05 20:14:42 +07:00
|
|
|
RodSuite: new(RodSuite),
|
2019-11-02 21:32:58 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TwoFactorSuite) SetupSuite() {
|
2021-11-05 20:14:42 +07:00
|
|
|
browser, err := StartRod()
|
2019-11-02 21:32:58 +07:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-11-05 20:14:42 +07:00
|
|
|
s.RodSession = browser
|
2019-11-02 21:32:58 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TwoFactorSuite) TearDownSuite() {
|
2021-11-05 20:14:42 +07:00
|
|
|
err := s.RodSession.Stop()
|
2019-11-02 21:32:58 +07:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TwoFactorSuite) SetupTest() {
|
2021-11-05 20:14:42 +07:00
|
|
|
s.Page = s.doCreateTab(s.T(), HomeBaseURL)
|
|
|
|
s.verifyIsHome(s.T(), s.Page)
|
|
|
|
}
|
2019-11-02 21:32:58 +07:00
|
|
|
|
2021-11-05 20:14:42 +07:00
|
|
|
func (s *TwoFactorSuite) TearDownTest() {
|
|
|
|
s.collectCoverage(s.Page)
|
|
|
|
s.MustClose()
|
2019-11-02 21:32:58 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TwoFactorSuite) TestShouldAuthorizeSecretAfterTwoFactor() {
|
2021-11-05 20:14:42 +07:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
|
|
|
defer func() {
|
|
|
|
cancel()
|
|
|
|
s.collectScreenshot(ctx.Err(), s.Page)
|
|
|
|
}()
|
2019-11-02 21:32:58 +07:00
|
|
|
|
2020-05-02 23:20:40 +07:00
|
|
|
username := testUsername
|
|
|
|
password := testPassword
|
2019-12-08 00:14:26 +07:00
|
|
|
|
2022-01-31 12:25:15 +07:00
|
|
|
// Login and register TOTP, logout and login again with 1FA & 2FA.
|
2019-11-02 21:32:58 +07:00
|
|
|
targetURL := fmt.Sprintf("%s/secret.html", AdminBaseURL)
|
2021-11-05 20:14:42 +07:00
|
|
|
_ = s.doRegisterAndLogin2FA(s.T(), s.Context(ctx), username, password, false, targetURL)
|
2019-12-08 00:14:26 +07:00
|
|
|
|
|
|
|
// And check if the user is redirected to the secret.
|
2021-11-05 20:14:42 +07:00
|
|
|
s.verifySecretAuthorized(s.T(), s.Context(ctx))
|
2019-11-25 03:27:59 +07:00
|
|
|
|
2022-01-31 12:25:15 +07:00
|
|
|
// Leave the secret.
|
2021-11-05 20:14:42 +07:00
|
|
|
s.doVisit(s.T(), s.Context(ctx), HomeBaseURL)
|
|
|
|
s.verifyIsHome(s.T(), s.Context(ctx))
|
2019-11-25 03:27:59 +07:00
|
|
|
|
2022-01-31 12:25:15 +07:00
|
|
|
// And try to reload it again to check the session is kept.
|
2021-11-05 20:14:42 +07:00
|
|
|
s.doVisit(s.T(), s.Context(ctx), targetURL)
|
|
|
|
s.verifySecretAuthorized(s.T(), s.Context(ctx))
|
2019-11-25 03:27:59 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TwoFactorSuite) TestShouldFailTwoFactor() {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
2021-11-05 20:14:42 +07:00
|
|
|
defer func() {
|
|
|
|
cancel()
|
|
|
|
s.collectScreenshot(ctx.Err(), s.Page)
|
|
|
|
}()
|
2019-11-25 03:27:59 +07:00
|
|
|
|
|
|
|
// Register TOTP secret and logout.
|
2021-11-05 20:14:42 +07:00
|
|
|
s.doRegisterThenLogout(s.T(), s.Context(ctx), testUsername, testPassword)
|
2019-11-25 03:27:59 +07:00
|
|
|
|
|
|
|
wrongPasscode := "123456"
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2021-11-05 20:14:42 +07:00
|
|
|
s.doLoginOneFactor(s.T(), s.Context(ctx), testUsername, testPassword, false, "")
|
|
|
|
s.verifyIsSecondFactorPage(s.T(), s.Context(ctx))
|
|
|
|
s.doEnterOTP(s.T(), s.Context(ctx), wrongPasscode)
|
|
|
|
s.verifyNotificationDisplayed(s.T(), s.Context(ctx), "The one-time password might be wrong")
|
2019-11-02 21:32:58 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunTwoFactor(t *testing.T) {
|
2021-03-14 14:08:26 +07:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping suite test in short mode")
|
|
|
|
}
|
|
|
|
|
2022-05-04 08:01:36 +07:00
|
|
|
suite.Run(t, New2FAScenario())
|
2019-11-02 21:32:58 +07:00
|
|
|
}
|