2015-10-11 17:41:23 +08:00
|
|
|
require 'rails_helper'
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
describe InvitesController do
|
|
|
|
|
2017-02-14 05:19:41 +08:00
|
|
|
context '.show' do
|
|
|
|
it "shows error if invite not found" do
|
|
|
|
get :show, id: 'nopeNOPEnope'
|
|
|
|
expect(response).to render_template(layout: 'no_ember')
|
|
|
|
expect(flash[:error]).to be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
it "renders the accept invite page if invite exists" do
|
|
|
|
i = Fabricate(:invite)
|
|
|
|
get :show, id: i.invite_key
|
|
|
|
expect(response).to render_template(layout: 'application')
|
|
|
|
expect(flash[:error]).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
context '.destroy' do
|
|
|
|
|
|
|
|
it 'requires you to be logged in' do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect {
|
2013-02-06 03:16:51 +08:00
|
|
|
delete :destroy, email: 'jake@adventuretime.ooo'
|
2015-01-10 01:04:02 +08:00
|
|
|
}.to raise_error(Discourse::NotLoggedIn)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
context 'while logged in' do
|
2013-02-06 03:16:51 +08:00
|
|
|
let!(:user) { log_in }
|
|
|
|
let!(:invite) { Fabricate(:invite, invited_by: user) }
|
|
|
|
let(:another_invite) { Fabricate(:invite, email: 'anotheremail@address.com') }
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
it 'raises an error when the email is missing' do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect { delete :destroy }.to raise_error(ActionController::ParameterMissing)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error when the email cannot be found" do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect { delete :destroy, email: 'finn@adventuretime.ooo' }.to raise_error(Discourse::InvalidParameters)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error when the invite is not yours' do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect { delete :destroy, email: another_invite.email }.to raise_error(Discourse::InvalidParameters)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "destroys the invite" do
|
2013-07-10 03:20:18 +08:00
|
|
|
Invite.any_instance.expects(:trash!).with(user)
|
2013-02-06 03:16:51 +08:00
|
|
|
delete :destroy, email: invite.email
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-11-07 01:56:26 +08:00
|
|
|
end
|
|
|
|
|
2017-07-21 14:12:24 +08:00
|
|
|
context '#create' do
|
2013-11-07 01:56:26 +08:00
|
|
|
it 'requires you to be logged in' do
|
2017-07-21 14:12:24 +08:00
|
|
|
expect do
|
2013-11-07 01:56:26 +08:00
|
|
|
post :create, email: 'jake@adventuretime.ooo'
|
2017-07-21 14:12:24 +08:00
|
|
|
end.to raise_error(Discourse::NotLoggedIn)
|
2013-11-07 01:56:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'while logged in' do
|
|
|
|
let(:email) { 'jake@adventuretime.ooo' }
|
|
|
|
|
|
|
|
it "fails if you can't invite to the forum" do
|
2014-05-09 16:22:15 +08:00
|
|
|
log_in
|
2013-11-07 01:56:26 +08:00
|
|
|
post :create, email: email
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).not_to be_success
|
2013-11-07 01:56:26 +08:00
|
|
|
end
|
|
|
|
|
2014-07-30 01:57:08 +08:00
|
|
|
it "fails for normal user if invite email already exists" do
|
2014-09-05 14:52:40 +08:00
|
|
|
user = log_in(:trust_level_4)
|
2014-07-30 01:57:08 +08:00
|
|
|
invite = Invite.invite_by_email("invite@example.com", user)
|
|
|
|
invite.reload
|
|
|
|
post :create, email: invite.email
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).not_to be_success
|
2017-06-14 00:59:02 +08:00
|
|
|
json = JSON.parse(response.body)
|
|
|
|
expect(json["failed"]).to be_present
|
2014-07-30 01:57:08 +08:00
|
|
|
end
|
|
|
|
|
2014-05-09 16:22:15 +08:00
|
|
|
it "allows admins to invite to groups" do
|
|
|
|
group = Fabricate(:group)
|
|
|
|
log_in(:admin)
|
|
|
|
post :create, email: email, group_names: group.name
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_success
|
|
|
|
expect(Invite.find_by(email: email).invited_groups.count).to eq(1)
|
2013-11-07 01:56:26 +08:00
|
|
|
end
|
2014-07-30 01:57:08 +08:00
|
|
|
|
2017-07-21 14:12:24 +08:00
|
|
|
it 'allows group owners to invite to groups' do
|
|
|
|
group = Fabricate(:group)
|
|
|
|
user = log_in
|
|
|
|
user.update!(trust_level: TrustLevel[2])
|
|
|
|
group.add_owner(user)
|
|
|
|
|
|
|
|
post :create, email: email, group_names: group.name
|
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
expect(Invite.find_by(email: email).invited_groups.count).to eq(1)
|
|
|
|
end
|
|
|
|
|
2014-07-30 01:57:08 +08:00
|
|
|
it "allows admin to send multiple invites to same email" do
|
|
|
|
user = log_in(:admin)
|
|
|
|
invite = Invite.invite_by_email("invite@example.com", user)
|
|
|
|
invite.reload
|
|
|
|
post :create, email: invite.email
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_success
|
2014-07-30 01:57:08 +08:00
|
|
|
end
|
2017-06-14 00:59:02 +08:00
|
|
|
|
|
|
|
it "responds with error message in case of validation failure" do
|
|
|
|
log_in(:admin)
|
|
|
|
post :create, email: "test@mailinator.com"
|
|
|
|
expect(response).not_to be_success
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
expect(json["errors"]).to be_present
|
|
|
|
end
|
2013-11-07 01:56:26 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-08-26 09:41:52 +08:00
|
|
|
context '.create_invite_link' do
|
|
|
|
it 'requires you to be logged in' do
|
|
|
|
expect {
|
|
|
|
post :create_invite_link, email: 'jake@adventuretime.ooo'
|
|
|
|
}.to raise_error(Discourse::NotLoggedIn)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'while logged in' do
|
|
|
|
let(:email) { 'jake@adventuretime.ooo' }
|
|
|
|
|
|
|
|
it "fails if you can't invite to the forum" do
|
|
|
|
log_in
|
|
|
|
post :create_invite_link, email: email
|
|
|
|
expect(response).not_to be_success
|
|
|
|
end
|
|
|
|
|
|
|
|
it "fails for normal user if invite email already exists" do
|
|
|
|
user = log_in(:trust_level_4)
|
|
|
|
invite = Invite.invite_by_email("invite@example.com", user)
|
|
|
|
invite.reload
|
|
|
|
post :create_invite_link, email: invite.email
|
|
|
|
expect(response).not_to be_success
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows admins to invite to groups" do
|
|
|
|
group = Fabricate(:group)
|
|
|
|
log_in(:admin)
|
|
|
|
post :create_invite_link, email: email, group_names: group.name
|
|
|
|
expect(response).to be_success
|
|
|
|
expect(Invite.find_by(email: email).invited_groups.count).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows multiple group invite" do
|
2017-07-08 08:24:39 +08:00
|
|
|
Fabricate(:group, name: "security")
|
|
|
|
Fabricate(:group, name: "support")
|
2015-08-26 09:41:52 +08:00
|
|
|
log_in(:admin)
|
|
|
|
post :create_invite_link, email: email, group_names: "security,support"
|
|
|
|
expect(response).to be_success
|
|
|
|
expect(Invite.find_by(email: email).invited_groups.count).to eq(2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-25 04:15:29 +08:00
|
|
|
context '.perform_accept_invitation' do
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
context 'with an invalid invite id' do
|
|
|
|
before do
|
2017-02-14 05:19:41 +08:00
|
|
|
xhr :put, :perform_accept_invitation, id: "doesn't exist", format: :json
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
it "redirects to the root" do
|
2017-01-25 04:15:29 +08:00
|
|
|
expect(response).to be_success
|
2017-02-14 05:19:41 +08:00
|
|
|
json = JSON.parse(response.body)
|
|
|
|
expect(json["success"]).to eq(false)
|
|
|
|
expect(json["message"]).to eq(I18n.t('invite.not_found'))
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
it "should not change the session" do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(session[:current_user_id]).to be_blank
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08: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
|
2017-02-14 05:19:41 +08:00
|
|
|
xhr :put, :perform_accept_invitation, id: deleted_invite.invite_key, format: :json
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
it "redirects to the root" do
|
2017-01-25 04:15:29 +08:00
|
|
|
expect(response).to be_success
|
2017-02-14 05:19:41 +08:00
|
|
|
json = JSON.parse(response.body)
|
|
|
|
expect(json["success"]).to eq(false)
|
|
|
|
expect(json["message"]).to eq(I18n.t('invite.not_found'))
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
it "should not change the session" do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(session[:current_user_id]).to be_blank
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08: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-26 00:42:20 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
it 'redeems the invite' do
|
|
|
|
Invite.any_instance.expects(:redeem)
|
2017-02-14 05:19:41 +08:00
|
|
|
xhr :put, :perform_accept_invitation, id: invite.invite_key, format: :json
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when redeem returns a user' do
|
|
|
|
let(:user) { Fabricate(:coding_horror) }
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
context 'success' do
|
2017-02-14 05:19:41 +08:00
|
|
|
subject { xhr :put, :perform_accept_invitation, id: invite.invite_key, format: :json }
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
before do
|
|
|
|
Invite.any_instance.expects(:redeem).returns(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'logs in the user' do
|
2017-06-01 16:19:42 +08:00
|
|
|
events = DiscourseEvent.track_events { subject }
|
|
|
|
|
|
|
|
expect(events.map { |event| event[:event_name] }).to include(
|
|
|
|
:user_logged_in, :user_first_logged_in
|
|
|
|
)
|
|
|
|
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(session[:current_user_id]).to eq(user.id)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'redirects to the first topic the user was invited to' do
|
2017-02-14 05:19:41 +08:00
|
|
|
subject
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
expect(json["success"]).to eq(true)
|
|
|
|
expect(json["redirect_to"]).to eq(topic.relative_url)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'failure' do
|
|
|
|
subject { xhr :put, :perform_accept_invitation, id: invite.invite_key, format: :json }
|
|
|
|
|
|
|
|
it "doesn't log in the user if there's a validation error" do
|
|
|
|
user.errors.add(:password, :common)
|
|
|
|
Invite.any_instance.expects(:redeem).raises(ActiveRecord::RecordInvalid.new(user))
|
|
|
|
subject
|
|
|
|
expect(response).to be_success
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
expect(json["success"]).to eq(false)
|
|
|
|
expect(json["errors"]["password"]).to be_present
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-18 16:55:52 +08:00
|
|
|
context '.post_process_invite' do
|
2013-02-06 03:16:51 +08:00
|
|
|
before do
|
|
|
|
Invite.any_instance.stubs(:redeem).returns(user)
|
|
|
|
Jobs.expects(:enqueue).with(:invite_email, has_key(:invite_id))
|
2017-04-15 17:18:05 +08:00
|
|
|
user.password_hash = nil
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
it 'sends a welcome message if set' do
|
|
|
|
user.send_welcome_message = true
|
2013-02-06 03:16:51 +08:00
|
|
|
user.expects(:enqueue_welcome_message).with('welcome_invite')
|
2017-04-18 16:55:52 +08:00
|
|
|
Jobs.expects(:enqueue).with(:invite_password_instructions_email, has_entries(username: user.username))
|
2017-02-14 05:19:41 +08:00
|
|
|
xhr :put, :perform_accept_invitation, id: invite.invite_key, format: :json
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2017-04-18 16:55:52 +08:00
|
|
|
it "sends password reset email if password is not set" do
|
2013-02-06 03:16:51 +08:00
|
|
|
user.expects(:enqueue_welcome_message).with('welcome_invite').never
|
2017-04-18 16:55:52 +08:00
|
|
|
Jobs.expects(:enqueue).with(:invite_password_instructions_email, has_entries(username: user.username))
|
2017-02-14 05:19:41 +08:00
|
|
|
xhr :put, :perform_accept_invitation, id: invite.invite_key, format: :json
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2017-04-15 17:18:05 +08:00
|
|
|
|
2017-04-18 19:04:21 +08:00
|
|
|
it "does not send password reset email if sso is enabled" do
|
|
|
|
SiteSetting.enable_sso = true
|
|
|
|
Jobs.expects(:enqueue).with(:invite_password_instructions_email, has_key(:username)).never
|
|
|
|
xhr :put, :perform_accept_invitation, id: invite.invite_key, format: :json
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not send password reset email if local login is disabled" do
|
|
|
|
SiteSetting.enable_local_logins = false
|
|
|
|
Jobs.expects(:enqueue).with(:invite_password_instructions_email, has_key(:username)).never
|
|
|
|
xhr :put, :perform_accept_invitation, id: invite.invite_key, format: :json
|
|
|
|
end
|
|
|
|
|
2017-04-15 17:18:05 +08:00
|
|
|
it 'sends an activation email if password is set' do
|
|
|
|
user.password_hash = 'qaw3ni3h2wyr63lakw7pea1nrtr44pls'
|
2017-04-18 19:04:21 +08:00
|
|
|
Jobs.expects(:enqueue).with(:invite_password_instructions_email, has_key(:username)).never
|
2017-04-15 17:18:05 +08:00
|
|
|
Jobs.expects(:enqueue).with(:critical_user_email, has_entries(type: :signup, user_id: user.id))
|
|
|
|
xhr :put, :perform_accept_invitation, id: invite.invite_key, format: :json
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-07-15 03:42:14 +08:00
|
|
|
context 'new registrations are disabled' do
|
|
|
|
let(:topic) { Fabricate(:topic) }
|
|
|
|
let(:invite) { topic.invite_by_email(topic.user, "iceking@adventuretime.ooo") }
|
2017-07-07 14:09:14 +08:00
|
|
|
before { SiteSetting.allow_new_registrations = false }
|
2014-07-15 03:42:14 +08:00
|
|
|
|
|
|
|
it "doesn't redeem the invite" do
|
|
|
|
Invite.any_instance.stubs(:redeem).never
|
2017-01-25 04:15:29 +08:00
|
|
|
put :perform_accept_invitation, id: invite.invite_key
|
2014-07-15 03:42:14 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-23 21:33:12 +08:00
|
|
|
context 'user is already logged in' do
|
|
|
|
let!(:user) { log_in }
|
|
|
|
let(:topic) { Fabricate(:topic) }
|
|
|
|
let(:invite) { topic.invite_by_email(topic.user, "iceking@adventuretime.ooo") }
|
|
|
|
|
|
|
|
it "doesn't redeem the invite" do
|
|
|
|
Invite.any_instance.stubs(:redeem).never
|
2017-01-25 04:15:29 +08:00
|
|
|
put :perform_accept_invitation, id: invite.invite_key
|
2016-02-23 21:33:12 +08:00
|
|
|
end
|
|
|
|
end
|
2014-05-28 04:14:37 +08:00
|
|
|
end
|
|
|
|
|
2014-10-07 02:48:56 +08:00
|
|
|
context '.resend_invite' do
|
|
|
|
|
|
|
|
it 'requires you to be logged in' do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect {
|
2014-10-07 02:48:56 +08:00
|
|
|
delete :resend_invite, email: 'first_name@example.com'
|
2015-01-10 01:04:02 +08:00
|
|
|
}.to raise_error(Discourse::NotLoggedIn)
|
2014-10-07 02:48:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'while logged in' do
|
|
|
|
let!(:user) { log_in }
|
|
|
|
let!(:invite) { Fabricate(:invite, invited_by: user) }
|
|
|
|
let(:another_invite) { Fabricate(:invite, email: 'last_name@example.com') }
|
|
|
|
|
|
|
|
it 'raises an error when the email is missing' do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect { post :resend_invite }.to raise_error(ActionController::ParameterMissing)
|
2014-10-07 02:48:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error when the email cannot be found" do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect { post :resend_invite, email: 'first_name@example.com' }.to raise_error(Discourse::InvalidParameters)
|
2014-10-07 02:48:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error when the invite is not yours' do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect { post :resend_invite, email: another_invite.email }.to raise_error(Discourse::InvalidParameters)
|
2014-10-07 02:48:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "resends the invite" do
|
|
|
|
Invite.any_instance.expects(:resend_invite)
|
|
|
|
post :resend_invite, email: invite.email
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2016-12-05 00:06:35 +08:00
|
|
|
context '.upload_csv' do
|
2014-05-28 04:14:37 +08:00
|
|
|
it 'requires you to be logged in' do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect {
|
2016-12-05 00:06:35 +08:00
|
|
|
xhr :post, :upload_csv
|
2015-01-10 01:04:02 +08:00
|
|
|
}.to raise_error(Discourse::NotLoggedIn)
|
2014-05-28 04:14:37 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'while logged in' do
|
|
|
|
let(:csv_file) { File.new("#{Rails.root}/spec/fixtures/csv/discourse.csv") }
|
|
|
|
let(:file) do
|
2017-07-28 09:20:09 +08:00
|
|
|
ActionDispatch::Http::UploadedFile.new(filename: 'discourse.csv', tempfile: csv_file)
|
2014-05-28 04:14:37 +08:00
|
|
|
end
|
2016-12-05 00:06:35 +08:00
|
|
|
let(:filename) { 'discourse.csv' }
|
2014-05-28 04:14:37 +08:00
|
|
|
|
|
|
|
it "fails if you can't bulk invite to the forum" do
|
|
|
|
log_in
|
2016-12-05 00:06:35 +08:00
|
|
|
xhr :post, :upload_csv, file: file, name: filename
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).not_to be_success
|
2014-05-28 04:14:37 +08:00
|
|
|
end
|
|
|
|
|
2016-12-05 00:06:35 +08:00
|
|
|
it "allows admin to bulk invite" do
|
2014-05-28 04:14:37 +08:00
|
|
|
log_in(:admin)
|
2016-12-05 00:06:35 +08:00
|
|
|
xhr :post, :upload_csv, file: file, name: filename
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_success
|
2014-05-28 04:14:37 +08:00
|
|
|
end
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|