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

import (
	http "net/http"
	"net/http/httptest"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestServer(t *testing.T) {
	handler := New()
	srv := httptest.NewServer(handler)
	defer srv.Close()

	t.Run("Ability.Allowed", func(t *testing.T) {
		client := NewAbilityProtobufClient(srv.URL, &http.Client{})

		t.Run("forbids", func(t *testing.T) {
			reply, err := client.Allowed(t.Context(), &AllowRequest{
				Subject:    "",
				Permission: "",
				Resource:   "",
			})
			require.NoError(t, err)
			assert.False(t, reply.Result)
		})

		t.Run("allows alice:view:jane_vacation", func(t *testing.T) {
			reply, err := client.Allowed(t.Context(), &AllowRequest{
				Subject:    "gid://example/User/alice",
				Permission: "view",
				Resource:   "gid://example/Album/jane_vacation",
			})
			require.NoError(t, err)
			assert.True(t, reply.Result)
		})

		t.Run("allows gid://User/1 read gid://Organization/2", func(t *testing.T) {
			reply, err := client.Allowed(t.Context(), &AllowRequest{
				Subject:    "gid://example/User/1",
				Permission: "read",
				Resource:   "gid://example/Organization/2",
			})
			require.NoError(t, err)
			assert.True(t, reply.Result)
		})
	})

	t.Run("GET /health", func(t *testing.T) {
		t.Run("returns OK", func(t *testing.T) {
			r := httptest.NewRequest("GET", "/health", nil)
			w := httptest.NewRecorder()

			handler.ServeHTTP(w, r)

			assert.Equal(t, http.StatusOK, w.Code)
		})
	})
}