Skip to content

Commit

Permalink
Added tests for system models. Fixed errors found.
Browse files Browse the repository at this point in the history
  • Loading branch information
Doug Youch committed Jun 21, 2010
1 parent 886b650 commit 425316d
Show file tree
Hide file tree
Showing 8 changed files with 259 additions and 7 deletions.
1 change: 1 addition & 0 deletions app/models/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Client < SystemModel
validates_numericality_of :max_file_storage

def before_validation
self.domain_limit = Client::DEFAULT_DOMAIN_LIMIT unless self.domain_limit
self.max_client_users = Client::DEFAULT_MAX_CLIENTS unless self.max_client_users
self.max_file_storage = Client::DEFAULT_MAX_FILE_STORAGE unless self.max_file_storage
end
Expand Down
4 changes: 3 additions & 1 deletion app/models/client_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def before_save # :nodoc:
end

def validate
self.errors.add(:client_id, 'is missing') unless self.client

if self.id.nil? && self.client
self.errors.add_to_base('Max users for client') if self.client.available_client_users == 0
end
Expand Down Expand Up @@ -92,7 +94,7 @@ def identifier_name #:nodoc:
end

def validate_password(pw) #:nodoc:
return ClientUser.hash_password(pw) == self.hashed_password
return ClientUser.hash_password(pw, self.salt) == self.hashed_password
end

def domain_database_select_options
Expand Down
8 changes: 7 additions & 1 deletion app/models/domain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,18 @@ class Domain < SystemModel
validates_format_of :name, :with => /^([a-zA-Z0-9\.\-]+?)\.([a-zA-Z]+)$/,
:message => ' is not a valid domain '

validates_presence_of :client_id
validates_presence_of :domain_type
validates_inclusion_of :domain_type, :in => %w(domain redirect)

def before_create #:nodoc:
self.inactive_message = 'Site Currently Down for Maintenance' if self.blank?
end

def validate
self.errors.add(:max_file_storage, 'is too large') if self.max_file_storage && self.max_file_storage > self.client.available_file_storage
self.errors.add(:client_id, 'is missing') unless self.client
self.errors.add(:max_file_storage, 'is too large') if self.max_file_storage && self.client && self.max_file_storage > self.client.available_file_storage
self.errors.add(:domain_database_id, 'is invalid') if self.domain_database && self.domain_database.client_id != self.client_id
end

def after_save #:nodoc:
Expand Down
13 changes: 12 additions & 1 deletion app/models/domain_database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class DomainDatabase < SystemModel

validates_presence_of :name
validates_uniqueness_of :name
validates_presence_of :client_id

validates_numericality_of :max_file_storage

Expand All @@ -19,7 +20,7 @@ def before_validation_on_create
end

def validate
self.errors.add(:max_file_storage, 'is too large') if self.max_file_storage > self.client.available_file_storage
self.errors.add(:max_file_storage, 'is too large') if self.client && self.max_file_storage > self.client.available_file_storage
end

def after_save #:nodoc:
Expand All @@ -36,4 +37,14 @@ def first_domain
def domain_name
self.first_domain ? self.first_domain.name : self.name
end

def options
opt = self.read_attribute(:options)
return opt if opt
database_file = "#{RAILS_ROOT}/config/sites/#{self.name}.yml"
return nil unless File.exists?(database_file)
info = YAML.load_file(database_file)
self.update_attribute(:options, info) if self.id
info
end
end
37 changes: 37 additions & 0 deletions spec/models/client_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require File.dirname(__FILE__) + "/../spec_helper"

describe Client do

reset_system_tables :clients

it "should be a valid client" do
@client = Client.new
@client.should have(1).errors_on(:name)
@client.should have(0).errors_on(:domain_limit)
@client.should have(0).errors_on(:max_client_users)
@client.should have(0).errors_on(:max_file_storage)
end

