2019-06-11 22:24:35 +00:00
|
|
|
require "helper"
|
|
|
|
require "bundles/inspec-supermarket/api"
|
2017-03-29 16:42:24 +00:00
|
|
|
|
|
|
|
def default_url?(supermarket_url)
|
|
|
|
supermarket_url == Supermarket::API::SUPERMARKET_URL
|
|
|
|
end
|
|
|
|
|
|
|
|
describe Supermarket::API do
|
|
|
|
let(:subject) { Supermarket::API }
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
[Supermarket::API::SUPERMARKET_URL, "https://my.custom.supermarket"].each do |supermarket_url|
|
2017-03-29 16:42:24 +00:00
|
|
|
|
2019-07-09 00:20:30 +00:00
|
|
|
describe "With #{default_url?(supermarket_url) ? "default" : supermarket_url} Supermarket URL" do
|
2017-03-29 16:42:24 +00:00
|
|
|
|
|
|
|
let(:profile_search_response_body) do
|
|
|
|
{
|
2019-06-11 22:24:35 +00:00
|
|
|
"start" => 0,
|
|
|
|
"total" => 1,
|
|
|
|
"items" => [
|
2017-03-29 16:42:24 +00:00
|
|
|
{
|
2019-06-11 22:24:35 +00:00
|
|
|
"tool_name" => "test_name",
|
|
|
|
"tool_type" => "compliance_profile",
|
|
|
|
"tool_source_url" => supermarket_url,
|
|
|
|
"tool_description" => "test_description",
|
|
|
|
"tool_owner" => "test_owner",
|
|
|
|
"tool" => "#{supermarket_url}/api/v1/tools/test_name",
|
2019-07-09 00:20:30 +00:00
|
|
|
},
|
2019-06-11 22:24:35 +00:00
|
|
|
],
|
2017-03-29 16:42:24 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
let(:profile_name) { "supermarket://test_owner/test_name" }
|
2017-03-29 16:42:24 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "#profiles" do
|
|
|
|
it "returns the profile list" do
|
|
|
|
stub_request(:get, "#{supermarket_url}/api/v1/tools-search?items=100&type=compliance_profile")
|
2019-07-09 00:20:30 +00:00
|
|
|
.to_return(status: 200, body: profile_search_response_body.to_json)
|
2017-03-29 16:42:24 +00:00
|
|
|
test_profile = default_url?(supermarket_url) ? subject.profiles.first : subject.profiles(supermarket_url).first
|
|
|
|
|
2019-09-30 22:31:55 +00:00
|
|
|
_(test_profile).must_equal(profile_search_response_body["items"].first.merge({ "slug" => "test_name" }))
|
2017-03-29 16:42:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "#profile_name" do
|
|
|
|
it "returns the profile name and owner from a supermarket://owner/name path" do
|
|
|
|
tool_owner, tool_name = subject.profile_name("supermarket://test_tool_owner/test_tool_name")
|
2017-03-29 16:42:24 +00:00
|
|
|
|
2019-09-30 22:31:55 +00:00
|
|
|
_(tool_owner).must_equal("test_tool_owner")
|
|
|
|
_(tool_name).must_equal("test_tool_name")
|
2017-03-29 16:42:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "#info" do
|
2017-03-29 16:42:24 +00:00
|
|
|
let(:profile_list_response_body) do
|
|
|
|
{
|
2019-06-11 22:24:35 +00:00
|
|
|
"name" => "test_name",
|
|
|
|
"slug" => "test_slug",
|
|
|
|
"type" => "test_type",
|
|
|
|
"source_url" => supermarket_url,
|
|
|
|
"description" => "test_description",
|
|
|
|
"instructions" => "test_instructions",
|
|
|
|
"owner" => "test_owner",
|
2017-03-29 16:42:24 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "returns profile info" do
|
|
|
|
stub_request(:get, "#{supermarket_url}/api/v1/tools/test_name")
|
2019-07-09 00:20:30 +00:00
|
|
|
.to_return(status: 200, body: profile_list_response_body.to_json)
|
2017-03-29 16:42:24 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
profile_info = default_url?(supermarket_url) ? subject.info("test_owner/test_name") : subject.info("test_owner/test_name", supermarket_url)
|
2017-03-29 16:42:24 +00:00
|
|
|
|
2019-09-30 22:31:55 +00:00
|
|
|
_(profile_info).must_equal(profile_list_response_body)
|
2017-03-29 16:42:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "#same?" do
|
2017-03-29 16:42:24 +00:00
|
|
|
let(:tool_url) { "#{supermarket_url}/api/v1/tools/test_name" }
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "is the same on a match" do
|
|
|
|
supermarket_tool = { "tool_owner" => "test_owner", "tool" => tool_url }
|
2017-03-29 16:42:24 +00:00
|
|
|
same = default_url?(supermarket_url) ? subject.same?(profile_name, supermarket_tool) : subject.same?(profile_name, supermarket_tool, supermarket_url)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(same).must_equal(true)
|
2017-03-29 16:42:24 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "is not the same on a mismatched owner" do
|
|
|
|
supermarket_tool = { "tool_owner" => "wrong_owner", "tool" => tool_url }
|
2017-03-29 16:42:24 +00:00
|
|
|
same = default_url?(supermarket_url) ? subject.same?(profile_name, supermarket_tool) : subject.same?(profile_name, supermarket_tool, supermarket_url)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(same).must_equal(false)
|
2017-03-29 16:42:24 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "is not the same on a mismatched supermarket tool" do
|
|
|
|
supermarket_tool = { "tool_owner" => "test_owner", "tool" => "garbage" }
|
2017-03-29 16:42:24 +00:00
|
|
|
same = default_url?(supermarket_url) ? subject.same?(profile_name, supermarket_tool) : subject.same?(profile_name, supermarket_tool, supermarket_url)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(same).must_equal(false)
|
2017-03-29 16:42:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "#find" do
|
2017-03-29 16:42:24 +00:00
|
|
|
let(:empty_profile_search_response_body) do
|
2019-06-11 22:24:35 +00:00
|
|
|
{ start: 0, total: 0, items: [] }
|
2017-03-29 16:42:24 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "returns nil if profiles are empty" do
|
|
|
|
stub_request(:get, "#{supermarket_url}/api/v1/tools-search?items=100&type=compliance_profile")
|
2019-07-09 00:20:30 +00:00
|
|
|
.to_return(status: 200, body: empty_profile_search_response_body.to_json)
|
2017-03-29 16:42:24 +00:00
|
|
|
|
|
|
|
search = default_url?(supermarket_url) ? subject.find(profile_name) : subject.find(profile_name, supermarket_url)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(search).must_be_nil
|
2017-03-29 16:42:24 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "returns nil if profile not found" do
|
|
|
|
stub_request(:get, "#{supermarket_url}/api/v1/tools-search?items=100&type=compliance_profile")
|
2019-07-09 00:20:30 +00:00
|
|
|
.to_return(status: 200, body: profile_search_response_body.to_json)
|
2017-03-29 16:42:24 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
profile_name_cant_find = "supermarket://cant_find/not_found"
|
2017-03-29 16:42:24 +00:00
|
|
|
search = default_url?(supermarket_url) ? subject.find(profile_name_cant_find) : subject.find(profile_name_cant_find, supermarket_url)
|
2019-09-30 22:31:55 +00:00
|
|
|
_(search).must_be_nil
|
2017-03-29 16:42:24 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "returns profile if it is found" do
|
|
|
|
stub_request(:get, "#{supermarket_url}/api/v1/tools-search?items=100&type=compliance_profile")
|
2019-07-09 00:20:30 +00:00
|
|
|
.to_return(status: 200, body: profile_search_response_body.to_json)
|
2017-03-29 16:42:24 +00:00
|
|
|
|
|
|
|
profile = default_url?(supermarket_url) ? subject.find(profile_name) : subject.find(profile_name, supermarket_url)
|
|
|
|
|
2019-09-30 22:31:55 +00:00
|
|
|
_(profile).must_equal(profile_search_response_body["items"].first.merge({ "slug" => "test_name" }))
|
2017-03-29 16:42:24 +00:00
|
|
|
end
|
2018-07-25 19:59:19 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "downcases profile name for Supermarket API URL" do
|
|
|
|
profile_name = "supermarket://test_owner/Test_Name"
|
|
|
|
stub_request(:get, "#{supermarket_url}/api/v1/tools-search?items=100&type=compliance_profile")
|
2019-07-09 00:20:30 +00:00
|
|
|
.to_return(status: 200, body: profile_search_response_body.to_json)
|
2018-07-25 19:59:19 +00:00
|
|
|
|
|
|
|
profile = if default_url?(supermarket_url)
|
|
|
|
subject.find(profile_name)
|
|
|
|
else
|
|
|
|
subject.find(profile_name, supermarket_url)
|
|
|
|
end
|
|
|
|
|
2019-09-30 22:31:55 +00:00
|
|
|
_(profile).must_equal(profile_search_response_body["items"].first.merge({ "slug" => "test_name" }))
|
2018-07-25 19:59:19 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "raises an error if tool name is not present" do
|
|
|
|
profile_name = "supermarket://owner_only"
|
|
|
|
stub_request(:get, "#{supermarket_url}/api/v1/tools-search?items=100&type=compliance_profile")
|
2019-07-09 00:20:30 +00:00
|
|
|
.to_return(status: 200, body: profile_search_response_body.to_json)
|
2018-07-25 19:59:19 +00:00
|
|
|
|
2019-09-30 22:31:55 +00:00
|
|
|
e = _ { subject.find(profile_name, supermarket_url) }.must_raise
|
|
|
|
_(e.message).must_equal("Could not parse tool name from #{profile_name}")
|
2018-07-25 19:59:19 +00:00
|
|
|
end
|
2017-03-29 16:42:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|