Skip to content
This repository has been archived by the owner on Nov 16, 2018. It is now read-only.

Commit

Permalink
add api request tests
Browse files Browse the repository at this point in the history
improve api
  • Loading branch information
pgolm committed Dec 17, 2013
1 parent 15808cf commit 40bb7e3
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/controllers/api/v1/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class API < Grape::API
error!('Not Found', 404) if inventory.nil?

host = inventory.hosts.find_by(alias: params[:host])
error!('Not Found', 404) if inventory.nil?
error!('Not Found', 404) if host.nil?

ActiveSupport::JSON.decode(host.variables)
end
Expand Down
5 changes: 5 additions & 0 deletions spec/factories/host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,10 @@
factory :host, class: Host do
id { generate(:random_id) }
self.alias { "host#{id}" }
variables "{}"

trait :with_variables do
variables "{ 'ansible_ssh_host': '127.0.0.1'}"
end
end
end
6 changes: 3 additions & 3 deletions spec/factories/inventory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
owner { create(:user) }

trait :with_host do
hosts { build_list(:host, 1) }
hosts { create_list(:host, 1, inventory_id: id) }
end

trait :with_hosts do
hosts { build_list(:host, 12) }
hosts { create_list(:host, 12, inventory_id: id) }
end

factory :small_inventory, traits: [:with_host]
factory :big_inventory, traits: [:with_host]
factory :big_inventory, traits: [:with_hosts]
end

end
60 changes: 60 additions & 0 deletions spec/requests/apis_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
require 'spec_helper'

describe API::API do

describe "APIv1" do
let(:empty_inventory) { create(:inventory) }
let(:unsafed_inventory) { build(:small_inventory) }
let(:small_inventory) { create(:small_inventory) }
let(:admin) { create(:user) }

it "unauthorized me" do
get "/api/v1/inventory/#{empty_inventory.key}"
response.status.should == 401 # unauthorized
end

describe "GET inventory" do
it "didn't found the inventory" do
get "/api/v1/inventory/#{unsafed_inventory.key}?token=#{admin.api_key}"

expect(response.status).to eq 404
end

it "returns an empty inventory" do
@expected = {
all: []
}.to_json

get "/api/v1/inventory/#{empty_inventory.key}?token=#{admin.api_key}"

expect(response.status).to eq 200
expect(response.body).to eq @expected
end

it "returns a inventory with host" do
@expected = {
all: [small_inventory.hosts[0].alias],
}.to_json

get "/api/v1/inventory/#{small_inventory.key}?token=#{admin.api_key}"

expect(response.status).to eq 200
expect(response.body).to eq @expected
end
end

describe "GET host" do
it "didn't found the host" do
get "/api/v1/inventory/#{small_inventory.key}/#{unsafed_inventory.hosts[0].alias}?token=#{admin.api_key}"

expect(response.status).to eq 404
end

it "return a host withouts variables" do
get "/api/v1/inventory/#{small_inventory.key}/#{small_inventory.hosts[0].alias}?token=#{admin.api_key}"

expect(response.status).to eq 200
end
end
end
end

0 comments on commit 40bb7e3

Please sign in to comment.