engine/spec/models/locomotive/account_spec.rb
Mario Visic 840609cbce Merge remote-tracking branch 'origin/master' into rails_3_1
Conflicts:
	Gemfile
	Gemfile.lock
	app/cells/locomotive/menu_cell.rb
	app/helpers/admin/content_types_helper.rb
	app/models/content_instance.rb
	app/models/page.rb
	app/views/admin/shared/menu/_contents.html.haml
	config/initializers/locomotive.rb
	config/locales/admin_ui.de.yml
	config/locales/admin_ui.en.yml
	config/locales/admin_ui.es.yml
	config/locales/admin_ui.fr.yml
	config/locales/admin_ui.it.yml
	config/locales/admin_ui.nl.yml
	config/locales/admin_ui.no.yml
	config/locales/admin_ui.pt-BR.yml
	config/locales/admin_ui.ru.yml
	lib/generators/locomotive/install/templates/locomotive.rb
	lib/locomotive/configuration.rb
	locomotive_cms.gemspec
	spec/models/locomotive/account_spec.rb
2012-01-25 22:16:43 +11:00

94 lines
3.0 KiB
Ruby

require 'spec_helper'
describe Locomotive::Account do
let!(:existing_account) { Factory(:account, :email => 'another@email.com') }
it 'should have a valid factory' do
FactoryGirl.build(:account).should be_valid
end
## Validations ##
it { should validate_presence_of :name }
it { should validate_presence_of :email }
it { should validate_presence_of :password }
it { should validate_uniqueness_of(:email).with_message(/is already taken/) }
it { should allow_value('valid@email.com').for(:email) }
it { should allow_value('prefix+suffix@email.com').for(:email) }
it { should_not allow_value('not-an-email').for(:email) }
it "should have a default locale" do
account = Locomotive::Account.new
account.locale.should == 'en'
end
it "should validate uniqueness of email" do
FactoryGirl.create(:account)
(account = FactoryGirl.build(:account)).should_not be_valid
account.errors[:email].should == ["is already taken"]
end
## Associations ##
it 'should own many sites' do
account = FactoryGirl.create(:account)
site_1 = FactoryGirl.create(:site, :memberships => [Locomotive::Membership.new(:account => account)])
site_2 = FactoryGirl.create(:site, :memberships => [Locomotive::Membership.new(:account => account)])
account.reload.sites.to_a.should == [site_1, site_2]
end
describe 'deleting' do
before(:each) do
@account = FactoryGirl.build(:account)
@site_1 = FactoryGirl.build(:site,:memberships => [FactoryGirl.build(:membership, :account => @account)])
@site_2 = FactoryGirl.build(:site,:memberships => [FactoryGirl.build(:membership, :account => @account)])
@account.stubs(:sites).returns([@site_1, @site_2])
Locomotive::Site.any_instance.stubs(:save).returns(true)
end
it 'should also delete memberships' do
Locomotive::Site.any_instance.stubs(:admin_memberships).returns(['junk', 'dirt'])
@site_1.memberships.first.expects(:destroy)
@site_2.memberships.first.expects(:destroy)
@account.destroy
end
it 'should raise an exception if account is the only remaining admin' do
@site_1.memberships.first.stubs(:admin?).returns(true)
@site_1.stubs(:admin_memberships).returns(['junk'])
lambda {
@account.destroy
}.should raise_error(Exception, "One admin account is required at least")
end
end
describe 'cross domain authentication' do
before(:each) do
@account = FactoryGirl.build(:account)
@account.stubs(:save).returns(true)
end
it 'sets a token' do
@account.reset_switch_site_token!.should be_true
@account.switch_site_token.should_not be_empty
end
context 'retrieving an account' do
it 'does not find it with an empty token' do
Locomotive::Account.find_using_switch_site_token(nil).should be_nil
end
it 'raises an exception if not found' do
expect {
Locomotive::Account.find_using_switch_site_token!(nil)
}.to raise_error Mongoid::Errors::DocumentNotFound
end
end
end
end