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
|
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 ##
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-04-13 13:24:12 +00:00
|
|
|
%w{name email password}.each do |attr|
|
|
|
|
it "should validate presence of #{attr}" do
|
2011-08-25 21:28:56 +00:00
|
|
|
account = FactoryGirl.build(:account, attr.to_sym => nil)
|
2010-04-13 13:24:12 +00:00
|
|
|
account.should_not be_valid
|
|
|
|
account.errors[attr.to_sym].should include("can't be blank")
|
|
|
|
end
|
|
|
|
end
|
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)
|
2011-11-20 11:47:41 +00:00
|
|
|
site_1 = FactoryGirl.create(:site, :memberships => [Locomotive::Membership.new(:account => account)])
|
|
|
|
site_2 = FactoryGirl.create(:site, :memberships => [Locomotive::Membership.new(:account => account)])
|
2011-06-28 13:50:37 +00:00
|
|
|
account.reload.sites.to_a.should == [site_1, site_2]
|
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-28 00:42:33 +00:00
|
|
|
describe 'cross domain authentication' do
|
2010-07-31 00:22:58 +00:00
|
|
|
|
2010-07-28 00:42:33 +00:00
|
|
|
before(:each) do
|
2011-08-25 21:28:56 +00:00
|
|
|
@account = FactoryGirl.build(:account)
|
2010-07-28 00:42:33 +00:00
|
|
|
@account.stubs(:save).returns(true)
|
|
|
|
end
|
2010-07-31 00:22:58 +00:00
|
|
|
|
2010-07-28 00:42:33 +00:00
|
|
|
it 'sets a token' do
|
|
|
|
@account.reset_switch_site_token!.should be_true
|
|
|
|
@account.switch_site_token.should_not be_empty
|
|
|
|
end
|
2010-07-31 00:22:58 +00:00
|
|
|
|
2010-07-28 00:42:33 +00:00
|
|
|
context 'retrieving an account' do
|
2010-07-31 00:22:58 +00:00
|
|
|
|
2010-07-28 00:42:33 +00:00
|
|
|
it 'does not find it with an empty token' do
|
2011-11-20 11:47:41 +00:00
|
|
|
Locomotive::Account.find_using_switch_site_token(nil).should be_nil
|
2010-07-28 00:42:33 +00:00
|
|
|
end
|
2010-07-31 00:22:58 +00:00
|
|
|
|
2010-07-28 00:42:33 +00:00
|
|
|
it 'raises an exception if not found' do
|
2011-11-20 11:47:41 +00:00
|
|
|
expect {
|
|
|
|
Locomotive::Account.find_using_switch_site_token!(nil)
|
|
|
|
}.to raise_error Mongoid::Errors::DocumentNotFound
|
2010-07-28 00:42:33 +00:00
|
|
|
end
|
2010-07-31 00:22:58 +00:00
|
|
|
|
2010-07-28 00:42:33 +00:00
|
|
|
end
|
2010-07-31 00:22:58 +00:00
|
|
|
|
2011-07-26 19:20:03 +00:00
|
|
|
end
|
2010-07-31 00:22:58 +00:00
|
|
|
|
2011-07-26 19:20:03 +00:00
|
|
|
after(:all) do
|
|
|
|
ENV['APP_TLD'] = nil
|
|
|
|
Locomotive.configure_for_test(true)
|
2010-07-28 00:42:33 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
|
|
|
end
|