summaryrefslogtreecommitdiff
path: root/test/e2e_test.go
blob: f14410a04f79e7a042de0c0bcdcf638921a9b1bf (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
72
73
74
75
76
77
78
79
80
81
package main

import (
	"net/http"
	"strings"
	"testing"

	"github.com/playwright-community/playwright-go"
	"github.com/stretchr/testify/assert"
	"github.com/xlgmokha/x/pkg/env"
	"github.com/xlgmokha/x/pkg/serde"
	"github.com/xlgmokha/x/pkg/x"
)

type OAuthTokens struct {
	AccessToken  string `json:"access_token"`
	TokenType    string `json:"token_type"`
	ExpiresIn    uint64 `json:"expires_in"`
	RefreshToken string `json:"refresh_token"`
}

func TestHelloWorld(t *testing.T) {
	_ = playwright.Install()

	pw := x.Must(playwright.Run())
	browser := x.Must(pw.Chromium.Launch(playwright.BrowserTypeLaunchOptions{
		Headless: playwright.Bool(env.Fetch("HEADLESS", "true") == "true"),
		SlowMo:   playwright.Float(1000),
	}))
	page := x.Must(browser.NewPage())

	defer func() {
		x.Check(browser.Close())
		x.Check(pw.Stop())
	}()

	t.Run("SAML", func(t *testing.T) {
		t.Run("IdP", func(t *testing.T) {
			t.Run("provides metadata", func(t *testing.T) {
				response := x.Must(http.Get("http://idp.example.com:8080/saml/metadata.xml"))
				assert.Equal(t, http.StatusOK, response.StatusCode)
			})
		})

		t.Run("Service provider", func(t *testing.T) {
			t.Run("provides metadata", func(t *testing.T) {
				response := x.Must(http.Get("http://ui.example.com:8080/saml/metadata.xml"))
				assert.Equal(t, http.StatusOK, response.StatusCode)
			})

			t.Run("starts a new session with the IdP", func(t *testing.T) {
				x.Must(page.Goto("http://ui.example.com:8080/saml/new"))
				action := x.Must(page.Locator("#idp-form").GetAttribute("action"))
				assert.Equal(t, "http://idp.example.com:8080/saml/new", action)
				assert.NoError(t, page.Locator("#submit-button").Click())

				action = x.Must(page.Locator("#postback-form").GetAttribute("action"))
				assert.Equal(t, "http://ui.example.com:8080/saml/assertions", action)
				assert.NoError(t, page.Locator("#submit-button").Click())
				assert.Contains(t, x.Must(page.Content()), "Received SAML Response")
			})
		})
	})

	t.Run("OIDC", func(t *testing.T) {
		t.Run("Performs an OIDC login", func(t *testing.T) {
			x.Must(page.Goto("http://ui.example.com:8080/oidc/new"))
			assert.Contains(t, page.URL(), "http://idp.example.com:8080/oauth/authorize")
			assert.NoError(t, page.Locator("#submit-button").Click())

			assert.Contains(t, page.URL(), "http://ui.example.com:8080/oauth/callback")
			content := x.Must(page.Locator("pre").First().InnerText())
			item := x.Must(serde.FromJSON[OAuthTokens](strings.NewReader(content)))
			assert.NotEmpty(t, item.AccessToken)
			assert.Equal(t, "Bearer", item.TokenType)
			assert.NotEmpty(t, item.RefreshToken)
			// header = { 'Authorization' => "Bearer #{token}" }
			// http.Get("http://api.example.com:8080/projects.json")
		})
	})
}