Project

General

Profile

Download (5.3 KB) Statistics
| Branch: | Tag: | Revision:
2f077f63 Ohad Levy
require 'test_helper'

class HostsControllerTest < ActionController::TestCase
d91160ea Lucas Tolchinsky
setup :initialize_host

b09b4515 Ohad Levy
def test_show
9c0e127b Paul Kelly
get :show, {:id => Host.first}, set_session_user
b09b4515 Ohad Levy
assert_template 'show'
2f077f63 Ohad Levy
end
8f10d0f8 Juan Manuel
b09b4515 Ohad Levy
def test_create_invalid
Host.any_instance.stubs(:valid?).returns(false)
9c0e127b Paul Kelly
post :create, {}, set_session_user
b09b4515 Ohad Levy
assert_template 'new'
8f10d0f8 Juan Manuel
end

b09b4515 Ohad Levy
def test_create_valid
Host.any_instance.stubs(:valid?).returns(true)
9c0e127b Paul Kelly
post :create, {:host => {:name => "test"}}, set_session_user
assert_redirected_to host_url(assigns('host'))
8f10d0f8 Juan Manuel
end

9c0e127b Paul Kelly
test "should get index" do
get :index, {}, set_session_user
assert_response :success
assert_template 'index'
end

test "should get new" do
get :new, {}, set_session_user
assert_response :success
assert_template 'new'
end

test "should create new host" do
assert_difference 'Host.count' do
post :create, { :commit => "Create",
:host => {:name => "myotherfullhost",
6874bbd9 Paul Kelly
:mac => "aabbecddee06",
:ip => "123.05.04.25",
9c0e127b Paul Kelly
:domain => Domain.find_or_create_by_name("othercompany.com"),
6874bbd9 Paul Kelly
:operatingsystem => Operatingsystem.first,
9c0e127b Paul Kelly
:architecture => Architecture.first,
:environment => Environment.first,
:disk => "empty partition"
6874bbd9 Paul Kelly
}
9c0e127b Paul Kelly
}, set_session_user
end
6874bbd9 Paul Kelly
assert_redirected_to host_url(assigns['host'])
9c0e127b Paul Kelly
end

test "should get edit" do
get :edit, {:id => @host.id}, set_session_user
assert_response :success
b09b4515 Ohad Levy
assert_template 'edit'
end
fa9836c1 Lucas Tolchinsky
9c0e127b Paul Kelly
test "should update host" do
put :update, { :commit => "Update", :id => @host.id, :host => {:disk => "ntfs"} }, set_session_user
@host = Host.find_by_id(@host.id)
assert_equal @host.disk, "ntfs"
end

b09b4515 Ohad Levy
def test_update_invalid
Host.any_instance.stubs(:valid?).returns(false)
9c0e127b Paul Kelly
put :update, {:id => Host.first}, set_session_user
b09b4515 Ohad Levy
assert_template 'edit'
8f10d0f8 Juan Manuel
end

b09b4515 Ohad Levy
def test_update_valid
Host.any_instance.stubs(:valid?).returns(true)
9c0e127b Paul Kelly
put :update, {:id => Host.first}, set_session_user
b09b4515 Ohad Levy
assert_redirected_to host_url(assigns(:host))
8f10d0f8 Juan Manuel
end
cd060345 Lucas Tolchinsky
6874bbd9 Paul Kelly
test "should destroy host" do
assert_difference('Host.count', -1) do
delete :destroy, {:id => @host.id}, set_session_user
end
b09b4515 Ohad Levy
assert_redirected_to hosts_url
end
9c0e127b Paul Kelly
test "externalNodes should render 404 when no params are given" do
get :externalNodes, {}, set_session_user
cd060345 Lucas Tolchinsky
assert_response :missing
assert_template :text => '404 Not Found'
end

test "externalNodes should render correctly when id is given" do
9c0e127b Paul Kelly
get :externalNodes, {:id => @host.id}, set_session_user
cd060345 Lucas Tolchinsky
assert_response :success
d91160ea Lucas Tolchinsky
assert_template :text => @host.info.to_yaml.gsub("\n","<br>")
cd060345 Lucas Tolchinsky
end

