summaryrefslogtreecommitdiff
path: root/test/integration/container_test.go
blob: 3d8f6bc46fd93581d128ccd988e1241be56c6618 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//go:build integration
// +build integration

package test

import (
	"context"
	"net/http"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"github.com/testcontainers/testcontainers-go"
	"github.com/xlgmokha/x/pkg/env"
	"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/web"
)

func environmentVariables(srv *web.OIDCServer, databaseURL string) map[string]string {
	return map[string]string{
		"APP_ENV":             "test",
		"AUTHZD_HOST":         "",
		"DATABASE_URL":        databaseURL,
		"DEBUG":               env.Fetch("DEBUG", ""),
		"HMAC_SESSION_SECRET": "secret",
		"LOG_LEVEL":           "warn",
		"OAUTH_CLIENT_ID":     srv.MockOIDC.ClientID,
		"OAUTH_CLIENT_SECRET": srv.MockOIDC.ClientSecret,
		"OIDC_ISSUER":         srv.Issuer(),
		"RUNWAY_PG_USER_POSTGRES_PASSWORD_SPARKLE": "secret",
		"SPICEDB_ENDPOINT":                         ":50051",
		"SPICEDB_TOKEN":                            "secret",
	}
}

func TestContainer(t *testing.T) {
	srv := web.NewOIDCServer(t)
	defer srv.Close()

	ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
	defer cancel()

	pgContainer := NewPgContainer(ctx, t)
	defer pgContainer.Terminate(ctx)

	databaseURL, err := pgContainer.ConnectionString(ctx, "sslmode=disable")
	require.NoError(t, err)

	container := NewContainer(t, ctx, environmentVariables(srv, databaseURL))
	defer testcontainers.TerminateContainer(container)

	require.True(t, container.IsRunning())

	envoyEndpoint, err := container.PortEndpoint(ctx, "10000", "http")
	require.NoError(t, err)

	for _, publicPath := range []string{
		envoyEndpoint + "/",
		envoyEndpoint + "/application.js",
		envoyEndpoint + "/favicon.ico",
		envoyEndpoint + "/favicon.png",
		envoyEndpoint + "/health",
		envoyEndpoint + "/index.html",
		envoyEndpoint + "/logo.png",
		srv.DiscoveryEndpoint(),
	} {
		t.Run(publicPath, func(t *testing.T) {
			assert.Equal(t, http.StatusOK, HttpGet(t, ctx, publicPath).StatusCode)
		})
	}
}