Project

General

Profile

Download (20.5 KB) Statistics
| Branch: | Tag: | Revision:
e1a1fa81 Joseph Mitchell Magen
require 'test_helper'

class Api::V2::DomainsControllerTest < ActionController::TestCase
dc38aad3 Joseph Magen
test "should get index" do
d041d4bb Dominic Cleal
get :index
dc38aad3 Joseph Magen
assert_response :success
assert_not_nil assigns(:domains)
end

test "should show domain" do
d041d4bb Dominic Cleal
get :show, params: { :id => Domain.first.to_param }
dc38aad3 Joseph Magen
assert_response :success
show_response = ActiveSupport::JSON.decode(@response.body)
assert !show_response.empty?
end

test "should create valid domain" do
d041d4bb Dominic Cleal
post :create, params: { :domain => { :name => "domain.net" } }
d575926a alongoldboim
assert_response :created
dc38aad3 Joseph Magen
show_response = ActiveSupport::JSON.decode(@response.body)
assert !show_response.empty?
end

56c90914 Joseph Magen
test "should not create invalid domain" do
d041d4bb Dominic Cleal
post :create, params: { :domain => { :fullname => "" } }
56c90914 Joseph Magen
assert_response :unprocessable_entity
end

8b3f1a97 Shlomi Zadok
test "should not create invalid dns_id" do
invalid_proxy_id = SmartProxy.last.id + 100
d041d4bb Dominic Cleal
post :create, params: { :domain => { :name => "doma.in", :dns_id => invalid_proxy_id } }
8b3f1a97 Shlomi Zadok
show_response = ActiveSupport::JSON.decode(@response.body)
assert_includes(show_response["error"]["full_messages"], "Dns Invalid smart-proxy id")
assert_response :unprocessable_entity
end

dc38aad3 Joseph Magen
test "should update valid domain" do
d041d4bb Dominic Cleal
put :update, params: { :id => Domain.first.to_param, :domain => { :name => "domain.new" } }
5f606e11 Daniel Lobato Garcia
assert_equal "domain.new", Domain.unscoped.first.name
dc38aad3 Joseph Magen
assert_response :success
end

test "should not update invalid domain" do
d041d4bb Dominic Cleal
put :update, params: { :id => Domain.first.to_param, :domain => { :name => "" } }
dc38aad3 Joseph Magen
assert_response :unprocessable_entity
end

5a144e4b Marek Hulán
test "should not create invalid dns_id" do
invalid_proxy_id = -1
d041d4bb Dominic Cleal
post :update, params: { :id => Domain.first.to_param, :domain => { :name => "domain.new", :dns_id => invalid_proxy_id } }
5a144e4b Marek Hulán
show_response = ActiveSupport::JSON.decode(@response.body)
assert_includes(show_response["error"]["full_messages"], "Dns Invalid smart-proxy id")
assert_response :unprocessable_entity
end

dc38aad3 Joseph Magen
test "should destroy domain" do
domain = Domain.first
domain.hosts.clear
domain.hostgroups.clear
domain.subnets.clear
d041d4bb Dominic Cleal
delete :destroy, params: { :id => domain.to_param }
dc38aad3 Joseph Magen
domain = ActiveSupport::JSON.decode(@response.body)
assert_response :ok
301e9dc1 Joseph Magen
refute Domain.find_by_id(domain['id'])
dc38aad3 Joseph Magen
end

68388bc2 Michael Moll
# test that taxonomy scope works for api for domains
e1a1fa81 Joseph Mitchell Magen
def setup
taxonomies(:location1).domain_ids = [domains(:mydomain).id, domains(:yourdomain).id]
taxonomies(:organization1).domain_ids = [domains(:mydomain).id]
end

