summaryrefslogtreecommitdiff
path: root/pkg/authz/server_test.go
blob: 47f2219126fa9f35492596622a22254c986a74a1 (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
package authz

import (
	"context"
	"net"
	"testing"

	auth "github.com/envoyproxy/go-control-plane/envoy/service/auth/v3"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"google.golang.org/grpc"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/credentials/insecure"
	"google.golang.org/grpc/test/bufconn"
)

type HTTPRequest = auth.AttributeContext_HttpRequest

func TestServer(t *testing.T) {
	socket := bufconn.Listen(1024 * 1024)
	srv := New(t.Context())

	defer srv.GracefulStop()
	go func() {
		require.NoError(t, srv.Serve(socket))
	}()

	connection, err := grpc.DialContext(
		t.Context(),
		"bufnet",
		grpc.WithContextDialer(func(context.Context, string) (net.Conn, error) {
			return socket.Dial()
		}),
		grpc.WithTransportCredentials(insecure.NewCredentials()),
	)
	require.NoError(t, err)
	defer connection.Close()

	client := auth.NewAuthorizationClient(connection)

	t.Run("CheckRequest", func(t *testing.T) {
		tt := []struct {
			http   *HTTPRequest
			status codes.Code
		}{
			{status: codes.OK, http: &HTTPRequest{Method: "GET", Path: "/application.js"}},
			{status: codes.OK, http: &HTTPRequest{Method: "GET", Path: "/favicon.ico"}},
			{status: codes.OK, http: &HTTPRequest{Method: "GET", Path: "/favicon.png"}},
			{status: codes.OK, http: &HTTPRequest{Method: "GET", Path: "/index.html"}},
			{status: codes.OK, http: &HTTPRequest{Method: "GET", Path: "/application.css"}},
		}

		for _, example := range tt {
			t.Run(example.http.Path, func(t *testing.T) {
				response, err := client.Check(t.Context(), &auth.CheckRequest{
					Attributes: &auth.AttributeContext{
						Request: &auth.AttributeContext_Request{
							Http: example.http,
						},
					},
				})
				require.NoError(t, err)
				assert.Equal(t, int32(example.status), response.Status.Code)
			})
		}
	})
}