FEATURE: topic support in disposable invites

This commit is contained in:
Arpit Jalan 2014-07-15 16:40:35 +05:30
parent 4c25fedf70
commit 8862a881f8
3 changed files with 54 additions and 3 deletions

View file

@ -57,17 +57,23 @@ class InvitesController < ApplicationController
def redeem_disposable_invite
params.require(:email)
params.permit(:username, :name)
params.permit(:username, :name, :topic)
invite = Invite.find_by(invite_key: params[:token])
if invite.present?
user = Invite.redeem_from_token(params[:token], params[:email], params[:username], params[:name])
user = Invite.redeem_from_token(params[:token], params[:email], params[:username], params[:name], params[:topic].to_i)
if user.present?
log_on_user(user)
# Send a welcome message if required
user.enqueue_welcome_message('welcome_invite') if user.send_welcome_message
topic = invite.topics.first
if topic.present?
redirect_to "#{Discourse.base_uri}#{topic.relative_url}"
return
end
end
end

View file

@ -168,10 +168,11 @@ class Invite < ActiveRecord::Base
invite
end
def self.redeem_from_token(token, email, username=nil, name=nil)
def self.redeem_from_token(token, email, username=nil, name=nil, topic_id=nil)
invite = Invite.find_by(invite_key: token)
if invite
invite.update_column(:email, email)
invite.topic_invites.create!(invite_id: invite.id, topic_id: topic_id) if topic_id && Topic.find_by_id(topic_id)
user = InviteRedeemer.new(invite, username, name).redeem
end
user

View file

@ -215,6 +215,50 @@ describe InvitesController do
end
context '.redeem_disposable_invite' do
context 'with an invalid invite token' do
before do
get :redeem_disposable_invite, email: "name@example.com", token: "doesn't exist"
end
it "redirects to the root" do
response.should redirect_to("/")
end
it "should not change the session" do
session[:current_user_id].should be_blank
end
end
context 'with a valid invite token' do
let(:topic) { Fabricate(:topic) }
let(:invitee) { Fabricate(:user) }
let(:invite) { Invite.create!(invited_by: invitee) }
it 'redeems the invite' do
Invite.expects(:redeem_from_token).with(invite.invite_key, "name@example.com", nil, nil, topic.id)
get :redeem_disposable_invite, email: "name@example.com", token: invite.invite_key, topic: topic.id
end
context 'when redeem returns a user' do
let(:user) { Fabricate(:user) }
before do
Invite.expects(:redeem_from_token).with(invite.invite_key, user.email, nil, nil, topic.id).returns(user)
get :redeem_disposable_invite, email: user.email, token: invite.invite_key, topic: topic.id
end
it 'logs in user' do
session[:current_user_id].should == user.id
end
end
end
end
context '.check_csv_chunk' do
it 'requires you to be logged in' do
lambda {