2010-04-13 13:24:12 +00:00
|
|
|
require 'spec_helper'
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2011-11-20 11:47:41 +00:00
|
|
|
describe Locomotive::Account do
|
2012-01-21 04:51:38 +00:00
|
|
|
let!(:existing_account) { Factory(:account, :email => 'another@email.com') }
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-04-13 13:24:12 +00:00
|
|
|
it 'should have a valid factory' do
|
2011-08-25 21:28:56 +00:00
|
|
|
FactoryGirl.build(:account).should be_valid
|
2010-04-13 13:24:12 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-04-13 13:24:12 +00:00
|
|
|
## Validations ##
|
2012-01-21 04:51:38 +00:00
|
|
|
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) }
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-04-13 13:24:12 +00:00
|
|
|
it "should have a default locale" do
|
2011-11-20 11:47:41 +00:00
|
|
|
account = Locomotive::Account.new
|
2010-04-13 13:24:12 +00:00
|
|
|
account.locale.should == 'en'
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-04-13 13:24:12 +00:00
|
|
|
it "should validate uniqueness of email" do
|
2011-08-25 21:28:56 +00:00
|
|
|
FactoryGirl.create(:account)
|
|
|
|
(account = FactoryGirl.build(:account)).should_not be_valid
|
2010-04-13 13:24:12 +00:00
|
|
|
account.errors[:email].should == ["is already taken"]
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-04-13 13:24:12 +00:00
|
|
|
## Associations ##
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-04-13 13:24:12 +00:00
|
|
|
it 'should own many sites' do
|
2011-08-25 21:28:56 +00:00
|
|
|
account = FactoryGirl.create(:account)
|
2012-02-16 11:42:41 +00:00
|
|
|
site_1 = FactoryGirl.create(:site, :memberships => [Locomotive::Membership.new(:account => account)])
|
|
|
|
site_2 = FactoryGirl.create(:site, :subdomain => 'another_one', :memberships => [Locomotive::Membership.new(:account => account)])
|
|
|
|
sites = [site_1, site_2].map(&:_id)
|
|
|
|
account.reload.sites.all? { |s| sites.include?(s._id) }.should be_true
|
2010-04-13 13:24:12 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-10 22:39:52 +00:00
|
|
|
describe 'deleting' do
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-10 22:39:52 +00:00
|
|
|
before(:each) do
|
2011-08-25 21:28:56 +00:00
|
|
|
@account = FactoryGirl.build(:account)
|
2011-11-20 11:47:41 +00:00
|
|
|
@site_1 = FactoryGirl.build(:site,:memberships => [FactoryGirl.build(:membership, :account => @account)])
|
|
|
|
@site_2 = FactoryGirl.build(:site,:memberships => [FactoryGirl.build(:membership, :account => @account)])
|
2010-05-10 22:39:52 +00:00
|
|
|
@account.stubs(:sites).returns([@site_1, @site_2])
|
2011-11-20 11:47:41 +00:00
|
|
|
Locomotive::Site.any_instance.stubs(:save).returns(true)
|
2010-05-10 22:39:52 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-10 22:39:52 +00:00
|
|
|
it 'should also delete memberships' do
|
2011-11-20 11:47:41 +00:00
|
|
|
Locomotive::Site.any_instance.stubs(:admin_memberships).returns(['junk', 'dirt'])
|
2011-09-28 16:27:29 +00:00
|
|
|
@site_1.memberships.first.expects(:destroy)
|
|
|
|
@site_2.memberships.first.expects(:destroy)
|
2010-05-10 22:39:52 +00:00
|
|
|
@account.destroy
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-10 22:39:52 +00:00
|
|
|
it 'should raise an exception if account is the only remaining admin' do
|
2011-09-28 16:27:29 +00:00
|
|
|
@site_1.memberships.first.stubs(:admin?).returns(true)
|
2010-05-10 22:39:52 +00:00
|
|
|
@site_1.stubs(:admin_memberships).returns(['junk'])
|
|
|
|
lambda {
|
|
|
|
@account.destroy
|
|
|
|
}.should raise_error(Exception, "One admin account is required at least")
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-10 22:39:52 +00:00
|
|
|
end
|
2010-07-31 00:22:58 +00:00
|
|
|
|
2010-07-23 20:09:54 +00:00
|
|
|
end
|