2020-05-27 11:55:44 +00:00
|
|
|
package suites
|
|
|
|
|
2020-11-28 00:06:42 +00:00
|
|
|
import (
|
2023-01-25 04:11:05 +00:00
|
|
|
"bufio"
|
2021-11-05 13:14:42 +00:00
|
|
|
"context"
|
2023-01-12 10:57:44 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"encoding/json"
|
2021-11-05 13:14:42 +00:00
|
|
|
"fmt"
|
2023-01-12 10:57:44 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
2020-11-28 00:06:42 +00:00
|
|
|
"os"
|
2021-10-08 00:17:08 +00:00
|
|
|
"path/filepath"
|
2021-11-05 13:14:42 +00:00
|
|
|
"runtime"
|
2021-10-08 00:17:08 +00:00
|
|
|
"strings"
|
2023-01-12 10:57:44 +00:00
|
|
|
"text/template"
|
2021-11-05 13:14:42 +00:00
|
|
|
|
|
|
|
"github.com/go-rod/rod"
|
2022-06-27 08:27:57 +00:00
|
|
|
"github.com/google/uuid"
|
2023-01-25 04:11:05 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-11-28 00:06:42 +00:00
|
|
|
)
|
|
|
|
|
2023-01-21 05:02:27 +00:00
|
|
|
var browserPaths = []string{"/usr/bin/chromium-browser", "/usr/bin/chromium"}
|
|
|
|
|
|
|
|
// ValidateBrowserPath validates the appropriate chromium browser path.
|
|
|
|
func ValidateBrowserPath(path string) (browserPath string, err error) {
|
|
|
|
var info os.FileInfo
|
|
|
|
|
|
|
|
if info, err = os.Stat(path); err != nil {
|
|
|
|
return "", err
|
|
|
|
} else if info.IsDir() {
|
|
|
|
return "", fmt.Errorf("browser cannot be a directory")
|
|
|
|
}
|
|
|
|
|
|
|
|
return path, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBrowserPath retrieves the appropriate chromium browser path.
|
|
|
|
func GetBrowserPath() (path string, err error) {
|
|
|
|
browserPath := os.Getenv("BROWSER_PATH")
|
|
|
|
|
|
|
|
if browserPath != "" {
|
|
|
|
return ValidateBrowserPath(browserPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, browserPath = range browserPaths {
|
|
|
|
if browserPath, err = ValidateBrowserPath(browserPath); err == nil {
|
|
|
|
return browserPath, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", fmt.Errorf("no chromium browser was detected in the known paths, set the BROWSER_PATH environment variable to override the path")
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:55:44 +00:00
|
|
|
// GetLoginBaseURL returns the URL of the login portal and the path prefix if specified.
|
2023-01-12 10:57:44 +00:00
|
|
|
func GetLoginBaseURL(baseDomain string) string {
|
2023-01-25 09:36:40 +00:00
|
|
|
return LoginBaseURLFmt(baseDomain) + GetPathPrefix()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLoginBaseURLWithFallbackPrefix overloads GetLoginBaseURL and includes '/' as a prefix if the prefix is empty.
|
|
|
|
func GetLoginBaseURLWithFallbackPrefix(baseDomain, fallback string) string {
|
|
|
|
prefix := GetPathPrefix()
|
|
|
|
|
|
|
|
if prefix == "" {
|
|
|
|
prefix = fallback
|
2020-05-27 11:55:44 +00:00
|
|
|
}
|
|
|
|
|
2023-01-25 09:36:40 +00:00
|
|
|
return LoginBaseURLFmt(baseDomain) + prefix
|
2020-05-27 11:55:44 +00:00
|
|
|
}
|
2020-11-28 00:06:42 +00:00
|
|
|
|
2021-11-05 13:14:42 +00:00
|
|
|
func (rs *RodSession) collectCoverage(page *rod.Page) {
|
|
|
|
coverageDir := "../../web/.nyc_output"
|
|
|
|
|
2022-05-03 12:37:56 +00:00
|
|
|
resp, err := page.Eval("() => JSON.stringify(window.__coverage__)")
|
2021-11-05 13:14:42 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
2020-11-28 00:06:42 +00:00
|
|
|
}
|
|
|
|
|
2021-11-05 13:14:42 +00:00
|
|
|
coverageData := fmt.Sprintf("%v", resp.Value)
|
2020-11-28 00:06:42 +00:00
|
|
|
|
2021-11-05 13:14:42 +00:00
|
|
|
_ = os.MkdirAll(coverageDir, 0775)
|
|
|
|
|
|
|
|
if coverageData != "<nil>" {
|
2022-06-27 08:27:57 +00:00
|
|
|
err = os.WriteFile(fmt.Sprintf("%s/coverage-%s.json", coverageDir, uuid.New().String()), []byte(coverageData), 0664) //nolint:gosec
|
2021-11-05 13:14:42 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = filepath.Walk("../../web/.nyc_output", fixCoveragePath)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-25 04:11:05 +00:00
|
|
|
func (s *BaseSuite) SetupSuite() {
|
|
|
|
s.SetupLogging()
|
|
|
|
s.SetupEnvironment()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *BaseSuite) SetupLogging() {
|
|
|
|
if os.Getenv("SUITE_SETUP_LOGGING") == t {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
level string
|
|
|
|
ok bool
|
|
|
|
)
|
|
|
|
|
|
|
|
if level, ok = os.LookupEnv("SUITES_LOG_LEVEL"); !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
l, err := log.ParseLevel(level)
|
|
|
|
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
log.SetLevel(l)
|
|
|
|
|
|
|
|
log.SetFormatter(&log.TextFormatter{
|
|
|
|
ForceColors: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
s.T().Setenv("SUITE_SETUP_LOGGING", t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *BaseSuite) SetupEnvironment() {
|
|
|
|
if s.Name == "" || os.Getenv("SUITE_SETUP_ENVIRONMENT") == t {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("Checking Suite %s for .env file", s.Name)
|
|
|
|
|
|
|
|
path := filepath.Join(s.Name, ".env")
|
|
|
|
|
|
|
|
var (
|
|
|
|
info os.FileInfo
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
path, err = filepath.Abs(path)
|
|
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
if info, err = os.Stat(path); err != nil {
|
|
|
|
s.Assert().True(os.IsNotExist(err))
|
|
|
|
|
|
|
|
log.Debugf("Suite %s does not have an .env file or it can't be read: %v", s.Name, err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Require().False(info.IsDir())
|
|
|
|
|
|
|
|
log.Debugf("Suite %s does have an .env file at path: %s", s.Name, path)
|
|
|
|
|
|
|
|
var file *os.File
|
|
|
|
|
|
|
|
file, err = os.Open(path)
|
|
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
v := strings.Split(scanner.Text(), "=")
|
|
|
|
|
|
|
|
s.Require().Len(v, 2)
|
|
|
|
|
|
|
|
s.T().Setenv(v[0], v[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
s.T().Setenv("SUITE_SETUP_ENVIRONMENT", t)
|
|
|
|
}
|
|
|
|
|
2021-11-05 13:14:42 +00:00
|
|
|
func (rs *RodSession) collectScreenshot(err error, page *rod.Page) {
|
2021-12-01 03:32:58 +00:00
|
|
|
if err == context.DeadlineExceeded && os.Getenv("CI") == t {
|
2021-11-05 13:14:42 +00:00
|
|
|
base := "/buildkite/screenshots"
|
|
|
|
build := os.Getenv("BUILDKITE_BUILD_NUMBER")
|
|
|
|
suite := strings.ToLower(os.Getenv("SUITE"))
|
|
|
|
job := os.Getenv("BUILDKITE_JOB_ID")
|
2022-02-09 22:07:53 +00:00
|
|
|
path := filepath.Join(base, build, suite, job)
|
2021-11-05 13:14:42 +00:00
|
|
|
|
|
|
|
if err := os.MkdirAll(path, 0755); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pc, _, _, _ := runtime.Caller(2)
|
|
|
|
fn := runtime.FuncForPC(pc)
|
|
|
|
p := "github.com/authelia/authelia/v4/internal/suites."
|
|
|
|
r := strings.NewReplacer(p, "", "(", "", ")", "", "*", "", ".", "-")
|
|
|
|
|
|
|
|
page.MustScreenshotFullPage(fmt.Sprintf("%s/%s.jpg", path, r.Replace(fn.Name())))
|
|
|
|
}
|
2020-11-28 00:06:42 +00:00
|
|
|
}
|
2021-10-08 00:17:08 +00:00
|
|
|
|
2023-01-25 09:36:40 +00:00
|
|
|
func (s *RodSuite) GetCookieNames() (names []string) {
|
|
|
|
cookies, err := s.Page.Cookies(nil)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
for _, cookie := range cookies {
|
|
|
|
names = append(names, cookie.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return names
|
|
|
|
}
|
|
|
|
|
2021-10-08 00:17:08 +00:00
|
|
|
func fixCoveragePath(path string, file os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if file.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
coverage, err := filepath.Match("*.json", file.Name())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if coverage {
|
2021-12-01 13:14:15 +00:00
|
|
|
read, err := os.ReadFile(path)
|
2021-10-08 00:17:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
wd, _ := os.Getwd()
|
|
|
|
ciPath := strings.TrimSuffix(wd, "internal/suites")
|
|
|
|
content := strings.ReplaceAll(string(read), "/node/src/app/", ciPath+"web/")
|
|
|
|
|
2021-12-01 13:14:15 +00:00
|
|
|
err = os.WriteFile(path, []byte(content), 0)
|
2021-10-08 00:17:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-01-12 10:57:44 +00:00
|
|
|
|
|
|
|
// getEnvInfoFromURL gets environments variables for specified cookie domain
|
2023-01-25 04:11:05 +00:00
|
|
|
// this func makes a http call to https://login.<domain>/devworkflow and is only useful for suite tests.
|
2023-02-26 02:22:22 +00:00
|
|
|
func getDomainEnvInfo(domain string) (info map[string]string, err error) {
|
|
|
|
info = make(map[string]string)
|
2023-01-12 10:57:44 +00:00
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
InsecureSkipVerify: true, //nolint:gosec
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
req *http.Request
|
|
|
|
resp *http.Response
|
|
|
|
body []byte
|
|
|
|
)
|
|
|
|
|
|
|
|
targetURL := LoginBaseURLFmt(domain) + "/devworkflow"
|
|
|
|
|
|
|
|
if req, err = http.NewRequest(http.MethodGet, targetURL, nil); err != nil {
|
|
|
|
return info, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("X-Forwarded-Proto", "https")
|
|
|
|
req.Header.Set("X-Forwarded-Host", domain)
|
|
|
|
|
|
|
|
if resp, err = client.Do(req); err != nil {
|
|
|
|
return info, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if body, err = io.ReadAll(resp.Body); err != nil {
|
|
|
|
return info, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if err = json.Unmarshal(body, &info); err != nil {
|
|
|
|
return info, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// generateDevEnvFile generates web/.env.development based on opts.
|
2023-02-26 02:22:22 +00:00
|
|
|
func generateDevEnvFile(info map[string]string) (err error) {
|
|
|
|
base, _ := os.Getwd()
|
|
|
|
base = strings.TrimSuffix(base, "/internal/suites")
|
|
|
|
|
|
|
|
var tmpl *template.Template
|
|
|
|
|
|
|
|
if tmpl, err = template.ParseFiles(base + envFileProd); err != nil {
|
2023-01-12 10:57:44 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-26 02:22:22 +00:00
|
|
|
file, _ := os.Create(base + envFileDev)
|
2023-01-12 10:57:44 +00:00
|
|
|
defer file.Close()
|
|
|
|
|
2023-02-26 02:22:22 +00:00
|
|
|
if err = tmpl.Execute(file, info); err != nil {
|
2023-01-12 10:57:44 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// updateDevEnvFileForDomain updates web/.env.development.
|
|
|
|
// this function only affects local dev environments.
|
2023-02-26 02:22:22 +00:00
|
|
|
func updateDevEnvFileForDomain(domain string, setup bool) (err error) {
|
2023-01-25 04:11:05 +00:00
|
|
|
if os.Getenv("CI") == t {
|
2023-01-12 10:57:44 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-26 02:22:22 +00:00
|
|
|
if _, err = os.Stat(envFileDev); err != nil && os.IsNotExist(err) {
|
2023-01-25 04:11:05 +00:00
|
|
|
file, _ := os.Create(envFileDev)
|
|
|
|
file.Close()
|
|
|
|
}
|
|
|
|
|
2023-02-26 02:22:22 +00:00
|
|
|
var info map[string]string
|
|
|
|
|
|
|
|
if info, err = getDomainEnvInfo(domain); err != nil {
|
2023-01-12 10:57:44 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-26 02:22:22 +00:00
|
|
|
if err = generateDevEnvFile(info); err != nil {
|
2023-01-12 10:57:44 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !setup {
|
2023-02-26 02:22:22 +00:00
|
|
|
if err = waitUntilAutheliaFrontendIsReady(multiCookieDomainDockerEnvironment); err != nil {
|
2023-01-12 10:57:44 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|