disable registration with invite-code when invitations are closed

also display message if the user has no invitations left and refactored
InvitationsController spec and remove unused message parameter
This commit is contained in:
Benjamin Neff 2016-08-12 23:30:16 +02:00
parent 3b1a5c6bdf
commit bc6c8a0598
5 changed files with 107 additions and 75 deletions

View file

@ -4,6 +4,7 @@
class InvitationsController < ApplicationController class InvitationsController < ApplicationController
before_action :authenticate_user! before_action :authenticate_user!
before_action :check_invitations_available!, only: :create
def new def new
@invite_code = current_user.invitation_code @invite_code = current_user.invitation_code
@ -46,6 +47,17 @@ class InvitationsController < ApplicationController
private private
def check_invitations_available!
return true if AppConfig.settings.enable_registrations? || current_user.invitation_code.can_be_used?
flash[:error] = if AppConfig.settings.invitations.open?
t("invitations.create.no_more")
else
t("invitations.create.closed")
end
redirect_to :back
end
def valid_email?(email) def valid_email?(email)
User.email_regexp.match(email).present? User.email_regexp.match(email).present?
end end

View file

@ -12,7 +12,7 @@ class InvitationCode < ActiveRecord::Base
end end
def can_be_used? def can_be_used?
self.count > 0 count > 0 && AppConfig.settings.invitations.open?
end end
def add_invites! def add_invites!

View file

@ -560,6 +560,7 @@ en:
no_more: "You have no more invitations." no_more: "You have no more invitations."
empty: "Please enter at least one email address." empty: "Please enter at least one email address."
note_already_sent: "Invitations have already been sent to: %{emails}" note_already_sent: "Invitations have already been sent to: %{emails}"
closed: "Invitations are closed on this diaspora* pod."
new: new:
language: "Language" language: "Language"
invite_someone_to_join: "Invite someone to join diaspora*!" invite_someone_to_join: "Invite someone to join diaspora*!"

View file

