2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
describe SessionController do
|
2017-08-16 01:43:05 -04:00
|
|
|
shared_examples 'failed to continue local login' do
|
|
|
|
it 'should return the right response' do
|
|
|
|
expect(response).not_to be_success
|
|
|
|
expect(response.status.to_i).to eq 500
|
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2017-04-20 11:17:24 -04:00
|
|
|
describe '#become' do
|
2014-10-07 12:25:25 -04:00
|
|
|
let!(:user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
it "does not work when not in development mode" do
|
|
|
|
Rails.env.stubs(:development?).returns(false)
|
2017-08-31 00:06:56 -04:00
|
|
|
get :become, params: { session_id: user.username }, format: :json
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).not_to be_redirect
|
|
|
|
expect(session[:current_user_id]).to be_blank
|
2014-10-07 12:25:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "works in developmenet mode" do
|
|
|
|
Rails.env.stubs(:development?).returns(true)
|
2017-08-31 00:06:56 -04:00
|
|
|
get :become, params: { session_id: user.username }, format: :json
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_redirect
|
|
|
|
expect(session[:current_user_id]).to eq(user.id)
|
2014-10-07 12:25:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-20 11:17:24 -04:00
|
|
|
describe '#sso_login' do
|
2014-02-24 22:30:49 -05:00
|
|
|
|
|
|
|
before do
|
|
|
|
@sso_url = "http://somesite.com/discourse_sso"
|
|
|
|
@sso_secret = "shjkfdhsfkjh"
|
|
|
|
|
2015-01-22 12:20:17 -05:00
|
|
|
request.host = Discourse.current_hostname
|
|
|
|
|
2014-11-26 01:25:54 -05:00
|
|
|
SiteSetting.sso_url = @sso_url
|
2017-12-23 03:46:48 -05:00
|
|
|
SiteSetting.enable_sso = true
|
2014-11-26 01:25:54 -05:00
|
|
|
SiteSetting.sso_secret = @sso_secret
|
2014-02-25 18:52:11 -05:00
|
|
|
|
|
|
|
# We have 2 options, either fabricate an admin or don't
|
|
|
|
# send welcome messages
|
|
|
|
Fabricate(:admin)
|
|
|
|
# skip for now
|
2017-07-07 02:09:14 -04:00
|
|
|
# SiteSetting.send_welcome_message = false
|
2014-02-24 22:30:49 -05:00
|
|
|
end
|
|
|
|
|
2014-02-25 17:58:30 -05:00
|
|
|
def get_sso(return_path)
|
2014-02-24 22:30:49 -05:00
|
|
|
nonce = SecureRandom.hex
|
|
|
|
dso = DiscourseSingleSignOn.new
|
|
|
|
dso.nonce = nonce
|
2014-02-25 17:58:30 -05:00
|
|
|
dso.register_nonce(return_path)
|
2014-02-24 22:30:49 -05:00
|
|
|
|
|
|
|
sso = SingleSignOn.new
|
|
|
|
sso.nonce = nonce
|
|
|
|
sso.sso_secret = @sso_secret
|
|
|
|
sso
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can take over an account' do
|
2014-02-25 17:58:30 -05:00
|
|
|
sso = get_sso("/")
|
2014-02-24 22:30:49 -05:00
|
|
|
user = Fabricate(:user)
|
|
|
|
sso.email = user.email
|
2014-02-27 19:48:46 -05:00
|
|
|
sso.external_id = 'abc'
|
|
|
|
sso.username = 'sam'
|
2014-02-24 22:30:49 -05:00
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
get :sso_login, params: Rack::Utils.parse_query(sso.payload)
|
2014-02-24 22:30:49 -05:00
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to redirect_to('/')
|
2014-02-24 22:30:49 -05:00
|
|
|
logged_on_user = Discourse.current_user_provider.new(request.env).current_user
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(logged_on_user.email).to eq(user.email)
|
|
|
|
expect(logged_on_user.single_sign_on_record.external_id).to eq("abc")
|
|
|
|
expect(logged_on_user.single_sign_on_record.external_username).to eq('sam')
|
2014-02-24 22:30:49 -05:00
|
|
|
end
|
|
|
|
|
2015-02-25 15:59:11 -05:00
|
|
|
def sso_for_ip_specs
|
2015-02-23 15:58:45 -05:00
|
|
|
sso = get_sso('/a/')
|
|
|
|
sso.external_id = '666' # the number of the beast
|
|
|
|
sso.email = 'bob@bob.com'
|
|
|
|
sso.name = 'Sam Saffron'
|
|
|
|
sso.username = 'sam'
|
2015-02-25 15:59:11 -05:00
|
|
|
sso
|
|
|
|
end
|
2015-02-23 15:58:45 -05:00
|
|
|
|
2015-02-25 15:59:11 -05:00
|
|
|
it 'respects IP restrictions on create' do
|
2015-02-23 15:58:45 -05:00
|
|
|
screened_ip = Fabricate(:screened_ip_address)
|
|
|
|
ActionDispatch::Request.any_instance.stubs(:remote_ip).returns(screened_ip.ip_address)
|
2015-02-25 15:59:11 -05:00
|
|
|
|
|
|
|
sso = sso_for_ip_specs
|
2017-08-31 00:06:56 -04:00
|
|
|
get :sso_login, params: Rack::Utils.parse_query(sso.payload)
|
2015-02-23 15:58:45 -05:00
|
|
|
|
|
|
|
logged_on_user = Discourse.current_user_provider.new(request.env).current_user
|
|
|
|
expect(logged_on_user).to eq(nil)
|
|
|
|
end
|
|
|
|
|
2015-02-25 15:59:11 -05:00
|
|
|
it 'respects IP restrictions on login' do
|
|
|
|
sso = sso_for_ip_specs
|
2015-03-26 18:57:43 -04:00
|
|
|
_user = DiscourseSingleSignOn.parse(sso.payload).lookup_or_create_user(request.remote_ip)
|
2015-02-25 15:59:11 -05:00
|
|
|
|
|
|
|
sso = sso_for_ip_specs
|
|
|
|
screened_ip = Fabricate(:screened_ip_address)
|
|
|
|
ActionDispatch::Request.any_instance.stubs(:remote_ip).returns(screened_ip.ip_address)
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
get :sso_login, params: Rack::Utils.parse_query(sso.payload)
|
2015-02-25 15:59:11 -05:00
|
|
|
logged_on_user = Discourse.current_user_provider.new(request.env).current_user
|
|
|
|
expect(logged_on_user).to be_blank
|
|
|
|
end
|
|
|
|
|
2015-02-23 17:14:54 -05:00
|
|
|
it 'respects email restrictions' do
|
|
|
|
sso = get_sso('/a/')
|
|
|
|
sso.external_id = '666' # the number of the beast
|
|
|
|
sso.email = 'bob@bob.com'
|
|
|
|
sso.name = 'Sam Saffron'
|
|
|
|
sso.username = 'sam'
|
|
|
|
|
|
|
|
ScreenedEmail.block('bob@bob.com')
|
2017-08-31 00:06:56 -04:00
|
|
|
get :sso_login, params: Rack::Utils.parse_query(sso.payload)
|
2015-02-23 17:14:54 -05:00
|
|
|
|
|
|
|
logged_on_user = Discourse.current_user_provider.new(request.env).current_user
|
|
|
|
expect(logged_on_user).to eq(nil)
|
|
|
|
end
|
|
|
|
|
2014-11-26 20:39:00 -05:00
|
|
|
it 'allows you to create an admin account' do
|
|
|
|
sso = get_sso('/a/')
|
|
|
|
sso.external_id = '666' # the number of the beast
|
|
|
|
sso.email = 'bob@bob.com'
|
|
|
|
sso.name = 'Sam Saffron'
|
|
|
|
sso.username = 'sam'
|
|
|
|
sso.custom_fields["shop_url"] = "http://my_shop.com"
|
|
|
|
sso.custom_fields["shop_name"] = "Sam"
|
|
|
|
sso.admin = true
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
get :sso_login, params: Rack::Utils.parse_query(sso.payload)
|
2014-11-26 20:39:00 -05:00
|
|
|
|
|
|
|
logged_on_user = Discourse.current_user_provider.new(request.env).current_user
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(logged_on_user.admin).to eq(true)
|
2015-01-22 12:20:17 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'redirects to a non-relative url' do
|
|
|
|
sso = get_sso("#{Discourse.base_url}/b/")
|
|
|
|
sso.external_id = '666' # the number of the beast
|
|
|
|
sso.email = 'bob@bob.com'
|
|
|
|
sso.name = 'Sam Saffron'
|
|
|
|
sso.username = 'sam'
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
get :sso_login, params: Rack::Utils.parse_query(sso.payload)
|
2015-01-22 12:20:17 -05:00
|
|
|
expect(response).to redirect_to('/b/')
|
|
|
|
end
|
|
|
|
|
2016-12-15 21:37:44 -05:00
|
|
|
it 'redirects to random url if it is allowed' do
|
|
|
|
SiteSetting.sso_allows_all_return_paths = true
|
|
|
|
|
|
|
|
sso = get_sso('https://gusundtrout.com')
|
|
|
|
sso.external_id = '666' # the number of the beast
|
|
|
|
sso.email = 'bob@bob.com'
|
|
|
|
sso.name = 'Sam Saffron'
|
|
|
|
sso.username = 'sam'
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
get :sso_login, params: Rack::Utils.parse_query(sso.payload)
|
2016-12-15 21:37:44 -05:00
|
|
|
expect(response).to redirect_to('https://gusundtrout.com')
|
|
|
|
end
|
|
|
|
|
2015-01-22 12:20:17 -05:00
|
|
|
it 'redirects to root if the host of the return_path is different' do
|
|
|
|
sso = get_sso('//eviltrout.com')
|
|
|
|
sso.external_id = '666' # the number of the beast
|
|
|
|
sso.email = 'bob@bob.com'
|
|
|
|
sso.name = 'Sam Saffron'
|
|
|
|
sso.username = 'sam'
|
2014-11-26 20:39:00 -05:00
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
get :sso_login, params: Rack::Utils.parse_query(sso.payload)
|
2015-01-22 12:20:17 -05:00
|
|
|
expect(response).to redirect_to('/')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'redirects to root if the host of the return_path is different' do
|
|
|
|
sso = get_sso('http://eviltrout.com')
|
|
|
|
sso.external_id = '666' # the number of the beast
|
|
|
|
sso.email = 'bob@bob.com'
|
|
|
|
sso.name = 'Sam Saffron'
|
|
|
|
sso.username = 'sam'
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
get :sso_login, params: Rack::Utils.parse_query(sso.payload)
|
2015-01-22 12:20:17 -05:00
|
|
|
expect(response).to redirect_to('/')
|
2014-11-26 20:39:00 -05:00
|
|
|
end
|
|
|
|
|
2014-02-24 22:30:49 -05:00
|
|
|
it 'allows you to create an account' do
|
2014-02-25 17:58:30 -05:00
|
|
|
sso = get_sso('/a/')
|
2014-02-24 22:30:49 -05:00
|
|
|
sso.external_id = '666' # the number of the beast
|
|
|
|
sso.email = 'bob@bob.com'
|
|
|
|
sso.name = 'Sam Saffron'
|
|
|
|
sso.username = 'sam'
|
2014-04-21 23:52:13 -04:00
|
|
|
sso.custom_fields["shop_url"] = "http://my_shop.com"
|
|
|
|
sso.custom_fields["shop_name"] = "Sam"
|
2014-02-24 22:30:49 -05:00
|
|
|
|
2017-06-01 04:19:42 -04:00
|
|
|
events = DiscourseEvent.track_events do
|
2017-08-31 00:06:56 -04:00
|
|
|
get :sso_login, params: Rack::Utils.parse_query(sso.payload)
|
2017-06-01 04:19:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
expect(events.map { |event| event[:event_name] }).to include(
|
|
|
|
:user_logged_in, :user_first_logged_in
|
|
|
|
)
|
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to redirect_to('/a/')
|
2014-02-24 22:30:49 -05:00
|
|
|
|
|
|
|
logged_on_user = Discourse.current_user_provider.new(request.env).current_user
|
|
|
|
|
2014-04-21 23:52:13 -04:00
|
|
|
# ensure nothing is transient
|
|
|
|
logged_on_user = User.find(logged_on_user.id)
|
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(logged_on_user.admin).to eq(false)
|
|
|
|
expect(logged_on_user.email).to eq('bob@bob.com')
|
|
|
|
expect(logged_on_user.name).to eq('Sam Saffron')
|
|
|
|
expect(logged_on_user.username).to eq('sam')
|
|
|
|
|
|
|
|
expect(logged_on_user.single_sign_on_record.external_id).to eq("666")
|
|
|
|
expect(logged_on_user.single_sign_on_record.external_username).to eq('sam')
|
|
|
|
expect(logged_on_user.active).to eq(true)
|
|
|
|
expect(logged_on_user.custom_fields["shop_url"]).to eq("http://my_shop.com")
|
|
|
|
expect(logged_on_user.custom_fields["shop_name"]).to eq("Sam")
|
|
|
|
expect(logged_on_user.custom_fields["bla"]).to eq(nil)
|
2014-02-24 22:30:49 -05:00
|
|
|
end
|
2014-04-21 23:52:13 -04:00
|
|
|
|
2015-05-15 13:01:30 -04:00
|
|
|
context 'when sso emails are not trusted' do
|
|
|
|
context 'if you have not activated your account' do
|
|
|
|
it 'does not log you in' do
|
|
|
|
sso = get_sso('/a/')
|
|
|
|
sso.external_id = '666' # the number of the beast
|
|
|
|
sso.email = 'bob@bob.com'
|
|
|
|
sso.name = 'Sam Saffron'
|
|
|
|
sso.username = 'sam'
|
2015-05-19 12:16:02 -04:00
|
|
|
sso.require_activation = true
|
2015-05-15 13:01:30 -04:00
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
get :sso_login, params: Rack::Utils.parse_query(sso.payload)
|
2015-05-15 13:01:30 -04:00
|
|
|
|
|
|
|
logged_on_user = Discourse.current_user_provider.new(request.env).current_user
|
|
|
|
expect(logged_on_user).to eq(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sends an activation email' do
|
2016-04-07 00:38:43 -04:00
|
|
|
Jobs.expects(:enqueue).with(:critical_user_email, has_entries(type: :signup))
|
2015-05-15 13:01:30 -04:00
|
|
|
sso = get_sso('/a/')
|
|
|
|
sso.external_id = '666' # the number of the beast
|
|
|
|
sso.email = 'bob@bob.com'
|
|
|
|
sso.name = 'Sam Saffron'
|
|
|
|
sso.username = 'sam'
|
2015-05-19 12:16:02 -04:00
|
|
|
sso.require_activation = true
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
get :sso_login, params: Rack::Utils.parse_query(sso.payload)
|
2015-05-15 13:01:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'if you have activated your account' do
|
|
|
|
it 'allows you to log in' do
|
|
|
|
sso = get_sso('/hello/world')
|
|
|
|
sso.external_id = '997'
|
|
|
|
sso.sso_url = "http://somewhere.over.com/sso_login"
|
2015-05-19 12:16:02 -04:00
|
|
|
sso.require_activation = true
|
2015-05-15 13:01:30 -04:00
|
|
|
|
|
|
|
user = Fabricate(:user)
|
|
|
|
user.create_single_sign_on_record(external_id: '997', last_payload: '')
|
|
|
|
user.stubs(:active?).returns(true)
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
get :sso_login, params: Rack::Utils.parse_query(sso.payload)
|
2015-05-15 13:01:30 -04:00
|
|
|
|
|
|
|
logged_on_user = Discourse.current_user_provider.new(request.env).current_user
|
|
|
|
expect(user.id).to eq(logged_on_user.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-24 22:30:49 -05:00
|
|
|
it 'allows login to existing account with valid nonce' do
|
2014-02-25 17:58:30 -05:00
|
|
|
sso = get_sso('/hello/world')
|
2014-02-24 22:30:49 -05:00
|
|
|
sso.external_id = '997'
|
2014-11-26 01:25:54 -05:00
|
|
|
sso.sso_url = "http://somewhere.over.com/sso_login"
|
2014-02-24 22:30:49 -05:00
|
|
|
|
|
|
|
user = Fabricate(:user)
|
|
|
|
user.create_single_sign_on_record(external_id: '997', last_payload: '')
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
get :sso_login, params: Rack::Utils.parse_query(sso.payload)
|
2014-02-24 22:30:49 -05:00
|
|
|
|
|
|
|
user.single_sign_on_record.reload
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(user.single_sign_on_record.last_payload).to eq(sso.unsigned_payload)
|
2014-02-24 22:30:49 -05:00
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to redirect_to('/hello/world')
|
2014-02-24 22:30:49 -05:00
|
|
|
logged_on_user = Discourse.current_user_provider.new(request.env).current_user
|
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(user.id).to eq(logged_on_user.id)
|
2014-02-24 22:30:49 -05:00
|
|
|
|
|
|
|
# nonce is bad now
|
2017-08-31 00:06:56 -04:00
|
|
|
get :sso_login, params: Rack::Utils.parse_query(sso.payload)
|
2015-05-19 22:35:22 -04:00
|
|
|
expect(response.code).to eq('419')
|
2014-02-24 22:30:49 -05:00
|
|
|
end
|
2014-03-26 00:39:44 -04:00
|
|
|
|
2015-10-25 01:00:19 -04:00
|
|
|
describe 'can act as an SSO provider' do
|
|
|
|
before do
|
|
|
|
SiteSetting.enable_sso_provider = true
|
|
|
|
SiteSetting.enable_sso = false
|
|
|
|
SiteSetting.enable_local_logins = true
|
|
|
|
SiteSetting.sso_secret = "topsecret"
|
|
|
|
|
|
|
|
@sso = SingleSignOn.new
|
|
|
|
@sso.nonce = "mynonce"
|
|
|
|
@sso.sso_secret = SiteSetting.sso_secret
|
|
|
|
@sso.return_sso_url = "http://somewhere.over.rainbow/sso"
|
|
|
|
|
2017-11-30 23:19:24 -05:00
|
|
|
@user = Fabricate(:user, password: "myfrogs123ADMIN", active: true, admin: true)
|
2017-10-25 20:49:17 -04:00
|
|
|
group = Fabricate(:group)
|
|
|
|
group.add(@user)
|
|
|
|
@user.reload
|
2015-10-25 01:00:19 -04:00
|
|
|
EmailToken.update_all(confirmed: true)
|
|
|
|
end
|
2014-11-26 01:25:54 -05:00
|
|
|
|
2015-10-25 01:00:19 -04:00
|
|
|
it "successfully logs in and redirects user to return_sso_url when the user is not logged in" do
|
2017-08-31 00:06:56 -04:00
|
|
|
get :sso_provider, params: Rack::Utils.parse_query(@sso.payload)
|
2015-10-25 01:00:19 -04:00
|
|
|
expect(response).to redirect_to("/login")
|
2014-11-26 01:25:54 -05:00
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
post :create,
|
2017-11-30 23:19:24 -05:00
|
|
|
params: { login: @user.username, password: "myfrogs123ADMIN" },
|
2017-08-31 00:06:56 -04:00
|
|
|
format: :json,
|
|
|
|
xhr: true
|
2014-11-26 01:25:54 -05:00
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
location = response.cookies["sso_destination_url"]
|
2015-10-25 01:00:19 -04:00
|
|
|
# javascript code will handle redirection of user to return_sso_url
|
|
|
|
expect(location).to match(/^http:\/\/somewhere.over.rainbow\/sso/)
|
2014-11-26 01:25:54 -05:00
|
|
|
|
2015-10-25 01:00:19 -04:00
|
|
|
payload = location.split("?")[1]
|
|
|
|
sso2 = SingleSignOn.parse(payload, "topsecret")
|
2014-11-26 01:25:54 -05:00
|
|
|
|
2015-10-25 01:00:19 -04:00
|
|
|
expect(sso2.email).to eq(@user.email)
|
|
|
|
expect(sso2.name).to eq(@user.name)
|
|
|
|
expect(sso2.username).to eq(@user.username)
|
|
|
|
expect(sso2.external_id).to eq(@user.id.to_s)
|
|
|
|
expect(sso2.admin).to eq(true)
|
|
|
|
expect(sso2.moderator).to eq(false)
|
2017-10-25 20:49:17 -04:00
|
|
|
expect(sso2.groups).to eq(@user.groups.pluck(:name))
|
2015-10-25 01:00:19 -04:00
|
|
|
end
|
2014-11-26 01:25:54 -05:00
|
|
|
|
2015-10-25 01:00:19 -04:00
|
|
|
it "successfully redirects user to return_sso_url when the user is logged in" do
|
|
|
|
log_in_user(@user)
|
2014-11-26 01:25:54 -05:00
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
get :sso_provider, params: Rack::Utils.parse_query(@sso.payload)
|
2014-11-26 01:25:54 -05:00
|
|
|
|
2015-10-25 01:00:19 -04:00
|
|
|
location = response.header["Location"]
|
|
|
|
expect(location).to match(/^http:\/\/somewhere.over.rainbow\/sso/)
|
2014-11-26 01:25:54 -05:00
|
|
|
|
2015-10-25 01:00:19 -04:00
|
|
|
payload = location.split("?")[1]
|
|
|
|
sso2 = SingleSignOn.parse(payload, "topsecret")
|
2014-11-26 01:25:54 -05:00
|
|
|
|
2015-10-25 01:00:19 -04:00
|
|
|
expect(sso2.email).to eq(@user.email)
|
|
|
|
expect(sso2.name).to eq(@user.name)
|
|
|
|
expect(sso2.username).to eq(@user.username)
|
|
|
|
expect(sso2.external_id).to eq(@user.id.to_s)
|
|
|
|
expect(sso2.admin).to eq(true)
|
|
|
|
expect(sso2.moderator).to eq(false)
|
|
|
|
end
|
2014-11-26 01:25:54 -05:00
|
|
|
end
|
|
|
|
|
2014-03-26 00:39:44 -04:00
|
|
|
describe 'local attribute override from SSO payload' do
|
2014-02-27 19:48:46 -05:00
|
|
|
before do
|
2017-07-09 22:12:21 -04:00
|
|
|
SiteSetting.email_editable = false
|
2017-07-07 02:09:14 -04:00
|
|
|
SiteSetting.sso_overrides_email = true
|
|
|
|
SiteSetting.sso_overrides_username = true
|
|
|
|
SiteSetting.sso_overrides_name = true
|
2014-03-26 00:39:44 -04:00
|
|
|
|
2014-02-27 19:48:46 -05:00
|
|
|
@user = Fabricate(:user)
|
2014-03-26 00:39:44 -04:00
|
|
|
|
2014-02-27 19:48:46 -05:00
|
|
|
@sso = get_sso('/hello/world')
|
|
|
|
@sso.external_id = '997'
|
2014-03-26 00:39:44 -04:00
|
|
|
|
2014-02-27 19:48:46 -05:00
|
|
|
@reversed_username = @user.username.reverse
|
|
|
|
@sso.username = @reversed_username
|
|
|
|
@sso.email = "#{@reversed_username}@garbage.org"
|
|
|
|
@reversed_name = @user.name.reverse
|
|
|
|
@sso.name = @reversed_name
|
2014-03-26 00:39:44 -04:00
|
|
|
|
2014-02-27 19:48:46 -05:00
|
|
|
@suggested_username = UserNameSuggester.suggest(@sso.username || @sso.name || @sso.email)
|
2014-12-10 01:17:49 -05:00
|
|
|
@suggested_name = User.suggest_name(@sso.name || @sso.username || @sso.email)
|
2014-02-27 19:48:46 -05:00
|
|
|
@user.create_single_sign_on_record(external_id: '997', last_payload: '')
|
|
|
|
end
|
2014-03-26 00:39:44 -04:00
|
|
|
|
2014-02-27 19:48:46 -05:00
|
|
|
it 'stores the external attributes' do
|
2017-08-31 00:06:56 -04:00
|
|
|
get :sso_login, params: Rack::Utils.parse_query(@sso.payload)
|
2014-02-27 19:48:46 -05:00
|
|
|
@user.single_sign_on_record.reload
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(@user.single_sign_on_record.external_username).to eq(@sso.username)
|
|
|
|
expect(@user.single_sign_on_record.external_email).to eq(@sso.email)
|
|
|
|
expect(@user.single_sign_on_record.external_name).to eq(@sso.name)
|
2014-02-27 19:48:46 -05:00
|
|
|
end
|
2014-03-26 00:39:44 -04:00
|
|
|
|
2014-02-27 19:48:46 -05:00
|
|
|
it 'overrides attributes' do
|
2017-08-31 00:06:56 -04:00
|
|
|
get :sso_login, params: Rack::Utils.parse_query(@sso.payload)
|
2014-03-26 00:39:44 -04:00
|
|
|
|
2014-02-27 19:48:46 -05:00
|
|
|
logged_on_user = Discourse.current_user_provider.new(request.env).current_user
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(logged_on_user.username).to eq(@suggested_username)
|
|
|
|
expect(logged_on_user.email).to eq("#{@reversed_username}@garbage.org")
|
2015-03-26 22:14:50 -04:00
|
|
|
expect(logged_on_user.name).to eq(@sso.name)
|
2014-02-27 19:48:46 -05:00
|
|
|
end
|
2014-03-26 00:39:44 -04:00
|
|
|
|
2014-02-27 19:48:46 -05:00
|
|
|
it 'does not change matching attributes for an existing account' do
|
|
|
|
@sso.username = @user.username
|
|
|
|
@sso.name = @user.name
|
|
|
|
@sso.email = @user.email
|
2014-03-26 00:39:44 -04:00
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
get :sso_login, params: Rack::Utils.parse_query(@sso.payload)
|
2014-03-26 00:39:44 -04:00
|
|
|
|
2014-02-27 19:48:46 -05:00
|
|
|
logged_on_user = Discourse.current_user_provider.new(request.env).current_user
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(logged_on_user.username).to eq(@user.username)
|
|
|
|
expect(logged_on_user.name).to eq(@user.name)
|
|
|
|
expect(logged_on_user.email).to eq(@user.email)
|
2014-02-27 19:48:46 -05:00
|
|
|
end
|
2014-03-26 00:39:44 -04:00
|
|
|
|
|
|
|
end
|
2014-02-24 22:30:49 -05:00
|
|
|
end
|
|
|
|
|
2017-04-20 11:17:24 -04:00
|
|
|
describe '#sso_provider' do
|
2016-08-28 21:28:19 -04:00
|
|
|
before do
|
|
|
|
SiteSetting.enable_sso_provider = true
|
|
|
|
SiteSetting.enable_sso = false
|
|
|
|
SiteSetting.enable_local_logins = true
|
|
|
|
SiteSetting.sso_secret = "topsecret"
|
|
|
|
|
|
|
|
@sso = SingleSignOn.new
|
|
|
|
@sso.nonce = "mynonce"
|
|
|
|
@sso.sso_secret = SiteSetting.sso_secret
|
|
|
|
@sso.return_sso_url = "http://somewhere.over.rainbow/sso"
|
|
|
|
|
2017-11-30 23:19:24 -05:00
|
|
|
@user = Fabricate(:user, password: "myfrogs123ADMIN", active: true, admin: true)
|
2016-08-28 21:28:19 -04:00
|
|
|
EmailToken.update_all(confirmed: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "successfully logs in and redirects user to return_sso_url when the user is not logged in" do
|
2017-08-31 00:06:56 -04:00
|
|
|
get :sso_provider, params: Rack::Utils.parse_query(@sso.payload)
|
2016-08-28 21:28:19 -04:00
|
|
|
expect(response).to redirect_to("/login")
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
post :create,
|
2017-11-30 23:19:24 -05:00
|
|
|
params: { login: @user.username, password: "myfrogs123ADMIN" },
|
2017-08-31 00:06:56 -04:00
|
|
|
format: :json,
|
|
|
|
xhr: true
|
2016-08-28 21:28:19 -04:00
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
location = response.cookies["sso_destination_url"]
|
2016-08-28 21:28:19 -04:00
|
|
|
# javascript code will handle redirection of user to return_sso_url
|
|
|
|
expect(location).to match(/^http:\/\/somewhere.over.rainbow\/sso/)
|
|
|
|
|
|
|
|
payload = location.split("?")[1]
|
|
|
|
sso2 = SingleSignOn.parse(payload, "topsecret")
|
|
|
|
|
|
|
|
expect(sso2.email).to eq(@user.email)
|
|
|
|
expect(sso2.name).to eq(@user.name)
|
|
|
|
expect(sso2.username).to eq(@user.username)
|
|
|
|
expect(sso2.external_id).to eq(@user.id.to_s)
|
|
|
|
expect(sso2.admin).to eq(true)
|
|
|
|
expect(sso2.moderator).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "successfully redirects user to return_sso_url when the user is logged in" do
|
|
|
|
log_in_user(@user)
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
get :sso_provider, params: Rack::Utils.parse_query(@sso.payload)
|
2016-08-28 21:28:19 -04:00
|
|
|
|
|
|
|
location = response.header["Location"]
|
|
|
|
expect(location).to match(/^http:\/\/somewhere.over.rainbow\/sso/)
|
|
|
|
|
|
|
|
payload = location.split("?")[1]
|
|
|
|
sso2 = SingleSignOn.parse(payload, "topsecret")
|
|
|
|
|
|
|
|
expect(sso2.email).to eq(@user.email)
|
|
|
|
expect(sso2.name).to eq(@user.name)
|
|
|
|
expect(sso2.username).to eq(@user.username)
|
|
|
|
expect(sso2.external_id).to eq(@user.id.to_s)
|
|
|
|
expect(sso2.admin).to eq(true)
|
|
|
|
expect(sso2.moderator).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-20 11:17:24 -04:00
|
|
|
describe '#create' do
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
|
2017-08-16 01:43:05 -04:00
|
|
|
context 'local login is disabled' do
|
|
|
|
before do
|
|
|
|
SiteSetting.enable_local_logins = false
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
post :create, params: {
|
|
|
|
login: user.username, password: 'myawesomepassword'
|
|
|
|
}, format: :json
|
2017-08-16 01:43:05 -04:00
|
|
|
end
|
|
|
|
it_behaves_like "failed to continue local login"
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'SSO is enabled' do
|
|
|
|
before do
|
2017-12-23 03:46:48 -05:00
|
|
|
SiteSetting.sso_url = "https://www.example.com/sso"
|
2017-08-16 01:43:05 -04:00
|
|
|
SiteSetting.enable_sso = true
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
post :create, params: {
|
|
|
|
login: user.username, password: 'myawesomepassword'
|
|
|
|
}, format: :json
|
2017-08-16 01:43:05 -04:00
|
|
|
end
|
|
|
|
it_behaves_like "failed to continue local login"
|
|
|
|
end
|
|
|
|
|
2013-02-11 11:18:26 -05:00
|
|
|
context 'when email is confirmed' do
|
2013-02-05 14:16:51 -05:00
|
|
|
before do
|
2014-05-06 09:41:59 -04:00
|
|
|
token = user.email_tokens.find_by(email: user.email)
|
2013-02-11 11:18:26 -05:00
|
|
|
EmailToken.confirm(token.token)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-11 11:18:26 -05:00
|
|
|
it "raises an error when the login isn't present" do
|
2017-08-31 00:06:56 -04:00
|
|
|
expect do
|
|
|
|
post :create, format: :json
|
|
|
|
end.to raise_error(ActionController::ParameterMissing)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-11 11:18:26 -05:00
|
|
|
describe 'invalid password' do
|
|
|
|
it "should return an error with an invalid password" do
|
2017-08-31 00:06:56 -04:00
|
|
|
post :create, params: {
|
|
|
|
login: user.username, password: 'sssss'
|
|
|
|
}, format: :json
|
|
|
|
|
2017-04-20 11:17:24 -04:00
|
|
|
expect(::JSON.parse(response.body)['error']).to eq(
|
|
|
|
I18n.t("login.incorrect_username_email_or_password")
|
|
|
|
)
|
2013-02-11 11:18:26 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-09-11 15:22:11 -04:00
|
|
|
describe 'invalid password' do
|
|
|
|
it "should return an error with an invalid password if too long" do
|
|
|
|
User.any_instance.expects(:confirm_password?).never
|
2017-08-31 00:06:56 -04:00
|
|
|
post :create, params: {
|
|
|
|
login: user.username, password: ('s' * (User.max_password_length + 1))
|
|
|
|
}, format: :json
|
|
|
|
|
2017-04-20 11:17:24 -04:00
|
|
|
expect(::JSON.parse(response.body)['error']).to eq(
|
|
|
|
I18n.t("login.incorrect_username_email_or_password")
|
|
|
|
)
|
2014-09-11 15:22:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-07 13:53:32 -05:00
|
|
|
describe 'suspended user' do
|
2013-06-27 15:14:42 -04:00
|
|
|
it 'should return an error' do
|
2016-07-28 11:36:48 -04:00
|
|
|
user.suspended_till = 2.days.from_now
|
|
|
|
user.suspended_at = Time.now
|
|
|
|
user.save!
|
|
|
|
StaffActionLogger.new(user).log_user_suspend(user, "<strike>banned</strike>")
|
2017-04-20 11:17:24 -04:00
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
post :create, params: {
|
|
|
|
login: user.username, password: 'myawesomepassword'
|
|
|
|
}, format: :json
|
2016-07-28 11:36:48 -04:00
|
|
|
|
2017-04-20 11:17:24 -04:00
|
|
|
expect(JSON.parse(response.body)['error']).to eq(I18n.t('login.suspended_with_reason',
|
|
|
|
date: I18n.l(user.suspended_till, format: :date_only),
|
|
|
|
reason: Rack::Utils.escape_html(user.suspend_reason)
|
|
|
|
))
|
2013-06-27 15:14:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-28 13:46:28 -04:00
|
|
|
describe 'deactivated user' do
|
|
|
|
it 'should return an error' do
|
|
|
|
User.any_instance.stubs(:active).returns(false)
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
post :create, params: {
|
|
|
|
login: user.username, password: 'myawesomepassword'
|
|
|
|
}, format: :json
|
|
|
|
|
2014-04-28 13:46:28 -04:00
|
|
|
expect(JSON.parse(response.body)['error']).to eq(I18n.t('login.not_activated'))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-11 11:18:26 -05:00
|
|
|
describe 'success by username' do
|
2014-03-26 00:39:44 -04:00
|
|
|
it 'logs in correctly' do
|
2017-06-01 04:19:42 -04:00
|
|
|
events = DiscourseEvent.track_events do
|
2017-08-31 00:06:56 -04:00
|
|
|
post :create, params: {
|
|
|
|
login: user.username, password: 'myawesomepassword'
|
|
|
|
}, format: :json
|
2017-06-01 04:19:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
expect(events.map { |event| event[:event_name] }).to include(
|
|
|
|
:user_logged_in, :user_first_logged_in
|
|
|
|
)
|
2014-03-26 00:39:44 -04:00
|
|
|
|
2013-02-11 11:18:26 -05:00
|
|
|
user.reload
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(session[:current_user_id]).to eq(user.id)
|
2017-01-31 17:21:37 -05:00
|
|
|
expect(user.user_auth_tokens.count).to eq(1)
|
|
|
|
expect(UserAuthToken.hash_token(cookies[:_t])).to eq(user.user_auth_tokens.first.auth_token)
|
2013-02-11 11:18:26 -05:00
|
|
|
end
|
2014-03-26 00:39:44 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2015-02-25 15:59:11 -05:00
|
|
|
describe 'with a blocked IP' do
|
|
|
|
before do
|
|
|
|
screened_ip = Fabricate(:screened_ip_address)
|
|
|
|
ActionDispatch::Request.any_instance.stubs(:remote_ip).returns(screened_ip.ip_address)
|
2017-08-31 00:06:56 -04:00
|
|
|
post :create, params: {
|
|
|
|
login: "@" + user.username, password: 'myawesomepassword'
|
|
|
|
}, format: :json
|
|
|
|
|
2015-02-25 15:59:11 -05:00
|
|
|
user.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't log in" do
|
|
|
|
expect(session[:current_user_id]).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-11 11:18:26 -05:00
|
|
|
describe 'strips leading @ symbol' do
|
|
|
|
before do
|
2017-08-31 00:06:56 -04:00
|
|
|
post :create, params: {
|
|
|
|
login: "@" + user.username, password: 'myawesomepassword'
|
|
|
|
}, format: :json
|
|
|
|
|
2013-02-11 11:18:26 -05:00
|
|
|
user.reload
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-11 11:18:26 -05:00
|
|
|
it 'sets a session id' do
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(session[:current_user_id]).to eq(user.id)
|
2013-02-11 11:18:26 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-11 11:18:26 -05:00
|
|
|
describe 'also allow login by email' do
|
2013-02-05 14:16:51 -05:00
|
|
|
before do
|
2017-08-31 00:06:56 -04:00
|
|
|
post :create, params: {
|
|
|
|
login: user.email, password: 'myawesomepassword'
|
|
|
|
}, format: :json
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-11 11:18:26 -05:00
|
|
|
it 'sets a session id' do
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(session[:current_user_id]).to eq(user.id)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-23 23:02:42 -04:00
|
|
|
context 'login has leading and trailing space' do
|
|
|
|
let(:username) { " #{user.username} " }
|
|
|
|
let(:email) { " #{user.email} " }
|
|
|
|
|
|
|
|
it "strips spaces from the username" do
|
2017-08-31 00:06:56 -04:00
|
|
|
post :create, params: {
|
|
|
|
login: username, password: 'myawesomepassword'
|
|
|
|
}, format: :json
|
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(::JSON.parse(response.body)['error']).not_to be_present
|
2013-07-23 23:02:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "strips spaces from the email" do
|
2017-08-31 00:06:56 -04:00
|
|
|
post :create, params: {
|
|
|
|
login: email, password: 'myawesomepassword'
|
|
|
|
}, format: :json
|
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(::JSON.parse(response.body)['error']).not_to be_present
|
2013-07-23 23:02:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-11 11:18:26 -05:00
|
|
|
describe "when the site requires approval of users" do
|
|
|
|
before do
|
|
|
|
SiteSetting.expects(:must_approve_users?).returns(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an unapproved user' do
|
|
|
|
before do
|
2017-08-31 00:06:56 -04:00
|
|
|
post :create, params: {
|
|
|
|
login: user.email, password: 'myawesomepassword'
|
|
|
|
}, format: :json
|
2013-02-11 11:18:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't log in the user" do
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(session[:current_user_id]).to be_blank
|
2013-02-11 11:18:26 -05:00
|
|
|
end
|
2013-06-05 21:21:19 -04:00
|
|
|
|
|
|
|
it "shows the 'not approved' error message" do
|
|
|
|
expect(JSON.parse(response.body)['error']).to eq(
|
|
|
|
I18n.t('login.not_approved')
|
|
|
|
)
|
|
|
|
end
|
2013-02-11 11:18:26 -05:00
|
|
|
end
|
2013-08-06 16:51:29 -04:00
|
|
|
|
|
|
|
context "with an unapproved user who is an admin" do
|
|
|
|
before do
|
|
|
|
User.any_instance.stubs(:admin?).returns(true)
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
post :create, params: {
|
|
|
|
login: user.email, password: 'myawesomepassword'
|
|
|
|
}, format: :json
|
2013-08-06 16:51:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets a session id' do
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(session[:current_user_id]).to eq(user.id)
|
2013-08-06 16:51:29 -04:00
|
|
|
end
|
|
|
|
end
|
2013-02-11 11:18:26 -05:00
|
|
|
end
|
2014-09-04 18:50:27 -04:00
|
|
|
|
|
|
|
context 'when admins are restricted by ip address' do
|
|
|
|
let(:permitted_ip_address) { '111.234.23.11' }
|
|
|
|
before do
|
|
|
|
Fabricate(:screened_ip_address, ip_address: permitted_ip_address, action_type: ScreenedIpAddress.actions[:allow_admin])
|
2017-07-07 02:09:14 -04:00
|
|
|
SiteSetting.use_admin_ip_whitelist = true
|
2014-09-04 18:50:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'is successful for admin at the ip address' do
|
|
|
|
User.any_instance.stubs(:admin?).returns(true)
|
|
|
|
ActionDispatch::Request.any_instance.stubs(:remote_ip).returns(permitted_ip_address)
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
post :create, params: {
|
|
|
|
login: user.username, password: 'myawesomepassword'
|
|
|
|
}, format: :json
|
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(session[:current_user_id]).to eq(user.id)
|
2014-09-04 18:50:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an error for admin not at the ip address' do
|
|
|
|
User.any_instance.stubs(:admin?).returns(true)
|
|
|
|
ActionDispatch::Request.any_instance.stubs(:remote_ip).returns("111.234.23.12")
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
post :create, params: {
|
|
|
|
login: user.username, password: 'myawesomepassword'
|
|
|
|
}, format: :json
|
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(JSON.parse(response.body)['error']).to be_present
|
|
|
|
expect(session[:current_user_id]).not_to eq(user.id)
|
2014-09-04 18:50:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'is successful for non-admin not at the ip address' do
|
|
|
|
User.any_instance.stubs(:admin?).returns(false)
|
|
|
|
ActionDispatch::Request.any_instance.stubs(:remote_ip).returns("111.234.23.12")
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
post :create, params: {
|
|
|
|
login: user.username, password: 'myawesomepassword'
|
|
|
|
}, format: :json
|
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(session[:current_user_id]).to eq(user.id)
|
2014-09-04 18:50:27 -04:00
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-11 11:18:26 -05:00
|
|
|
context 'when email has not been confirmed' do
|
2013-06-05 21:21:19 -04:00
|
|
|
def post_login
|
2017-08-31 00:06:56 -04:00
|
|
|
post :create, params: {
|
|
|
|
login: user.email, password: 'myawesomepassword'
|
|
|
|
}, format: :json
|
2013-02-11 11:18:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't log in the user" do
|
2013-06-05 21:21:19 -04:00
|
|
|
post_login
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(session[:current_user_id]).to be_blank
|
2013-02-11 11:18:26 -05:00
|
|
|
end
|
|
|
|
|
2013-06-05 21:21:19 -04:00
|
|
|
it "shows the 'not activated' error message" do
|
|
|
|
post_login
|
|
|
|
expect(JSON.parse(response.body)['error']).to eq(
|
|
|
|
I18n.t 'login.not_activated'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "and the 'must approve users' site setting is enabled" do
|
|
|
|
before { SiteSetting.expects(:must_approve_users?).returns(true) }
|
|
|
|
|
|
|
|
it "shows the 'not approved' error message" do
|
|
|
|
post_login
|
|
|
|
expect(JSON.parse(response.body)['error']).to eq(
|
|
|
|
I18n.t 'login.not_approved'
|
|
|
|
)
|
|
|
|
end
|
2013-02-11 11:18:26 -05:00
|
|
|
end
|
|
|
|
end
|
2017-02-21 12:23:35 -05:00
|
|
|
|
|
|
|
context 'rate limited' do
|
|
|
|
it 'rate limits login' do
|
|
|
|
SiteSetting.max_logins_per_ip_per_hour = 2
|
2017-12-04 05:23:11 -05:00
|
|
|
RateLimiter.enable
|
2017-02-21 12:23:35 -05:00
|
|
|
RateLimiter.clear_all!
|
|
|
|
|
|
|
|
2.times do
|
2017-08-31 00:06:56 -04:00
|
|
|
post :create, params: {
|
|
|
|
login: user.username, password: 'myawesomepassword'
|
|
|
|
}, format: :json
|
|
|
|
|
2017-02-21 12:23:35 -05:00
|
|
|
expect(response).to be_success
|
|
|
|
end
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
post :create, params: {
|
|
|
|
login: user.username, password: 'myawesomepassword'
|
|
|
|
}, format: :json
|
|
|
|
|
2017-02-21 12:23:35 -05:00
|
|
|
expect(response).not_to be_success
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
expect(json["error_type"]).to eq("rate_limit")
|
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '.destroy' do
|
|
|
|
before do
|
|
|
|
@user = log_in
|
2017-08-31 00:06:56 -04:00
|
|
|
delete :destroy, params: { id: @user.username }, format: :json
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes the session variable' do
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(session[:current_user_id]).to be_blank
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes the auth token cookie' do
|
2017-08-31 00:06:56 -04:00
|
|
|
expect(response.cookies["_t"]).to be_blank
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.forgot_password' do
|
|
|
|
|
|
|
|
it 'raises an error without a username parameter' do
|
2017-08-31 00:06:56 -04:00
|
|
|
expect do
|
|
|
|
post :forgot_password, format: :json
|
|
|
|
end.to raise_error(ActionController::ParameterMissing)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'for a non existant username' do
|
2013-02-25 11:42:20 -05:00
|
|
|
it "doesn't generate a new token for a made up username" do
|
2017-08-31 00:06:56 -04:00
|
|
|
expect do
|
|
|
|
post :forgot_password, params: { login: 'made_up' }, format: :json
|
|
|
|
end.not_to change(EmailToken, :count)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it "doesn't enqueue an email" do
|
2013-02-05 14:16:51 -05:00
|
|
|
Jobs.expects(:enqueue).with(:user_mail, anything).never
|
2017-08-31 00:06:56 -04:00
|
|
|
post :forgot_password, params: { login: 'made_up' }, format: :json
|
2013-02-25 11:42:20 -05:00
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
context 'for an existing username' do
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
|
2017-08-16 01:43:05 -04:00
|
|
|
context 'local login is disabled' do
|
|
|
|
before do
|
|
|
|
SiteSetting.enable_local_logins = false
|
2017-08-31 00:06:56 -04:00
|
|
|
post :forgot_password, params: { login: user.username }, format: :json
|
2017-08-16 01:43:05 -04:00
|
|
|
end
|
|
|
|
it_behaves_like "failed to continue local login"
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'SSO is enabled' do
|
|
|
|
before do
|
2017-12-23 03:46:48 -05:00
|
|
|
SiteSetting.sso_url = "https://www.example.com/sso"
|
2017-08-16 01:43:05 -04:00
|
|
|
SiteSetting.enable_sso = true
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
post :create, params: {
|
|
|
|
login: user.username, password: 'myawesomepassword'
|
|
|
|
}, format: :json
|
2017-08-16 01:43:05 -04:00
|
|
|
end
|
|
|
|
it_behaves_like "failed to continue local login"
|
2014-03-26 00:39:44 -04:00
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it "generates a new token for a made up username" do
|
2017-08-31 00:06:56 -04:00
|
|
|
expect do
|
|
|
|
post :forgot_password, params: { login: user.username }, format: :json
|
|
|
|
end.to change(EmailToken, :count)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it "enqueues an email" do
|
2016-04-07 00:38:43 -04:00
|
|
|
Jobs.expects(:enqueue).with(:critical_user_email, has_entries(type: :forgot_password, user_id: user.id))
|
2017-08-31 00:06:56 -04:00
|
|
|
post :forgot_password, params: { login: user.username }, format: :json
|
2013-02-25 11:42:20 -05:00
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-12-10 01:17:49 -05:00
|
|
|
context 'do nothing to system username' do
|
2015-11-09 11:37:33 -05:00
|
|
|
let(:system) { Discourse.system_user }
|
2014-12-10 01:17:49 -05:00
|
|
|
|
|
|
|
it 'generates no token for system username' do
|
2017-08-31 00:06:56 -04:00
|
|
|
expect do
|
|
|
|
post :forgot_password, params: { login: system.username }, format: :json
|
|
|
|
end.not_to change(EmailToken, :count)
|
2014-12-10 01:17:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'enqueues no email' do
|
|
|
|
Jobs.expects(:enqueue).never
|
2017-08-31 00:06:56 -04:00
|
|
|
post :forgot_password, params: { login: system.username }, format: :json
|
2015-11-09 11:37:33 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for a staged account' do
|
|
|
|
let!(:staged) { Fabricate(:staged) }
|
|
|
|
|
|
|
|
it 'generates no token for staged username' do
|
2017-08-31 00:06:56 -04:00
|
|
|
expect do
|
|
|
|
post :forgot_password, params: { login: staged.username }, format: :json
|
|
|
|
end.not_to change(EmailToken, :count)
|
2015-11-09 11:37:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'enqueues no email' do
|
|
|
|
Jobs.expects(:enqueue).never
|
2017-08-31 00:06:56 -04:00
|
|
|
post :forgot_password, params: { login: staged.username }, format: :json
|
2014-12-10 01:17:49 -05:00
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2017-04-20 11:17:24 -04:00
|
|
|
describe '#current' do
|
2014-02-05 13:46:24 -05:00
|
|
|
context "when not logged in" do
|
|
|
|
it "retuns 404" do
|
2017-08-31 00:06:56 -04:00
|
|
|
get :current, format: :json
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).not_to be_success
|
2014-02-05 13:46:24 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in" do
|
|
|
|
let!(:user) { log_in }
|
|
|
|
|
|
|
|
it "returns the JSON for the user" do
|
2017-08-31 00:06:56 -04:00
|
|
|
get :current, format: :json
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
2014-02-05 13:46:24 -05:00
|
|
|
json = ::JSON.parse(response.body)
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(json['current_user']).to be_present
|
|
|
|
expect(json['current_user']['id']).to eq(user.id)
|
2014-02-05 13:46:24 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|