Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cmd/portal/cmd/cmdinternal/configsource.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,18 @@ var cmdInternalConfigSourceCreate = &cobra.Command{
return err
}

upsert, err := cmd.Flags().GetBool("upsert")
if err != nil {
return err
}

resourceDir := args[0]

err = internal.Create(cmd.Context(), &internal.CreateOptions{
DatabaseURL: dbURL,
DatabaseSchema: dbSchema,
ResourceDir: resourceDir,
Upsert: upsert,
})
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions cmd/portal/cmd/cmdinternal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func init() {

binder.BindString(cmdInternalConfigSourceCreate.Flags(), portalcmd.ArgDatabaseURL)
binder.BindString(cmdInternalConfigSourceCreate.Flags(), portalcmd.ArgDatabaseSchema)
cmdInternalConfigSourceCreate.Flags().Bool("upsert", false, "Update existing config source if app ID already exists")

binder.BindString(cmdInternalConfigSourceUnpack.Flags(), portalcmd.ArgDataJSONFilePath)
binder.BindString(cmdInternalConfigSourceUnpack.Flags(), portalcmd.ArgOutputDirectoryPath)
Expand Down
9 changes: 7 additions & 2 deletions cmd/portal/internal/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type CreateOptions struct {
DatabaseURL string
DatabaseSchema string
ResourceDir string
Upsert bool
}

func Create(ctx context.Context, opt *CreateOptions) error {
Expand Down Expand Up @@ -47,7 +48,7 @@ func Create(ctx context.Context, opt *CreateOptions) error {
}
defer func() { _ = tx.Rollback() }()

if err := createConfigSource(ctx, tx, appID, data); err != nil {
if err := createConfigSource(ctx, tx, appID, data, opt.Upsert); err != nil {
return fmt.Errorf("failed to create config source record: %w", err)
}

Expand All @@ -59,7 +60,7 @@ func Create(ctx context.Context, opt *CreateOptions) error {
}

// create config source record in db
func createConfigSource(ctx context.Context, tx *sql.Tx, appID string, data map[string]string) error {
func createConfigSource(ctx context.Context, tx *sql.Tx, appID string, data map[string]string, upsert bool) error {
dataJSON, err := json.Marshal(data)
if err != nil {
return err
Expand All @@ -84,6 +85,10 @@ func createConfigSource(ctx context.Context, tx *sql.Tx, appID string, data map[
time.Now().UTC(),
)

if upsert {
builder = builder.Suffix("ON CONFLICT (app_id) DO UPDATE SET data = EXCLUDED.data, updated_at = EXCLUDED.updated_at")
}

q, args, err := builder.ToSql()
if err != nil {
return err
Expand Down
8 changes: 8 additions & 0 deletions e2e/.env
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,16 @@ DATABASE_CONFIG_MAX_OPEN_CONN=10
DATABASE_CONFIG_MAX_IDLE_CONN=10

ADMIN_API_AUTH=none
ADMIN_API_ENDPOINT=http://localhost:4002

SMTP_HOST=127.0.0.1
SMTP_PORT=2525
SMTP_USERNAME=
SMTP_PASSWORD=

# Site Admin API server
SITEADMIN_LISTEN_ADDR=0.0.0.0:4003
SITEADMIN_INTERNAL_LISTEN_ADDR=0.0.0.0:14003
SITEADMIN_AUTHGEAR_APP_ID=e2e-portal
# Required by config validation even in cookie mode; value is not used for JWKS in cookie mode
SITEADMIN_AUTHGEAR_ENDPOINT=http://localhost:4000
9 changes: 7 additions & 2 deletions e2e/cmd/e2e/pkg/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ import (
"os/exec"
)

func CreatePortalConfigSource(dbURL string, dbSchema string, resourceDir string) error {
func CreatePortalConfigSource(dbURL string, dbSchema string, resourceDir string, upsert bool) error {
upsertFlag := ""
if upsert {
upsertFlag = "--upsert"
}
cmd := fmt.Sprintf(
"../dist/authgear-portal internal configsource create %s --database-url=\"%s\" --database-schema=\"%s\"",
"../dist/authgear-portal internal configsource create %s --database-url=\"%s\" --database-schema=\"%s\" %s",
resourceDir,
dbURL,
dbSchema,
upsertFlag,
)
return ExecCmd(cmd)
}
Expand Down
1 change: 1 addition & 0 deletions e2e/cmd/e2e/pkg/configsource.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func (c *End2End) CreateApp(ctx context.Context, appID string, baseConfigSourceD
cfg.GlobalDatabase.DatabaseURL,
cfg.GlobalDatabase.DatabaseSchema,
configSourceDir,
true,
)
if err != nil {
return err
Expand Down
7 changes: 6 additions & 1 deletion e2e/pkg/testrunner/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,18 @@ func generateAppID() string {
}

type NewEnd2EndCmdOptions struct {
AppID string
TestCase *TestCase
Test testing.TB
}

func NewEnd2EndCmd(options NewEnd2EndCmdOptions) (*End2EndCmd, error) {
appID := options.AppID
if appID == "" {
appID = generateAppID()
}
e := &End2EndCmd{
AppID: generateAppID(),
AppID: appID,
TestCase: *options.TestCase,
Test: options.Test,
}
Expand Down
6 changes: 5 additions & 1 deletion e2e/pkg/testrunner/testcase.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var _ = TestCaseSchema.Add("TestCase", `
"properties": {
"name": { "type": "string" },
"focus": { "type": "boolean" },
"app_id": { "type": "string", "description": "Fixed app ID; random if omitted" },
"authgear.yaml": { "$ref": "#/$defs/AuthgearYAMLSource" },
"authgear.features.yaml": { "$ref": "#/$defs/AuthgearFeaturesYAMLSource" },
"extra_files_directory": { "type": "string" },
Expand All @@ -44,7 +45,9 @@ type TestCase struct {
Path string `json:"path"`
// Applying focus to a test case will make it the only test case to run,
// mainly used for debugging new test cases.
Focus bool `json:"focus"`
Focus bool `json:"focus"`
// AppID fixes the app ID for this test case. If empty, a random ID is generated.
AppID string `json:"app_id"`
AuthgearYAMLSource AuthgearYAMLSource `json:"authgear.yaml"`
AuthgearFeaturesYAMLSource AuthgearFeaturesYAMLSource `json:"authgear.features.yaml"`
ExtraFilesDirectory string `json:"extra_files_directory"`
Expand All @@ -59,6 +62,7 @@ func (tc *TestCase) FullName() string {
func (tc *TestCase) Run(t *testing.T) {
// Create project per test case
cmd, err := NewEnd2EndCmd(NewEnd2EndCmdOptions{
AppID: tc.AppID,
TestCase: tc,
Test: t,
})
Expand Down
17 changes: 17 additions & 0 deletions e2e/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,30 @@ function setup {( set -e
authgear audit database migrate up
authgear images database migrate up
authgear-portal database migrate up

echo "[ ] Starting siteadmin..."
authgear-portal start siteadmin > ./logs/siteadmin.log 2>&1 &
success=false
for i in $(seq 10); do \
if [ "$(curl -sL -w '%{http_code}' -o /dev/null http://localhost:4003/healthz)" = "200" ]; then
echo " - started siteadmin."
success=true
break
fi
sleep 1
done
if [ "$success" = false ]; then
echo "Error: Failed to start siteadmin."
exit 1
fi
)}

function teardown {( set -e
echo "[ ] Teardown..."
kill -9 $(lsof -ti:4000) > /dev/null 2>&1 || true
kill -9 $(lsof -ti:4001) > /dev/null 2>&1 || true
kill -9 $(lsof -ti:4002) > /dev/null 2>&1 || true
kill -9 $(lsof -ti:4003) > /dev/null 2>&1 || true
kill -9 $(lsof -ti:8080) > /dev/null 2>&1 || true
kill -9 $(lsof -ti:2525) > /dev/null 2>&1 || true
docker compose down
Expand Down
Loading
Loading