2019-11-02 21:32:58 +07:00
|
|
|
package suites
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
)
|
|
|
|
|
|
|
|
type OneFactorSuite struct {
|
2021-11-05 20:14:42 +07:00
|
|
|
*RodSuite
|
2019-11-02 21:32:58 +07:00
|
|
|
}
|
|
|
|
|
2019-11-25 03:27:59 +07:00
|
|
|
func NewOneFactorScenario() *OneFactorSuite {
|
2019-11-02 21:32:58 +07:00
|
|
|
return &OneFactorSuite{
|
2021-11-05 20:14:42 +07:00
|
|
|
RodSuite: new(RodSuite),
|
2019-11-02 21:32:58 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *OneFactorSuite) 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 *OneFactorSuite) 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 *OneFactorSuite) 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 *OneFactorSuite) TearDownTest() {
|
|
|
|
s.collectCoverage(s.Page)
|
|
|
|
s.MustClose()
|
2019-11-02 21:32:58 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *OneFactorSuite) TestShouldAuthorizeSecretAfterOneFactor() {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
2021-11-05 20:14:42 +07:00
|
|
|
defer func() {
|
|
|
|
cancel()
|
|
|
|
s.collectScreenshot(ctx.Err(), s.Page)
|
|
|
|
}()
|
2019-11-02 21:32:58 +07:00
|
|
|
|
|
|
|
targetURL := fmt.Sprintf("%s/secret.html", SingleFactorBaseURL)
|
2021-11-05 20:14:42 +07:00
|
|
|
s.doLoginOneFactor(s.T(), s.Context(ctx), "john", "password", false, targetURL)
|
|
|
|
s.verifySecretAuthorized(s.T(), s.Page)
|
2019-11-02 21:32:58 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *OneFactorSuite) TestShouldRedirectToSecondFactor() {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
2021-11-05 20:14:42 +07:00
|
|
|
defer func() {
|
|
|
|
cancel()
|
|
|
|
s.collectScreenshot(ctx.Err(), s.Page)
|
|
|
|
}()
|
2019-11-02 21:32:58 +07:00
|
|
|
|
|
|
|
targetURL := fmt.Sprintf("%s/secret.html", AdminBaseURL)
|
2021-11-05 20:14:42 +07:00
|
|
|
s.doLoginOneFactor(s.T(), s.Context(ctx), "john", "password", false, targetURL)
|
|
|
|
s.verifyIsSecondFactorPage(s.T(), s.Context(ctx))
|
2019-11-02 21:32:58 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *OneFactorSuite) TestShouldDenyAccessOnBadPassword() {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
2021-11-05 20:14:42 +07:00
|
|
|
defer func() {
|
|
|
|
cancel()
|
|
|
|
s.collectScreenshot(ctx.Err(), s.Page)
|
|
|
|
}()
|
2019-11-02 21:32:58 +07:00
|
|
|
|
|
|
|
targetURL := fmt.Sprintf("%s/secret.html", AdminBaseURL)
|
2021-11-05 20:14:42 +07:00
|
|
|
s.doLoginOneFactor(s.T(), s.Context(ctx), "john", "bad-password", false, targetURL)
|
|
|
|
s.verifyIsFirstFactorPage(s.T(), s.Context(ctx))
|
|
|
|
s.verifyNotificationDisplayed(s.T(), s.Context(ctx), "Incorrect username or password.")
|
2019-11-02 21:32:58 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunOneFactor(t *testing.T) {
|
2021-03-14 14:08:26 +07:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping suite test in short mode")
|
|
|
|
}
|
|
|
|
|
2019-11-25 03:27:59 +07:00
|
|
|
suite.Run(t, NewOneFactorScenario())
|
2019-11-02 21:32:58 +07:00
|
|
|
}
|