added hooks for user registration and first login

pull/7734/head
Markus Kahl 5 years ago
parent 1719f668b1
commit aa15b6c589
  1. 2
      app/controllers/account_controller.rb
  2. 3
      app/controllers/concerns/redirect_after_login.rb
  3. 104
      spec/controllers/account_controller_spec.rb

@ -134,6 +134,8 @@ class AccountController < ApplicationController
end
self_registration!
call_hook :user_registered, { user: @user } if @user.persisted?
end
end

@ -33,6 +33,9 @@ module Concerns::RedirectAfterLogin
def redirect_after_login(user)
if user.first_login
user.update_attribute(:first_login, false)
call_hook :user_first_login, { user: user }
first_login_redirect
else
default_redirect

@ -29,6 +29,32 @@
require 'spec_helper'
describe AccountController, type: :controller do
# class AccountHook < Redmine::Hook::ViewListener; end
class UserHook < Redmine::Hook::ViewListener
attr_reader :registered_user
attr_reader :first_login_user
def user_registered(context)
@registered_user = context[:user]
end
def user_first_login(context)
@first_login_user = context[:user]
end
def reset!
@registered_user = nil
@first_login_user = nil
end
end
let(:hook) { UserHook.instance }
before do
hook.reset!
end
after do
User.delete_all
User.current = nil
@ -91,6 +117,36 @@ describe AccountController, type: :controller do
end
end
context 'with first login' do
before do
admin.update first_login: true
post :login, params: { username: admin.login, password: 'adminADMIN!' }
end
it 'redirect to default path with ?first_time_user=true' do
expect(response).to redirect_to "/?first_time_user=true"
end
it 'calls the user_first_login hook' do
expect(hook.first_login_user).to eq admin
end
end
context 'without first login' do
before do
post :login, params: { username: admin.login, password: 'adminADMIN!' }
end
it 'redirect to the my page' do
expect(response).to redirect_to "/my/page"
end
it 'does not call the user_first_login hook' do
expect(hook.first_login_user).to be_nil
end
end
describe 'User logging in with back_url' do
it 'should redirect to a relative path' do
post :login,
@ -413,6 +469,10 @@ describe AccountController, type: :controller do
it 'informs the user that registration is disabled' do
expect(flash[:error]).to eq(I18n.t('account.error_self_registration_disabled'))
end
it 'does not call the user_registered callback' do
expect(hook.registered_user).to be_nil
end
end
context 'GET #register' do
@ -498,7 +558,7 @@ describe AccountController, type: :controller do
}
end
it 'redirects to my page' do
it 'redirects to the expected path' do
is_expected.to respond_with :redirect
expect(assigns[:user]).not_to be_nil
is_expected.to redirect_to(redirect_to_path)
@ -510,6 +570,23 @@ describe AccountController, type: :controller do
expect(user).not_to be_nil
expect(user.status).to eq(User::STATUSES[:active])
end
it 'calls the user_registered callback' do
user = hook.registered_user
expect(user.mail).to eq "register@example.com"
expect(user).to be_active
end
end
it_behaves_like 'automatic self registration succeeds' do
let(:redirect_to_path) { "/?first_time_user=true" }
it "calls the user_first_login callback" do
user = hook.first_login_user
expect(user.mail).to eq "register@example.com"
end
end
context "with user limit reached" do
@ -547,6 +624,10 @@ describe AccountController, type: :controller do
expect(mail.to.first).to eq admin.mail
expect(mail.body.parts.first.to_s).to match /new user \(#{params[:user][:mail]}\)/
end
it 'does not call the user_registered callback' do
expect(hook.registered_user).to be_nil
end
end
end
@ -592,6 +673,13 @@ describe AccountController, type: :controller do
expect(token.user.mail).to eq('register@example.com')
expect(token).not_to be_expired
end
it 'calls the user_registered callback' do
user = hook.registered_user
expect(user.mail).to eq "register@example.com"
expect(user).to be_registered
end
end
context 'with password login disabled' do
@ -631,6 +719,13 @@ describe AccountController, type: :controller do
it "doesn't activate the user" do
expect(User.find_by_login('register')).not_to be_active
end
it 'calls the user_registered callback' do
user = hook.registered_user
expect(user.mail).to eq "register@example.com"
expect(user).to be_registered
end
end
context 'with back_url' do
@ -642,6 +737,13 @@ describe AccountController, type: :controller do
expect(response).to redirect_to(
'/login?back_url=https%3A%2F%2Fexample.net%2Fsome_back_url')
end
it 'calls the user_registered callback' do
user = hook.registered_user
expect(user.mail).to eq "register@example.com"
expect(user).to be_registered
end
end
context 'with password login disabled' do

Loading…
Cancel
Save