2019-11-25 03:27:59 +07:00
|
|
|
package suites
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-11-30 23:49:52 +07:00
|
|
|
"encoding/json"
|
2019-11-25 03:27:59 +07:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2019-11-30 23:49:52 +07:00
|
|
|
"strings"
|
2019-11-25 03:27:59 +07:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-11-30 23:49:52 +07:00
|
|
|
mapset "github.com/deckarep/golang-set"
|
2019-11-25 03:27:59 +07:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CustomHeadersScenario struct {
|
2021-11-05 20:14:42 +07:00
|
|
|
*RodSuite
|
2019-11-25 03:27:59 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewCustomHeadersScenario() *CustomHeadersScenario {
|
|
|
|
return &CustomHeadersScenario{
|
2021-11-05 20:14:42 +07:00
|
|
|
RodSuite: new(RodSuite),
|
2019-11-25 03:27:59 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CustomHeadersScenario) 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
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CustomHeadersScenario) 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 *CustomHeadersScenario) 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 *CustomHeadersScenario) TearDownTest() {
|
|
|
|
s.collectCoverage(s.Page)
|
|
|
|
s.MustClose()
|
2019-11-25 03:27:59 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CustomHeadersScenario) TestShouldNotForwardCustomHeaderForUnauthenticatedUser() {
|
|
|
|
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-25 03:27:59 +07:00
|
|
|
|
2021-11-05 20:14:42 +07:00
|
|
|
s.doVisit(s.T(), s.Context(ctx), fmt.Sprintf("%s/headers", PublicBaseURL))
|
2019-11-25 03:27:59 +07:00
|
|
|
|
2021-11-05 20:14:42 +07:00
|
|
|
body, err := s.Context(ctx).Element("body")
|
2019-11-25 03:27:59 +07:00
|
|
|
s.Assert().NoError(err)
|
2020-05-06 08:50:37 +07:00
|
|
|
|
|
|
|
b, err := body.Text()
|
|
|
|
s.Assert().NoError(err)
|
|
|
|
s.Assert().NotContains(b, "john")
|
|
|
|
s.Assert().NotContains(b, "admins")
|
2020-10-26 18:38:08 +07:00
|
|
|
s.Assert().NotContains(b, "John Doe")
|
|
|
|
s.Assert().NotContains(b, "john.doe@authelia.com")
|
2019-11-25 03:27:59 +07:00
|
|
|
}
|
|
|
|
|
2019-11-30 23:49:52 +07:00
|
|
|
type Headers struct {
|
2020-10-26 18:38:08 +07:00
|
|
|
ForwardedEmail string `json:"Remote-Email"`
|
2020-05-06 08:50:37 +07:00
|
|
|
ForwardedGroups string `json:"Remote-Groups"`
|
2020-10-26 18:38:08 +07:00
|
|
|
ForwardedName string `json:"Remote-Name"`
|
2020-05-06 08:50:37 +07:00
|
|
|
ForwardedUser string `json:"Remote-User"`
|
2019-11-30 23:49:52 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
type HeadersPayload struct {
|
|
|
|
Headers Headers `json:"headers"`
|
|
|
|
}
|
|
|
|
|
2019-11-25 03:27:59 +07:00
|
|
|
func (s *CustomHeadersScenario) TestShouldForwardCustomHeaderForAuthenticatedUser() {
|
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
|
|
|
|
2019-11-30 23:49:52 +07:00
|
|
|
expectedGroups := mapset.NewSetWith("dev", "admins")
|
|
|
|
|
2019-11-25 03:27:59 +07:00
|
|
|
targetURL := fmt.Sprintf("%s/headers", PublicBaseURL)
|
2021-11-05 20:14:42 +07:00
|
|
|
s.doLoginOneFactor(s.T(), s.Context(ctx), "john", "password", false, targetURL)
|
|
|
|
s.verifyIsPublic(s.T(), s.Context(ctx))
|
|
|
|
|
|
|
|
body, err := s.Context(ctx).Element("body")
|
|
|
|
s.Assert().NoError(err)
|
|
|
|
s.Assert().NotNil(body)
|
|
|
|
|
|
|
|
content, err := body.Text()
|
|
|
|
s.Assert().NoError(err)
|
|
|
|
s.Assert().NotNil(content)
|
|
|
|
|
|
|
|
payload := HeadersPayload{}
|
|
|
|
if err := json.Unmarshal([]byte(content), &payload); err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
groups := strings.Split(payload.Headers.ForwardedGroups, ",")
|
|
|
|
actualGroups := mapset.NewSet()
|
|
|
|
|
|
|
|
for _, group := range groups {
|
|
|
|
actualGroups.Add(group)
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(payload.Headers.ForwardedUser, "john") && expectedGroups.Equal(actualGroups) &&
|
|
|
|
strings.Contains(payload.Headers.ForwardedName, "John Doe") && strings.Contains(payload.Headers.ForwardedEmail, "john.doe@authelia.com") {
|
|
|
|
err = nil
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("headers do not include user information")
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Require().NoError(err)
|
2019-11-25 03:27:59 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCustomHeadersScenario(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, NewCustomHeadersScenario())
|
|
|
|
}
|