2019-11-02 14:32:58 +00:00
|
|
|
package suites
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2023-01-25 09:36:40 +00:00
|
|
|
"net/url"
|
|
|
|
"regexp"
|
2019-11-02 14:32:58 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
)
|
|
|
|
|
|
|
|
type OneFactorSuite struct {
|
2021-11-05 13:14:42 +00:00
|
|
|
*RodSuite
|
2019-11-02 14:32:58 +00:00
|
|
|
}
|
|
|
|
|
2022-05-04 01:01:36 +00:00
|
|
|
func New1FAScenario() *OneFactorSuite {
|
2019-11-02 14:32:58 +00:00
|
|
|
return &OneFactorSuite{
|
2023-01-25 04:11:05 +00:00
|
|
|
RodSuite: NewRodSuite(""),
|
2019-11-02 14:32:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *OneFactorSuite) SetupSuite() {
|
2021-11-05 13:14:42 +00:00
|
|
|
browser, err := StartRod()
|
2019-11-02 14:32:58 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-11-05 13:14:42 +00:00
|
|
|
s.RodSession = browser
|
2019-11-02 14:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *OneFactorSuite) TearDownSuite() {
|
2021-11-05 13:14:42 +00:00
|
|
|
err := s.RodSession.Stop()
|
2019-11-02 14:32:58 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *OneFactorSuite) SetupTest() {
|
2021-11-05 13:14:42 +00:00
|
|
|
s.Page = s.doCreateTab(s.T(), HomeBaseURL)
|
|
|
|
s.verifyIsHome(s.T(), s.Page)
|
|
|
|
}
|
2019-11-02 14:32:58 +00:00
|
|
|
|
2021-11-05 13:14:42 +00:00
|
|
|
func (s *OneFactorSuite) TearDownTest() {
|
|
|
|
s.collectCoverage(s.Page)
|
|
|
|
s.MustClose()
|
2019-11-02 14:32:58 +00:00
|
|
|
}
|
|
|
|
|
2023-01-25 09:36:40 +00:00
|
|
|
func (s *OneFactorSuite) TestShouldNotAuthorizeSecretBeforeOneFactor() {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
|
|
|
defer func() {
|
|
|
|
cancel()
|
|
|
|
s.collectScreenshot(ctx.Err(), s.Page)
|
|
|
|
}()
|
|
|
|
|
|
|
|
targetURL := fmt.Sprintf("%s/secret.html", SingleFactorBaseURL)
|
|
|
|
|
|
|
|
s.doVisit(s.T(), s.Context(ctx), targetURL)
|
|
|
|
|
|
|
|
s.verifyIsFirstFactorPage(s.T(), s.Context(ctx))
|
|
|
|
|
|
|
|
raw := GetLoginBaseURLWithFallbackPrefix(BaseDomain, "/")
|
|
|
|
|
|
|
|
expected, err := url.ParseRequestURI(raw)
|
|
|
|
s.Assert().NoError(err)
|
|
|
|
s.Require().NotNil(expected)
|
|
|
|
|
|
|
|
query := expected.Query()
|
|
|
|
|
|
|
|
query.Set("rd", targetURL)
|
|
|
|
|
|
|
|
expected.RawQuery = query.Encode()
|
|
|
|
|
|
|
|
rx := regexp.MustCompile(fmt.Sprintf(`^%s(&rm=GET)?$`, regexp.QuoteMeta(expected.String())))
|
|
|
|
|
|
|
|
s.verifyURLIsRegexp(s.T(), s.Context(ctx), rx)
|
|
|
|
}
|
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
func (s *OneFactorSuite) TestShouldAuthorizeSecretAfterOneFactor() {
|
2023-01-25 04:11:05 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
2021-11-05 13:14:42 +00:00
|
|
|
defer func() {
|
|
|
|
cancel()
|
|
|
|
s.collectScreenshot(ctx.Err(), s.Page)
|
|
|
|
}()
|
2019-11-02 14:32:58 +00:00
|
|
|
|
|
|
|
targetURL := fmt.Sprintf("%s/secret.html", SingleFactorBaseURL)
|
2023-01-12 10:57:44 +00:00
|
|
|
s.doLoginOneFactor(s.T(), s.Context(ctx), "john", "password", false, BaseDomain, targetURL)
|
2021-11-05 13:14:42 +00:00
|
|
|
s.verifySecretAuthorized(s.T(), s.Page)
|
2019-11-02 14:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *OneFactorSuite) TestShouldRedirectToSecondFactor() {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
2021-11-05 13:14:42 +00:00
|
|
|
defer func() {
|
|
|
|
cancel()
|
|
|
|
s.collectScreenshot(ctx.Err(), s.Page)
|
|
|
|
}()
|
2019-11-02 14:32:58 +00:00
|
|
|
|
|
|
|
targetURL := fmt.Sprintf("%s/secret.html", AdminBaseURL)
|
2023-01-12 10:57:44 +00:00
|
|
|
s.doLoginOneFactor(s.T(), s.Context(ctx), "john", "password", false, BaseDomain, targetURL)
|
2021-11-05 13:14:42 +00:00
|
|
|
s.verifyIsSecondFactorPage(s.T(), s.Context(ctx))
|
2019-11-02 14:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *OneFactorSuite) TestShouldDenyAccessOnBadPassword() {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
2021-11-05 13:14:42 +00:00
|
|
|
defer func() {
|
|
|
|
cancel()
|
|
|
|
s.collectScreenshot(ctx.Err(), s.Page)
|
|
|
|
}()
|
2019-11-02 14:32:58 +00:00
|
|
|
|
|
|
|
targetURL := fmt.Sprintf("%s/secret.html", AdminBaseURL)
|
2023-01-12 10:57:44 +00:00
|
|
|
s.doLoginOneFactor(s.T(), s.Context(ctx), "john", "bad-password", false, BaseDomain, targetURL)
|
2021-11-05 13:14:42 +00:00
|
|
|
s.verifyIsFirstFactorPage(s.T(), s.Context(ctx))
|
|
|
|
s.verifyNotificationDisplayed(s.T(), s.Context(ctx), "Incorrect username or password.")
|
2019-11-02 14:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunOneFactor(t *testing.T) {
|
2021-03-14 07:08:26 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping suite test in short mode")
|
|
|
|
}
|
|
|
|
|
2022-05-04 01:01:36 +00:00
|
|
|
suite.Run(t, New1FAScenario())
|
2019-11-02 14:32:58 +00:00
|
|
|
}
|