|
| 1 | +local helpers = require "spec.helpers" |
| 2 | +local cjson = require "cjson" |
| 3 | +local uuid = require("kong.tools.uuid").uuid |
| 4 | + |
| 5 | + |
| 6 | +describe("an old plugin: translate_backwards", function() |
| 7 | + local bp, db, route, admin_client |
| 8 | + local plugin_id = uuid() |
| 9 | + |
| 10 | + lazy_setup(function() |
| 11 | + helpers.test_conf.lua_package_path = helpers.test_conf.lua_package_path .. ";./spec-ee/fixtures/custom_plugins/?.lua" |
| 12 | + bp, db = helpers.get_db_utils(nil, { |
| 13 | + "plugins", |
| 14 | + }, { 'translate-backwards-older-plugin' }) |
| 15 | + |
| 16 | + route = assert(bp.routes:insert { |
| 17 | + hosts = { "redis.test" }, |
| 18 | + }) |
| 19 | + |
| 20 | + assert(helpers.start_kong({ |
| 21 | + plugins = "bundled,translate-backwards-older-plugin", |
| 22 | + nginx_conf = "spec/fixtures/custom_nginx.template", |
| 23 | + lua_package_path = "?./spec-ee/fixtures/custom_plugins/?.lua", |
| 24 | + })) |
| 25 | + |
| 26 | + admin_client = assert(helpers.admin_client()) |
| 27 | + end) |
| 28 | + |
| 29 | + lazy_teardown(function() |
| 30 | + if admin_client then |
| 31 | + admin_client:close() |
| 32 | + end |
| 33 | + |
| 34 | + helpers.stop_kong() |
| 35 | + end) |
| 36 | + |
| 37 | + describe("when creating custom plugin", function() |
| 38 | + after_each(function() |
| 39 | + db:truncate("plugins") |
| 40 | + end) |
| 41 | + |
| 42 | + describe("when using the new field", function() |
| 43 | + it("creates the custom plugin and fills in old field in response", function() |
| 44 | + -- POST |
| 45 | + local res = assert(admin_client:send { |
| 46 | + method = "POST", |
| 47 | + route = { |
| 48 | + id = route.id |
| 49 | + }, |
| 50 | + path = "/plugins", |
| 51 | + headers = { ["Content-Type"] = "application/json" }, |
| 52 | + body = { |
| 53 | + id = plugin_id, |
| 54 | + name = "translate-backwards-older-plugin", |
| 55 | + config = { |
| 56 | + new_field = "ABC" |
| 57 | + }, |
| 58 | + }, |
| 59 | + }) |
| 60 | + |
| 61 | + local json = cjson.decode(assert.res_status(201, res)) |
| 62 | + assert.same(json.config.new_field, "ABC") |
| 63 | + assert.same(json.config.old_field, "ABC") |
| 64 | + |
| 65 | + -- PATCH |
| 66 | + res = assert(admin_client:send { |
| 67 | + method = "PATCH", |
| 68 | + path = "/plugins/" .. plugin_id, |
| 69 | + headers = { ["Content-Type"] = "application/json" }, |
| 70 | + body = { |
| 71 | + name = "translate-backwards-older-plugin", |
| 72 | + config = { |
| 73 | + new_field = "XYZ" |
| 74 | + }, |
| 75 | + }, |
| 76 | + }) |
| 77 | + |
| 78 | + json = cjson.decode(assert.res_status(200, res)) |
| 79 | + assert.same(json.config.new_field, "XYZ") |
| 80 | + assert.same(json.config.old_field, "XYZ") |
| 81 | + |
| 82 | + -- GET |
| 83 | + res = assert(admin_client:send { |
| 84 | + method = "GET", |
| 85 | + path = "/plugins/" .. plugin_id |
| 86 | + }) |
| 87 | + |
| 88 | + json = cjson.decode(assert.res_status(200, res)) |
| 89 | + assert.same(json.config.new_field, "XYZ") |
| 90 | + assert.same(json.config.old_field, "XYZ") |
| 91 | + end) |
| 92 | + end) |
| 93 | + |
| 94 | + describe("when using the old field", function() |
| 95 | + it("creates the custom plugin and fills in old field in response", function() |
| 96 | + -- POST |
| 97 | + local res = assert(admin_client:send { |
| 98 | + method = "POST", |
| 99 | + route = { |
| 100 | + id = route.id |
| 101 | + }, |
| 102 | + path = "/plugins", |
| 103 | + headers = { ["Content-Type"] = "application/json" }, |
| 104 | + body = { |
| 105 | + id = plugin_id, |
| 106 | + name = "translate-backwards-older-plugin", |
| 107 | + config = { |
| 108 | + old_field = "ABC" |
| 109 | + }, |
| 110 | + }, |
| 111 | + }) |
| 112 | + |
| 113 | + local json = cjson.decode(assert.res_status(201, res)) |
| 114 | + assert.same(json.config.new_field, "ABC") |
| 115 | + assert.same(json.config.old_field, "ABC") |
| 116 | + |
| 117 | + -- PATCH |
| 118 | + res = assert(admin_client:send { |
| 119 | + method = "PATCH", |
| 120 | + path = "/plugins/" .. plugin_id, |
| 121 | + headers = { ["Content-Type"] = "application/json" }, |
| 122 | + body = { |
| 123 | + name = "translate-backwards-older-plugin", |
| 124 | + config = { |
| 125 | + old_field = "XYZ" |
| 126 | + }, |
| 127 | + }, |
| 128 | + }) |
| 129 | + |
| 130 | + json = cjson.decode(assert.res_status(200, res)) |
| 131 | + assert.same(json.config.new_field, "XYZ") |
| 132 | + assert.same(json.config.old_field, "XYZ") |
| 133 | + |
| 134 | + -- GET |
| 135 | + res = assert(admin_client:send { |
| 136 | + method = "GET", |
| 137 | + path = "/plugins/" .. plugin_id |
| 138 | + }) |
| 139 | + |
| 140 | + json = cjson.decode(assert.res_status(200, res)) |
| 141 | + assert.same(json.config.new_field, "XYZ") |
| 142 | + assert.same(json.config.old_field, "XYZ") |
| 143 | + end) |
| 144 | + end) |
| 145 | + |
| 146 | + describe("when using the both new and old fields", function() |
| 147 | + describe("when their values match", function() |
| 148 | + it("creates the custom plugin and fills in old field in response", function() |
| 149 | + -- POST |
| 150 | + local res = assert(admin_client:send { |
| 151 | + method = "POST", |
| 152 | + route = { |
| 153 | + id = route.id |
| 154 | + }, |
| 155 | + path = "/plugins", |
| 156 | + headers = { ["Content-Type"] = "application/json" }, |
| 157 | + body = { |
| 158 | + id = plugin_id, |
| 159 | + name = "translate-backwards-older-plugin", |
| 160 | + config = { |
| 161 | + new_field = "ABC", |
| 162 | + old_field = "ABC" |
| 163 | + }, |
| 164 | + }, |
| 165 | + }) |
| 166 | + |
| 167 | + local json = cjson.decode(assert.res_status(201, res)) |
| 168 | + assert.same(json.config.new_field, "ABC") |
| 169 | + assert.same(json.config.old_field, "ABC") |
| 170 | + |
| 171 | + -- PATCH |
| 172 | + res = assert(admin_client:send { |
| 173 | + method = "PATCH", |
| 174 | + path = "/plugins/" .. plugin_id, |
| 175 | + headers = { ["Content-Type"] = "application/json" }, |
| 176 | + body = { |
| 177 | + name = "translate-backwards-older-plugin", |
| 178 | + config = { |
| 179 | + new_field = "XYZ", |
| 180 | + old_field = "XYZ" |
| 181 | + }, |
| 182 | + }, |
| 183 | + }) |
| 184 | + |
| 185 | + json = cjson.decode(assert.res_status(200, res)) |
| 186 | + assert.same(json.config.new_field, "XYZ") |
| 187 | + assert.same(json.config.old_field, "XYZ") |
| 188 | + |
| 189 | + -- GET |
| 190 | + res = assert(admin_client:send { |
| 191 | + method = "GET", |
| 192 | + path = "/plugins/" .. plugin_id |
| 193 | + }) |
| 194 | + |
| 195 | + json = cjson.decode(assert.res_status(200, res)) |
| 196 | + assert.same(json.config.new_field, "XYZ") |
| 197 | + assert.same(json.config.old_field, "XYZ") |
| 198 | + end) |
| 199 | + end) |
| 200 | + |
| 201 | + describe("when their values mismatch", function() |
| 202 | + it("rejects such plugin", function() |
| 203 | + -- POST --- with mismatched values |
| 204 | + local res = assert(admin_client:send { |
| 205 | + method = "POST", |
| 206 | + route = { |
| 207 | + id = route.id |
| 208 | + }, |
| 209 | + path = "/plugins", |
| 210 | + headers = { ["Content-Type"] = "application/json" }, |
| 211 | + body = { |
| 212 | + id = plugin_id, |
| 213 | + name = "translate-backwards-older-plugin", |
| 214 | + config = { |
| 215 | + new_field = "ABC", |
| 216 | + old_field = "XYZ" |
| 217 | + }, |
| 218 | + }, |
| 219 | + }) |
| 220 | + |
| 221 | + assert.res_status(400, res) |
| 222 | + |
| 223 | + -- POST --- with correct values so that we can send PATCH below |
| 224 | + res = assert(admin_client:send { |
| 225 | + method = "POST", |
| 226 | + route = { |
| 227 | + id = route.id |
| 228 | + }, |
| 229 | + path = "/plugins", |
| 230 | + headers = { ["Content-Type"] = "application/json" }, |
| 231 | + body = { |
| 232 | + id = plugin_id, |
| 233 | + name = "translate-backwards-older-plugin", |
| 234 | + config = { |
| 235 | + new_field = "ABC", |
| 236 | + old_field = "ABC" |
| 237 | + }, |
| 238 | + }, |
| 239 | + }) |
| 240 | + |
| 241 | + local json = cjson.decode(assert.res_status(201, res)) |
| 242 | + assert.same(json.config.new_field, "ABC") |
| 243 | + assert.same(json.config.old_field, "ABC") |
| 244 | + |
| 245 | + -- PATCH |
| 246 | + res = assert(admin_client:send { |
| 247 | + method = "PATCH", |
| 248 | + path = "/plugins/" .. plugin_id, |
| 249 | + headers = { ["Content-Type"] = "application/json" }, |
| 250 | + body = { |
| 251 | + name = "translate-backwards-older-plugin", |
| 252 | + config = { |
| 253 | + new_field = "EFG", |
| 254 | + old_field = "XYZ" |
| 255 | + }, |
| 256 | + }, |
| 257 | + }) |
| 258 | + |
| 259 | + assert.res_status(400, res) |
| 260 | + |
| 261 | + -- GET |
| 262 | + res = assert(admin_client:send { |
| 263 | + method = "GET", |
| 264 | + path = "/plugins/" .. plugin_id |
| 265 | + }) |
| 266 | + |
| 267 | + json = cjson.decode(assert.res_status(200, res)) |
| 268 | + assert.same(json.config.new_field, "ABC") |
| 269 | + assert.same(json.config.old_field, "ABC") |
| 270 | + end) |
| 271 | + end) |
| 272 | + end) |
| 273 | + end) |
| 274 | +end) |
0 commit comments