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
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-04-13 13:24:12 +00:00
|
|
|
it 'should have a valid factory' do
|
|
|
|
Factory.build(:account).should be_valid
|
|
|
|
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
|
|
|
|
account = Factory.build(:account, attr.to_sym => nil)
|
|
|
|
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
|
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
|
|
|
|
Factory(:account)
|
|
|
|
(account = Factory.build(:account)).should_not be_valid
|
|
|
|
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
|
|
|
|
account = Factory(:account)
|
2010-05-10 22:39:52 +00:00
|
|
|
site_1 = Factory(:site, :memberships => [Membership.new(:account => account)])
|
|
|
|
site_2 = Factory(:site, :subdomain => 'foo', :memberships => [Membership.new(:account => account)])
|
2010-04-13 13:24:12 +00:00
|
|
|
account.sites.should == [site_1, site_2]
|
|
|
|
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
|
|
|
|
@account = Factory.build(:account)
|
|
|
|
@site_1 = Factory.build(:site, :subdomain => 'foo', :memberships => [Factory.build(:membership, :account => @account)])
|
|
|
|
@site_2 = Factory.build(:site, :subdomain => 'bar', :memberships => [Factory.build(:membership, :account => @account)])
|
|
|
|
@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
|
|
|
|
Site.any_instance.stubs(:admin_memberships).returns(['junk'])
|
|
|
|
@account.destroy
|
|
|
|
@site_1.memberships.should be_empty
|
|
|
|
@site_2.memberships.should be_empty
|
|
|
|
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
|
|
|
|
@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
|
|
|
|
@account = Factory.build(:account)
|
|
|
|
@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
|
|
|
|
|
|
|
|
2010-07-28 00:42:33 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
|
|
|
end
|