it "should be able to create a new client with just a name" do
@client = Client.create :name => 'New Client'
@client.id.should_not be_nil

@client.num_databases.should == 0
@client.domain_limit.should > 0
@client.can_add_database?.should be_true
@client.num_client_users.should == 0
@client.max_client_users.should > 0
@client.available_client_users.should == @client.max_client_users
@client.used_file_storage.should == 0
@client.max_file_storage.should > 0
@client.available_file_storage.should == @client.max_file_storage
end

it "can not create a client with the same name" do
@client = Client.create :name => 'New Client'
@client.id.should_not be_nil

@client = Client.create :name => 'New Client'
@client.id.should be_nil
end
end
72 changes: 72 additions & 0 deletions spec/models/client_user_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
require File.dirname(__FILE__) + "/../spec_helper"

describe ClientUser do

reset_system_tables :clients, :client_users, :domains, :domain_databases
reset_domain_tables :end_users

it "should be a valid client user" do
@user = ClientUser.new
@user.should have(1).errors_on(:username)
@user.should have(1).errors_on(:client_id)
end

it "should be able to create a client user" do
@client = Client.create :name => 'New Client'
@client.id.should_not be_nil
@user = @client.client_users.create :username => 'my_test_user'
@user.id.should_not be_nil

@user = @client.client_users.create :username => 'my_test_user'
@user.id.should be_nil
end

it "should be able to setup the password and login" do
@client = Client.create :name => 'New Client'
@client.id.should_not be_nil
@user = @client.client_users.create :username => 'my_test_user', :password => 'salt-this'
@user.id.should_not be_nil
@user.hashed_password.should_not be_nil
@user.salt.should_not be_nil

@user.name.should == 'my_test_user'
@user.identifier_name.should == "CLIENT USER:#{@user.username} (#{@user.id})"
@user.validate_password('unknown').should be_false
@user.validate_password('salt-this').should be_true

@user = ClientUser.login_by_name 'my_test_user', 'unknown', @client.id
@user.should be_nil

@user = ClientUser.login_by_name 'my_test_user', 'salt-this', @client.id
@user.should_not be_nil

@user.password = 'new-password'
@user.save

@user = ClientUser.login_by_name 'my_test_user', 'salt-this', @client.id
@user.should be_nil

@user = ClientUser.login_by_name 'my_test_user', 'new-password', @client.id
@user.should_not be_nil
end

it "should create an end user" do
@domain = Domain.find CMS_DEFAULTS['testing_domain']
@domain.id.should_not be_nil
@user = @domain.client.client_users.create :username => 'my_test_user', :password => 'salt-this'
@user.id.should_not be_nil

assert_difference 'EndUser.count', 1 do
@end_user = @user.end_user
end

@end_user.client_user_id.should == @user.id

@user = ClientUser.find @user.id
assert_difference 'EndUser.count', 0 do
@end_user = @user.end_user
end

@end_user.client_user_id.should == @user.id
end
end
75 changes: 75 additions & 0 deletions spec/models/domain_database_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
require File.dirname(__FILE__) + "/../spec_helper"

describe DomainDatabase do

reset_system_tables :domains, :clients, :domain_databases

it "should be a valid domain database" do
@db = DomainDatabase.new
@db.valid?
@db.should have(1).errors_on(:name)
@db.should have(1).errors_on(:client_id)
end

it "should be to create a domain database" do
@client = Client.create :name => 'New Client'
@db = DomainDatabase.create :name => 'webiva_001_test_dev', :client_id => @client.id
@db.id.should_not be_nil

@db = DomainDatabase.create :name => 'webiva_001_test_dev', :client_id => @client.id
@db.id.should be_nil
end

it "should not allow an invalid max_file_storage" do
@client = Client.create :name => 'New Client'
@db = DomainDatabase.create :name => 'test_webiva_001_test_dev', :client_id => @client.id, :max_file_storage => @client.max_file_storage + 1
@db.id.should be_nil
end

