2019-11-02 14:32:58 +00:00
|
|
|
package suites
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TwoFactorSuite struct {
|
|
|
|
*SeleniumSuite
|
|
|
|
}
|
|
|
|
|
2019-11-24 20:27:59 +00:00
|
|
|
func NewTwoFactorScenario() *TwoFactorSuite {
|
2019-11-02 14:32:58 +00:00
|
|
|
return &TwoFactorSuite{
|
|
|
|
SeleniumSuite: new(SeleniumSuite),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TwoFactorSuite) SetupSuite() {
|
|
|
|
wds, err := StartWebDriver()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-11-24 20:27:59 +00:00
|
|
|
s.WebDriverSession = wds
|
2019-11-02 14:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TwoFactorSuite) TearDownSuite() {
|
|
|
|
err := s.WebDriverSession.Stop()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TwoFactorSuite) SetupTest() {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2019-11-24 20:27:59 +00:00
|
|
|
s.doLogout(ctx, s.T())
|
|
|
|
s.doVisit(s.T(), HomeBaseURL)
|
|
|
|
s.verifyIsHome(ctx, s.T())
|
2019-11-02 14:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TwoFactorSuite) TestShouldAuthorizeSecretAfterTwoFactor() {
|
2019-11-24 20:27:59 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
2019-11-02 14:32:58 +00:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// Register TOTP secret and logout.
|
2019-11-24 20:27:59 +00:00
|
|
|
secret := s.doRegisterThenLogout(ctx, s.T(), "john", "password")
|
2019-11-02 14:32:58 +00:00
|
|
|
|
|
|
|
targetURL := fmt.Sprintf("%s/secret.html", AdminBaseURL)
|
2019-11-24 20:27:59 +00:00
|
|
|
s.doLoginTwoFactor(ctx, s.T(), "john", "password", false, secret, targetURL)
|
|
|
|
s.verifySecretAuthorized(ctx, s.T())
|
|
|
|
|
|
|
|
s.doVisit(s.T(), HomeBaseURL)
|
|
|
|
s.verifyIsHome(ctx, s.T())
|
|
|
|
|
|
|
|
s.doVisit(s.T(), targetURL)
|
|
|
|
s.verifySecretAuthorized(ctx, s.T())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TwoFactorSuite) TestShouldFailTwoFactor() {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// Register TOTP secret and logout.
|
|
|
|
s.doRegisterThenLogout(ctx, s.T(), "john", "password")
|
|
|
|
|
|
|
|
wrongPasscode := "123456"
|
|
|
|
s.doLoginOneFactor(ctx, s.T(), "john", "password", false, "")
|
|
|
|
s.verifyIsSecondFactorPage(ctx, s.T())
|
|
|
|
s.doEnterOTP(ctx, s.T(), wrongPasscode)
|
2019-11-02 14:32:58 +00:00
|
|
|
|
2019-11-24 20:27:59 +00:00
|
|
|
s.verifyNotificationDisplayed(ctx, s.T(), "The one-time password might be wrong")
|
2019-11-02 14:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunTwoFactor(t *testing.T) {
|
2019-11-24 20:27:59 +00:00
|
|
|
suite.Run(t, NewTwoFactorScenario())
|
2019-11-02 14:32:58 +00:00
|
|
|
}
|