/
04-constraints_spec.lua
214 lines (186 loc) · 6.65 KB
/
04-constraints_spec.lua
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
local helpers = require "spec.02-integration.02-dao.helpers"
local Factory = require "kong.dao.factory"
local utils = require "kong.tools.utils"
local api_tbl = {
name = "mockbin",
hosts = { "mockbin.com" },
uris = { "/mockbin" },
strip_uri = true,
upstream_url = "https://mockbin.com"
}
local plugin_tbl = {
name = "key-auth"
}
helpers.for_each_dao(function(kong_config)
describe("Model (Constraints) with DB: #"..kong_config.database, function()
local plugin_fixture, api_fixture
local factory, apis, plugins
setup(function()
factory = assert(Factory.new(kong_config))
apis = factory.apis
plugins = factory.plugins
assert(factory:run_migrations())
factory:truncate_tables()
end)
before_each(function()
plugin_fixture = utils.shallow_copy(plugin_tbl)
local api, err = apis:insert(api_tbl)
assert.falsy(err)
api_fixture = api
end)
after_each(function()
factory:truncate_tables()
end)
-- Check behavior just in case
describe("plugins insert()", function()
it("insert a valid plugin", function()
plugin_fixture.api_id = api_fixture.id
local plugin, err = plugins:insert(plugin_fixture)
assert.falsy(err)
assert.is_table(plugin)
assert.equal(api_fixture.id, plugin.api_id)
assert.same({hide_credentials = false, key_names = {"apikey"}, anonymous = "", key_in_body = false,}, plugin.config)
end)
it("insert a valid plugin bis", function()
plugin_fixture.api_id = api_fixture.id
plugin_fixture.config = {key_names = {"api-key"}}
local plugin, err = plugins:insert(plugin_fixture)
assert.falsy(err)
assert.is_table(plugin)
assert.equal(api_fixture.id, plugin.api_id)
assert.same({hide_credentials = false, key_names = {"api-key"}, anonymous = "", key_in_body = false}, plugin.config)
end)
describe("unique per API/Consumer", function()
it("API/Plugin", function()
plugin_fixture.api_id = api_fixture.id
local plugin, err = plugins:insert(plugin_fixture)
assert.falsy(err)
assert.truthy(plugin)
plugin, err = plugins:insert(plugin_fixture)
assert.truthy(err)
assert.falsy(plugin)
assert.True(err.unique)
assert.matches("[" .. kong_config.database .. " error] " ..
"name=already exists with value 'key-auth'",
err, nil, true)
end)
it("API/Consumer/Plugin", function()
local consumer, err = factory.consumers:insert {
username = "bob"
}
assert.falsy(err)
assert.truthy(consumer)
local plugin_tbl = {
name = "rate-limiting",
api_id = api_fixture.id,
consumer_id = consumer.id,
config = {minute = 1}
}
local plugin, err = plugins:insert(plugin_tbl)
assert.falsy(err)
assert.truthy(plugin)
assert.equal(consumer.id, plugin.consumer_id)
plugin, err = plugins:insert(plugin_tbl)
assert.truthy(err)
assert.falsy(plugin)
assert.True(err.unique)
assert.matches("[" .. kong_config.database .. " error] " ..
"name=already exists with value 'rate-limiting'",
err, nil, true)
end)
end)
end)
describe("FOREIGN constraints", function()
it("not insert plugin if invalid API foreign key", function()
plugin_fixture.api_id = utils.uuid()
local plugin, err = plugins:insert(plugin_fixture)
assert.falsy(plugin)
assert.truthy(err)
assert.True(err.foreign)
assert.matches("api_id=does not exist with value '"..plugin_fixture.api_id.."'", tostring(err), nil, true)
end)
it("not insert plugin if invalid Consumer foreign key", function()
local plugin_tbl = {
name = "rate-limiting",
api_id = api_fixture.id,
consumer_id = utils.uuid(),
config = {minute = 1}
}
local plugin, err = plugins:insert(plugin_tbl)
assert.falsy(plugin)
assert.truthy(err)
assert.True(err.foreign)
assert.matches("consumer_id=does not exist with value '"..plugin_tbl.consumer_id.."'", tostring(err), nil, true)
end)
it("does not update plugin if invalid foreign key", function()
plugin_fixture.api_id = api_fixture.id
local plugin, err = plugins:insert(plugin_fixture)
assert.falsy(err)
assert.truthy(plugin)
local fake_api_id = utils.uuid()
plugin.api_id = fake_api_id
plugin, err = plugins:update(plugin, {id = plugin.id})
assert.falsy(plugin)
assert.truthy(err)
assert.True(err.foreign)
assert.matches("api_id=does not exist with value '"..fake_api_id.."'", tostring(err), nil, true)
end)
end)
describe("CASCADE delete", function()
local api_fixture, consumer_fixture
before_each(function()
local err
api_fixture, err = apis:insert {
name = "to-delete",
hosts = { "to-delete.com" },
uris = { "/to-delete" },
upstream_url = "https://mockbin.com"
}
assert.falsy(err)
consumer_fixture, err = factory.consumers:insert {
username = "bob"
}
assert.falsy(err)
end)
after_each(function()
factory:truncate_tables()
end)
it("delete", function()
local plugin, err = plugins:insert {
name = "key-auth",
api_id = api_fixture.id
}
assert.falsy(err)
local res, err = apis:delete(api_fixture)
assert.falsy(err)
assert.is_table(res)
-- no more API
local api, err = apis:find(api_fixture)
assert.falsy(err)
assert.falsy(api)
-- no more plugin
local plugin, err = plugins:find(plugin)
assert.falsy(err)
assert.falsy(plugin)
end)
it("delete bis", function()
local plugin, err = plugins:insert {
name = "rate-limiting",
api_id = api_fixture.id,
consumer_id = consumer_fixture.id,
config = {minute = 1}
}
assert.falsy(err)
local res, err = factory.consumers:delete(consumer_fixture)
assert.falsy(err)
assert.is_table(res)
local consumer, err = factory.consumers:find(consumer_fixture)
assert.falsy(err)
assert.falsy(consumer)
plugin, err = plugins:find(plugin)
assert.falsy(err)
assert.falsy(plugin)
end)
end)
end) -- describe
end) -- for each db