forked from Kong/lua-resty-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04-RemoteCredentials_spec.lua
87 lines (69 loc) · 2.49 KB
/
04-RemoteCredentials_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
local json = require("cjson.safe").new()
local restore = require "spec.helpers"
-- Mock for HTTP client
local response = {} -- override in tests
local http = {
new = function()
return {
connect = function() return true end,
set_timeout = function() return true end,
set_timeouts = function() return true end,
request = function(self, opts)
if opts.path == "/test/path" then
return { -- the response for the credentials
status = (response or {}).status or 200,
read_body = function() return json.encode {
AccessKeyId = (response or {}).AccessKeyId or "access",
SecretAccessKey = (response or {}).SecretAccessKey or "secret",
Token = (response or {}).Token or "token",
Expiration = (response or {}).Expiration or "2030-01-01T20:00:00Z",
}
end,
}
else
error("bad test path provided??? " .. tostring(opts.path))
end
end,
}
end,
}
describe("RemoteCredentials", function()
local RemoteCredentials
before_each(function()
restore()
restore.setenv("AWS_CONTAINER_CREDENTIALS_FULL_URI", "https://localhost/test/path")
local _ = require("resty.aws.config").global -- load config before mocking http client
package.loaded["resty.luasocket.http"] = http
RemoteCredentials = require "resty.aws.credentials.RemoteCredentials"
end)
after_each(function()
restore()
end)
it("fetches credentials", function()
local cred = RemoteCredentials:new()
local success, key, secret, token = cred:get()
assert.equal(true, success)
assert.equal("access", key)
assert.equal("secret", secret)
assert.equal("token", token)
end)
end)
describe("RemoteCredentials with customized full URI", function ()
it("fetches credentials", function ()
local RemoteCredentials
restore()
restore.setenv("AWS_CONTAINER_CREDENTIALS_FULL_URI", "http://localhost:12345/test/path")
local _ = require("resty.aws.config").global -- load config before mocking http client
package.loaded["resty.luasocket.http"] = http
RemoteCredentials = require "resty.aws.credentials.RemoteCredentials"
finally(function()
restore()
end)
local cred = RemoteCredentials:new()
local success, key, secret, token = cred:get()
assert.equal(true, success)
assert.equal("access", key)
assert.equal("secret", secret)
assert.equal("token", token)
end)
end)