Skip to content

Commit

Permalink
tests(jwt) add tests for X-Credential-Identifier merged with #4… (#5261)
Browse files Browse the repository at this point in the history
### Summary

Adds test for a feature contributed by community member @davinwang.
  • Loading branch information
bungle authored and kikito committed Nov 27, 2019
1 parent 647c409 commit e1bbd98
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions spec/03-plugins/16-jwt/03-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ for _, strategy in helpers.each_strategy() do
local body = cjson.decode(assert.res_status(200, res))
assert.equal(authorization, body.headers.authorization)
assert.equal("jwt_tests_consumer", body.headers["x-consumer-username"])
assert.equal(jwt_secret.key, body.headers["x-credential-identifier"])
assert.is_nil(body.headers["x-anonymous-consumer"])
end)
it("proxies the request if the key is found in headers", function()
Expand Down Expand Up @@ -388,6 +389,7 @@ for _, strategy in helpers.each_strategy() do
local body = cjson.decode(assert.res_status(200, res))
assert.equal(authorization, body.headers.authorization)
assert.equal("jwt_tests_consumer", body.headers["x-consumer-username"])
assert.equal(jwt_secret.key, body.headers["x-credential-identifier"])
end)
it("proxies the request if secret is base64", function()
PAYLOAD.iss = base64_jwt_secret.key
Expand Down Expand Up @@ -418,6 +420,7 @@ for _, strategy in helpers.each_strategy() do
local body = cjson.decode(assert.res_status(200, res))
assert.equal(authorization, body.headers.authorization)
assert.equal("jwt_tests_base64_consumer", body.headers["x-consumer-username"])
assert.equal(base64_jwt_secret.key, body.headers["x-credential-identifier"])
end)
it("returns 200 the JWT is found in the cookie crumble", function()
PAYLOAD.iss = jwt_secret.key
Expand Down Expand Up @@ -565,6 +568,7 @@ for _, strategy in helpers.each_strategy() do
local body = cjson.decode(assert.res_status(200, res))
assert.equal(authorization, body.headers.authorization)
assert.equal("jwt_tests_rsa_consumer_1", body.headers["x-consumer-username"])
assert.equal(rsa_jwt_secret_1.key, body.headers["x-credential-identifier"])
end)
it("identifies Consumer", function()
PAYLOAD.iss = rsa_jwt_secret_2.key
Expand All @@ -581,6 +585,7 @@ for _, strategy in helpers.each_strategy() do
local body = cjson.decode(assert.res_status(200, res))
assert.equal(authorization, body.headers.authorization)
assert.equal("jwt_tests_rsa_consumer_2", body.headers["x-consumer-username"])
assert.equal(rsa_jwt_secret_2.key, body.headers["x-credential-identifier"])
end)
end)

Expand All @@ -600,6 +605,7 @@ for _, strategy in helpers.each_strategy() do
local body = cjson.decode(assert.res_status(200, res))
assert.equal(authorization, body.headers.authorization)
assert.equal("jwt_tests_rsa_consumer_5", body.headers["x-consumer-username"])
assert.equal(rsa_jwt_secret_3.key, body.headers["x-credential-identifier"])
end)
it("identifies Consumer", function()
PAYLOAD.iss = rsa_jwt_secret_3.key
Expand All @@ -616,6 +622,7 @@ for _, strategy in helpers.each_strategy() do
local body = cjson.decode(assert.res_status(200, res))
assert.equal(authorization, body.headers.authorization)
assert.equal("jwt_tests_rsa_consumer_5", body.headers["x-consumer-username"])
assert.equal(rsa_jwt_secret_3.key, body.headers["x-credential-identifier"])
end)
end)

