2019-11-25 03:27:59 +07:00
|
|
|
package suites
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
)
|
|
|
|
|
|
|
|
type InactivityScenario struct {
|
2021-11-05 20:14:42 +07:00
|
|
|
*RodSuite
|
2019-11-25 03:27:59 +07:00
|
|
|
secret string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewInactivityScenario() *InactivityScenario {
|
|
|
|
return &InactivityScenario{
|
2021-11-05 20:14:42 +07:00
|
|
|
RodSuite: new(RodSuite),
|
2019-11-25 03:27:59 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *InactivityScenario) SetupSuite() {
|
2021-11-05 20:14:42 +07:00
|
|
|
browser, err := StartRod()
|
2019-11-25 03:27:59 +07:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-11-05 20:14:42 +07:00
|
|
|
s.RodSession = browser
|
2019-11-25 03:27:59 +07:00
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 20*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
|
|
|
|
2021-11-05 20:14:42 +07:00
|
|
|
s.collectCoverage(s.Page)
|
|
|
|
s.MustClose()
|
|
|
|
}()
|
|
|
|
|
|
|
|
s.Page = s.doCreateTab(s.T(), HomeBaseURL)
|
2019-11-25 03:27:59 +07:00
|
|
|
targetURL := fmt.Sprintf("%s/secret.html", AdminBaseURL)
|
2021-11-05 20:14:42 +07:00
|
|
|
s.secret = s.doRegisterAndLogin2FA(s.T(), s.Context(ctx), "john", "password", false, targetURL)
|
|
|
|
s.verifySecretAuthorized(s.T(), s.Context(ctx))
|
2019-11-25 03:27:59 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *InactivityScenario) TearDownSuite() {
|
2021-11-05 20:14:42 +07:00
|
|
|
err := s.RodSession.Stop()
|
2019-11-25 03:27:59 +07:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *InactivityScenario) SetupTest() {
|
2021-11-05 20:14:42 +07:00
|
|
|
s.Page = s.doCreateTab(s.T(), HomeBaseURL)
|
|
|
|
s.verifyIsHome(s.T(), s.Page)
|
|
|
|
}
|
2019-11-25 03:27:59 +07:00
|
|
|
|
2021-11-05 20:14:42 +07:00
|
|
|
func (s *InactivityScenario) TearDownTest() {
|
|
|
|
s.collectCoverage(s.Page)
|
|
|
|
s.MustClose()
|
2019-11-25 03:27:59 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *InactivityScenario) TestShouldRequireReauthenticationAfterInactivityPeriod() {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 20*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
|
|
|
|
|
|
|
targetURL := fmt.Sprintf("%s/secret.html", AdminBaseURL)
|
|
|
|
|
2021-11-05 20:14:42 +07:00
|
|
|
s.doLoginTwoFactor(s.T(), s.Context(ctx), "john", "password", false, s.secret, "")
|
|
|
|
s.doVisit(s.T(), s.Context(ctx), HomeBaseURL)
|
|
|
|
s.verifyIsHome(s.T(), s.Context(ctx))
|
2019-11-25 03:27:59 +07:00
|
|
|
|
|
|
|
time.Sleep(6 * time.Second)
|
|
|
|
|
2021-11-05 20:14:42 +07:00
|
|
|
s.doVisit(s.T(), s.Context(ctx), targetURL)
|
|
|
|
s.verifyIsFirstFactorPage(s.T(), s.Context(ctx))
|
2019-11-25 03:27:59 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *InactivityScenario) TestShouldRequireReauthenticationAfterCookieExpiration() {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 20*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
|
|
|
|
|
|
|
targetURL := fmt.Sprintf("%s/secret.html", AdminBaseURL)
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2021-11-05 20:14:42 +07:00
|
|
|
s.doLoginTwoFactor(s.T(), s.Context(ctx), "john", "password", false, s.secret, "")
|
2019-11-25 03:27:59 +07:00
|
|
|
|
|
|
|
for i := 0; i < 3; i++ {
|
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
|
|
|
|
|
|
|
time.Sleep(2 * time.Second)
|
2020-05-06 02:35:32 +07:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
2021-11-05 20:14:42 +07:00
|
|
|
s.doVisit(s.T(), s.Context(ctx), targetURL)
|
|
|
|
s.verifyIsFirstFactorPage(s.T(), s.Context(ctx))
|
2019-11-25 03:27:59 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *InactivityScenario) TestShouldDisableCookieExpirationAndInactivity() {
|
2021-11-05 20:14:42 +07:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
defer func() {
|
|
|
|
cancel()
|
|
|
|
s.collectScreenshot(ctx.Err(), s.Page)
|
|
|
|
}()
|
2019-11-25 03:27:59 +07:00
|
|
|
|
|
|
|
targetURL := fmt.Sprintf("%s/secret.html", AdminBaseURL)
|
|
|
|
|
2021-11-05 20:14:42 +07:00
|
|
|
s.doLoginTwoFactor(s.T(), s.Context(ctx), "john", "password", true, s.secret, "")
|
|
|
|
s.doVisit(s.T(), s.Context(ctx), HomeBaseURL)
|
|
|
|
s.verifyIsHome(s.T(), s.Context(ctx))
|
2019-11-25 03:27:59 +07:00
|
|
|
|
2020-01-18 05:48:48 +07:00
|
|
|
time.Sleep(10 * time.Second)
|
2019-11-25 03:27:59 +07:00
|
|
|
|
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 TestInactivityScenario(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, NewInactivityScenario())
|
|
|
|
}
|