File: entity_crud_test.go

package info (click to toggle)
golang-github-kong-go-kong 0.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 620 kB
  • sloc: sh: 18; makefile: 4
file content (91 lines) | stat: -rw-r--r-- 2,097 bytes parent folder | download
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
82
83
84
85
86
87
88
89
90
91
package custom

import (
	"testing"

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

func TestRender(t *testing.T) {
	assert := assert.New(t)

	entity := NewEntityObject("key-auth")
	entity.AddRelation("consumer_id", "bob")
	result, err := render("/consumers/${consumer_id}/key-auths", entity)
	assert.Nil(err)
	assert.Equal(result, "/consumers/bob/key-auths")

	result, err = render("/consumers/${random_id}/key-auths", entity)
	assert.NotNil(err)
	assert.Equal(result, "")
}

func TestEntityCRUDDefinition(t *testing.T) {
	assert := assert.New(t)

	var typ Type = "foo"
	e := EntityCRUDDefinition{
		Name:       typ,
		CRUDPath:   "/consumers/${consumer_id}/foo",
		PrimaryKey: "id",
	}

	var object Object = map[string]interface{}{
		"id":   "unique-id",
		"key1": "value1",
		"key2": "value2",
	}
	entity := NewEntityObject(typ)
	entity.SetObject(object)
	entity.AddRelation("consumer_id", "gopher")

	assert.Equal(typ, e.Type())
	url, err := e.GetEndpoint(entity)
	assert.Nil(err)
	assert.Equal("/consumers/gopher/foo/unique-id", url)

	url, err = e.PatchEndpoint(entity)
	assert.Nil(err)
	assert.Equal("/consumers/gopher/foo/unique-id", url)

	url, err = e.DeleteEndpoint(entity)
	assert.Nil(err)
	assert.Equal("/consumers/gopher/foo/unique-id", url)

	url, err = e.PostEndpoint(entity)
	assert.Nil(err)
	assert.Equal("/consumers/gopher/foo", url)

	url, err = e.ListEndpoint(entity)
	assert.Nil(err)
	assert.Equal("/consumers/gopher/foo", url)

	entity = NewEntityObject(typ)
	entity.SetObject(object)
	url, err = e.GetEndpoint(entity)
	assert.NotNil(err)
	assert.Empty(url)

	object = map[string]interface{}{
		"key1": "value1",
		"key2": "value2",
	}
	entity = NewEntityObject(typ)
	entity.AddRelation("consumer_id", "gopher")
	entity.SetObject(object)
	url, err = e.GetEndpoint(entity)
	assert.NotNil(err)
	assert.Empty(url)

	object = map[string]interface{}{
		"id":   -1,
		"key1": "value1",
		"key2": "value2",
	}
	entity = NewEntityObject(typ)
	entity.SetObject(object)
	entity.AddRelation("consumer_id", "gopher")
	url, err = e.GetEndpoint(entity)
	assert.NotNil(err)
	assert.Empty(url)
}