summaryrefslogtreecommitdiff
path: root/pkg/gid/gid_test.go
blob: e1f6285b099a300a3f7a8dc33f2a974aac9dfc13 (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
package gid

import (
	"testing"

	"github.com/cedar-policy/cedar-go"
	"github.com/stretchr/testify/assert"
)

func TestNewEntityUID(t *testing.T) {
	t.Run("returns an Entity UID with an integer id", func(t *testing.T) {
		result := NewEntityUID("gid://example/User/1")

		assert.Equal(t, cedar.EntityType("User"), result.Type)
		assert.Equal(t, cedar.String("1"), result.ID)
	})

	t.Run("returns an Entity UID with a UUID", func(t *testing.T) {
		result := NewEntityUID("gid://example/User/4707ce42-1017-11f0-acdf-7ec11f4b308c")

		assert.Equal(t, cedar.EntityType("User"), result.Type)
		assert.Equal(t, cedar.String("4707ce42-1017-11f0-acdf-7ec11f4b308c"), result.ID)
	})

	t.Run("returns an Entity UID with a namespace", func(t *testing.T) {
		result := NewEntityUID("gid://example/Authn::User/1")

		assert.Equal(t, cedar.EntityType("Authn::User"), result.Type)
		assert.Equal(t, cedar.String("1"), result.ID)
	})

	t.Run("returns a default when a global id is not provided", func(t *testing.T) {
		result := NewEntityUID("alice")

		assert.Equal(t, cedar.EntityType("User"), result.Type)
		assert.Equal(t, cedar.String("alice"), result.ID)
	})
}