Commit e749bbef authored by Benjamin Neff's avatar Benjamin Neff

don't reduce number of available invites if there were errors.

parent 2a553940
......@@ -5,14 +5,14 @@
class RegistrationsController < Devise::RegistrationsController
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
@user = User.build(user_params)
@user.process_invite_acceptence(invite) if invite.present?
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.send_welcome_message
sign_in_and_redirect(:user, @user)
......@@ -22,14 +22,10 @@ class RegistrationsController < Devise::RegistrationsController
flash.now[:error] = @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
def new
super
end
private
def check_valid_invite!
......@@ -48,9 +44,7 @@ class RegistrationsController < Devise::RegistrationsController
end
def invite
if params[:invite].present?
@invite ||= InvitationCode.find_by_token(params[:invite][:token])
end
@invite ||= InvitationCode.find_by_token(params[:invite][:token]) if params[:invite].present?
end
helper_method :invite
......
......@@ -6,9 +6,9 @@ class InvitationCode < ActiveRecord::Base
before_create :generate_token, :set_default_invite_count
delegate :name, to: :user, prefix: true
def to_param
token
token
end
def can_be_used?
......
......@@ -2,20 +2,23 @@
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
require 'spec_helper'
require "spec_helper"
describe RegistrationsController, type: :controller do
before do
request.env["devise.mapping"] = Devise.mappings[:user]
@valid_params = {:user => {
:username => "jdoe",
:email => "jdoe@example.com",
:password => "password",
:password_confirmation => "password"
end
let(:valid_params) {
{
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
before do
......@@ -29,7 +32,7 @@ describe RegistrationsController, type: :controller do
end
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(response).to redirect_to new_user_session_path
end
......@@ -58,58 +61,71 @@ describe RegistrationsController, type: :controller do
it "creates a user" do
expect {
get :create, @valid_params
get :create, valid_params
}.to change(User, :count).by(1)
end
it "assigns @user" do
get :create, @valid_params
get :create, valid_params
expect(assigns(:user)).to be_truthy
end
it "sets the flash" do
get :create, @valid_params
get :create, valid_params
expect(flash[:notice]).not_to be_blank
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
get :create, @valid_params
get :create, valid_params
expect(response).to be_redirect
expect(response.location).to match /^#{stream_url}\??$/
end
end
context "with invalid parameters" do
before do
@invalid_params = @valid_params
@invalid_params[:user][:password_confirmation] = "baddword"
end
let(:invalid_params) { valid_params.deep_merge(user: {password_confirmation: "baddword"}) }
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
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
it "assigns @user" do
get :create, @invalid_params
get :create, invalid_params
expect(assigns(:user)).not_to be_nil
end
it "sets the flash error" do
get :create, @invalid_params
get :create, invalid_params
expect(flash[:error]).not_to be_blank
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
get :create, @invalid_params
get :create, invalid_params
expect(response).to render_template("registrations/new")
end
it "keeps invalid params in form" do
get :create, @invalid_params
get :create, invalid_params
expect(response.body).to match /jdoe@example.com/m
end
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment