2023-04-25 12:04:35 +00:00
|
|
|
// Package integration provides helpers for integration testing.
|
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
2023-04-25 15:11:04 +00:00
|
|
|
"bytes"
|
2023-04-25 12:04:35 +00:00
|
|
|
"context"
|
2023-04-28 11:39:53 +00:00
|
|
|
"database/sql"
|
2023-04-25 15:11:04 +00:00
|
|
|
_ "embed"
|
2023-04-28 11:39:53 +00:00
|
|
|
"errors"
|
2023-04-25 12:04:35 +00:00
|
|
|
"fmt"
|
2023-09-15 14:58:45 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2023-04-25 12:04:35 +00:00
|
|
|
"os"
|
2023-09-15 14:58:45 +00:00
|
|
|
"reflect"
|
2023-04-25 15:11:04 +00:00
|
|
|
"strings"
|
2023-04-25 12:04:35 +00:00
|
|
|
"sync"
|
2023-04-26 16:54:47 +00:00
|
|
|
"time"
|
2023-04-25 12:04:35 +00:00
|
|
|
|
2023-04-25 15:11:04 +00:00
|
|
|
"github.com/spf13/viper"
|
2023-04-25 12:04:35 +00:00
|
|
|
"github.com/zitadel/logging"
|
2023-10-17 15:19:51 +00:00
|
|
|
"github.com/zitadel/oidc/v3/pkg/client"
|
|
|
|
"github.com/zitadel/oidc/v3/pkg/oidc"
|
2023-04-25 12:04:35 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
2023-04-28 11:39:53 +00:00
|
|
|
"google.golang.org/grpc/metadata"
|
2023-04-25 12:04:35 +00:00
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/cmd"
|
|
|
|
"github.com/zitadel/zitadel/cmd/start"
|
2023-04-28 11:39:53 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
2023-07-06 06:38:13 +00:00
|
|
|
http_util "github.com/zitadel/zitadel/internal/api/http"
|
2023-04-28 11:39:53 +00:00
|
|
|
z_oidc "github.com/zitadel/zitadel/internal/api/oidc"
|
|
|
|
"github.com/zitadel/zitadel/internal/command"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
2023-09-15 14:58:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/net"
|
2023-04-28 11:39:53 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/query"
|
2023-06-07 15:28:42 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/webauthn"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2023-04-26 16:54:47 +00:00
|
|
|
"github.com/zitadel/zitadel/pkg/grpc/admin"
|
2023-04-25 12:04:35 +00:00
|
|
|
)
|
|
|
|
|
2023-04-25 15:11:04 +00:00
|
|
|
var (
|
|
|
|
//go:embed config/zitadel.yaml
|
|
|
|
zitadelYAML []byte
|
|
|
|
//go:embed config/cockroach.yaml
|
|
|
|
cockroachYAML []byte
|
|
|
|
//go:embed config/postgres.yaml
|
|
|
|
postgresYAML []byte
|
2023-07-06 06:38:13 +00:00
|
|
|
//go:embed config/system-user-key.pem
|
|
|
|
systemUserKey []byte
|
2023-04-25 15:11:04 +00:00
|
|
|
)
|
|
|
|
|
2023-08-11 14:19:14 +00:00
|
|
|
// NotEmpty can be used as placeholder, when the returned values is unknown.
|
|
|
|
// It can be used in tests to assert whether a value should be empty or not.
|
|
|
|
const NotEmpty = "not empty"
|
|
|
|
|
2023-05-02 16:24:24 +00:00
|
|
|
// UserType provides constants that give
|
|
|
|
// a short explinanation with the purpose
|
|
|
|
// a serverice user.
|
|
|
|
// This allows to pre-create users with
|
|
|
|
// different permissions and reuse them.
|
2023-04-28 11:39:53 +00:00
|
|
|
type UserType int
|
|
|
|
|
|
|
|
//go:generate stringer -type=UserType
|
|
|
|
const (
|
|
|
|
Unspecified UserType = iota
|
|
|
|
OrgOwner
|
2023-07-10 13:27:00 +00:00
|
|
|
Login
|
2023-07-06 06:38:13 +00:00
|
|
|
IAMOwner
|
|
|
|
SystemUser // SystemUser is a user with access to the system service.
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
FirstInstanceUsersKey = "first"
|
2023-07-14 11:16:16 +00:00
|
|
|
UserPassword = "VeryS3cret!"
|
2023-04-28 11:39:53 +00:00
|
|
|
)
|
|
|
|
|
2023-09-15 14:58:45 +00:00
|
|
|
const (
|
|
|
|
PortMilestoneServer = "8081"
|
|
|
|
PortQuotaServer = "8082"
|
|
|
|
)
|
|
|
|
|
2023-05-02 16:24:24 +00:00
|
|
|
// User information with a Personal Access Token.
|
2023-04-28 11:39:53 +00:00
|
|
|
type User struct {
|
|
|
|
*query.User
|
|
|
|
Token string
|
|
|
|
}
|
|
|
|
|
2023-07-10 13:27:00 +00:00
|
|
|
type InstanceUserMap map[string]map[UserType]*User
|
|
|
|
|
|
|
|
func (m InstanceUserMap) Set(instanceID string, typ UserType, user *User) {
|
|
|
|
if m[instanceID] == nil {
|
|
|
|
m[instanceID] = make(map[UserType]*User)
|
|
|
|
}
|
|
|
|
m[instanceID][typ] = user
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m InstanceUserMap) Get(instanceID string, typ UserType) *User {
|
|
|
|
if users, ok := m[instanceID]; ok {
|
|
|
|
return users[typ]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-02 16:24:24 +00:00
|
|
|
// Tester is a Zitadel server and client with all resources available for testing.
|
2023-04-25 12:04:35 +00:00
|
|
|
type Tester struct {
|
|
|
|
*start.Server
|
2023-04-28 11:39:53 +00:00
|
|
|
|
|
|
|
Instance authz.Instance
|
|
|
|
Organisation *query.Org
|
2023-07-10 13:27:00 +00:00
|
|
|
Users InstanceUserMap
|
2023-04-28 11:39:53 +00:00
|
|
|
|
2023-09-15 14:58:45 +00:00
|
|
|
MilestoneChan chan []byte
|
|
|
|
milestoneServer *httptest.Server
|
|
|
|
QuotaNotificationChan chan []byte
|
|
|
|
quotaNotificationServer *httptest.Server
|
|
|
|
|
2023-06-07 15:28:42 +00:00
|
|
|
Client Client
|
|
|
|
WebAuthN *webauthn.Client
|
|
|
|
wg sync.WaitGroup // used for shutdown
|
2023-04-25 12:04:35 +00:00
|
|
|
}
|
|
|
|
|
2023-05-03 13:10:27 +00:00
|
|
|
const commandLine = `start --masterkeyFromEnv`
|
|
|
|
|
|
|
|
func (s *Tester) Host() string {
|
|
|
|
return fmt.Sprintf("%s:%d", s.Config.ExternalDomain, s.Config.Port)
|
|
|
|
}
|
2023-04-25 15:11:04 +00:00
|
|
|
|
2023-11-22 09:29:38 +00:00
|
|
|
func (s *Tester) createClientConn(ctx context.Context, target string) {
|
|
|
|
cc, err := grpc.DialContext(ctx, target,
|
2023-07-06 06:38:13 +00:00
|
|
|
grpc.WithBlock(),
|
|
|
|
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
2023-11-22 09:29:38 +00:00
|
|
|
)
|
2023-04-25 12:04:35 +00:00
|
|
|
if err != nil {
|
|
|
|
s.Shutdown <- os.Interrupt
|
|
|
|
s.wg.Wait()
|
|
|
|
}
|
|
|
|
logging.OnError(err).Fatal("integration tester client dial")
|
|
|
|
logging.New().WithField("target", target).Info("finished dialing grpc client conn")
|
|
|
|
|
2023-06-07 15:28:42 +00:00
|
|
|
s.Client = newClient(cc)
|
2023-04-26 16:54:47 +00:00
|
|
|
err = s.pollHealth(ctx)
|
|
|
|
logging.OnError(err).Fatal("integration tester health")
|
|
|
|
}
|
|
|
|
|
|
|
|
// pollHealth waits until a healthy status is reported.
|
|
|
|
// TODO: remove when we make the setup blocking on all
|
|
|
|
// projections completed.
|
|
|
|
func (s *Tester) pollHealth(ctx context.Context) (err error) {
|
|
|
|
for {
|
|
|
|
err = func(ctx context.Context) error {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2023-06-07 15:28:42 +00:00
|
|
|
_, err := s.Client.Admin.Healthz(ctx, &admin.HealthzRequest{})
|
2023-04-26 16:54:47 +00:00
|
|
|
return err
|
|
|
|
}(ctx)
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
logging.WithError(err).Info("poll healthz")
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2023-04-25 12:04:35 +00:00
|
|
|
}
|
|
|
|
|
2023-04-28 11:39:53 +00:00
|
|
|
const (
|
2023-08-11 14:19:14 +00:00
|
|
|
LoginUser = "loginClient"
|
|
|
|
MachineUserOrgOwner = "integrationOrgOwner"
|
|
|
|
MachineUserInstanceOwner = "integrationInstanceOwner"
|
2023-04-28 11:39:53 +00:00
|
|
|
)
|
|
|
|
|
2023-08-11 14:19:14 +00:00
|
|
|
func (s *Tester) createMachineUserOrgOwner(ctx context.Context) {
|
2023-04-28 11:39:53 +00:00
|
|
|
var err error
|
|
|
|
|
2023-08-11 14:19:14 +00:00
|
|
|
ctx, user := s.createMachineUser(ctx, MachineUserOrgOwner, OrgOwner)
|
|
|
|
_, err = s.Commands.AddOrgMember(ctx, user.ResourceOwner, user.ID, "ORG_OWNER")
|
2023-12-08 14:30:55 +00:00
|
|
|
target := new(zerrors.AlreadyExistsError)
|
2023-04-28 11:39:53 +00:00
|
|
|
if !errors.As(err, &target) {
|
|
|
|
logging.OnError(err).Fatal("add org member")
|
|
|
|
}
|
2023-08-11 14:19:14 +00:00
|
|
|
}
|
2023-04-28 11:39:53 +00:00
|
|
|
|
2023-08-11 14:19:14 +00:00
|
|
|
func (s *Tester) createMachineUserInstanceOwner(ctx context.Context) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
ctx, user := s.createMachineUser(ctx, MachineUserInstanceOwner, IAMOwner)
|
|
|
|
_, err = s.Commands.AddInstanceMember(ctx, user.ID, "IAM_OWNER")
|
2023-12-08 14:30:55 +00:00
|
|
|
target := new(zerrors.AlreadyExistsError)
|
2023-08-11 14:19:14 +00:00
|
|
|
if !errors.As(err, &target) {
|
|
|
|
logging.OnError(err).Fatal("add instance member")
|
|
|
|
}
|
2023-07-10 13:27:00 +00:00
|
|
|
}
|
2023-04-28 11:39:53 +00:00
|
|
|
|
2023-07-10 13:27:00 +00:00
|
|
|
func (s *Tester) createLoginClient(ctx context.Context) {
|
2023-08-11 14:19:14 +00:00
|
|
|
s.createMachineUser(ctx, LoginUser, Login)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Tester) createMachineUser(ctx context.Context, username string, userType UserType) (context.Context, *query.User) {
|
2023-07-10 13:27:00 +00:00
|
|
|
var err error
|
2024-08-12 20:32:01 +00:00
|
|
|
ctx = s.updateInstanceAndOrg(ctx, s.Host())
|
2023-08-11 14:19:14 +00:00
|
|
|
usernameQuery, err := query.NewUserUsernameSearchQuery(username, query.TextEquals)
|
|
|
|
logging.OnError(err).Fatal("user query")
|
2023-11-21 12:11:38 +00:00
|
|
|
user, err := s.Queries.GetUser(ctx, true, usernameQuery)
|
2023-07-10 13:27:00 +00:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
_, err = s.Commands.AddMachine(ctx, &command.Machine{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
ResourceOwner: s.Organisation.ID,
|
|
|
|
},
|
2023-08-11 14:19:14 +00:00
|
|
|
Username: username,
|
|
|
|
Name: username,
|
2023-07-10 13:27:00 +00:00
|
|
|
Description: "who cares?",
|
|
|
|
AccessTokenType: domain.OIDCTokenTypeJWT,
|
|
|
|
})
|
2023-08-11 14:19:14 +00:00
|
|
|
logging.WithFields("username", username).OnError(err).Fatal("add machine user")
|
2023-11-21 12:11:38 +00:00
|
|
|
user, err = s.Queries.GetUser(ctx, true, usernameQuery)
|
2023-07-06 06:38:13 +00:00
|
|
|
}
|
2023-08-11 14:19:14 +00:00
|
|
|
logging.WithFields("username", username).OnError(err).Fatal("get user")
|
2023-07-10 13:27:00 +00:00
|
|
|
|
2023-08-11 14:19:14 +00:00
|
|
|
scopes := []string{oidc.ScopeOpenID, oidc.ScopeProfile, z_oidc.ScopeUserMetaData, z_oidc.ScopeResourceOwner}
|
2023-07-10 13:27:00 +00:00
|
|
|
pat := command.NewPersonalAccessToken(user.ResourceOwner, user.ID, time.Now().Add(time.Hour), scopes, domain.UserTypeMachine)
|
|
|
|
_, err = s.Commands.AddPersonalAccessToken(ctx, pat)
|
2023-08-11 14:19:14 +00:00
|
|
|
logging.WithFields("username", SystemUser).OnError(err).Fatal("add pat")
|
|
|
|
s.Users.Set(FirstInstanceUsersKey, userType, &User{
|
2023-07-06 06:38:13 +00:00
|
|
|
User: user,
|
|
|
|
Token: pat.Token,
|
2023-07-10 13:27:00 +00:00
|
|
|
})
|
2023-08-11 14:19:14 +00:00
|
|
|
return ctx, user
|
2023-04-28 11:39:53 +00:00
|
|
|
}
|
|
|
|
|
2023-07-06 06:38:13 +00:00
|
|
|
func (s *Tester) WithAuthorization(ctx context.Context, u UserType) context.Context {
|
|
|
|
return s.WithInstanceAuthorization(ctx, u, FirstInstanceUsersKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Tester) WithInstanceAuthorization(ctx context.Context, u UserType, instanceID string) context.Context {
|
|
|
|
if u == SystemUser {
|
|
|
|
s.ensureSystemUser()
|
|
|
|
}
|
2023-08-02 16:57:53 +00:00
|
|
|
return s.WithAuthorizationToken(ctx, s.Users.Get(instanceID, u).Token)
|
|
|
|
}
|
|
|
|
|
2024-03-08 08:37:23 +00:00
|
|
|
func (s *Tester) GetUserID(u UserType) string {
|
|
|
|
if u == SystemUser {
|
|
|
|
s.ensureSystemUser()
|
|
|
|
}
|
|
|
|
return s.Users.Get(FirstInstanceUsersKey, u).ID
|
|
|
|
}
|
|
|
|
|
2023-08-02 16:57:53 +00:00
|
|
|
func (s *Tester) WithAuthorizationToken(ctx context.Context, token string) context.Context {
|
2023-08-15 09:50:42 +00:00
|
|
|
md, ok := metadata.FromOutgoingContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
md = make(metadata.MD)
|
|
|
|
}
|
|
|
|
md.Set("Authorization", fmt.Sprintf("Bearer %s", token))
|
|
|
|
return metadata.NewOutgoingContext(ctx, md)
|
2023-07-06 06:38:13 +00:00
|
|
|
}
|
|
|
|
|
2024-01-17 10:16:48 +00:00
|
|
|
func (s *Tester) BearerToken(ctx context.Context) string {
|
|
|
|
md, ok := metadata.FromOutgoingContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return md.Get("Authorization")[0]
|
|
|
|
}
|
|
|
|
|
2023-07-06 06:38:13 +00:00
|
|
|
func (s *Tester) ensureSystemUser() {
|
|
|
|
const ISSUER = "tester"
|
2023-07-10 13:27:00 +00:00
|
|
|
if s.Users.Get(FirstInstanceUsersKey, SystemUser) != nil {
|
2023-07-06 06:38:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
audience := http_util.BuildOrigin(s.Host(), s.Server.Config.ExternalSecure)
|
|
|
|
signer, err := client.NewSignerFromPrivateKeyByte(systemUserKey, "")
|
|
|
|
logging.OnError(err).Fatal("system key signer")
|
|
|
|
jwt, err := client.SignedJWTProfileAssertion(ISSUER, []string{audience}, time.Hour, signer)
|
|
|
|
logging.OnError(err).Fatal("system key jwt")
|
2023-07-10 13:27:00 +00:00
|
|
|
s.Users.Set(FirstInstanceUsersKey, SystemUser, &User{Token: jwt})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Tester) WithSystemAuthorizationHTTP(u UserType) map[string]string {
|
|
|
|
return map[string]string{"Authorization": fmt.Sprintf("Bearer %s", s.Users.Get(FirstInstanceUsersKey, u).Token)}
|
2023-04-28 11:39:53 +00:00
|
|
|
}
|
|
|
|
|
2023-05-02 16:24:24 +00:00
|
|
|
// Done send an interrupt signal to cleanly shutdown the server.
|
2023-04-25 12:04:35 +00:00
|
|
|
func (s *Tester) Done() {
|
2023-06-07 15:28:42 +00:00
|
|
|
err := s.Client.CC.Close()
|
2023-04-25 12:04:35 +00:00
|
|
|
logging.OnError(err).Error("integration tester client close")
|
|
|
|
|
|
|
|
s.Shutdown <- os.Interrupt
|
|
|
|
s.wg.Wait()
|
2023-09-15 14:58:45 +00:00
|
|
|
s.milestoneServer.Close()
|
|
|
|
s.quotaNotificationServer.Close()
|
2023-04-25 12:04:35 +00:00
|
|
|
}
|
|
|
|
|
2023-05-02 16:24:24 +00:00
|
|
|
// NewTester start a new Zitadel server by passing the default commandline.
|
|
|
|
// The server will listen on the configured port.
|
|
|
|
// The database configuration that will be used can be set by the
|
|
|
|
// INTEGRATION_DB_FLAVOR environment variable and can have the values "cockroach"
|
|
|
|
// or "postgres". Defaults to "cockroach".
|
|
|
|
//
|
2023-09-15 14:58:45 +00:00
|
|
|
// The default Instance and Organisation are read from the DB and system
|
2023-05-02 16:24:24 +00:00
|
|
|
// users are created as needed.
|
|
|
|
//
|
|
|
|
// After the server is started, a [grpc.ClientConn] will be created and
|
|
|
|
// the server is polled for it's health status.
|
|
|
|
//
|
2023-09-15 14:58:45 +00:00
|
|
|
// Note: the database must already be setup and initialized before
|
2023-05-02 16:24:24 +00:00
|
|
|
// using NewTester. See the CONTRIBUTING.md document for details.
|
2024-03-14 09:56:23 +00:00
|
|
|
|
2024-01-17 10:16:48 +00:00
|
|
|
func NewTester(ctx context.Context, zitadelConfigYAML ...string) *Tester {
|
2023-04-25 15:11:04 +00:00
|
|
|
args := strings.Split(commandLine, " ")
|
|
|
|
|
2023-04-25 12:04:35 +00:00
|
|
|
sc := make(chan *start.Server)
|
2023-04-26 17:49:33 +00:00
|
|
|
//nolint:contextcheck
|
2023-04-25 15:11:04 +00:00
|
|
|
cmd := cmd.New(os.Stdout, os.Stdin, args, sc)
|
|
|
|
cmd.SetArgs(args)
|
2024-01-17 10:16:48 +00:00
|
|
|
for _, yaml := range append([]string{string(zitadelYAML)}, zitadelConfigYAML...) {
|
|
|
|
err := viper.MergeConfig(bytes.NewBuffer([]byte(yaml)))
|
|
|
|
logging.OnError(err).Fatal()
|
|
|
|
}
|
|
|
|
var err error
|
2023-04-25 15:11:04 +00:00
|
|
|
flavor := os.Getenv("INTEGRATION_DB_FLAVOR")
|
|
|
|
switch flavor {
|
2023-04-26 16:54:47 +00:00
|
|
|
case "cockroach", "":
|
2023-04-25 15:11:04 +00:00
|
|
|
err = viper.MergeConfig(bytes.NewBuffer(cockroachYAML))
|
|
|
|
case "postgres":
|
|
|
|
err = viper.MergeConfig(bytes.NewBuffer(postgresYAML))
|
|
|
|
default:
|
|
|
|
logging.New().WithField("flavor", flavor).Fatal("unknown db flavor set in INTEGRATION_DB_FLAVOR")
|
|
|
|
}
|
|
|
|
logging.OnError(err).Fatal()
|
|
|
|
|
2023-07-06 06:38:13 +00:00
|
|
|
tester := Tester{
|
2023-07-10 13:27:00 +00:00
|
|
|
Users: make(InstanceUserMap),
|
2023-07-06 06:38:13 +00:00
|
|
|
}
|
2023-09-15 14:58:45 +00:00
|
|
|
tester.MilestoneChan = make(chan []byte, 100)
|
|
|
|
tester.milestoneServer, err = runMilestoneServer(ctx, tester.MilestoneChan)
|
|
|
|
logging.OnError(err).Fatal()
|
|
|
|
tester.QuotaNotificationChan = make(chan []byte, 100)
|
|
|
|
tester.quotaNotificationServer, err = runQuotaServer(ctx, tester.QuotaNotificationChan)
|
|
|
|
logging.OnError(err).Fatal()
|
|
|
|
|
2023-04-25 12:04:35 +00:00
|
|
|
tester.wg.Add(1)
|
|
|
|
go func(wg *sync.WaitGroup) {
|
|
|
|
logging.OnError(cmd.Execute()).Fatal()
|
2023-04-25 15:11:04 +00:00
|
|
|
wg.Done()
|
2023-04-25 12:04:35 +00:00
|
|
|
}(&tester.wg)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case tester.Server = <-sc:
|
|
|
|
case <-ctx.Done():
|
|
|
|
logging.OnError(ctx.Err()).Fatal("waiting for integration tester server")
|
|
|
|
}
|
2023-11-22 09:29:38 +00:00
|
|
|
host := tester.Host()
|
|
|
|
tester.createClientConn(ctx, host)
|
2023-07-10 13:27:00 +00:00
|
|
|
tester.createLoginClient(ctx)
|
2023-11-22 09:29:38 +00:00
|
|
|
tester.WebAuthN = webauthn.NewClient(tester.Config.WebAuthNName, tester.Config.ExternalDomain, http_util.BuildOrigin(host, tester.Config.ExternalSecure))
|
2023-08-11 14:19:14 +00:00
|
|
|
tester.createMachineUserOrgOwner(ctx)
|
|
|
|
tester.createMachineUserInstanceOwner(ctx)
|
2023-06-07 15:28:42 +00:00
|
|
|
tester.WebAuthN = webauthn.NewClient(tester.Config.WebAuthNName, tester.Config.ExternalDomain, "https://"+tester.Host())
|
2023-07-06 06:38:13 +00:00
|
|
|
return &tester
|
2023-04-25 12:04:35 +00:00
|
|
|
}
|
2023-04-28 11:39:53 +00:00
|
|
|
|
|
|
|
func Contexts(timeout time.Duration) (ctx, errCtx context.Context, cancel context.CancelFunc) {
|
|
|
|
errCtx, cancel = context.WithCancel(context.Background())
|
|
|
|
cancel()
|
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), timeout)
|
|
|
|
return ctx, errCtx, cancel
|
|
|
|
}
|
2023-09-15 14:58:45 +00:00
|
|
|
|
|
|
|
func runMilestoneServer(ctx context.Context, bodies chan []byte) (*httptest.Server, error) {
|
|
|
|
mockServer := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
body, err := io.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if r.Header.Get("single-value") != "single-value" {
|
|
|
|
http.Error(w, "single-value header not set", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if reflect.DeepEqual(r.Header.Get("multi-value"), "multi-value-1,multi-value-2") {
|
|
|
|
http.Error(w, "single-value header not set", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
bodies <- body
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}))
|
|
|
|
config := net.ListenConfig()
|
|
|
|
listener, err := config.Listen(ctx, "tcp", ":"+PortMilestoneServer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
mockServer.Listener = listener
|
|
|
|
mockServer.Start()
|
|
|
|
return mockServer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func runQuotaServer(ctx context.Context, bodies chan []byte) (*httptest.Server, error) {
|
|
|
|
mockServer := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
body, err := io.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
bodies <- body
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}))
|
|
|
|
config := net.ListenConfig()
|
|
|
|
listener, err := config.Listen(ctx, "tcp", ":"+PortQuotaServer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
mockServer.Listener = listener
|
|
|
|
mockServer.Start()
|
|
|
|
return mockServer, nil
|
|
|
|
}
|
2024-08-12 20:32:01 +00:00
|
|
|
|
|
|
|
func (s *Tester) updateInstanceAndOrg(ctx context.Context, domain string) context.Context {
|
|
|
|
var err error
|
|
|
|
s.Instance, err = s.Queries.InstanceByHost(ctx, domain, "")
|
|
|
|
logging.OnError(err).Fatal("query instance")
|
|
|
|
ctx = authz.WithInstance(ctx, s.Instance)
|
|
|
|
|
|
|
|
s.Organisation, err = s.Queries.OrgByID(ctx, true, s.Instance.DefaultOrganisationID())
|
|
|
|
logging.OnError(err).Fatal("query organisation")
|
|
|
|
return ctx
|
|
|
|
}
|
2024-08-17 14:48:06 +00:00
|
|
|
|
|
|
|
func await(af func() error) error {
|
|
|
|
maxTimer := time.NewTimer(15 * time.Minute)
|
|
|
|
for {
|
|
|
|
err := af()
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-maxTimer.C:
|
|
|
|
return err
|
|
|
|
case <-time.After(time.Second / 10):
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|