discourse/spec/controllers/invites_controller_spec.rb

156 lines
4.3 KiB
Ruby
Raw Normal View History

2013-02-05 14:16:51 -05:00
require 'spec_helper'
describe InvitesController do
context '.destroy' do
it 'requires you to be logged in' do
lambda {
delete :destroy, email: 'jake@adventuretime.ooo'
}.should raise_error(Discourse::NotLoggedIn)
end
2013-02-25 11:42:20 -05:00
context 'while logged in' do
2013-02-05 14:16:51 -05:00
let!(:user) { log_in }
let!(:invite) { Fabricate(:invite, invited_by: user) }
let(:another_invite) { Fabricate(:invite, email: 'anotheremail@address.com') }
2013-02-25 11:42:20 -05:00
2013-02-05 14:16:51 -05:00
it 'raises an error when the email is missing' do
lambda { delete :destroy }.should raise_error(ActionController::ParameterMissing)
2013-02-05 14:16:51 -05:00
end
it "raises an error when the email cannot be found" do
lambda { delete :destroy, email: 'finn@adventuretime.ooo' }.should raise_error(Discourse::InvalidParameters)
end
it 'raises an error when the invite is not yours' do
lambda { delete :destroy, email: another_invite.email }.should raise_error(Discourse::InvalidParameters)
end
it "destroys the invite" do
2013-07-09 15:20:18 -04:00
Invite.any_instance.expects(:trash!).with(user)
2013-02-05 14:16:51 -05:00
delete :destroy, email: invite.email
end
end
end
context '.create' do
it 'requires you to be logged in' do
lambda {
post :create, email: 'jake@adventuretime.ooo'
}.should raise_error(Discourse::NotLoggedIn)
end
context 'while logged in' do
let!(:user) { log_in }
let(:email) { 'jake@adventuretime.ooo' }
it "fails if you can't invite to the forum" do
Guardian.any_instance.stubs(:can_invite_to_forum?).returns(false)
Invite.expects(:invite_by_email).never
post :create, email: email
response.should_not be_success
end
it "delegates to Invite#invite_by_email and returns success if you can invite" do
Guardian.any_instance.stubs(:can_invite_to_forum?).returns(true)
Invite.expects(:invite_by_email).with(email, user).returns(Invite.new)
post :create, email: email
response.should be_success
end
end
2013-02-05 14:16:51 -05:00
end
context '.show' do
context 'with an invalid invite id' do
before do
get :show, id: "doesn't exist"
end
2013-02-25 11:42:20 -05:00
it "redirects to the root" do
2013-02-05 14:16:51 -05:00
response.should redirect_to("/")
2013-02-25 11:42:20 -05:00
end
2013-02-05 14:16:51 -05:00
2013-02-25 11:42:20 -05:00
it "should not change the session" do
2013-02-05 14:16:51 -05:00
session[:current_user_id].should be_blank
2013-02-25 11:42:20 -05:00
end
2013-02-05 14:16:51 -05:00
end
context 'with a deleted invite' do
let(:topic) { Fabricate(:topic) }
let(:invite) { topic.invite_by_email(topic.user, "iceking@adventuretime.ooo") }
let(:deleted_invite) { invite.destroy; invite }
before do
get :show, id: deleted_invite.invite_key
end
2013-02-25 11:42:20 -05:00
it "redirects to the root" do
2013-02-05 14:16:51 -05:00
response.should redirect_to("/")
2013-02-25 11:42:20 -05:00
end
2013-02-05 14:16:51 -05:00
2013-02-25 11:42:20 -05:00
it "should not change the session" do
2013-02-05 14:16:51 -05:00
session[:current_user_id].should be_blank
2013-02-25 11:42:20 -05:00
end
2013-02-05 14:16:51 -05:00
end
context 'with a valid invite id' do
let(:topic) { Fabricate(:topic) }
let(:invite) { topic.invite_by_email(topic.user, "iceking@adventuretime.ooo") }
2013-02-25 11:42:20 -05:00
2013-02-05 14:16:51 -05:00
it 'redeems the invite' do
Invite.any_instance.expects(:redeem)
get :show, id: invite.invite_key
end
context 'when redeem returns a user' do
let(:user) { Fabricate(:coding_horror) }
2013-02-25 11:42:20 -05:00
context 'success' do
2013-02-05 14:16:51 -05:00
before do
Invite.any_instance.expects(:redeem).returns(user)
2013-02-25 11:42:20 -05:00
get :show, id: invite.invite_key
2013-02-05 14:16:51 -05:00
end
it 'logs in the user' do
session[:current_user_id].should == user.id
end
it 'redirects to the first topic the user was invited to' do
response.should redirect_to(topic.relative_url)
end
end
context 'welcome message' do
before do
Invite.any_instance.stubs(:redeem).returns(user)
Jobs.expects(:enqueue).with(:invite_email, has_key(:invite_id))
end
2013-02-25 11:42:20 -05:00
it 'sends a welcome message if set' do
user.send_welcome_message = true
2013-02-05 14:16:51 -05:00
user.expects(:enqueue_welcome_message).with('welcome_invite')
get :show, id: invite.invite_key
end
2013-02-25 11:42:20 -05:00
it "doesn't send a welcome message if not set" do
2013-02-05 14:16:51 -05:00
user.expects(:enqueue_welcome_message).with('welcome_invite').never
get :show, id: invite.invite_key
end
end
2013-02-25 11:42:20 -05:00
end
2013-02-05 14:16:51 -05:00
end
end
end