test "should get domains for location only" do
d041d4bb Dominic Cleal
get :index, params: { :location_id => taxonomies(:location1).id }
e1a1fa81 Joseph Mitchell Magen
assert_response :success
5f606e11 Daniel Lobato Garcia
assert_equal taxonomies(:location1).domains.length, assigns(:domains).length
assert_equal assigns(:domains), taxonomies(:location1).domains
e1a1fa81 Joseph Mitchell Magen
end

test "should get domains for organization only" do
d041d4bb Dominic Cleal
get :index, params: { :organization_id => taxonomies(:organization1).id }
e1a1fa81 Joseph Mitchell Magen
assert_response :success
5f606e11 Daniel Lobato Garcia
assert_equal taxonomies(:organization1).domains.length, assigns(:domains).length
assert_equal taxonomies(:organization1).domains, assigns(:domains)
e1a1fa81 Joseph Mitchell Magen
end

test "should get domains for both location and organization" do
d041d4bb Dominic Cleal
get :index, params: { :location_id => taxonomies(:location1).id, :organization_id => taxonomies(:organization1).id }
e1a1fa81 Joseph Mitchell Magen
assert_response :success
feacea35 Amos Benari
assert_equal 1, assigns(:domains).length
e1a1fa81 Joseph Mitchell Magen
assert_equal assigns(:domains), [domains(:mydomain)]
end

8d113bd8 Joseph Magen
test "should show domain with correct child nodes including location and organization" do
d041d4bb Dominic Cleal
get :show, params: { :id => domains(:mydomain).to_param }
8d113bd8 Joseph Magen
assert_response :success
show_response = ActiveSupport::JSON.decode(@response.body)
assert !show_response.empty?
68388bc2 Michael Moll
# assert child nodes are included in response'
8d113bd8 Joseph Magen
NODES = ["locations", "organizations", "parameters", "subnets"]
NODES.sort.each do |node|
e0910b7e Michael Moll
assert show_response.key?(node), "'#{node}' child node should be in response but was not"
8d113bd8 Joseph Magen
end
end
94265cf7 Ori Rabin
3576f8fb Marek Hulán
test "admin should be able to create domain in any context" do
34f84c8d Michael Moll
domain = FactoryBot.build_stubbed(:domain)
3576f8fb Marek Hulán
post :create, params: { :domain => { :name => domain.name } }
assert_response :success
domain = Domain.unscoped.find(JSON.parse(response.body)['id'])
assert_empty domain.organizations
assert_empty domain.locations
end

test "admin should be able to create domain in any context even if they have default context set but they explicitly override it" do
34f84c8d Michael Moll
domain = FactoryBot.build_stubbed(:domain)
3576f8fb Marek Hulán
User.current.default_organization = taxonomies(:organization1)
User.current.save
post :create, params: { :organization_id => nil, :domain => { :name => domain.name } }
assert_response :success
domain = Domain.unscoped.find(JSON.parse(response.body)['id'])
assert_empty domain.organizations
assert_empty domain.locations
end

94265cf7 Ori Rabin
test "user without view_params permission can't see domain parameters" do
8c6bc83e Marek Hulan
domain_with_parameter = FactoryBot.create(:domain, :with_parameter)
8dcc90be Marek Hulán
setup_user "view", "domains"
d041d4bb Dominic Cleal
get :show, params: { :id => domain_with_parameter.to_param, :format => 'json' }
94265cf7 Ori Rabin
assert_empty JSON.parse(response.body)['parameters']
end

test "user with view_params permission can see domain parameters" do
8c6bc83e Marek Hulan
domain_with_parameter = FactoryBot.create(:domain, :with_parameter)
94265cf7 Ori Rabin
setup_user "view", "domains"
setup_user "view", "params"
d041d4bb Dominic Cleal
get :show, params: { :id => domain_with_parameter.to_param, :format => 'json' }
94265cf7 Ori Rabin
assert_not_empty JSON.parse(response.body)['parameters']
end
38a9ed0c orrabin
3576f8fb Marek Hulán
context 'user with resource permissions scoped to single org and loc' do
def setup
@org1 = FactoryBot.create(:organization)
@org2 = FactoryBot.create(:organization)
@loc1 = FactoryBot.create(:location)
@role = FactoryBot.build(:role)
@role.add_permissions!([:view_domains, :edit_domains, :view_locations, :assign_locations, :view_organizations, :assign_organizations])
@user = FactoryBot.create(:user, :organization_ids => [@org1.id], :location_ids => [@loc1.id], :roles => [ @role ])