@ -2,131 +2,142 @@
# licensed under the Affero General Public License version 3 or later. See # licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file. # the COPYRIGHT file.
require 'spec_helper' require "spec_helper"
describe InvitationsController, :type => :controller do
before do
AppConfig.settings.invitations.open = true
@user = alice
@invite = {'email_inviter' => {'message' => "test", 'emails' => "abc@example.com"}}
end
describe InvitationsController, type: :controller do
describe "#create" do describe "#create" do
let(:referer) { "http://test.host/cats/foo" }
let(:invite_params) { {email_inviter: {emails: "abc@example.com"}} }
before do before do
sign_in @user, scope: :user sign_in alice, scope: :user
allow(@controller).to receive(:current_user).and_return(@user) request.env["HTTP_REFERER"] = referer
@referer = 'http://test.host/cats/foo'
request.env["HTTP_REFERER"] = @referer
end end
context "no emails" do context "no emails" do
before do let(:invite_params) { {email_inviter: {emails: ""}} }
@invite = {'email_inviter' => {'message' => "test", 'emails' => ""}}
end
it 'does not create an EmailInviter' do it "does not create an EmailInviter" do
expect(Workers::Mail::InviteEmail).not_to receive(:perform_async) expect(Workers::Mail::InviteEmail).not_to receive(:perform_async)
post :create, @invite post :create, invite_params
end end
it 'returns to the previous page' do it "returns to the previous page" do
post :create, @invite post :create, invite_params
expect(response).to redirect_to @referer expect(response).to redirect_to referer
end end
it 'flashes an error' do it "flashes an error" do
post :create, @invite post :create, invite_params
expect(flash[:error]).to eq(I18n.t("invitations.create.empty")) expect(flash[:error]).to eq(I18n.t("invitations.create.empty"))
end end
end end
context 'only valid emails' do context "only valid emails" do
before do let(:emails) { "mbs@gmail.com" }
@emails = 'mbs@gmail.com' let(:invite_params) { {email_inviter: {emails: emails}} }
@invite = {'email_inviter' => {'message' => "test", 'emails' => @emails}}
it "creates an InviteEmail worker" do
expect(Workers::Mail::InviteEmail).to receive(:perform_async).with(
emails, alice.id, invite_params[:email_inviter]
)
post :create, invite_params
end end
it 'creates an InviteEmail worker' do it "returns to the previous page on success" do
inviter = double(:emails => [@emails], :send! => true) post :create, invite_params
expect(Workers::Mail::InviteEmail).to receive(:perform_async).with(@invite['email_inviter']['emails'], @user.id, @invite['email_inviter']) expect(response).to redirect_to referer
post :create, @invite
end end
it 'returns to the previous page on success' do it "flashes a notice" do
post :create, @invite post :create, invite_params
expect(response).to redirect_to @referer expected = I18n.t("invitations.create.sent", emails: emails)
end
it 'flashes a notice' do
post :create, @invite
expected = I18n.t('invitations.create.sent', :emails => @emails.split(',').join(', '))
expect(flash[:notice]).to eq(expected) expect(flash[:notice]).to eq(expected)
end end
end end
context 'only invalid emails' do context "only invalid emails" do
before do let(:emails) { "invalid_email" }
@emails = 'invalid_email' let(:invite_params) { {email_inviter: {emails: emails}} }
@invite = {'email_inviter' => {'message' => "test", 'emails' => @emails}}
end
it 'does not create an InviteEmail worker' do it "does not create an InviteEmail worker" do
expect(Workers::Mail::InviteEmail).not_to receive(:perform_async) expect(Workers::Mail::InviteEmail).not_to receive(:perform_async)
post :create, @invite post :create, invite_params
end end
it 'returns to the previous page' do it "returns to the previous page" do
post :create, @invite post :create, invite_params
expect(response).to redirect_to @referer expect(response).to redirect_to referer
end end
it "flashes an error" do it "flashes an error" do
post :create, @invite post :create, invite_params
expected = I18n.t("invitations.create.rejected", emails: @emails.split(",").join(", ")) expected = I18n.t("invitations.create.rejected", emails: emails)
expect(flash[:error]).to eq(expected) expect(flash[:error]).to eq(expected)
end end
end end
context 'mixed valid and invalid emails' do context "mixed valid and invalid emails" do
before do let(:valid_emails) { "foo@bar.com,mbs@gmail.com" }
@valid_emails = 'foo@bar.com,mbs@gmail.com' let(:invalid_emails) { "invalid_email" }
@invalid_emails = 'invalid' let(:invite_params) { {email_inviter: {emails: valid_emails + "," + invalid_emails}} }
@invite = {'email_inviter' => {'message' => "test", 'emails' =>
@valid_emails + ',' + @invalid_emails}} it "creates an InviteEmail worker" do
expect(Workers::Mail::InviteEmail).to receive(:perform_async).with(
valid_emails, alice.id, invite_params[:email_inviter]
)
post :create, invite_params
end end
it 'creates an InviteEmail worker' do it "returns to the previous page" do
inviter = double(:emails => [@emails], :send! => true) post :create, invite_params
expect(Workers::Mail::InviteEmail).to receive(:perform_async).with(@valid_emails, @user.id, @invite['email_inviter']) expect(response).to redirect_to referer
post :create, @invite
end
it 'returns to the previous page' do
post :create, @invite
expect(response).to redirect_to @referer
end end
it "flashes a notice" do it "flashes a notice" do
post :create, @invite post :create, invite_params
expected = I18n.t("invitations.create.sent", emails: @valid_emails.split(",").join(", ")) + ". " + expected = I18n.t("invitations.create.sent", emails: valid_emails.split(",").join(", ")) + ". " +
I18n.t("invitations.create.rejected", emails: @invalid_emails.split(",").join(", ")) I18n.t("invitations.create.rejected", emails: invalid_emails)
expect(flash[:error]).to eq(expected) expect(flash[:error]).to eq(expected)
end end
end end
it 'redirects if invitations are closed' do context "with registration disabled" do
before do
AppConfig.settings.enable_registrations = false
end
it "displays an error if invitations are closed" do
AppConfig.settings.invitations.open = false AppConfig.settings.invitations.open = false
post :create, @invite post :create, invite_params
expect(response).to be_redirect
expect(flash[:error]).to eq(I18n.t("invitations.create.closed"))
end
it "displays an error when no invitations are left" do
alice.invitation_code.update_attributes(count: 0)
post :create, invite_params
expect(flash[:error]).to eq(I18n.t("invitations.create.no_more"))
end
end
it "does not display an error when registration is open" do
AppConfig.settings.invitations.open = false
alice.invitation_code.update_attributes(count: 0)
post :create, invite_params
expect(flash[:error]).to be_nil
end end
end end
describe '#new' do describe '#new' do
it 'renders' do it 'renders' do
sign_in @user, scope: :user sign_in alice, scope: :user
get :new get :new
end end
end end

View file

@ -56,6 +56,14 @@ describe RegistrationsController, type: :controller do
expect(response).to redirect_to new_user_session_path expect(response).to redirect_to new_user_session_path
end end
it "does redirect when invitations are closed now" do
code = InvitationCode.create(user: bob)
AppConfig.settings.invitations.open = false
get :new, invite: {token: code.token}
expect(response).to redirect_to new_user_session_path
end
it "does not redirect when the registration is open" do it "does not redirect when the registration is open" do
AppConfig.settings.enable_registrations = true AppConfig.settings.enable_registrations = true