foreman/test/controllers/api/v2/realms_controller_test.rb @ e0910b7e
77f70152 | Stephen Benjamin | require 'test_helper'
|
|
class Api::V2::RealmsControllerTest < ActionController::TestCase
|
|||
test "should get index" do
|
|||
d041d4bb | Dominic Cleal | get :index
|
|
77f70152 | Stephen Benjamin | assert_response :success
|
|
assert_not_nil assigns(:realms)
|
|||
end
|
|||
test "should show realm" do
|
|||
d041d4bb | Dominic Cleal | get :show, params: { :id => Realm.first.to_param }
|
|
77f70152 | Stephen Benjamin | assert_response :success
|
|
show_response = ActiveSupport::JSON.decode(@response.body)
|
|||
assert !show_response.empty?
|
|||
end
|
|||
test "should not create invalid realm" do
|
|||
d041d4bb | Dominic Cleal | post :create, params: { :realm => { :name => "" } }
|
|
77f70152 | Stephen Benjamin | assert_response :unprocessable_entity
|
|
end
|
|||
test "should create valid realm" do
|
|||
d041d4bb | Dominic Cleal | post :create, params: { :realm => { :name => "realm.net", :realm_proxy_id => smart_proxies(:realm).to_param, :realm_type => "FreeIPA" } }
|
|
d575926a | alongoldboim | assert_response :created
|
|
77f70152 | Stephen Benjamin | show_response = ActiveSupport::JSON.decode(@response.body)
|
|
assert !show_response.empty?
|
|||
end
|
|||
test "should update valid realm" do
|
|||
5f606e11 | Daniel Lobato Garcia | realm_id = Realm.unscoped.first.id
|
|
d041d4bb | Dominic Cleal | put :update, params: { :id => realm_id, :realm => { :name => "realm.new" } }
|
|
5f606e11 | Daniel Lobato Garcia | assert_equal "realm.new", Realm.unscoped.find(realm_id).name
|
|
77f70152 | Stephen Benjamin | assert_response :success
|
|
end
|
|||
test "should not update invalid realm" do
|
|||
d041d4bb | Dominic Cleal | put :update, params: { :id => Realm.first.to_param, :realm => { :name => "" } }
|
|
77f70152 | Stephen Benjamin | assert_response :unprocessable_entity
|
|
end
|
|||
test "should destroy realm" do
|
|||
realm = Realm.first
|
|||
realm.hosts.clear
|
|||
realm.hostgroups.clear
|
|||
d041d4bb | Dominic Cleal | delete :destroy, params: { :id => realm.to_param }
|
|
77f70152 | Stephen Benjamin | realm = ActiveSupport::JSON.decode(@response.body)
|
|
assert_response :ok
|
|||
5f606e11 | Daniel Lobato Garcia | refute Realm.unscoped.find_by_id(realm['id'])
|
|
77f70152 | Stephen Benjamin | end
|
|
68388bc2 | Michael Moll | # test that taxonomy scope works for api for realms
|
|
77f70152 | Stephen Benjamin | def setup
|
|
taxonomies(:location1).realm_ids = [realms(:myrealm).id, realms(:yourrealm).id]
|
|||
taxonomies(:organization1).realm_ids = [realms(:myrealm).id]
|
|||
end
|
|||
test "should get realms for location only" do
|
|||
d041d4bb | Dominic Cleal | get :index, params: { :location_id => taxonomies(:location1).id }
|
|
77f70152 | Stephen Benjamin | assert_response :success
|
|
5f606e11 | Daniel Lobato Garcia | assert_equal taxonomies(:location1).realms.length, assigns(:realms).length
|
|
assert_equal assigns(:realms), taxonomies(:location1).realms
|
|||
77f70152 | Stephen Benjamin | end
|
|
test "should get realms for organization only" do
|
|||
d041d4bb | Dominic Cleal | get :index, params: { :organization_id => taxonomies(:organization1).id }
|
|
77f70152 | Stephen Benjamin | assert_response :success
|
|
5f606e11 | Daniel Lobato Garcia | assert_equal taxonomies(:organization1).realms.length, assigns(:realms).length
|
|
assert_equal assigns(:realms), taxonomies(:organization1).realms
|
|||
77f70152 | Stephen Benjamin | end
|
|
test "should get realms for both location and organization" do
|
|||
d041d4bb | Dominic Cleal | get :index, params: { :location_id => taxonomies(:location1).id, :organization_id => taxonomies(:organization1).id }
|
|
77f70152 | Stephen Benjamin | assert_response :success
|
|
assert_equal 1, assigns(:realms).length
|
|||
assert_equal assigns(:realms), [realms(:myrealm)]
|
|||
end
|
|||
test "should show realm with correct child nodes including location and organization" do
|
|||
d041d4bb | Dominic Cleal | get :show, params: { :id => realms(:myrealm).to_param }
|
|
77f70152 | Stephen Benjamin | assert_response :success
|
|
show_response = ActiveSupport::JSON.decode(@response.body)
|
|||
assert !show_response.empty?
|
|||
68388bc2 | Michael Moll | # assert child nodes are included in response'
|
|
77f70152 | Stephen Benjamin | NODES = ["locations", "organizations"]
|
|
NODES.sort.each do |node|
|
|||
e0910b7e | Michael Moll | assert show_response.key?(node), "'#{node}' child node should be in response but was not"
|
|
77f70152 | Stephen Benjamin | end
|
|
end
|
|||
end
|