@domain1 = FactoryBot.create(:domain, :organization_ids => [@org1.id], :location_ids => [@loc1.id])
@domain2 = FactoryBot.create(:domain, :organization_ids => [@org2.id], :location_ids => [@loc1.id])
end

test 'user can view domains from his organization and location' do
as_user @user do
get :index, params: { :organization_id => @org1.id, :location_id => @loc1.id }
end
assert_response :success
domains = JSON.parse(response.body)['results'].map { |r| r['id'] }
assert_includes domains, @domain1.id
refute_includes domains, @domain2.id
end

test 'without default org and explicit parameter, user gets record from his only org' do
as_user @user do
get :index
end
assert_response :success
domains = JSON.parse(response.body)['results'].map { |r| r['id'] }
assert_includes domains, @domain1.id
refute_includes domains, @domain2.id
end

context 'user permissions are scoped to single organization' do
def setup
super
@role.organization_ids = [ @org1.id ]
@role.location_ids = [ @loc1.id ]
@role.save # to trigger taxonomy propagation to filters
end

test 'user can view domains from his organization and location' do
as_user @user do
get :index, params: { :organization_id => @org1.id, :location_id => @loc1.id }
end
assert_response :success
domains = JSON.parse(response.body)['results'].map { |r| r['id'] }
assert_includes domains, @domain1.id
refute_includes domains, @domain2.id
end

test 'without default org and explicit parameter, user gets record from his only org' do
as_user @user do
get :index
end
assert_response :success
domains = JSON.parse(response.body)['results'].map { |r| r['id'] }
assert_includes domains, @domain1.id
refute_includes domains, @domain2.id
end
end
end

context 'user with resource permissions scoped to 2 organizations' do
def setup
@org1 = FactoryBot.create(:organization)
@org2 = FactoryBot.create(:organization)
@org3 = FactoryBot.create(:organization)
@loc1 = FactoryBot.create(:location)
@role = FactoryBot.build(:role, :organization_ids => [@org1.id, @org2.id], :location_ids => [@loc1.id])
# note that edit_organizations is required for API calls like POST /organization/1/domain, for GET we require only view_organizations
@role.add_permissions!([:view_domains, :edit_domains, :view_locations, :assign_locations, :view_organizations, :assign_organizations, :create_domains, :edit_organizations, :edit_locations])
@user = FactoryBot.create(:user, :organization_ids => [@org1.id, @org2.id], :location_ids => [@loc1.id], :roles => [@role])

@domain1 = FactoryBot.create(:domain, :organization_ids => [@org1.id], :location_ids => [@loc1.id])
@domain2 = FactoryBot.create(:domain, :organization_ids => [@org2.id], :location_ids => [@loc1.id])
@domain3 = FactoryBot.create(:domain, :organization_ids => [@org3.id], :location_ids => [@loc1.id])
end

test 'user can view domains from organization they chose org 1 explicitly' do
as_user @user do
get :index, params: { :organization_id => @org1.id, :location_id => @loc1.id }
end
assert_response :success
domains = JSON.parse(response.body)['results'].map { |r| r['id'] }
assert_includes domains, @domain1.id
refute_includes domains, @domain2.id
refute_includes domains, @domain3.id
end

test 'user can view domains from organization they chose org 2 explicitly' do
as_user @user do
get :index, params: { :organization_id => @org2.id, :location_id => @loc1.id }
end
assert_response :success
domains = JSON.parse(response.body)['results'].map { |r| r['id'] }
refute_includes domains, @domain1.id
assert_includes domains, @domain2.id
refute_includes domains, @domain3.id
end

