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

Commit

Permalink
add apiv1 inventory test
Browse files Browse the repository at this point in the history
  • Loading branch information
pgolm committed Dec 17, 2013
1 parent adb5b7d commit a1be622
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
13 changes: 13 additions & 0 deletions spec/factories/group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FactoryGirl.define do
factory :group, class: Group do
id { generate(:random_id) }
name { "group#{id}" }
association :inventory, factory: :inventory

trait :with_hosts do
hosts { create_list(:host, 2, inventory_id: inventory_id) }
end

factory :group_with_hosts, traits: [:with_hosts]
end
end
8 changes: 6 additions & 2 deletions spec/factories/inventory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
hosts { create_list(:host, 3, inventory_id: id) }
end

factory :small_inventory, traits: [:with_hosts]
factory :big_inventory, traits: [:with_hosts]
trait :with_groups do
groups { create_list(:group, 3, inventory_id: id) }
end

factory :inventory_with_hosts, traits: [:with_hosts]
factory :inventory_with_groups, traits: [:with_groups]
end

end
27 changes: 20 additions & 7 deletions spec/requests/apis_spec.rb → spec/requests/api_v1_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

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

it "unauthorized me" do
Expand Down Expand Up @@ -33,27 +34,39 @@

it "returns a inventory with hosts" do
@expected = {
all: small_inventory.hosts.map(&:alias),
all: host_inventory.hosts.map(&:alias),
}.to_json

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

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

it "returns an inventory with groups"
it "returns an inventory with groups" do
@expected = {
all: [],
}
group_inventory.groups.each do |group|
@expected[group.name] = {hosts: [], vars:{} }
end

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

expect(response.status).to eq 200
expect(response.body).to eq @expected.to_json
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}"
get "/api/v1/inventory/#{host_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}"
get "/api/v1/inventory/#{host_inventory.key}/#{host_inventory.hosts[0].alias}?token=#{admin.api_key}"

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

0 comments on commit a1be622

Please sign in to comment.