File: consumer_test.go

package info (click to toggle)
deck 1.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,080 kB
  • sloc: makefile: 17; sh: 3
file content (161 lines) | stat: -rw-r--r-- 3,428 bytes parent folder | download | duplicates (2)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package state

import (
	"testing"

	"github.com/kong/go-kong/kong"
	"github.com/stretchr/testify/assert"
)

func consumersCollection() *ConsumersCollection {
	return state().Consumers
}

func TestConsumerInsert(t *testing.T) {
	assert := assert.New(t)
	collection := consumersCollection()

	var consumer Consumer

	assert.NotNil(collection.Add(consumer))

	consumer.ID = kong.String("first")
	assert.Nil(collection.Add(consumer))

	//re-insert
	consumer.Username = kong.String("my-name")
	assert.NotNil(collection.Add(consumer))
}

func TestConsumerGetUpdate(t *testing.T) {
	assert := assert.New(t)
	collection := consumersCollection()

	var consumer Consumer
	consumer.ID = kong.String("first")
	consumer.Username = kong.String("my-name")
	err := collection.Add(consumer)
	assert.Nil(err)

	c, err := collection.Get("")
	assert.NotNil(err)
	assert.Nil(c)

	c, err = collection.Get("first")
	assert.Nil(err)
	assert.NotNil(c)

	c.ID = nil
	c.Username = kong.String("my-updated-name")
	err = collection.Update(*c)
	assert.NotNil(err)

	c.ID = kong.String("does-not-exist")
	assert.NotNil(collection.Update(*c))

	c.ID = kong.String("first")
	assert.Nil(collection.Update(*c))

	c, err = collection.Get("my-name")
	assert.NotNil(err)
	assert.Nil(c)

	c, err = collection.Get("my-updated-name")
	assert.Nil(err)
	assert.NotNil(c)
}

// Test to ensure that the memory reference of the pointer returned by Get()
// is different from the one stored in MemDB.
func TestConsumerGetMemoryReference(t *testing.T) {
	assert := assert.New(t)
	collection := consumersCollection()

	var consumer Consumer
	consumer.ID = kong.String("first")
	consumer.Username = kong.String("my-name")
	err := collection.Add(consumer)
	assert.Nil(err)

	c, err := collection.Get("first")
	assert.Nil(err)
	assert.NotNil(c)
	c.Username = kong.String("update-should-not-reflect")

	c, err = collection.Get("first")
	assert.Nil(err)
	assert.Equal("my-name", *c.Username)
}

func TestConsumersInvalidType(t *testing.T) {
	assert := assert.New(t)
	collection := consumersCollection()

	type c2 Consumer
	var c c2
	c.Username = kong.String("my-name")
	c.ID = kong.String("first")
	txn := collection.db.Txn(true)
	assert.Nil(txn.Insert(consumerTableName, &c))
	txn.Commit()

	assert.Panics(func() {
		collection.Get("my-name")
	})
	assert.Panics(func() {
		collection.GetAll()
	})
}

func TestConsumerDelete(t *testing.T) {
	assert := assert.New(t)
	collection := consumersCollection()

	var consumer Consumer
	consumer.ID = kong.String("first")
	consumer.Username = kong.String("my-consumer")
	err := collection.Add(consumer)
	assert.Nil(err)

	c, err := collection.Get("my-consumer")
	assert.Nil(err)
	assert.NotNil(c)
	assert.Equal("first", *c.ID)

	err = collection.Delete("first")
	assert.Nil(err)

	err = collection.Delete("")
	assert.NotNil(err)

	err = collection.Delete(*c.ID)
	assert.NotNil(err)
}

func TestConsumerGetAll(t *testing.T) {
	assert := assert.New(t)
	collection := consumersCollection()

	consumers := []Consumer{
		{
			Consumer: kong.Consumer{
				ID:       kong.String("first"),
				Username: kong.String("my-consumer1"),
			},
		},
		{
			Consumer: kong.Consumer{
				ID:       kong.String("second"),
				Username: kong.String("my-consumer2"),
			},
		},
	}
	for _, s := range consumers {
		assert.Nil(collection.Add(s))
	}

	allConsumers, err := collection.GetAll()

	assert.Nil(err)
	assert.Equal(len(consumers), len(allConsumers))
}