test 'user can view domains from his both organizations without explicitly specifying a parameter' do
as_user @user do
get :index
end
assert_response :success
domains = JSON.parse(response.body)['results'].map { |r| r['id'] }
assert_includes domains, @domain1.id
assert_includes domains, @domain2.id
refute_includes domains, @domain3.id
end

test 'user can create domains in specific organization of his but the current context must be specified' do
34f84c8d Michael Moll
domain = FactoryBot.build_stubbed(:domain)
3576f8fb Marek Hulán
as_user @user do
post :create, params: { :organization_id => @org1.id, :location_id => @loc1.id, :domain => { :name => domain.name, :organization_ids => [@org1.id], :location_ids => [@loc1.id] } }
end
assert_response :success
domain = Domain.unscoped.find(JSON.parse(response.body)['id'])
assert_includes domain.organization_ids, @org1.id
assert_includes domain.location_ids, @loc1.id
end

test 'user does not have to specify taxonomy if he is assigned to only one, it is selected automatically' do
34f84c8d Michael Moll
domain = FactoryBot.build_stubbed(:domain)
3576f8fb Marek Hulán
as_user @user do
8609c702 Eric D Helms
post :create, params: { :organization_id => @org2.id, :domain => { :name => domain.name, :organization_ids => [@org2.id], :location_ids => [@loc1.id] } }
3576f8fb Marek Hulán
end
assert_response :success
domain = Domain.unscoped.find(JSON.parse(response.body)['id'])
assert_includes domain.organization_ids, @org2.id
assert_includes domain.location_ids, @loc1.id
end

test 'user can not create domain in taxonomy she does not belong to' do
34f84c8d Michael Moll
domain = FactoryBot.build_stubbed(:domain)
3576f8fb Marek Hulán
as_user @user do
post :create, params: { :domain => { :name => domain.name, :organization_ids => [@org3.id] } }
end
errors = JSON.parse(response.body)['error']['errors'].keys
assert_response 422
assert_includes errors, 'organization_ids'
end

test 'user can create domain in current context thanks to the fact organization_ids defaults to organization_id' do
34f84c8d Michael Moll
domain = FactoryBot.build_stubbed(:domain)
3576f8fb Marek Hulán
as_user @user do
8609c702 Eric D Helms
post :create, params: { :organization_id => @org2.id, :domain => { :name => domain.name, :location_ids => [@loc1.id] } }
3576f8fb Marek Hulán
end
assert_response :success
domain = Domain.unscoped.find(JSON.parse(response.body)['id'])
assert_includes domain.organization_ids, @org2.id
assert_includes domain.location_ids, @loc1.id
end

test 'user can create domain in different organization than he set as a current organization' do
34f84c8d Michael Moll
domain = FactoryBot.build_stubbed(:domain)
3576f8fb Marek Hulán
as_user @user do
8609c702 Eric D Helms
post :create, params: { :organization_id => @org1.id, :domain => { :name => domain.name, :organization_ids => [@org1.id, @org2.id], :location_ids => [@loc1.id] } }
3576f8fb Marek Hulán
end
assert_response :success
domain = Domain.unscoped.find(JSON.parse(response.body)['id'])
assert_includes domain.organization_ids, @org1.id
assert_includes domain.organization_ids, @org2.id
end

test 'user can create domain but current context is always preselected as organization_ids' do
34f84c8d Michael Moll
domain = FactoryBot.build_stubbed(:domain)
3576f8fb Marek Hulán
as_user @user do
8609c702 Eric D Helms
post :create, params: { :organization_id => @org1.id, :domain => { :name => domain.name, :organization_ids => [@org2.id], :location_ids => [@loc1.id] } }
3576f8fb Marek Hulán
end
assert_response :success
domain = Domain.unscoped.find(JSON.parse(response.body)['id'])
# this is because organization_id (current context) is preselected, consistent with UI
assert_includes domain.organization_ids, @org1.id
assert_includes domain.organization_ids, @org2.id
end

test 'user can not create resource in any context but they can use it to reset default value of organization_ids' do
34f84c8d Michael Moll
domain = FactoryBot.build_stubbed(:domain)
3576f8fb Marek Hulán
as_user @user do
8609c702 Eric D Helms
post :create, params: { :organization_id => nil, :domain => { :name => domain.name, :organization_ids => [@org2.id], :location_ids => [@loc1.id] } }
3576f8fb Marek Hulán
end
assert_response :success
domain = Domain.unscoped.find(JSON.parse(response.body)['id'])
refute_includes domain.organization_ids, @org1.id
assert_includes domain.organization_ids, @org2.id
end

test 'user can not create resource without specifying organization explicitly' do
# this would only work if user had default organization set, covered by test below
34f84c8d Michael Moll
domain = FactoryBot.build_stubbed(:domain)
3576f8fb Marek Hulán
as_user @user do
post :create, params: { :domain => { :name => domain.name } }
end
assert_response 422
errors = JSON.parse(response.body)['error']['errors'].keys
assert_includes errors, 'organization_ids'
end

context 'user has default organization set' do
def setup
super
@user.default_organization = @org1
@user.default_location = @loc1
@user.save!
end

test 'user with default organization can override the selection with explicit parameter' do
as_user @user do
get :index, params: { :organization_id => @org2.id, :location_id => @loc1.id }
end
assert_response :success
domains = JSON.parse(response.body)['results'].map { |r| r['id'] }
refute_includes domains, @domain1.id
assert_includes domains, @domain2.id
refute_includes domains, @domain3.id
end

test 'user with default organization sees resources from it if he/she does not specify explicit parameter' do
as_user @user do
get :index, params: { :organization_id => @org1.id, :location_id => @loc1.id }
end
assert_response :success
domains = JSON.parse(response.body)['results'].map { |r| r['id'] }
assert_includes domains, @domain1.id
refute_includes domains, @domain2.id
refute_includes domains, @domain3.id
end

test 'user with default organization can enforce any context via explicit parameter' do
as_user @user do
get :index, params: { :organization_id => nil, :location_id => @loc1.id }
end
assert_response :success
domains = JSON.parse(response.body)['results'].map { |r| r['id'] }
assert_includes domains, @domain1.id
assert_includes domains, @domain2.id
refute_includes domains, @domain3.id
end

test 'user can still create domains in specific organization even if it is default one' do
34f84c8d Michael Moll
domain = FactoryBot.build_stubbed(:domain)
3576f8fb Marek Hulán
as_user @user do
8609c702 Eric D Helms
post :create, params: { :organization_id => @org1.id, :domain => { :name => domain.name, :organization_ids => [@org1.id], :location_ids => [@loc1.id] } }
3576f8fb Marek Hulán
end
assert_response :success
domain = Domain.unscoped.find(JSON.parse(response.body)['id'])
assert_includes domain.organization_ids, @org1.id
end

test 'user can create domain in other than default organization' do
34f84c8d Michael Moll
domain = FactoryBot.build_stubbed(:domain)
3576f8fb Marek Hulán
as_user @user do
8609c702 Eric D Helms
post :create, params: { :organization_id => @org2.id, :domain => { :name => domain.name, :organization_ids => [@org2.id], :location_ids => [@loc1.id] } }
3576f8fb Marek Hulán
end
assert_response :success
domain = Domain.unscoped.find(JSON.parse(response.body)['id'])
assert_includes domain.organization_ids, @org2.id
end

