don't reduce number of available invites if there were errors.
This commit is contained in:
parent
2a553940d4
commit
e749bbef15
3 changed files with 46 additions and 36 deletions
|
|
@ -5,14 +5,14 @@
|
||||||
class RegistrationsController < Devise::RegistrationsController
|
class RegistrationsController < Devise::RegistrationsController
|
||||||
before_action :check_registrations_open_or_valid_invite!, :check_valid_invite!
|
before_action :check_registrations_open_or_valid_invite!, :check_valid_invite!
|
||||||
|
|
||||||
layout ->(c) { request.format == :mobile ? "application" : "with_header" }, :only => [:new]
|
layout -> { request.format == :mobile ? "application" : "with_header" }
|
||||||
|
|
||||||
def create
|
def create
|
||||||
@user = User.build(user_params)
|
@user = User.build(user_params)
|
||||||
@user.process_invite_acceptence(invite) if invite.present?
|
|
||||||
|
|
||||||
if @user.sign_up
|
if @user.sign_up
|
||||||
flash[:notice] = I18n.t 'registrations.create.success'
|
flash[:notice] = t("registrations.create.success")
|
||||||
|
@user.process_invite_acceptence(invite) if invite.present?
|
||||||
@user.seed_aspects
|
@user.seed_aspects
|
||||||
@user.send_welcome_message
|
@user.send_welcome_message
|
||||||
sign_in_and_redirect(:user, @user)
|
sign_in_and_redirect(:user, @user)
|
||||||
|
|
@ -22,14 +22,10 @@ class RegistrationsController < Devise::RegistrationsController
|
||||||
|
|
||||||
flash.now[:error] = @user.errors.full_messages.join(" - ")
|
flash.now[:error] = @user.errors.full_messages.join(" - ")
|
||||||
logger.info "event=registration status=failure errors='#{@user.errors.full_messages.join(', ')}'"
|
logger.info "event=registration status=failure errors='#{@user.errors.full_messages.join(', ')}'"
|
||||||
render action: "new", layout: request.format == :mobile ? "application" : "with_header"
|
render action: "new"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def new
|
|
||||||
super
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def check_valid_invite!
|
def check_valid_invite!
|
||||||
|
|
@ -48,9 +44,7 @@ class RegistrationsController < Devise::RegistrationsController
|
||||||
end
|
end
|
||||||
|
|
||||||
def invite
|
def invite
|
||||||
if params[:invite].present?
|
@invite ||= InvitationCode.find_by_token(params[:invite][:token]) if params[:invite].present?
|
||||||
@invite ||= InvitationCode.find_by_token(params[:invite][:token])
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
helper_method :invite
|
helper_method :invite
|
||||||
|
|
|
||||||
|
|
@ -2,20 +2,23 @@
|
||||||
# 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 RegistrationsController, type: :controller do
|
describe RegistrationsController, type: :controller do
|
||||||
before do
|
before do
|
||||||
request.env["devise.mapping"] = Devise.mappings[:user]
|
request.env["devise.mapping"] = Devise.mappings[:user]
|
||||||
@valid_params = {:user => {
|
end
|
||||||
:username => "jdoe",
|
|
||||||
:email => "jdoe@example.com",
|
let(:valid_params) {
|
||||||
:password => "password",
|
{
|
||||||
:password_confirmation => "password"
|
user: {
|
||||||
|
username: "jdoe",
|
||||||
|
email: "jdoe@example.com",
|
||||||
|
password: "password",
|
||||||
|
password_confirmation: "password"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
allow(Person).to receive(:find_or_fetch_by_identifier).and_return(FactoryGirl.create(:person))
|
}
|
||||||
end
|
|
||||||
|
|
||||||
describe '#check_registrations_open!' do
|
describe '#check_registrations_open!' do
|
||||||
before do
|
before do
|
||||||
|
|
@ -29,7 +32,7 @@ describe RegistrationsController, type: :controller do
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'redirects #create to the login page' do
|
it 'redirects #create to the login page' do
|
||||||
post :create, @valid_params
|
post :create, valid_params
|
||||||
expect(flash[:error]).to eq(I18n.t('registrations.closed'))
|
expect(flash[:error]).to eq(I18n.t('registrations.closed'))
|
||||||
expect(response).to redirect_to new_user_session_path
|
expect(response).to redirect_to new_user_session_path
|
||||||
end
|
end
|
||||||
|
|
@ -58,58 +61,71 @@ describe RegistrationsController, type: :controller do
|
||||||
|
|
||||||
it "creates a user" do
|
it "creates a user" do
|
||||||
expect {
|
expect {
|
||||||
get :create, @valid_params
|
get :create, valid_params
|
||||||
}.to change(User, :count).by(1)
|
}.to change(User, :count).by(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "assigns @user" do
|
it "assigns @user" do
|
||||||
get :create, @valid_params
|
get :create, valid_params
|
||||||
expect(assigns(:user)).to be_truthy
|
expect(assigns(:user)).to be_truthy
|
||||||
end
|
end
|
||||||
|
|
||||||
it "sets the flash" do
|
it "sets the flash" do
|
||||||
get :create, @valid_params
|
get :create, valid_params
|
||||||
expect(flash[:notice]).not_to be_blank
|
expect(flash[:notice]).not_to be_blank
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "uses the invite code" do
|
||||||
|
code = InvitationCode.create(user: bob)
|
||||||
|
|
||||||
|
expect {
|
||||||
|
get :create, valid_params.merge(invite: {token: code.token})
|
||||||
|
}.to change { code.reload.count }.by(-1)
|
||||||
|
end
|
||||||
|
|
||||||
it "redirects to the home path" do
|
it "redirects to the home path" do
|
||||||
get :create, @valid_params
|
get :create, valid_params
|
||||||
expect(response).to be_redirect
|
expect(response).to be_redirect
|
||||||
expect(response.location).to match /^#{stream_url}\??$/
|
expect(response.location).to match /^#{stream_url}\??$/
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "with invalid parameters" do
|
context "with invalid parameters" do
|
||||||
before do
|
let(:invalid_params) { valid_params.deep_merge(user: {password_confirmation: "baddword"}) }
|
||||||
@invalid_params = @valid_params
|
|
||||||
@invalid_params[:user][:password_confirmation] = "baddword"
|
|
||||||
end
|
|
||||||
|
|
||||||
it "does not create a user" do
|
it "does not create a user" do
|
||||||
expect { get :create, @invalid_params }.not_to change(User, :count)
|
expect { get :create, invalid_params }.not_to change(User, :count)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "does not create a person" do
|
it "does not create a person" do
|
||||||
expect { get :create, @invalid_params }.not_to change(Person, :count)
|
expect { get :create, invalid_params }.not_to change(Person, :count)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "assigns @user" do
|
it "assigns @user" do
|
||||||
get :create, @invalid_params
|
get :create, invalid_params
|
||||||
expect(assigns(:user)).not_to be_nil
|
expect(assigns(:user)).not_to be_nil
|
||||||
end
|
end
|
||||||
|
|
||||||
it "sets the flash error" do
|
it "sets the flash error" do
|
||||||
get :create, @invalid_params
|
get :create, invalid_params
|
||||||
expect(flash[:error]).not_to be_blank
|
expect(flash[:error]).not_to be_blank
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "doesn't reduce number of available invites" do
|
||||||
|
code = InvitationCode.create(user: bob)
|
||||||
|
|
||||||
|
expect {
|
||||||
|
get :create, invalid_params.merge(invite: {token: code.token})
|
||||||
|
}.not_to change { code.reload.count }
|
||||||
|
end
|
||||||
|
|
||||||
it "renders new" do
|
it "renders new" do
|
||||||
get :create, @invalid_params
|
get :create, invalid_params
|
||||||
expect(response).to render_template("registrations/new")
|
expect(response).to render_template("registrations/new")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "keeps invalid params in form" do
|
it "keeps invalid params in form" do
|
||||||
get :create, @invalid_params
|
get :create, invalid_params
|
||||||
expect(response.body).to match /jdoe@example.com/m
|
expect(response.body).to match /jdoe@example.com/m
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue