2010-04-13 13:24:12 +00:00
|
|
|
require 'spec_helper'
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-04-13 13:24:12 +00:00
|
|
|
describe 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
|
2010-05-10 22:39:52 +00:00
|
|
|
account = 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)
|
|
|
|
site_1 = FactoryGirl.create(:site, :memberships => [Membership.new(:account => account)])
|
|
|
|
site_2 = FactoryGirl.create(:site, :subdomain => 'foo', :memberships => [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)
|
|
|
|
@site_1 = FactoryGirl.build(:site, :subdomain => 'foo', :memberships => [FactoryGirl.build(:membership, :account => @account)])
|
|
|
|
@site_2 = FactoryGirl.build(:site, :subdomain => 'bar', :memberships => [FactoryGirl.build(:membership, :account => @account)])
|
2010-05-10 22:39:52 +00:00
|
|
|
@account.stubs(:sites).returns([@site_1, @site_2])
|
|
|
|
Site.any_instance.stubs(:save).returns(true)
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-10 22:39:52 +00:00
|
|
|
it 'should also delete memberships' do
|
2011-09-28 16:27:29 +00:00
|
|
|
Site.any_instance.stubs(:admin_memberships).returns(['junk', 'dirt'])
|
|
|
|
@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
|
|
|
|
Account.find_using_switch_site_token(nil).should be_nil
|
|
|
|
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
|
|
|
|
lambda {
|
|
|
|
Account.find_using_switch_site_token!(nil)
|
|
|
|
}.should raise_error(Mongoid::Errors::DocumentNotFound)
|
|
|
|
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
|