test 'user can create domain in other than default organization without need to specify organization_ids' do
# preselected value is the currect context which overrides the default
34f84c8d Michael Moll
domain = FactoryBot.build_stubbed(:domain)
3576f8fb Marek Hulán
as_user @user do
8609c702 Eric D Helms
post :create, params: { :organization_id => @org2.id, :domain => { :name => domain.name, :location_ids => [@loc1.id] } }
3576f8fb Marek Hulán
end
assert_response :success
domain = Domain.unscoped.find(JSON.parse(response.body)['id'])
assert_includes domain.organization_ids, @org2.id
end
end
end

38a9ed0c orrabin
context 'hidden parameters' do
test "should show a domain parameter as hidden unless show_hidden_parameters is true" do
8c6bc83e Marek Hulan
domain = FactoryBot.create(:domain)
38a9ed0c orrabin
domain.domain_parameters.create!(:name => "foo", :value => "bar", :hidden_value => true)
d041d4bb Dominic Cleal
get :show, params: { :id => domain.id }
38a9ed0c orrabin
show_response = ActiveSupport::JSON.decode(@response.body)
assert_equal '*****', show_response['parameters'].first['value']
end

test "should show a domain parameter as unhidden when show_hidden_parameters is true" do
8c6bc83e Marek Hulan
domain = FactoryBot.create(:domain)
38a9ed0c orrabin
domain.domain_parameters.create!(:name => "foo", :value => "bar", :hidden_value => true)
d041d4bb Dominic Cleal
get :show, params: { :id => domain.id, :show_hidden_parameters => 'true' }
38a9ed0c orrabin
show_response = ActiveSupport::JSON.decode(@response.body)
assert_equal 'bar', show_response['parameters'].first['value']
end
end
00ed8ba5 Ondřej Pražák
test "should update existing domain parameters" do
8c6bc83e Marek Hulan
domain = FactoryBot.create(:domain)
00ed8ba5 Ondřej Pražák
param_params = { :name => "foo", :value => "bar" }
domain.domain_parameters.create!(param_params)
d041d4bb Dominic Cleal
put :update, params: { :id => domain.id, :domain => { :domain_parameters_attributes => [{ :name => param_params[:name], :value => "new_value" }] } }
00ed8ba5 Ondřej Pražák
assert_response :success
assert param_params[:name], domain.parameters.first.name
end

test "should delete existing domain parameters" do
8c6bc83e Marek Hulan
domain = FactoryBot.create(:domain)
00ed8ba5 Ondřej Pražák
param_1 = { :name => "foo", :value => "bar" }
param_2 = { :name => "boo", :value => "test" }
domain.domain_parameters.create!([param_1, param_2])
d041d4bb Dominic Cleal
put :update, params: { :id => domain.id, :domain => { :domain_parameters_attributes => [{ :name => param_1[:name], :value => "new_value" }] } }
00ed8ba5 Ondřej Pražák
assert_response :success
assert_equal 1, domain.parameters.count
end
114973b3 Michael Moll
test "should get domains for searched location only" do
taxonomies(:location2).domain_ids = [domains(:unuseddomain).id]
d041d4bb Dominic Cleal
get :index, params: { :search => "location_id=#{taxonomies(:location2).id}" }
114973b3 Michael Moll
assert_response :success
assert_equal taxonomies(:location2).domains.length, assigns(:domains).length
assert_equal assigns(:domains), taxonomies(:location2).domains
end
8fe2ad71 Tomer Brisker
test "should get domains when searching with organization_id" do
8c6bc83e Marek Hulan
domain = FactoryBot.create(:domain)
org = FactoryBot.create(:organization)
8fe2ad71 Tomer Brisker
org.domain_ids = [domain.id]
d041d4bb Dominic Cleal
get :index, params: {:search => domain.name, :organization_id => org.id }
8fe2ad71 Tomer Brisker
assert_response :success
assert_equal org.domains.length, assigns(:domains).length
assert_equal assigns(:domains), org.domains
end
e1a1fa81 Joseph Mitchell Magen
end