test "externalNodes should render correctly when name is given" do
9c0e127b Paul Kelly
get :externalNodes, {:name => @host.name}, set_session_user
cd060345 Lucas Tolchinsky
assert_response :success
d91160ea Lucas Tolchinsky
assert_template :text => @host.info.to_yaml.gsub("\n","<br>")
cd060345 Lucas Tolchinsky
end

test "externalNodes should render yml request correctly" do
9c0e127b Paul Kelly
get :externalNodes, {:id => @host.id, :format => "yml"}, set_session_user
cd060345 Lucas Tolchinsky
assert_response :success
d91160ea Lucas Tolchinsky
assert_template :text => @host.info.to_yaml
cd060345 Lucas Tolchinsky
end

6874bbd9 Paul Kelly
test "when host is saved after setBuild, the flash should inform it" do
d91160ea Lucas Tolchinsky
mock(@host).setBuild {true}
mock(Host).find(@host.id.to_s) {@host}
05880730 Lucas Tolchinsky
@request.env['HTTP_REFERER'] = hosts_path

9c0e127b Paul Kelly
get :setBuild, {:id => @host.id}, set_session_user
05880730 Lucas Tolchinsky
assert_response :found
assert_redirected_to hosts_path
assert_not_nil flash[:foreman_notice]
6874bbd9 Paul Kelly
assert flash[:foreman_notice] == "Enabled myfullhost.company.com for rebuild on next boot"
05880730 Lucas Tolchinsky
end

6874bbd9 Paul Kelly
test "when host is not saved after setBuild, the flash should inform it" do
07ad7166 Lucas Tolchinsky
mock(@host).setBuild {false}
mock(Host).find(@host.id.to_s) {@host}
cd060345 Lucas Tolchinsky
@request.env['HTTP_REFERER'] = hosts_path

9c0e127b Paul Kelly
get :setBuild, {:id => @host.id}, set_session_user
cd060345 Lucas Tolchinsky
assert_response :found
assert_redirected_to hosts_path
05880730 Lucas Tolchinsky
assert_not_nil flash[:foreman_error]
assert flash[:foreman_error] == "Failed to enable myfullhost.company.com for installation"
cd060345 Lucas Tolchinsky
end

test "rrdreport should print error message if host has no last_report" do
9c0e127b Paul Kelly
get :rrdreport, {:id => @host.id}, set_session_user
cd060345 Lucas Tolchinsky
assert_response :success
assert_template :text => "Sorry, no graphs for this host"
end

test "rrdreport should render graphics" do
d91160ea Lucas Tolchinsky
@host.last_report = Date.today
assert @host.save!
cd060345 Lucas Tolchinsky
SETTINGS[:rrd_report_url] = "/some/url"

9c0e127b Paul Kelly
get :rrdreport, {:id => @host.id}, set_session_user
cd060345 Lucas Tolchinsky
assert_response :success
assert_template :partial => "_rrdreport"
end

test "report should redirect to host's last report" do
9c0e127b Paul Kelly
get :report, {:id => @host.id}, set_session_user
cd060345 Lucas Tolchinsky
assert_response :found
edf4198b Ohad Levy
assert_redirected_to :controller => "reports", :action => "show", :id => Report.maximum(:id, :conditions => {:host_id => @host})
cd060345 Lucas Tolchinsky
end

test "query in .yml format should return host.to_yml" do
9c0e127b Paul Kelly
get :query, {:format => "yml"}, set_session_user
d91160ea Lucas Tolchinsky
assert_template :text => @host.to_yaml
end

private
def initialize_host
@host = Host.create :name => "myfullhost",
:mac => "aabbecddeeff",
:ip => "123.05.02.03",
:domain => Domain.find_or_create_by_name("company.com"),
:operatingsystem => Operatingsystem.first,
:architecture => Architecture.first,
:environment => Environment.first,
:disk => "empty partition"
cd060345 Lucas Tolchinsky
end
2f077f63 Ohad Levy
end