Skip to content

Commit

Permalink
Merge pull request #940 from Mashape/feature/oauth2tokens-api
Browse files Browse the repository at this point in the history
Closes #897
  • Loading branch information
subnetmarco committed Feb 3, 2016
2 parents ecc440a + 0863c42 commit 6611f8c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
4 changes: 4 additions & 0 deletions kong/plugins/oauth2/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
62 changes: 60 additions & 2 deletions spec/plugins/oauth2/api_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -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)
Expand Down

0 comments on commit 6611f8c

Please sign in to comment.