2020-05-27 11:55:44 +00:00
|
|
|
package suites
|
|
|
|
|
2020-11-28 00:06:42 +00:00
|
|
|
import (
|
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"
|
2021-11-05 13:14:42 +00:00
|
|
|
"log"
|
2023-01-12 10:57:44 +00:00
|
|
|
"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"
|
2020-11-28 00:06:42 +00:00
|
|
|
)
|
|
|
|
|
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 {
|
2020-05-27 11:55:44 +00:00
|
|
|
if PathPrefix != "" {
|
2023-01-12 10:57:44 +00:00
|
|
|
return LoginBaseURLFmt(baseDomain) + PathPrefix
|
2020-05-27 11:55:44 +00:00
|
|
|
}
|
|
|
|
|
2023-01-12 10:57:44 +00:00
|
|
|
return LoginBaseURLFmt(baseDomain)
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
// this func makes a http call to https://login.<domain>/override and is only useful for suite tests.
|
|
|
|
func getDomainEnvInfo(domain string) (map[string]string, error) {
|
|
|
|
info := make(map[string]string)
|
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
InsecureSkipVerify: true, //nolint:gosec
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
req *http.Request
|
|
|
|
resp *http.Response
|
|
|
|
body []byte
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
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.
|
|
|
|
func generateDevEnvFile(opts map[string]string) error {
|
|
|
|
wd, _ := os.Getwd()
|
|
|
|
path := strings.TrimSuffix(wd, "internal/suites")
|
|
|
|
|
|
|
|
src := fmt.Sprintf("%s/web/.env.production", path)
|
|
|
|
dst := fmt.Sprintf("%s/web/.env.development", path)
|
|
|
|
|
|
|
|
tmpl, err := template.ParseFiles(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
file, _ := os.Create(dst)
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
if err := tmpl.Execute(file, opts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// updateDevEnvFileForDomain updates web/.env.development.
|
|
|
|
// this function only affects local dev environments.
|
|
|
|
func updateDevEnvFileForDomain(domain string, setup bool) error {
|
|
|
|
if os.Getenv("CI") == "true" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err := getDomainEnvInfo(domain)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = generateDevEnvFile(info)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !setup {
|
|
|
|
err = waitUntilAutheliaFrontendIsReady(multiCookieDomainDockerEnvironment)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|