don't reduce number of invites when registration is open
otherwise the counter goes into negative ;) also reset all negative counters
This commit is contained in:
parent
e749bbef15
commit
3b1a5c6bdf
7 changed files with 81 additions and 45 deletions
|
|
@ -3,7 +3,7 @@
|
||||||
# the COPYRIGHT file.
|
# the COPYRIGHT file.
|
||||||
|
|
||||||
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!
|
||||||
|
|
||||||
layout -> { request.format == :mobile ? "application" : "with_header" }
|
layout -> { request.format == :mobile ? "application" : "with_header" }
|
||||||
|
|
||||||
|
|
@ -28,20 +28,12 @@ class RegistrationsController < Devise::RegistrationsController
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def check_valid_invite!
|
|
||||||
return true if AppConfig.settings.enable_registrations? #this sucks
|
|
||||||
return true if invite && invite.can_be_used?
|
|
||||||
flash[:error] = t('registrations.invalid_invite')
|
|
||||||
redirect_to new_user_session_path
|
|
||||||
end
|
|
||||||
|
|
||||||
def check_registrations_open_or_valid_invite!
|
def check_registrations_open_or_valid_invite!
|
||||||
return true if invite.present?
|
return true if AppConfig.settings.enable_registrations? || invite.try(:can_be_used?)
|
||||||
unless AppConfig.settings.enable_registrations?
|
|
||||||
flash[:error] = t('registrations.closed')
|
flash[:error] = params[:invite] ? t("registrations.invalid_invite") : t("registrations.closed")
|
||||||
redirect_to new_user_session_path
|
redirect_to new_user_session_path
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def invite
|
def invite
|
||||||
@invite ||= InvitationCode.find_by_token(params[:invite][:token]) if params[:invite].present?
|
@invite ||= InvitationCode.find_by_token(params[:invite][:token]) if params[:invite].present?
|
||||||
|
|
|
||||||
|
|
@ -99,10 +99,9 @@ class User < ActiveRecord::Base
|
||||||
|
|
||||||
def process_invite_acceptence(invite)
|
def process_invite_acceptence(invite)
|
||||||
self.invited_by = invite.user
|
self.invited_by = invite.user
|
||||||
invite.use!
|
invite.use! unless AppConfig.settings.enable_registrations?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def invitation_code
|
def invitation_code
|
||||||
InvitationCode.find_or_create_by(user_id: self.id)
|
InvitationCode.find_or_create_by(user_id: self.id)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
#paste_link
|
#paste_link
|
||||||
= t('.paste_link')
|
= t('.paste_link')
|
||||||
%span#codes_left
|
%span#codes_left
|
||||||
= '(' + t('.codes_left', count: @invite_code.count) + ')'
|
= "(" + t(".codes_left", count: @invite_code.count) + ")" unless AppConfig.settings.enable_registrations?
|
||||||
.form-horizontal
|
.form-horizontal
|
||||||
.control-group
|
.control-group
|
||||||
= invite_link(@invite_code)
|
= invite_link(@invite_code)
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,7 @@
|
||||||
class CleanupInvitationColumnsFromUsers < ActiveRecord::Migration
|
class CleanupInvitationColumnsFromUsers < ActiveRecord::Migration
|
||||||
|
class InvitationCode < ActiveRecord::Base
|
||||||
|
end
|
||||||
|
|
||||||
class User < ActiveRecord::Base
|
class User < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -20,7 +23,7 @@ class CleanupInvitationColumnsFromUsers < ActiveRecord::Migration
|
||||||
|
|
||||||
add_index :users, :email, name: :index_users_on_email, unique: true, length: 191
|
add_index :users, :email, name: :index_users_on_email, unique: true, length: 191
|
||||||
|
|
||||||
drop_invitations_table
|
cleanup_invitations
|
||||||
end
|
end
|
||||||
|
|
||||||
def username_not_null
|
def username_not_null
|
||||||
|
|
@ -36,10 +39,14 @@ class CleanupInvitationColumnsFromUsers < ActiveRecord::Migration
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def drop_invitations_table
|
def cleanup_invitations
|
||||||
reversible do |dir|
|
reversible do |dir|
|
||||||
dir.up do
|
dir.up do
|
||||||
drop_table :invitations
|
drop_table :invitations
|
||||||
|
|
||||||
|
# reset negative invitation counters
|
||||||
|
new_counter = AppConfig.settings.enable_registrations? ? AppConfig["settings.invitations.count"] : 0
|
||||||
|
InvitationCode.where("count < 0").update_all(count: new_counter)
|
||||||
end
|
end
|
||||||
|
|
||||||
dir.down do
|
dir.down do
|
||||||
|
|
|
||||||
|
|
@ -47,6 +47,7 @@ Given /^I have been invited by an admin$/ do
|
||||||
end
|
end
|
||||||
|
|
||||||
Given /^I have been invited by "([^\"]+)"$/ do |email|
|
Given /^I have been invited by "([^\"]+)"$/ do |email|
|
||||||
|
AppConfig.settings.enable_registrations = false
|
||||||
@inviter = User.find_by_email(email)
|
@inviter = User.find_by_email(email)
|
||||||
@inviter_invite_count = @inviter.invitation_code.count
|
@inviter_invite_count = @inviter.invitation_code.count
|
||||||
i = EmailInviter.new("new_invitee@example.com", @inviter)
|
i = EmailInviter.new("new_invitee@example.com", @inviter)
|
||||||
|
|
|
||||||
|
|
@ -70,6 +70,9 @@ Before do |scenario|
|
||||||
page.driver.headers = if scenario.source_tag_names.include? "@mobile"
|
page.driver.headers = if scenario.source_tag_names.include? "@mobile"
|
||||||
{"User-Agent" => "Mozilla/5.0 (Mobile; rv:18.0) Gecko/18.0 Firefox/18.0"}
|
{"User-Agent" => "Mozilla/5.0 (Mobile; rv:18.0) Gecko/18.0 Firefox/18.0"}
|
||||||
else
|
else
|
||||||
page.driver.headers = {}
|
{}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Reset overridden settings
|
||||||
|
AppConfig.reset_dynamic!
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -20,32 +20,50 @@ describe RegistrationsController, type: :controller do
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
describe '#check_registrations_open!' do
|
describe "#check_registrations_open_or_valid_invite!" do
|
||||||
before do
|
before do
|
||||||
AppConfig.settings.enable_registrations = false
|
AppConfig.settings.enable_registrations = false
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'redirects #new to the login page' do
|
it "redirects #new to the login page" do
|
||||||
get :new
|
get :new
|
||||||
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
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
it 'does not redirect if there is a valid invite token' do
|
it "does not redirect if there is a valid invite token" do
|
||||||
i = InvitationCode.create(:user => bob)
|
code = InvitationCode.create(user: bob)
|
||||||
get :new, :invite => {:token => i.token}
|
get :new, invite: {token: code.token}
|
||||||
expect(response).not_to be_redirect
|
expect(response).not_to be_redirect
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'does redirect if there is an invalid invite token' do
|
it "does redirect if there is an invalid invite token" do
|
||||||
get :new, :invite => {:token => 'fssdfsd'}
|
get :new, invite: {token: "fssdfsd"}
|
||||||
expect(response).to be_redirect
|
expect(response).to redirect_to new_user_session_path
|
||||||
|
end
|
||||||
|
|
||||||
|
it "does redirect if there are no invites available with this code" do
|
||||||
|
code = InvitationCode.create(user: bob)
|
||||||
|
code.update_attributes(count: 0)
|
||||||
|
|
||||||
|
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
|
||||||
|
AppConfig.settings.enable_registrations = true
|
||||||
|
|
||||||
|
code = InvitationCode.create(user: bob)
|
||||||
|
code.update_attributes(count: 0)
|
||||||
|
|
||||||
|
get :new, invite: {token: code.token}
|
||||||
|
expect(response).not_to be_redirect
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -53,12 +71,6 @@ describe RegistrationsController, type: :controller do
|
||||||
render_views
|
render_views
|
||||||
|
|
||||||
context "with valid parameters" do
|
context "with valid parameters" do
|
||||||
before do
|
|
||||||
AppConfig.settings.enable_registrations = true
|
|
||||||
user = FactoryGirl.build(:user)
|
|
||||||
allow(User).to receive(:build).and_return(user)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "creates a user" do
|
it "creates a user" do
|
||||||
expect {
|
expect {
|
||||||
get :create, valid_params
|
get :create, valid_params
|
||||||
|
|
@ -75,7 +87,16 @@ describe RegistrationsController, type: :controller do
|
||||||
expect(flash[:notice]).not_to be_blank
|
expect(flash[:notice]).not_to be_blank
|
||||||
end
|
end
|
||||||
|
|
||||||
it "uses the invite code" do
|
it "redirects to the home path" do
|
||||||
|
get :create, valid_params
|
||||||
|
expect(response).to be_redirect
|
||||||
|
expect(response.location).to match(/^#{getting_started_url}$/)
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with invite code" do
|
||||||
|
it "reduces number of available invites when the registration is closed" do
|
||||||
|
AppConfig.settings.enable_registrations = false
|
||||||
|
|
||||||
code = InvitationCode.create(user: bob)
|
code = InvitationCode.create(user: bob)
|
||||||
|
|
||||||
expect {
|
expect {
|
||||||
|
|
@ -83,10 +104,21 @@ describe RegistrationsController, type: :controller do
|
||||||
}.to change { code.reload.count }.by(-1)
|
}.to change { code.reload.count }.by(-1)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "redirects to the home path" do
|
it "doesn't reduce number of available invites when the registration is open" do
|
||||||
get :create, valid_params
|
code = InvitationCode.create(user: bob)
|
||||||
expect(response).to be_redirect
|
|
||||||
expect(response.location).to match /^#{stream_url}\??$/
|
expect {
|
||||||
|
get :create, valid_params.merge(invite: {token: code.token})
|
||||||
|
}.not_to change { code.reload.count }
|
||||||
|
end
|
||||||
|
|
||||||
|
it "links inviter with the user" do
|
||||||
|
code = InvitationCode.create(user: bob)
|
||||||
|
|
||||||
|
post :create, valid_params.merge(invite: {token: code.token})
|
||||||
|
|
||||||
|
expect(User.find_by(username: "jdoe").invited_by).to eq(bob)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -112,6 +144,8 @@ describe RegistrationsController, type: :controller do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "doesn't reduce number of available invites" do
|
it "doesn't reduce number of available invites" do
|
||||||
|
AppConfig.settings.enable_registrations = false
|
||||||
|
|
||||||
code = InvitationCode.create(user: bob)
|
code = InvitationCode.create(user: bob)
|
||||||
|
|
||||||
expect {
|
expect {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue