From 8862a881f87f900fef961738c69012910b5223ce Mon Sep 17 00:00:00 2001 From: Arpit Jalan Date: Tue, 15 Jul 2014 16:40:35 +0530 Subject: [PATCH] FEATURE: topic support in disposable invites --- app/controllers/invites_controller.rb | 10 ++++- app/models/invite.rb | 3 +- spec/controllers/invites_controller_spec.rb | 44 +++++++++++++++++++++ 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/app/controllers/invites_controller.rb b/app/controllers/invites_controller.rb index 47bf42bc5..8664f86c8 100644 --- a/app/controllers/invites_controller.rb +++ b/app/controllers/invites_controller.rb @@ -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 diff --git a/app/models/invite.rb b/app/models/invite.rb index 86c24d231..54b8131a7 100644 --- a/app/models/invite.rb +++ b/app/models/invite.rb @@ -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 diff --git a/spec/controllers/invites_controller_spec.rb b/spec/controllers/invites_controller_spec.rb index 6dcc79dc1..dce252b64 100644 --- a/spec/controllers/invites_controller_spec.rb +++ b/spec/controllers/invites_controller_spec.rb @@ -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 {