Expand All @@ -635,6 +642,7 @@ for _, strategy in helpers.each_strategy() do
local body = cjson.decode(assert.res_status(200, res))
assert.equal(authorization, body.headers.authorization)
assert.equal("jwt_tests_hs_consumer_7", body.headers["x-consumer-username"])
assert.equal(hs_jwt_secret_1.key, body.headers["x-credential-identifier"])
assert.is_nil(body.headers["x-anonymous-consumer"])
end)
end)
Expand All @@ -655,6 +663,7 @@ for _, strategy in helpers.each_strategy() do
local body = cjson.decode(assert.res_status(200, res))
assert.equal(authorization, body.headers.authorization)
assert.equal("jwt_tests_hs_consumer_8", body.headers["x-consumer-username"])
assert.equal(hs_jwt_secret_2.key, body.headers["x-credential-identifier"])
assert.is_nil(body.headers["x-anonymous-consumer"])
end)
end)
Expand Down Expand Up @@ -745,6 +754,7 @@ for _, strategy in helpers.each_strategy() do
})
local body = cjson.decode(assert.res_status(200, res))
assert.equal('jwt_tests_consumer', body.headers["x-consumer-username"])
assert.equal(jwt_secret.key, body.headers["x-credential-identifier"])
assert.is_nil(body.headers["x-anonymous-consumer"])
end)
it("works with wrong credentials and anonymous", function()
Expand All @@ -758,6 +768,7 @@ for _, strategy in helpers.each_strategy() do
local body = cjson.decode(assert.res_status(200, res))
assert.equal('true', body.headers["x-anonymous-consumer"])
assert.equal('no-body', body.headers["x-consumer-username"])
assert.equal(nil, body.headers["x-credential-identifier"])
end)
it("errors when anonymous user doesn't exist", function()
local res = assert(proxy_client:send {
Expand Down Expand Up @@ -890,8 +901,10 @@ for _, strategy in helpers.each_strategy() do
assert.response(res).has.status(200)
assert.request(res).has.no.header("x-anonymous-consumer")
local id = assert.request(res).has.header("x-consumer-id")
local key = assert.request(res).has.header("x-credential-identifier")
assert.not_equal(id, anonymous.id)
assert(id == user1.id or id == user2.id)
assert.equal(PAYLOAD.iss, key)
end)

it("fails 401, with only the first credential provided", function()
Expand Down Expand Up @@ -946,8 +959,10 @@ for _, strategy in helpers.each_strategy() do
assert.response(res).has.status(200)
assert.request(res).has.no.header("x-anonymous-consumer")
local id = assert.request(res).has.header("x-consumer-id")
local key = assert.request(res).has.header("x-credential-identifier")
assert.not_equal(id, anonymous.id)
assert(id == user1.id or id == user2.id)
assert.equal(PAYLOAD.iss, key)
end)

it("passes with only the first credential provided", function()
Expand All @@ -964,6 +979,7 @@ for _, strategy in helpers.each_strategy() do
local id = assert.request(res).has.header("x-consumer-id")
assert.not_equal(id, anonymous.id)
assert.equal(user1.id, id)
assert.not_equal(PAYLOAD.iss, res.headers["x-credential-identifier"])
end)

it("passes with only the second credential provided", function()
Expand All @@ -978,8 +994,10 @@ for _, strategy in helpers.each_strategy() do
assert.response(res).has.status(200)
assert.request(res).has.no.header("x-anonymous-consumer")
local id = assert.request(res).has.header("x-consumer-id")
local key = assert.request(res).has.header("x-credential-identifier")
assert.not_equal(id, anonymous.id)
assert.equal(user2.id, id)
assert.equal(PAYLOAD.iss, key)
end)

it("passes with no credential provided", function()
Expand All @@ -993,6 +1011,7 @@ for _, strategy in helpers.each_strategy() do
assert.response(res).has.status(200)
assert.request(res).has.header("x-anonymous-consumer")
local id = assert.request(res).has.header("x-consumer-id")
assert.not_equal(PAYLOAD.iss, res.headers["x-credential-identifier"])
assert.equal(id, anonymous.id)
end)
end)
Expand Down

0 comments on commit e1bbd98

Please sign in to comment.