A bit of refactoring on the invitation spec. Remove old pending specs.

This commit is contained in:
Sarah Mei 2011-03-19 23:10:20 -07:00
parent c3612ee66d
commit 69c0d8e1d9

View file

@ -5,9 +5,9 @@
require 'spec_helper' require 'spec_helper'
describe Invitation do describe Invitation do
let(:user) {alice} let(:user) { alice }
let(:aspect) {user.aspects.first} let(:aspect) { user.aspects.first }
let(:user2) {eve} let(:user2) { eve }
before do before do
user.invites = 20 user.invites = 20
user.save user.save
@ -46,8 +46,8 @@ describe Invitation do
end end
describe '.new_user_by_service_and_identifier' do describe '.new_user_by_service_and_identifier' do
let(:inv){Invitation.new_user_by_service_and_identifier(@type, @identifier)} let(:inv) { Invitation.new_user_by_service_and_identifier(@type, @identifier) }
it 'returns User.new for a non-existent user for email' do it 'returns User.new for a non-existent user for email' do
@type = "email" @type = "email"
@identifier = "maggie@example.org" @identifier = "maggie@example.org"
@ -58,7 +58,7 @@ describe Invitation do
inv.reload inv.reload
}.should raise_error ActiveRecord::RecordNotFound }.should raise_error ActiveRecord::RecordNotFound
end end
it 'returns User.new for a non-existent user' do it 'returns User.new for a non-existent user' do
@type = "facebook" @type = "facebook"
@identifier = "1234892323" @identifier = "1234892323"
@ -72,7 +72,7 @@ describe Invitation do
end end
describe '.find_existing_user' do describe '.find_existing_user' do
let(:inv){Invitation.find_existing_user(@type, @identifier)} let(:inv) { Invitation.find_existing_user(@type, @identifier) }
before do before do
@users = [] @users = []
8.times do 8.times do
@ -132,7 +132,7 @@ describe Invitation do
it 'associates the invitation with the inviter' do it 'associates the invitation with the inviter' do
lambda { lambda {
Invitation.invite(:service => 'email', :identifier => @email, :from => user, :into => aspect) Invitation.invite(:service => 'email', :identifier => @email, :from => user, :into => aspect)
}.should change{user.reload.invitations_from_me.count}.by(1) }.should change { user.reload.invitations_from_me.count }.by(1)
end end
it 'associates the invitation with the invitee' do it 'associates the invitation with the invitee' do
@ -165,7 +165,7 @@ describe Invitation do
it 'sends a contact request to a user with that email into the aspect' do it 'sends a contact request to a user with that email into the aspect' do
user2 user2
user.should_receive(:send_contact_request_to){ |a, b| user.should_receive(:send_contact_request_to) { |a, b|
a.should == user2.person a.should == user2.person
b.should == aspect b.should == aspect
} }
@ -174,7 +174,7 @@ describe Invitation do
it 'decrements the invite count of the from user' do it 'decrements the invite count of the from user' do
message = "How've you been?" message = "How've you been?"
lambda{ lambda {
new_user = Invitation.invite(:from => user, :service => 'email', :identifier => @email, :into => aspect, :message => message) new_user = Invitation.invite(:from => user, :service => 'email', :identifier => @email, :into => aspect, :message => message)
}.should change(user, :invites).by(-1) }.should change(user, :invites).by(-1)
end end
@ -184,7 +184,7 @@ describe Invitation do
user.save! user.save!
message = "How've you been?" message = "How've you been?"
lambda { lambda {
new_user = Invitation.invite(:from => user, :service => 'email', :identifier => @email, :into => aspect, :message => message) Invitation.invite(:from => user, :service => 'email', :identifier => @email, :into => aspect, :message => message)
}.should_not change(user, :invites) }.should_not change(user, :invites)
end end
@ -198,37 +198,37 @@ describe Invitation do
end end
describe '.create_invitee' do describe '.create_invitee' do
context 'with an existing invitee' do context "when we're resending an invitation" do
before do before do
@valid_params = {:from => user, @valid_params = {:from => user,
:service => 'email', :service => 'email',
:identifier => @email, :identifier => @email,
:into => aspect, :into => aspect,
:message => @message} :message => @message}
@invitee = Invitation.create_invitee(:service => 'email', :identifier => @email) @invitee = Invitation.create_invitee(:service => 'email', :identifier => @email)
@valid_params[:existing_user] = @invitee @valid_params[:existing_user] = @invitee
end end
it 'creates no user' do
@valid_params[:existing_user] = @invitee it "does not create a user" do
lambda { expect { Invitation.create_invitee(@valid_params) }.should_not change(User, :count)
Invitation.create_invitee(@valid_params)
}.should_not change(User, :count)
end end
it 'sends mail' do
lambda { it "sends mail" do
expect {
Invitation.create_invitee(@valid_params) Invitation.create_invitee(@valid_params)
}.should change{Devise.mailer.deliveries.size}.by(1) }.should change { Devise.mailer.deliveries.size }.by(1)
end end
it 'does not set the key' do
lambda { it "does not set the key" do
expect {
Invitation.create_invitee(@valid_params) Invitation.create_invitee(@valid_params)
}.should_not change{@invitee.reload.serialized_private_key} }.should_not change { @invitee.reload.serialized_private_key }
end end
it 'does not change the invitation token' do
pending "until this passes, old invitation emails will be invalidated by new ones" it "changes the invitation token" do
lambda { old_token = @invitee.invitation_token
Invitation.create_invitee(@valid_params) Invitation.create_invitee(@valid_params)
}.should_not change{@invitee.reload.invitation_token} @invitee.reload.invitation_token.should_not == old_token
end end
end end
context 'with an inviter' do context 'with an inviter' do
@ -237,63 +237,56 @@ describe Invitation do
@valid_params = {:from => user, :service => 'email', :identifier => @email, :into => aspect, :message => @message} @valid_params = {:from => user, :service => 'email', :identifier => @email, :into => aspect, :message => @message}
end end
it 'sends mail' do it "sends mail" do
lambda { expect {
Invitation.create_invitee(@valid_params) Invitation.create_invitee(@valid_params)
}.should change{Devise.mailer.deliveries.size}.by(1) }.should change { Devise.mailer.deliveries.size }.by(1)
end end
it 'mails the optional message' do it "includes the message in the email" do
new_user = Invitation.create_invitee(@valid_params) Invitation.create_invitee(@valid_params)
Devise.mailer.deliveries.first.to_s.include?(@message).should be_true Devise.mailer.deliveries.last.to_s.should include(@message)
end end
it 'has no translation missing' do it "has no translation missing" do
new_user = Invitation.create_invitee(@valid_params) Invitation.create_invitee(@valid_params)
Devise.mailer.deliveries.first.body.raw_source.match(/(translation_missing.+)/).should be_nil Devise.mailer.deliveries.last.body.raw_source.should_not match(/(translation_missing.+)/)
end end
it "doesn't create an invitation if the email is invalid" do it "doesn't create a user if the email is invalid" do
new_user = Invitation.create_invitee(@valid_params.merge(:identifier => 'fdfdfdfdf')) new_user = Invitation.create_invitee(@valid_params.merge(:identifier => 'fdfdfdfdf'))
new_user.should_not be_persisted new_user.should_not be_persisted
new_user.should have(1).error_on(:email) new_user.should have(1).error_on(:email)
end end
it 'does not save a user with an empty string email' do it "does not save a user with an empty string email" do
@valid_params[:service] = 'facebook' @valid_params[:service] = 'facebook'
@valid_params[:identifier] = '3423423' @valid_params[:identifier] = '3423423'
Invitation.create_invitee(@valid_params) Invitation.create_invitee(@valid_params)
@valid_params[:identifier] = 'dfadsfdas' @valid_params[:identifier] = 'dfadsfdas'
lambda { expect { Invitation.create_invitee(@valid_params) }.should_not raise_error
Invitation.create_invitee(@valid_params)
}.should_not raise_error
end end
end end
context 'with no inviter' do context 'with no inviter' do
it 'sends an email that includes the right things' do it 'sends an email that includes the right things' do
Invitation.create_invitee(:service => 'email', :identifier => @email) Invitation.create_invitee(:service => 'email', :identifier => @email)
Devise.mailer.deliveries.first.to_s.include?("Welcome #{@email}").should == true Devise.mailer.deliveries.first.to_s.should include("Welcome #{@email}")
end end
it 'creates a user' do it 'creates a user' do
lambda { expect {
Invitation.create_invitee(:service => 'email', :identifier => @email) Invitation.create_invitee(:service => 'email', :identifier => @email)
}.should change(User, :count).by(1) }.should change(User, :count).by(1)
end end
it 'sends email to the invited user' do it 'sends email to the invited user' do
lambda { expect {
Invitation.create_invitee(:service => 'email', :identifier => @email) Invitation.create_invitee(:service => 'email', :identifier => @email)
}.should change{Devise.mailer.deliveries.size}.by(1) }.should change { Devise.mailer.deliveries.size }.by(1)
end end
it 'does not render nonsensical emails' do it 'does not create an invitation' do
Invitation.create_invitee(:service => 'email', :identifier => @email) expect {
Devise.mailer.deliveries.first.body.raw_source.match(/have invited you to join/i).should be_false
end
it 'creates an invitation' do
pending "Invitations should be more flexible, allowing custom messages to be passed in without an inviter."
lambda {
Invitation.create_invitee(:service => 'email', :identifier => @email) Invitation.create_invitee(:service => 'email', :identifier => @email)
}.should change(Invitation, :count).by(1) }.should_not change(Invitation, :count)
end end
end end
end end
@ -306,7 +299,7 @@ describe Invitation do
end end
it 'sends another email' do it 'sends another email' do
lambda{@invitation.resend}.should change(Devise.mailer.deliveries, :count).by(1) lambda { @invitation.resend }.should change(Devise.mailer.deliveries, :count).by(1)
end end
end end
@ -314,12 +307,11 @@ describe Invitation do
before do before do
@new_user = Invitation.invite(:from => user, :service => 'email', :identifier => @email, :into => aspect) @new_user = Invitation.invite(:from => user, :service => 'email', :identifier => @email, :into => aspect)
acceptance_params = {:invitation_token => "abc", acceptance_params = {:invitation_token => "abc",
:username => "user", :username => "user",
:email => @email, :email => @email,
:password => "secret", :password => "secret",
:password_confirmation => "secret", :password_confirmation => "secret",
:person => {:profile => {:first_name => "Bob", :person => {:profile => {:first_name => "Bob", :last_name => "Smith"}}}
:last_name => "Smith"}}}
@new_user.setup(acceptance_params) @new_user.setup(acceptance_params)
@new_user.person.save @new_user.person.save
@new_user.save @new_user.save