it "should set the options to the yml file if it exists" do
database_file = "#{RAILS_ROOT}/config/sites/test_webiva_001_test_dev.yml"
File.unlink(database_file) if File.exists?(database_file) # incase this test failed previously

@client = Client.create :name => 'New Client'
@db = DomainDatabase.create :name => 'test_webiva_001_test_dev', :client_id => @client.id
@db.id.should_not be_nil
@db.options.should be_nil

output = {'production' => {'host' => 'localhost', 'username' => 'test', 'password' => ''}}
File.open(database_file,"w") { |fd| fd.write(YAML.dump(output)) }
@db.options.should_not be_nil
@db.options['production']['host'].should == 'localhost'

File.unlink database_file
@db.reload
@db.options['production']['host'].should == 'localhost'
end

it "should be able to get the first domain" do
@client = Client.create :name => 'New Client'
@db = @client.domain_databases.create :name => 'test_webiva_001_test_dev'
@db.id.should_not be_nil
@db.first_domain.should be_nil
@db.domain_name.should == 'test_webiva_001_test_dev'

@domain = @client.domains.create :name => 'test-webiva.dev', :domain_database_id => @db.id
@domain.id.should_not be_nil

@db.reload
@db.first_domain.id.should == @domain.id
@db.domain_name.should == 'test-webiva.dev'
end

it "should be clearing the domain info cache after saving" do
@client = Client.create :name => 'New Client'
@db = @client.domain_databases.create :name => 'test_webiva_001_test_dev'
@db.id.should_not be_nil
@domain = @client.domains.create :name => 'test-webiva.dev', :domain_database_id => @db.id
@domain.id.should_not be_nil

@db.reload

DataCache.should_receive(:set_domain_info).with('test-webiva.dev', nil)
@db.update_attribute(:max_file_storage, 2)
end
end
56 changes: 52 additions & 4 deletions spec/models/domain_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,58 @@

describe Domain do

reset_system_tables :domains
reset_system_tables :domains, :clients, :domain_databases

it "should require a name" do
domain = Domain.new
domain.valid?.should be_false
before(:each) do
@client = Client.find :first
end

it "should be a valid domain" do
@client = Client.create :name => 'New Client'
@domain = Domain.new
@domain.valid?
@domain.should have(2).errors_on(:name)
@domain.should have(1).errors_on(:client_id)
@domain = Domain.new :client_id => @client.id
@domain.name = '_ape.com'
@domain.should have(1).errors_on(:name)
@domain.name = 'ape.com'
@domain.valid?.should be_true
@domain.name = 'my.ape.com'
@domain.valid?.should be_true
@domain.name = 'ape.c-om'
@domain.should have(1).errors_on(:name)
@domain.name = 'ape.com'
@domain.domain_type = 'new_domain'
@domain.valid?.should be_false
@domain.should have(1).errors_on(:domain_type)
end

it "should be able to create a domain" do
@client = Client.create :name => 'New Client'
@domain = Domain.create :name => 'test-webiva.dev', :client_id => @client.id
@domain.id.should_not be_nil
end

it "should make this domain the primary" do
@client = Client.create :name => 'New Client'
@db = @client.domain_databases.create :name => 'test_webiva_dev_100'
@domain = @client.domains.create :name => 'test-webiva.dev', :database => @db.name
@domain.id.should_not be_nil
@domain.set_primary
@domain.reload
@domain.primary.should be_true
end

it "should create a domain database if max_file_storage is set" do
@client = Client.create :name => 'New Client'

# this would be for existing systems that were setup before domain_databases table existed
assert_difference 'DomainDatabase.count', 1 do
@domain = @client.domains.create :name => 'test-webiva.dev', :database => 'test_webiva_dev_100', :max_file_storage => 1
end

@db = DomainDatabase.find :last
@db.id.should == @domain.domain_database_id
end
end

0 comments on commit 425316d

Please sign in to comment.