require File.dirname(__FILE__) + '/../spec_helper.rb' require 'ruby-debug' describe Server do before(:each) do @server = Server.create(:host => TEST_SERVER) end it "should be valid when created" do @server.should be_a Server end describe "'create_group' method" do before(:each) do @group = Group.new(:name => TEST_GROUP) end after(:each) do @server.execute_command("sudo /usr/sbin/groupdel #{TEST_GROUP}") end it "should create a group" do @server.create_group(@group) @server.execute_command("cat /etc/group | grep #{TEST_GROUP}") # Not the strongest test - grep not ideal for this @server.output.should_not == '' end end describe "running plesk" do before(:each) do # mock a plesk server @server.execute_command("sudo mkdir /usr/local/psa") end after(:each) do # how can we tear this down safely? @server.execute_command("sudo rm -rf /usr/local/psa") end it "should know it is a plesk server" do @server.is_running_plesk?.should be_true end end describe "not running plesk" do it "should know it is not a plesk server" do @server.is_running_plesk?.should be_false end end describe "with accounts" do before(:each) do @role = Role.create(:name => 'test') @role.servers << @server @role.users.create(:name => TEST_USER) end it "should have an account object for each of its role's users" do @server.accounts.first.user.should == @role.users.first end it "should check if an account exists" do # Runner has to exist for the login to have worked runner = User.new(:name => TEST_RUNNER) @server.account_exists_for?(runner).should be_true @server.account_exists_for?(@role.users.first).should be_false end it "should identify account that require adding" do @server.accounts_to_add.should == Array(@role.users.first) end end describe "with an account that should be deleted" do before(:each) do @role = Role.create(:name => 'test') @role.servers << @server # We'll add the test user too; just to check it doesn't get deleted @role.users.create(:name => TEST_USER) @server.accounts.first.add_user @doomed_user = User.create(:name => TEST_DOOMED_USER) @server.execute_command("sudo /usr/sbin/useradd -m #{@doomed_user.name}") end after(:each) do @server.execute_command("sudo /usr/sbin/userdel --remove #{TEST_USER}") @server.execute_command("sudo /usr/sbin/userdel --remove #{TEST_DOOMED_USER}") end it "should identify accounts that require removing" do debugger @server.accounts_to_remove.should == Array(@doomed_user) end it "should delete the account" do @server.account_exists_for?(@doomed_user).should be_true @server.delete_account_for(@doomed_user) @server.account_exists_for?(@doomed_user).should be_false end end end