summaryrefslogtreecommitdiff
path: root/pkg/sts/server_test.go
blob: 9c544751d962bcc89c6c6fd1b2e3e3a7abcd02a7 (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
package sts

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

	"github.com/oauth2-proxy/mockoidc"
	"github.com/stretchr/testify/assert"
	"github.com/xlgmokha/x/pkg/test"
	"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/web"
)

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

	t.Run("New", func(t *testing.T) {
		server := New(t.Context())

		t.Run("GET /", func(t *testing.T) {
			r, w := test.RequestResponse("GET", "/")

			server.ServeHTTP(w, r)
			assert.Equal(t, http.StatusNotFound, w.Code)
		})

		t.Run("POST /oauth/token", func(t *testing.T) {
			code := strconv.FormatInt(time.Now().Unix(), 10)
			user := mockoidc.DefaultUser()
			srv.MockOIDC.QueueUser(user)
			srv.MockOIDC.QueueCode(code)

			r, w := test.RequestResponse("POST", "/oauth/token")

			server.ServeHTTP(w, r)
			assert.Equal(t, http.StatusCreated, w.Code)
		})
	})
}