From 0863c42f0eed73e8089fa104a148c6cdfc49774c Mon Sep 17 00:00:00 2001 From: thefosk Date: Wed, 3 Feb 2016 13:15:59 -0800 Subject: [PATCH] Closes #897 --- kong/plugins/oauth2/api.lua | 4 +++ spec/plugins/oauth2/api_spec.lua | 62 ++++++++++++++++++++++++++++++-- 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/kong/plugins/oauth2/api.lua b/kong/plugins/oauth2/api.lua index 74ebbda64371..5d9ec1a98125 100644 --- a/kong/plugins/oauth2/api.lua +++ b/kong/plugins/oauth2/api.lua @@ -24,6 +24,10 @@ return { crud.patch(self.params, dao_factory.oauth2_tokens) end, + PUT = function(self, dao_factory) + crud.put(self.params, dao_factory.oauth2_tokens) + end, + DELETE = function(self, dao_factory) crud.delete(self.params, dao_factory.oauth2_tokens) end diff --git a/spec/plugins/oauth2/api_spec.lua b/spec/plugins/oauth2/api_spec.lua index a0010804593f..5e765efb6903 100644 --- a/spec/plugins/oauth2/api_spec.lua +++ b/spec/plugins/oauth2/api_spec.lua @@ -136,7 +136,6 @@ describe("OAuth 2 Credentials API", function() BASE_URL = spec_helper.API_URL.."/oauth2_tokens/" describe("POST", function() - it("[SUCCESS] should create a oauth2 token", function() local response, status = http_client.post(BASE_URL, {credential_id = credential.id, expires_in = 10}) assert.equal(201, status) @@ -153,7 +152,25 @@ describe("OAuth 2 Credentials API", function() assert.equal(400, status) assert.equal('{"credential_id":"credential_id is required","expires_in":"expires_in is required"}\n', response) end) + end) + + describe("PUT", function() + it("[SUCCESS] should create a oauth2 token", function() + local response, status = http_client.put(BASE_URL, {credential_id = credential.id, expires_in = 10}) + assert.equal(201, status) + token = json.decode(response) + assert.equal(credential.id, token.credential_id) + assert.equal(10, token.expires_in) + assert.truthy(token.access_token) + assert.truthy(token.refresh_token) + assert.equal("bearer", token.token_type) + end) + it("[FAILURE] should return proper errors", function() + local response, status = http_client.put(BASE_URL, {}) + assert.equal(400, status) + assert.equal('{"credential_id":"credential_id is required","expires_in":"expires_in is required"}\n', response) + end) end) describe("GET", function() @@ -169,7 +186,48 @@ describe("OAuth 2 Credentials API", function() local response, status = http_client.get(BASE_URL) assert.equal(200, status) local body = json.decode(response) - assert.equals(1, body.total) + assert.equals(2, body.total) + end) + + end) + + describe("PATCH", function() + + it("should update partial fields", function() + local response, status = http_client.patch(BASE_URL..token.id, { access_token = "helloworld" }) + assert.equal(200, status) + local body = json.decode(response) + assert.equals("helloworld", body.access_token) + + -- Check it has really been updated + response, status = http_client.get(BASE_URL..token.id) + assert.equal(200, status) + body = json.decode(response) + assert.equals("helloworld", body.access_token) + end) + + end) + + describe("PUT", function() + + it("should update the entire object", function() + local response, status = http_client.get(BASE_URL..token.id) + assert.equal(200, status) + local body = json.decode(response) + body.access_token = "puthelloworld" + body.created_at = nil + + response, status = http_client.put(BASE_URL..token.id, body) + assert.equal(200, status) + body = json.decode(response) + assert.equals("puthelloworld", body.access_token) + + -- Check it has really been updated + response, status = http_client.get(BASE_URL..token.id) + assert.equal(200, status) + body = json.decode(response) + assert.equals("puthelloworld", body.access_token) + assert.truthy(body.created_at) end) end)