Commit 3b1a5c6b authored by Benjamin Neff's avatar Benjamin Neff

don't reduce number of invites when registration is open

otherwise the counter goes into negative ;)

also  reset all negative counters
parent e749bbef
......@@ -3,7 +3,7 @@
# the COPYRIGHT file.
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" }
......@@ -28,19 +28,11 @@ class RegistrationsController < Devise::RegistrationsController
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!
return true if invite.present?
unless AppConfig.settings.enable_registrations?
flash[:error] = t('registrations.closed')
redirect_to new_user_session_path
end
return true if AppConfig.settings.enable_registrations? || invite.try(:can_be_used?)
flash[:error] = params[:invite] ? t("registrations.invalid_invite") : t("registrations.closed")
redirect_to new_user_session_path
end
def invite
......
......@@ -99,10 +99,9 @@ class User < ActiveRecord::Base
def process_invite_acceptence(invite)
self.invited_by = invite.user
invite.use!
invite.use! unless AppConfig.settings.enable_registrations?
end
def invitation_code
InvitationCode.find_or_create_by(user_id: self.id)
end
......
#paste_link
= t('.paste_link')
%span#codes_left
= '(' + t('.codes_left', count: @invite_code.count) + ')'
= "(" + t(".codes_left", count: @invite_code.count) + ")" unless AppConfig.settings.enable_registrations?
.form-horizontal
.control-group
= invite_link(@invite_code)
......
class CleanupInvitationColumnsFromUsers < ActiveRecord::Migration
class InvitationCode < ActiveRecord::Base
end
class User < ActiveRecord::Base
end
......@@ -20,7 +23,7 @@ class CleanupInvitationColumnsFromUsers < ActiveRecord::Migration
add_index :users, :email, name: :index_users_on_email, unique: true, length: 191
drop_invitations_table
cleanup_invitations
end
def username_not_null
......@@ -36,10 +39,14 @@ class CleanupInvitationColumnsFromUsers < ActiveRecord::Migration
end
end
def drop_invitations_table
def cleanup_invitations
reversible do |dir|
dir.up do
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
dir.down do
......
......@@ -47,6 +47,7 @@ Given /^I have been invited by an admin$/ do
end
Given /^I have been invited by "([^\"]+)"$/ do |email|
AppConfig.settings.enable_registrations = false
@inviter = User.find_by_email(email)
@inviter_invite_count = @inviter.invitation_code.count
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"
{"User-Agent" => "Mozilla/5.0 (Mobile; rv:18.0) Gecko/18.0 Firefox/18.0"}
else
page.driver.headers = {}
{}
end
# Reset overridden settings
AppConfig.reset_dynamic!
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
AppConfig.settings.enable_registrations = false
end
it 'redirects #new to the login page' do
it "redirects #new to the login page" do
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
end
it 'redirects #create to the login page' do
it "redirects #create to the login page" do
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
end
it 'does not redirect if there is a valid invite token' do
i = InvitationCode.create(:user => bob)
get :new, :invite => {:token => i.token}
it "does not redirect if there is a valid invite token" do
code = InvitationCode.create(user: bob)
get :new, invite: {token: code.token}
expect(response).not_to be_redirect
end
it 'does redirect if there is an invalid invite token' do
get :new, :invite => {:token => 'fssdfsd'}
expect(response).to be_redirect
it "does redirect if there is an invalid invite token" do
get :new, invite: {token: "fssdfsd"}
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
......@@ -53,12 +71,6 @@ describe RegistrationsController, type: :controller do
render_views
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
expect {
get :create, valid_params
......@@ -75,18 +87,38 @@ describe RegistrationsController, type: :controller do
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
expect(response).to be_redirect
expect(response.location).to match /^#{stream_url}\??$/
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)
expect {
get :create, valid_params.merge(invite: {token: code.token})
}.to change { code.reload.count }.by(-1)
end
it "doesn't reduce number of available invites when the registration is open" do
code = InvitationCode.create(user: bob)
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
......@@ -112,6 +144,8 @@ describe RegistrationsController, type: :controller do
end
it "doesn't reduce number of available invites" do
AppConfig.settings.enable_registrations = false
code = InvitationCode.create(user: bob)
expect {
......
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