Skip to content

Commit

Permalink
Merge pull request #23 from imtayadeway/url-helpers
Browse files Browse the repository at this point in the history
Replace custom entrypoint_url with Rails helper
  • Loading branch information
abellotti authored Aug 29, 2017
2 parents 25e85c8 + c8370b8 commit 4a4d1b3
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 39 deletions.
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Enablement for the REST API

namespace :api, :path => "api(/:version)", :version => Api::VERSION_REGEX, :defaults => {:format => "json"} do
root :to => "api#index"
root :to => "api#index", :as => :entrypoint
match "/", :to => "api#options", :via => :options

# Redirect of /tasks subcollections to /request_tasks
Expand Down
36 changes: 18 additions & 18 deletions spec/requests/authentication_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
it "test basic authentication with bad credentials" do
basic_authorize "baduser", "badpassword"

run_get entrypoint_url
run_get api_entrypoint_url

expect(response).to have_http_status(:unauthorized)
end

it "test basic authentication with correct credentials" do
api_basic_authorize

run_get entrypoint_url
run_get api_entrypoint_url

expect(response).to have_http_status(:ok)
expect_result_to_have_keys(ENTRYPOINT_KEYS)
Expand All @@ -28,7 +28,7 @@

api_basic_authorize

run_get entrypoint_url
run_get api_entrypoint_url

expect(response).to have_http_status(:unauthorized)
end
Expand All @@ -39,7 +39,7 @@

api_basic_authorize

run_get entrypoint_url
run_get api_entrypoint_url

expect(response).to have_http_status(:unauthorized)
end
Expand All @@ -57,23 +57,23 @@
it "test basic authentication with incorrect group" do
api_basic_authorize

run_get entrypoint_url, :headers => {Api::HttpHeaders::MIQ_GROUP => "bogus_group"}
run_get api_entrypoint_url, :headers => {Api::HttpHeaders::MIQ_GROUP => "bogus_group"}

expect(response).to have_http_status(:unauthorized)
end

it "test basic authentication with a primary group" do
api_basic_authorize

run_get entrypoint_url, :headers => {Api::HttpHeaders::MIQ_GROUP => group1.description}
run_get api_entrypoint_url, :headers => {Api::HttpHeaders::MIQ_GROUP => group1.description}

expect(response).to have_http_status(:ok)
end

it "test basic authentication with a secondary group" do
api_basic_authorize

run_get entrypoint_url, :headers => {Api::HttpHeaders::MIQ_GROUP => group2.description}
run_get api_entrypoint_url, :headers => {Api::HttpHeaders::MIQ_GROUP => group2.description}

expect(response).to have_http_status(:ok)
end
Expand All @@ -91,7 +91,7 @@
it "basic authentication with a secondary group" do
api_basic_authorize

run_get entrypoint_url, :headers => {Api::HttpHeaders::MIQ_GROUP => group2.description}
run_get api_entrypoint_url, :headers => {Api::HttpHeaders::MIQ_GROUP => group2.description}

expect(response).to have_http_status(:ok)
expect_result_to_have_keys(ENTRYPOINT_KEYS)
Expand All @@ -112,7 +112,7 @@
it "querying user's authorization" do
api_basic_authorize

run_get entrypoint_url, :attributes => "authorization"
run_get api_entrypoint_url, :attributes => "authorization"

expect(response).to have_http_status(:ok)
expected = {"authorization" => hash_including("product_features")}
Expand All @@ -136,7 +136,7 @@
end

it "authentication using a bad token" do
run_get entrypoint_url, :headers => {Api::HttpHeaders::AUTH_TOKEN => "badtoken"}
run_get api_entrypoint_url, :headers => {Api::HttpHeaders::AUTH_TOKEN => "badtoken"}

expect(response).to have_http_status(:unauthorized)
end
Expand All @@ -151,7 +151,7 @@

auth_token = response.parsed_body["auth_token"]

run_get entrypoint_url, :headers => {Api::HttpHeaders::AUTH_TOKEN => auth_token}
run_get api_entrypoint_url, :headers => {Api::HttpHeaders::AUTH_TOKEN => auth_token}

expect(response).to have_http_status(:ok)
expect_result_to_have_keys(ENTRYPOINT_KEYS)
Expand All @@ -173,7 +173,7 @@
expect(token_info[:expires_on].utc.iso8601).to eq(token_expires_on)

expect_any_instance_of(TokenManager).to receive(:reset_token).with(auth_token)
run_get entrypoint_url, :headers => {Api::HttpHeaders::AUTH_TOKEN => auth_token}
run_get api_entrypoint_url, :headers => {Api::HttpHeaders::AUTH_TOKEN => auth_token}

expect(response).to have_http_status(:ok)
expect_result_to_have_keys(ENTRYPOINT_KEYS)
Expand Down Expand Up @@ -232,7 +232,7 @@
run_get auth_url, :requester_type => 'ws'
ws_token = response.parsed_body["auth_token"]

run_get entrypoint_url, :headers => {Api::HttpHeaders::AUTH_TOKEN => ws_token}
run_get api_entrypoint_url, :headers => {Api::HttpHeaders::AUTH_TOKEN => ws_token}

expect(response).to have_http_status(:unauthorized)
end
Expand All @@ -249,7 +249,7 @@ def systoken(server_guid, userid, timestamp)
end

it "authentication using a bad token" do
run_get entrypoint_url,
run_get api_entrypoint_url,
:headers => {Api::HttpHeaders::MIQ_TOKEN => "badtoken"}

expect(response).to have_http_status(:unauthorized)
Expand All @@ -259,7 +259,7 @@ def systoken(server_guid, userid, timestamp)
end

it "authentication using a token with a bad server guid" do
run_get entrypoint_url,
run_get api_entrypoint_url,
:headers => {Api::HttpHeaders::MIQ_TOKEN => systoken("bad_server_guid", api_config(:user), Time.now.utc)}

expect(response).to have_http_status(:unauthorized)
Expand All @@ -269,7 +269,7 @@ def systoken(server_guid, userid, timestamp)
end

it "authentication using a token with bad user" do
run_get entrypoint_url,
run_get api_entrypoint_url,
:headers => {Api::HttpHeaders::MIQ_TOKEN => systoken(MiqServer.first.guid, "bad_user_id", Time.now.utc)}

expect(response).to have_http_status(:unauthorized)
Expand All @@ -281,7 +281,7 @@ def systoken(server_guid, userid, timestamp)
it "authentication using a token with an old timestamp" do
miq_token = systoken(MiqServer.first.guid, api_config(:user), 10.minutes.ago.utc)

run_get entrypoint_url, :headers => {Api::HttpHeaders::MIQ_TOKEN => miq_token}
run_get api_entrypoint_url, :headers => {Api::HttpHeaders::MIQ_TOKEN => miq_token}

expect(response).to have_http_status(:unauthorized)
expect(response.parsed_body).to include(
Expand All @@ -292,7 +292,7 @@ def systoken(server_guid, userid, timestamp)
it "authentication using a valid token succeeds" do
miq_token = systoken(MiqServer.first.guid, api_config(:user), Time.now.utc)

run_get entrypoint_url, :headers => {Api::HttpHeaders::MIQ_TOKEN => miq_token}
run_get api_entrypoint_url, :headers => {Api::HttpHeaders::MIQ_TOKEN => miq_token}

expect(response).to have_http_status(:ok)
expect_result_to_have_keys(ENTRYPOINT_KEYS)
Expand Down
12 changes: 6 additions & 6 deletions spec/requests/entrypoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
it "returns a :settings hash" do
api_basic_authorize

run_get entrypoint_url
run_get api_entrypoint_url

expect(response).to have_http_status(:ok)
expect_result_to_have_keys(%w(settings))
Expand All @@ -12,7 +12,7 @@
it "returns a locale" do
api_basic_authorize

run_get entrypoint_url
run_get api_entrypoint_url

expect(%w(en en_US)).to include(response.parsed_body['settings']['locale'])
end
Expand All @@ -23,15 +23,15 @@
test_settings = {:cartoons => {:saturday => {:tom_jerry => 'n', :bugs_bunny => 'y'}}}
@user.update_attributes!(:settings => test_settings)

run_get entrypoint_url
run_get api_entrypoint_url

expect(response.parsed_body).to include("settings" => a_hash_including(test_settings.deep_stringify_keys))
end

it "collection query is sorted" do
api_basic_authorize

run_get entrypoint_url
run_get api_entrypoint_url

collection_names = response.parsed_body['collections'].map { |c| c['name'] }
expect(collection_names).to eq(collection_names.sort)
Expand All @@ -40,7 +40,7 @@
it "returns server_info" do
api_basic_authorize

run_get entrypoint_url
run_get api_entrypoint_url

expect(response.parsed_body).to include(
"server_info" => a_hash_including(
Expand All @@ -57,7 +57,7 @@
it "returns product_info" do
api_basic_authorize

run_get entrypoint_url
run_get api_entrypoint_url

expect(response.parsed_body).to include(
"product_info" => a_hash_including(
Expand Down
8 changes: 4 additions & 4 deletions spec/requests/headers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
it "returns JSON when set to application/json" do
api_basic_authorize

get entrypoint_url, :headers => headers.merge("Accept" => "application/json")
get api_entrypoint_url, :headers => headers.merge("Accept" => "application/json")

expect(response.parsed_body).to include("name" => "API", "description" => "REST API")
expect(response).to have_http_status(:ok)
Expand All @@ -12,7 +12,7 @@
it "returns JSON when not provided" do
api_basic_authorize

get entrypoint_url, :headers => headers
get api_entrypoint_url, :headers => headers

expect(response.parsed_body).to include("name" => "API", "description" => "REST API")
expect(response).to have_http_status(:ok)
Expand All @@ -21,7 +21,7 @@
it "responds with an error for unsupported mime-types" do
api_basic_authorize

get entrypoint_url, :headers => headers.merge("Accept" => "application/xml")
get api_entrypoint_url, :headers => headers.merge("Accept" => "application/xml")

expected = {
"error" => a_hash_including(
Expand Down Expand Up @@ -69,7 +69,7 @@
it "returns some headers related to security" do
api_basic_authorize

run_get(entrypoint_url)
run_get(api_entrypoint_url)

expected = {
"X-Content-Type-Options" => "nosniff",
Expand Down
4 changes: 2 additions & 2 deletions spec/requests/logging_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
it "logs all hash entries about the request" do
api_basic_authorize

run_get entrypoint_url
run_get api_entrypoint_url

@log.rewind
request_log_line = @log.readlines.detect { |l| l =~ /MIQ\(.*\) Request:/ }
Expand Down Expand Up @@ -54,7 +54,7 @@

miq_token = MiqPassword.encrypt({:server_guid => server_guid, :userid => userid, :timestamp => timestamp}.to_yaml)

run_get entrypoint_url, :headers => {Api::HttpHeaders::MIQ_TOKEN => miq_token}
run_get api_entrypoint_url, :headers => {Api::HttpHeaders::MIQ_TOKEN => miq_token}

expect(@log.string).to include(
"System Auth: {:x_miq_token=>\"#{miq_token}\", :server_guid=>\"#{server_guid}\", " \
Expand Down
8 changes: 4 additions & 4 deletions spec/requests/versioning_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
it "test versioning query" do
api_basic_authorize

run_get entrypoint_url
run_get api_entrypoint_url

expect(response).to have_http_status(:ok)
expect_result_to_have_keys(%w(name description version versions collections))
Expand All @@ -16,7 +16,7 @@
api_basic_authorize

# Let's get the versions
run_get entrypoint_url
run_get api_entrypoint_url

expect(response).to have_http_status(:ok)
expect_result_to_have_keys(%w(versions))
Expand All @@ -33,7 +33,7 @@
ident = ver["href"].split("/").last

# Let's try to access that version API URL
run_get "#{entrypoint_url}/#{ident}"
run_get api_entrypoint_url(ident)

expect(response).to have_http_status(:ok)
expect_result_to_have_keys(%w(name description version versions collections))
Expand All @@ -42,7 +42,7 @@
it "test query with an invalid version" do
api_basic_authorize

run_get "#{entrypoint_url}/v9999.9999"
run_get api_entrypoint_url("v9999.9999")

expect(response).to have_http_status(:bad_request)
end
Expand Down
4 changes: 0 additions & 4 deletions spec/support/api_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ def init_api_spec_env
define_user
end

def entrypoint_url
api_config(:entrypoint)
end

def auth_url
"#{api_config(:entrypoint)}/auth"
end
Expand Down

0 comments on commit 4a4d1b3

Please sign in to comment.