FEATURE: Add DiscourseEvent trigger when a user logs in.

* Also adds a event trigger when user logs in for the first time.
This commit is contained in:
Guo Xiang Tan 2017-06-01 17:19:42 +09:00
parent bd486100c0
commit 2ee144c27f
8 changed files with 86 additions and 23 deletions

View File

@ -45,4 +45,3 @@ class UsersEmailController < ApplicationController
end
end

View File

@ -912,6 +912,14 @@ class User < ActiveRecord::Base
DiscourseEvent.trigger(:user_logged_out, self)
end
def logged_in
DiscourseEvent.trigger(:user_logged_in, self)
if !self.seen_before?
DiscourseEvent.trigger(:user_first_logged_in, self)
end
end
protected
def badge_grant

View File

@ -16,6 +16,7 @@ module CurrentUser
def log_on_user(user)
current_user_provider.log_on_user(user,session,cookies)
user.logged_in
end
def log_off_user

View File

@ -197,7 +197,12 @@ describe InvitesController do
end
it 'logs in the user' do
subject
events = DiscourseEvent.track_events { subject }
expect(events.map { |event| event[:event_name] }).to include(
:user_logged_in, :user_first_logged_in
)
expect(session[:current_user_id]).to eq(user.id)
end
@ -378,10 +383,20 @@ describe InvitesController do
before do
Invite.expects(:redeem_from_token).with(invite.invite_key, user.email, nil, nil, topic.id).returns(user)
get :redeem_disposable_invite, email: user.email, token: invite.invite_key, topic: topic.id
end
it 'logs in user' do
events = DiscourseEvent.track_events do
get :redeem_disposable_invite,
email: user.email,
token: invite.invite_key,
topic: topic.id
end
expect(events.map { |event| event[:event_name] }).to include(
:user_logged_in, :user_first_logged_in
)
expect(session[:current_user_id]).to eq(user.id)
end

View File

@ -185,7 +185,14 @@ describe SessionController do
sso.custom_fields["shop_url"] = "http://my_shop.com"
sso.custom_fields["shop_name"] = "Sam"
get :sso_login, Rack::Utils.parse_query(sso.payload)
events = DiscourseEvent.track_events do
get :sso_login, Rack::Utils.parse_query(sso.payload)
end
expect(events.map { |event| event[:event_name] }).to include(
:user_logged_in, :user_first_logged_in
)
expect(response).to redirect_to('/a/')
logged_on_user = Discourse.current_user_provider.new(request.env).current_user
@ -500,7 +507,13 @@ describe SessionController do
describe 'success by username' do
it 'logs in correctly' do
xhr :post, :create, login: user.username, password: 'myawesomepassword'
events = DiscourseEvent.track_events do
xhr :post, :create, login: user.username, password: 'myawesomepassword'
end
expect(events.map { |event| event[:event_name] }).to include(
:user_logged_in, :user_first_logged_in
)
user.reload

View File

@ -152,10 +152,17 @@ describe UsersController do
before do
Guardian.any_instance.expects(:can_access_forum?).returns(true)
EmailToken.expects(:confirm).with('asdfasdf').returns(user)
put :perform_account_activation, token: 'asdfasdf'
end
it 'correctly logs on user' do
events = DiscourseEvent.track_events do
put :perform_account_activation, token: 'asdfasdf'
end
expect(events.map { |event| event[:event_name] }).to include(
:user_logged_in, :user_first_logged_in
)
expect(response).to be_success
expect(flash[:error]).to be_blank
expect(session[:current_user_id]).to be_present
@ -266,9 +273,15 @@ describe UsersController do
user = Fabricate(:user)
user_auth_token = UserAuthToken.generate!(user_id: user.id)
token = user.email_tokens.create(email: user.email).token
get :password_reset, token: token
put :password_reset, token: token, password: 'hg9ow8yhg98o'
events = DiscourseEvent.track_events do
put :password_reset, token: token, password: 'hg9ow8yhg98o'
end
expect(events.map { |event| event[:event_name] }).to include(
:user_logged_in, :user_first_logged_in
)
expect(response).to be_success
expect(assigns[:error]).to be_blank
@ -386,22 +399,24 @@ describe UsersController do
expect(session[:current_user_id]).to be_blank
end
it 'does log in admin with valid token and SSO disabled' do
SiteSetting.enable_sso = false
token = admin.email_tokens.create(email: admin.email).token
context 'valid token' do
it 'does log in admin with SSO disabled' do
SiteSetting.enable_sso = false
token = admin.email_tokens.create(email: admin.email).token
get :admin_login, token: token
expect(response).to redirect_to('/')
expect(session[:current_user_id]).to eq(admin.id)
end
get :admin_login, token: token
expect(response).to redirect_to('/')
expect(session[:current_user_id]).to eq(admin.id)
end
it 'logs in admin with valid token and SSO enabled' do
SiteSetting.enable_sso = true
token = admin.email_tokens.create(email: admin.email).token
it 'logs in admin with SSO enabled' do
SiteSetting.enable_sso = true
token = admin.email_tokens.create(email: admin.email).token
get :admin_login, token: token
expect(response).to redirect_to('/')
expect(session[:current_user_id]).to eq(admin.id)
get :admin_login, token: token
expect(response).to redirect_to('/')
expect(session[:current_user_id]).to eq(admin.id)
end
end
end
end

View File

@ -35,7 +35,13 @@ describe UsersEmailController do
it 'confirms with a correct token' do
user.user_stat.update_columns(bounce_score: 42, reset_bounce_score_after: 1.week.from_now)
get :confirm, token: user.email_tokens.last.token
events = DiscourseEvent.track_events do
get :confirm, token: user.email_tokens.last.token
end
expect(events.map { |event| event[:event_name] }).to include(
:user_logged_in, :user_first_logged_in
)
expect(response).to be_success
expect(assigns(:update_result)).to eq(:complete)

View File

@ -49,7 +49,13 @@ RSpec.describe "OmniAuth Callbacks" do
end
it 'should return the right response' do
get "/auth/google_oauth2/callback.json"
events = DiscourseEvent.track_events do
get "/auth/google_oauth2/callback.json"
end
expect(events.map { |event| event[:event_name] }).to include(
:user_logged_in, :user_first_logged_in
)
expect(response).to be_success