2016-04-01 05:33:25 +08:00
|
|
|
require 'rails_helper'
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-02-07 19:50:59 +08:00
|
|
|
describe UsersController do
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-02-07 19:50:59 +08:00
|
|
|
describe '.show' do
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2016-04-01 05:33:25 +08:00
|
|
|
context "anon" do
|
2014-07-09 13:31:49 +08:00
|
|
|
|
2016-04-01 05:33:25 +08:00
|
|
|
let(:user) { Discourse.system_user }
|
2014-07-09 13:31:49 +08:00
|
|
|
|
2016-04-01 05:33:25 +08:00
|
|
|
it "returns success" do
|
|
|
|
xhr :get, :show, username: user.username, format: :json
|
|
|
|
expect(response).to be_success
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2016-04-01 05:33:25 +08:00
|
|
|
it "raises an error for anon when profiles are hidden" do
|
|
|
|
SiteSetting.stubs(:hide_user_profiles_from_public).returns(true)
|
|
|
|
xhr :get, :show, username: user.username, format: :json
|
|
|
|
expect(response).not_to be_success
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-08-14 01:30:25 +08:00
|
|
|
end
|
|
|
|
|
2016-04-01 05:33:25 +08:00
|
|
|
context "logged in" do
|
2014-06-19 02:40:15 +08:00
|
|
|
|
2016-04-01 05:33:25 +08:00
|
|
|
let(:user) { log_in }
|
2014-06-19 02:40:15 +08:00
|
|
|
|
2016-04-01 05:33:25 +08:00
|
|
|
it 'returns success' do
|
|
|
|
xhr :get, :show, username: user.username, format: :json
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_success
|
2016-04-01 05:33:25 +08:00
|
|
|
json = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(json["user"]["has_title_badges"]).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns not found when the username doesn't exist" do
|
|
|
|
xhr :get, :show, username: 'madeuppity'
|
|
|
|
expect(response).not_to be_success
|
2014-06-19 02:40:15 +08:00
|
|
|
end
|
|
|
|
|
2016-04-01 05:33:25 +08:00
|
|
|
it 'returns not found when the user is inactive' do
|
|
|
|
inactive = Fabricate(:user, active: false)
|
|
|
|
xhr :get, :show, username: inactive.username
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).not_to be_success
|
2014-06-19 02:40:15 +08:00
|
|
|
end
|
2016-04-01 05:33:25 +08:00
|
|
|
|
|
|
|
it "raises an error on invalid access" do
|
|
|
|
Guardian.any_instance.expects(:can_see?).with(user).returns(false)
|
|
|
|
xhr :get, :show, username: user.username
|
|
|
|
expect(response).to be_forbidden
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "user profile views" do
|
|
|
|
let(:other_user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
it "should track a user profile view for a signed in user" do
|
|
|
|
UserProfileView.expects(:add).with(other_user.user_profile.id, request.remote_ip, user.id)
|
|
|
|
xhr :get, :show, username: other_user.username
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not track a user profile view for a user viewing his own profile" do
|
|
|
|
UserProfileView.expects(:add).never
|
|
|
|
xhr :get, :show, username: user.username
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should track a user profile view for an anon user" do
|
|
|
|
UserProfileView.expects(:add).with(other_user.user_profile.id, request.remote_ip, nil)
|
|
|
|
xhr :get, :show, username: other_user.username
|
|
|
|
end
|
|
|
|
|
|
|
|
it "skips tracking" do
|
|
|
|
UserProfileView.expects(:add).never
|
|
|
|
xhr :get, :show, { username: user.username, skip_track_visit: true }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "fetching a user by external_id" do
|
|
|
|
before { user.create_single_sign_on_record(external_id: '997', last_payload: '') }
|
|
|
|
|
|
|
|
it "returns fetch for a matching external_id" do
|
|
|
|
xhr :get, :show, external_id: '997'
|
|
|
|
expect(response).to be_success
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns not found when external_id doesn't match" do
|
|
|
|
xhr :get, :show, external_id: '99'
|
|
|
|
expect(response).not_to be_success
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-19 02:40:15 +08:00
|
|
|
end
|
2016-04-01 05:33:25 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '.user_preferences_redirect' do
|
|
|
|
it 'requires the user to be logged in' do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect { get :user_preferences_redirect }.to raise_error(Discourse::NotLoggedIn)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "redirects to their profile when logged in" do
|
|
|
|
user = log_in
|
|
|
|
get :user_preferences_redirect
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to redirect_to("/users/#{user.username_lower}/preferences")
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.activate_account' do
|
2014-07-16 02:07:19 +08:00
|
|
|
before do
|
|
|
|
UsersController.any_instance.stubs(:honeypot_or_challenge_fails?).returns(false)
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
context 'invalid token' do
|
|
|
|
|
|
|
|
it 'return success' do
|
2015-02-20 07:28:38 +08:00
|
|
|
EmailToken.expects(:confirm).with('asdfasdf').returns(nil)
|
|
|
|
put :perform_account_activation, token: 'asdfasdf'
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_success
|
|
|
|
expect(flash[:error]).to be_present
|
2013-02-07 19:50:59 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'valid token' do
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
context 'welcome message' do
|
|
|
|
before do
|
|
|
|
EmailToken.expects(:confirm).with('asdfasdf').returns(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'enqueues a welcome message if the user object indicates so' do
|
2013-02-07 19:50:59 +08:00
|
|
|
user.send_welcome_message = true
|
2013-02-06 03:16:51 +08:00
|
|
|
user.expects(:enqueue_welcome_message).with('welcome_user')
|
2014-07-15 00:25:42 +08:00
|
|
|
put :perform_account_activation, token: 'asdfasdf'
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't enqueue the welcome message if the object returns false" do
|
2013-02-07 19:50:59 +08:00
|
|
|
user.send_welcome_message = false
|
2013-02-06 03:16:51 +08:00
|
|
|
user.expects(:enqueue_welcome_message).with('welcome_user').never
|
2014-07-15 00:25:42 +08:00
|
|
|
put :perform_account_activation, token: 'asdfasdf'
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2014-07-16 02:07:19 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-07-16 02:07:19 +08:00
|
|
|
context "honeypot" do
|
|
|
|
it "raises an error if the honeypot is invalid" do
|
|
|
|
UsersController.any_instance.stubs(:honeypot_or_challenge_fails?).returns(true)
|
|
|
|
put :perform_account_activation, token: 'asdfasdf'
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).not_to be_success
|
2014-07-16 02:07:19 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-03-26 12:39:44 +08:00
|
|
|
context 'response' do
|
2013-02-06 03:16:51 +08:00
|
|
|
before do
|
2013-04-04 00:23:28 +08:00
|
|
|
Guardian.any_instance.expects(:can_access_forum?).returns(true)
|
2013-02-06 03:16:51 +08:00
|
|
|
EmailToken.expects(:confirm).with('asdfasdf').returns(user)
|
2014-07-15 00:25:42 +08:00
|
|
|
put :perform_account_activation, token: 'asdfasdf'
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns success' do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_success
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't set an error" do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(flash[:error]).to be_blank
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'logs in as the user' do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(session[:current_user_id]).to be_present
|
2013-02-07 19:50:59 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
it "doesn't set @needs_approval" do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(assigns[:needs_approval]).to be_blank
|
2013-02-07 19:50:59 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-04-04 00:23:28 +08:00
|
|
|
context 'user is not approved' do
|
2013-02-06 03:16:51 +08:00
|
|
|
before do
|
2013-04-04 00:23:28 +08:00
|
|
|
Guardian.any_instance.expects(:can_access_forum?).returns(false)
|
2013-02-06 03:16:51 +08:00
|
|
|
EmailToken.expects(:confirm).with('asdfasdf').returns(user)
|
2014-07-15 00:25:42 +08:00
|
|
|
put :perform_account_activation, token: 'asdfasdf'
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns success' do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_success
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets @needs_approval' do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(assigns[:needs_approval]).to be_present
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't set an error" do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(flash[:error]).to be_blank
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't log the user in" do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(session[:current_user_id]).to be_blank
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.password_reset' do
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
|
2013-07-16 00:12:54 +08:00
|
|
|
context "you can view it even if login is required" do
|
|
|
|
it "returns success" do
|
2014-07-02 11:06:55 +08:00
|
|
|
SiteSetting.login_required = true
|
|
|
|
get :password_reset, token: 'asdfasdf'
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_success
|
2013-07-16 00:12:54 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-26 03:30:52 +08:00
|
|
|
context 'missing token' do
|
2013-02-06 03:16:51 +08:00
|
|
|
before do
|
2014-07-02 11:06:55 +08:00
|
|
|
get :password_reset, token: SecureRandom.hex
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-07-02 11:06:55 +08:00
|
|
|
it 'disallows login' do
|
2015-02-20 07:28:38 +08:00
|
|
|
expect(assigns[:error]).to be_present
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(session[:current_user_id]).to be_blank
|
|
|
|
expect(assigns[:invalid_token]).to eq(nil)
|
|
|
|
expect(response).to be_success
|
2013-02-07 19:50:59 +08:00
|
|
|
end
|
2014-08-26 03:30:52 +08:00
|
|
|
end
|
2014-07-02 11:06:55 +08:00
|
|
|
|
2014-08-26 03:30:52 +08:00
|
|
|
context 'invalid token' do
|
|
|
|
before do
|
|
|
|
get :password_reset, token: "evil_trout!"
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'disallows login' do
|
2015-02-20 07:28:38 +08:00
|
|
|
expect(assigns[:error]).to be_present
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(session[:current_user_id]).to be_blank
|
|
|
|
expect(assigns[:invalid_token]).to eq(true)
|
|
|
|
expect(response).to be_success
|
2014-08-26 03:30:52 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'valid token' do
|
|
|
|
it 'returns success' do
|
2015-04-15 06:57:43 +08:00
|
|
|
user = Fabricate(:user, auth_token: SecureRandom.hex(16))
|
2014-07-02 11:06:55 +08:00
|
|
|
token = user.email_tokens.create(email: user.email).token
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2015-04-15 06:57:43 +08:00
|
|
|
old_token = user.auth_token
|
|
|
|
|
2014-07-02 11:06:55 +08:00
|
|
|
get :password_reset, token: token
|
2016-04-01 05:33:25 +08:00
|
|
|
put :password_reset, token: token, password: 'hg9ow8yhg98o'
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_success
|
2015-02-20 07:28:38 +08:00
|
|
|
expect(assigns[:error]).to be_blank
|
2015-04-15 06:57:43 +08:00
|
|
|
|
|
|
|
user.reload
|
|
|
|
expect(user.auth_token).to_not eq old_token
|
|
|
|
expect(user.auth_token.length).to eq 32
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2016-04-01 05:33:25 +08:00
|
|
|
|
|
|
|
it "doesn't invalidate the token when loading the page" do
|
|
|
|
user = Fabricate(:user, auth_token: SecureRandom.hex(16))
|
|
|
|
email_token = user.email_tokens.create(email: user.email)
|
|
|
|
|
|
|
|
get :password_reset, token: email_token.token
|
|
|
|
|
|
|
|
email_token.reload
|
|
|
|
expect(email_token.confirmed).to eq(false)
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'submit change' do
|
2014-08-26 03:30:52 +08:00
|
|
|
let(:token) { EmailToken.generate_token }
|
2013-02-06 03:16:51 +08:00
|
|
|
before do
|
2014-08-26 03:30:52 +08:00
|
|
|
EmailToken.expects(:confirm).with(token).returns(user)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-09-12 03:22:11 +08:00
|
|
|
it "fails when the password is blank" do
|
|
|
|
put :password_reset, token: token, password: ''
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(assigns(:user).errors).to be_present
|
|
|
|
expect(session[:current_user_id]).to be_blank
|
2014-09-12 03:22:11 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails when the password is too long" do
|
|
|
|
put :password_reset, token: token, password: ('x' * (User.max_password_length + 1))
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(assigns(:user).errors).to be_present
|
|
|
|
expect(session[:current_user_id]).to be_blank
|
2014-09-12 03:22:11 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
it "logs in the user" do
|
2016-04-01 05:33:25 +08:00
|
|
|
put :password_reset, token: token, password: 'ksjafh928r'
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(assigns(:user).errors).to be_blank
|
|
|
|
expect(session[:current_user_id]).to be_present
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't log in the user when not approved" do
|
|
|
|
SiteSetting.expects(:must_approve_users?).returns(true)
|
2016-04-01 05:33:25 +08:00
|
|
|
put :password_reset, token: token, password: 'ksjafh928r'
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(assigns(:user).errors).to be_blank
|
|
|
|
expect(session[:current_user_id]).to be_blank
|
2013-02-07 19:50:59 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-01 05:33:25 +08:00
|
|
|
describe '.confirm_email_token' do
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
it "token doesn't match any records" do
|
|
|
|
email_token = user.email_tokens.create(email: user.email)
|
|
|
|
get :confirm_email_token, token: SecureRandom.hex, format: :json
|
|
|
|
expect(response).to be_success
|
|
|
|
expect(email_token.reload.confirmed).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "token matches" do
|
|
|
|
email_token = user.email_tokens.create(email: user.email)
|
|
|
|
get :confirm_email_token, token: email_token.token, format: :json
|
|
|
|
expect(response).to be_success
|
|
|
|
expect(email_token.reload.confirmed).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-27 18:59:48 +08:00
|
|
|
describe '.admin_login' do
|
|
|
|
let(:admin) { Fabricate(:admin) }
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
context 'enqueues mail' do
|
|
|
|
it 'enqueues mail with admin email and sso enabled' do
|
|
|
|
Jobs.expects(:enqueue).with(:user_email, has_entries(type: :admin_login, user_id: admin.id))
|
|
|
|
put :admin_login, email: admin.email
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'logs in admin' do
|
|
|
|
it 'does not log in admin with invalid token' do
|
|
|
|
SiteSetting.enable_sso = true
|
|
|
|
get :admin_login, token: "invalid"
|
|
|
|
expect(session[:current_user_id]).to be_blank
|
|
|
|
end
|
|
|
|
|
2015-06-05 16:58:20 +08:00
|
|
|
it 'does log in admin with valid token and SSO disabled' do
|
2015-04-27 18:59:48 +08:00
|
|
|
SiteSetting.enable_sso = false
|
|
|
|
token = admin.email_tokens.create(email: admin.email).token
|
|
|
|
|
|
|
|
get :admin_login, token: token
|
|
|
|
expect(response).to redirect_to('/')
|
2015-06-05 16:58:20 +08:00
|
|
|
expect(session[:current_user_id]).to eq(admin.id)
|
2015-04-27 18:59:48 +08:00
|
|
|
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
|
|
|
|
|
|
|
|
get :admin_login, token: token
|
|
|
|
expect(response).to redirect_to('/')
|
|
|
|
expect(session[:current_user_id]).to eq(admin.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-07 16:02:10 +08:00
|
|
|
describe '#toggle_anon' do
|
|
|
|
it 'allows you to toggle anon if enabled' do
|
|
|
|
SiteSetting.allow_anonymous_posting = true
|
|
|
|
|
|
|
|
user = log_in
|
|
|
|
user.trust_level = 1
|
|
|
|
user.save
|
|
|
|
|
|
|
|
post :toggle_anon
|
|
|
|
expect(response).to be_success
|
|
|
|
expect(session[:current_user_id]).to eq(AnonymousShadowCreator.get(user).id)
|
|
|
|
|
|
|
|
post :toggle_anon
|
|
|
|
expect(response).to be_success
|
|
|
|
expect(session[:current_user_id]).to eq(user.id)
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-13 06:37:38 +08:00
|
|
|
describe '#create' do
|
2014-07-16 02:07:19 +08:00
|
|
|
|
2013-02-07 19:50:59 +08:00
|
|
|
before do
|
2014-07-16 02:07:19 +08:00
|
|
|
UsersController.any_instance.stubs(:honeypot_value).returns(nil)
|
|
|
|
UsersController.any_instance.stubs(:challenge_value).returns(nil)
|
2014-07-15 03:42:14 +08:00
|
|
|
SiteSetting.stubs(:allow_new_registrations).returns(true)
|
2013-02-06 03:16:51 +08:00
|
|
|
@user = Fabricate.build(:user)
|
2013-02-07 19:50:59 +08:00
|
|
|
@user.password = "strongpassword"
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2016-08-05 23:57:13 +08:00
|
|
|
let(:post_user_params) do
|
|
|
|
{ name: @user.name,
|
2013-06-06 08:09:22 +08:00
|
|
|
username: @user.username,
|
|
|
|
password: "strongpassword",
|
2016-08-05 23:57:13 +08:00
|
|
|
email: @user.email }
|
|
|
|
end
|
|
|
|
|
|
|
|
def post_user
|
|
|
|
xhr :post, :create, post_user_params
|
2013-06-06 08:09:22 +08:00
|
|
|
end
|
|
|
|
|
2016-04-01 05:33:25 +08:00
|
|
|
context 'when creating a user' do
|
|
|
|
it 'sets the user locale to I18n.locale' do
|
|
|
|
SiteSetting.stubs(:default_locale).returns('en')
|
|
|
|
I18n.stubs(:locale).returns(:fr)
|
|
|
|
post_user
|
|
|
|
expect(User.find_by(username: @user.username).locale).to eq('fr')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
context 'when creating a non active user (unconfirmed email)' do
|
2014-03-26 12:39:44 +08:00
|
|
|
|
|
|
|
it 'returns a 500 when local logins are disabled' do
|
|
|
|
SiteSetting.expects(:enable_local_logins).returns(false)
|
2013-06-06 08:09:22 +08:00
|
|
|
post_user
|
2014-03-26 12:39:44 +08:00
|
|
|
|
|
|
|
expect(response.status).to eq(500)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-07-15 03:42:14 +08:00
|
|
|
it 'returns an error when new registrations are disabled' do
|
|
|
|
SiteSetting.stubs(:allow_new_registrations).returns(false)
|
|
|
|
post_user
|
|
|
|
json = JSON.parse(response.body)
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(json['success']).to eq(false)
|
|
|
|
expect(json['message']).to be_present
|
2014-07-15 03:42:14 +08:00
|
|
|
end
|
|
|
|
|
2014-03-26 12:39:44 +08:00
|
|
|
it 'creates a user correctly' do
|
|
|
|
Jobs.expects(:enqueue).with(:user_email, has_entries(type: :signup))
|
2013-02-06 03:16:51 +08:00
|
|
|
User.any_instance.expects(:enqueue_welcome_message).with('welcome_user').never
|
2013-06-06 08:09:22 +08:00
|
|
|
|
2013-06-06 09:19:27 +08:00
|
|
|
post_user
|
2014-03-26 12:39:44 +08:00
|
|
|
|
2014-09-25 23:44:48 +08:00
|
|
|
expect(JSON.parse(response.body)['active']).to be_falsey
|
2014-10-02 01:33:49 +08:00
|
|
|
|
2014-11-05 04:47:32 +08:00
|
|
|
# should save user_created_message in session
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(session["user_created_message"]).to be_present
|
2013-02-07 19:50:59 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-06-06 09:19:27 +08:00
|
|
|
context "and 'must approve users' site setting is enabled" do
|
|
|
|
before { SiteSetting.expects(:must_approve_users).returns(true) }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-06-06 09:19:27 +08:00
|
|
|
it 'does not enqueue an email' do
|
|
|
|
Jobs.expects(:enqueue).never
|
|
|
|
post_user
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not login the user' do
|
|
|
|
post_user
|
|
|
|
expect(session[:current_user_id]).to be_blank
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'indicates the user is not active in the response' do
|
|
|
|
post_user
|
2014-09-25 23:44:48 +08:00
|
|
|
expect(JSON.parse(response.body)['active']).to be_falsey
|
2013-06-06 09:19:27 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "shows the 'waiting approval' message" do
|
|
|
|
post_user
|
2014-09-25 23:44:48 +08:00
|
|
|
expect(JSON.parse(response.body)['message']).to eq(I18n.t 'login.wait_approval')
|
2013-06-06 09:19:27 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-06-06 09:19:27 +08:00
|
|
|
end
|
|
|
|
|
2016-08-05 23:57:13 +08:00
|
|
|
context "creating as active" do
|
|
|
|
it "won't create the user as active" do
|
|
|
|
xhr :post, :create, post_user_params.merge(active: true)
|
|
|
|
expect(JSON.parse(response.body)['active']).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with a regular api key" do
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:api_key) { Fabricate(:api_key, user: user) }
|
|
|
|
|
|
|
|
it "won't create the user as active with a regular key" do
|
|
|
|
xhr :post, :create, post_user_params.merge(active: true, api_key: api_key.key)
|
|
|
|
expect(JSON.parse(response.body)['active']).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with an admin api key" do
|
|
|
|
let(:user) { Fabricate(:admin) }
|
|
|
|
let(:api_key) { Fabricate(:api_key, user: user) }
|
|
|
|
|
|
|
|
it "creates the user as active with a regular key" do
|
|
|
|
xhr :post, :create, post_user_params.merge(active: true, api_key: api_key.key)
|
|
|
|
expect(JSON.parse(response.body)['active']).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it "won't create the developer as active" do
|
|
|
|
UsernameCheckerService.expects(:is_developer?).returns(true)
|
|
|
|
|
|
|
|
xhr :post, :create, post_user_params.merge(active: true, api_key: api_key.key)
|
|
|
|
expect(JSON.parse(response.body)['active']).to be_falsy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "creating as staged" do
|
|
|
|
it "won't create the user as staged" do
|
|
|
|
xhr :post, :create, post_user_params.merge(staged: true)
|
|
|
|
new_user = User.where(username: post_user_params[:username]).first
|
|
|
|
expect(new_user.staged?).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with a regular api key" do
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:api_key) { Fabricate(:api_key, user: user) }
|
|
|
|
|
|
|
|
it "won't create the user as staged with a regular key" do
|
|
|
|
xhr :post, :create, post_user_params.merge(staged: true, api_key: api_key.key)
|
|
|
|
new_user = User.where(username: post_user_params[:username]).first
|
|
|
|
expect(new_user.staged?).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with an admin api key" do
|
|
|
|
let(:user) { Fabricate(:admin) }
|
|
|
|
let(:api_key) { Fabricate(:api_key, user: user) }
|
|
|
|
|
|
|
|
it "creates the user as staged with a regular key" do
|
|
|
|
xhr :post, :create, post_user_params.merge(staged: true, api_key: api_key.key)
|
|
|
|
|
|
|
|
new_user = User.where(username: post_user_params[:username]).first
|
|
|
|
expect(new_user.staged?).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "won't create the developer as staged" do
|
|
|
|
UsernameCheckerService.expects(:is_developer?).returns(true)
|
|
|
|
xhr :post, :create, post_user_params.merge(staged: true, api_key: api_key.key)
|
|
|
|
|
|
|
|
new_user = User.where(username: post_user_params[:username]).first
|
|
|
|
expect(new_user.staged?).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-06 09:19:27 +08:00
|
|
|
context 'when creating an active user (confirmed email)' do
|
|
|
|
before { User.any_instance.stubs(:active?).returns(true) }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-06-06 07:50:24 +08:00
|
|
|
it 'enqueues a welcome email' do
|
2013-02-06 03:16:51 +08:00
|
|
|
User.any_instance.expects(:enqueue_welcome_message).with('welcome_user')
|
2013-06-06 08:09:22 +08:00
|
|
|
post_user
|
2014-10-02 01:33:49 +08:00
|
|
|
|
2014-11-05 04:47:32 +08:00
|
|
|
# should save user_created_message in session
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(session["user_created_message"]).to be_present
|
2013-06-06 08:09:22 +08:00
|
|
|
end
|
2013-06-06 09:19:27 +08:00
|
|
|
|
|
|
|
it "shows the 'active' message" do
|
|
|
|
User.any_instance.expects(:enqueue_welcome_message)
|
|
|
|
post_user
|
|
|
|
expect(JSON.parse(response.body)['message']).to eq(
|
|
|
|
I18n.t 'login.active'
|
|
|
|
)
|
2013-02-07 19:50:59 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
it "should be logged in" do
|
|
|
|
User.any_instance.expects(:enqueue_welcome_message)
|
2013-06-06 08:09:22 +08:00
|
|
|
post_user
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(session[:current_user_id]).to be_present
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-06-06 09:19:27 +08:00
|
|
|
it 'indicates the user is active in the response' do
|
2013-02-06 03:16:51 +08:00
|
|
|
User.any_instance.expects(:enqueue_welcome_message)
|
2013-06-06 08:09:22 +08:00
|
|
|
post_user
|
2014-09-25 23:44:48 +08:00
|
|
|
expect(JSON.parse(response.body)['active']).to be_truthy
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-07-15 03:42:14 +08:00
|
|
|
it 'returns 500 status when new registrations are disabled' do
|
|
|
|
SiteSetting.stubs(:allow_new_registrations).returns(false)
|
|
|
|
post_user
|
|
|
|
json = JSON.parse(response.body)
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(json['success']).to eq(false)
|
|
|
|
expect(json['message']).to be_present
|
2014-07-15 03:42:14 +08:00
|
|
|
end
|
|
|
|
|
2013-04-13 06:46:55 +08:00
|
|
|
context 'authentication records for' do
|
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.expects(:must_approve_users).returns(true)
|
|
|
|
end
|
|
|
|
|
2013-08-26 09:04:16 +08:00
|
|
|
it 'should create twitter user info if required' do
|
|
|
|
SiteSetting.stubs(:enable_twitter_logins?).returns(true)
|
2013-04-13 06:46:55 +08:00
|
|
|
twitter_auth = { twitter_user_id: 42, twitter_screen_name: "bruce" }
|
2013-08-26 09:04:16 +08:00
|
|
|
auth = session[:authentication] = {}
|
|
|
|
auth[:authenticator_name] = 'twitter'
|
|
|
|
auth[:extra_data] = twitter_auth
|
2013-04-13 06:46:55 +08:00
|
|
|
TwitterUserInfo.expects(:create)
|
|
|
|
|
2013-06-06 08:09:22 +08:00
|
|
|
post_user
|
2013-04-13 06:46:55 +08:00
|
|
|
end
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'after success' do
|
2013-06-06 08:09:22 +08:00
|
|
|
before { post_user }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-02-07 19:50:59 +08:00
|
|
|
it 'should succeed' do
|
2015-01-10 01:04:02 +08:00
|
|
|
is_expected.to respond_with(:success)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'has the proper JSON' do
|
|
|
|
json = JSON::parse(response.body)
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(json["success"]).to eq(true)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-07 19:50:59 +08:00
|
|
|
it 'should not result in an active account' do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(User.find_by(username: @user.username).active).to eq(false)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
2013-02-07 08:25:21 +08:00
|
|
|
|
2013-06-29 02:43:35 +08:00
|
|
|
shared_examples 'honeypot fails' do
|
2013-02-07 08:25:21 +08:00
|
|
|
it 'should not create a new user' do
|
|
|
|
expect {
|
|
|
|
xhr :post, :create, create_params
|
|
|
|
}.to_not change { User.count }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not send an email' do
|
|
|
|
User.any_instance.expects(:enqueue_welcome_message).never
|
|
|
|
xhr :post, :create, create_params
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should say it was successful' do
|
|
|
|
xhr :post, :create, create_params
|
|
|
|
json = JSON::parse(response.body)
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(json["success"]).to eq(true)
|
2014-10-02 01:33:49 +08:00
|
|
|
|
|
|
|
# should not change the session
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(session["user_created_message"]).to be_blank
|
2013-02-07 08:25:21 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when honeypot value is wrong' do
|
|
|
|
before do
|
|
|
|
UsersController.any_instance.stubs(:honeypot_value).returns('abc')
|
|
|
|
end
|
2013-03-23 23:02:59 +08:00
|
|
|
let(:create_params) { {name: @user.name, username: @user.username, password: "strongpassword", email: @user.email, password_confirmation: 'wrong'} }
|
2013-06-29 02:43:35 +08:00
|
|
|
include_examples 'honeypot fails'
|
2013-02-07 08:25:21 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when challenge answer is wrong' do
|
|
|
|
before do
|
|
|
|
UsersController.any_instance.stubs(:challenge_value).returns('abc')
|
|
|
|
end
|
2013-03-23 23:02:59 +08:00
|
|
|
let(:create_params) { {name: @user.name, username: @user.username, password: "strongpassword", email: @user.email, challenge: 'abc'} }
|
2013-06-29 02:43:35 +08:00
|
|
|
include_examples 'honeypot fails'
|
2013-02-07 08:25:21 +08:00
|
|
|
end
|
2013-02-13 04:42:04 +08:00
|
|
|
|
2013-06-06 02:08:21 +08:00
|
|
|
context "when 'invite only' setting is enabled" do
|
|
|
|
before { SiteSetting.expects(:invite_only?).returns(true) }
|
|
|
|
|
|
|
|
let(:create_params) {{
|
|
|
|
name: @user.name,
|
|
|
|
username: @user.username,
|
|
|
|
password: 'strongpassword',
|
|
|
|
email: @user.email
|
|
|
|
}}
|
|
|
|
|
2013-06-29 02:43:35 +08:00
|
|
|
include_examples 'honeypot fails'
|
2013-06-06 02:08:21 +08:00
|
|
|
end
|
|
|
|
|
2013-06-29 02:43:35 +08:00
|
|
|
shared_examples 'failed signup' do
|
2013-02-13 04:42:04 +08:00
|
|
|
it 'should not create a new User' do
|
|
|
|
expect { xhr :post, :create, create_params }.to_not change { User.count }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should report failed' do
|
|
|
|
xhr :post, :create, create_params
|
|
|
|
json = JSON::parse(response.body)
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(json["success"]).not_to eq(true)
|
2014-10-29 08:06:59 +08:00
|
|
|
|
2014-10-02 01:33:49 +08:00
|
|
|
# should not change the session
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(session["user_created_message"]).to be_blank
|
2013-02-13 04:42:04 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when password is blank' do
|
2013-03-23 23:02:59 +08:00
|
|
|
let(:create_params) { {name: @user.name, username: @user.username, password: "", email: @user.email} }
|
2013-06-29 02:43:35 +08:00
|
|
|
include_examples 'failed signup'
|
2013-02-13 04:42:04 +08:00
|
|
|
end
|
|
|
|
|
2014-09-12 03:22:11 +08:00
|
|
|
context 'when password is too long' do
|
|
|
|
let(:create_params) { {name: @user.name, username: @user.username, password: "x" * (User.max_password_length + 1), email: @user.email} }
|
|
|
|
include_examples 'failed signup'
|
|
|
|
end
|
|
|
|
|
2013-02-13 04:42:04 +08:00
|
|
|
context 'when password param is missing' do
|
2013-03-23 23:02:59 +08:00
|
|
|
let(:create_params) { {name: @user.name, username: @user.username, email: @user.email} }
|
2013-06-29 02:43:35 +08:00
|
|
|
include_examples 'failed signup'
|
2013-03-08 03:56:28 +08:00
|
|
|
end
|
|
|
|
|
2015-07-02 04:44:53 +08:00
|
|
|
context 'with a reserved username' do
|
|
|
|
let(:create_params) { {name: @user.name, username: 'Reserved', email: @user.email, password: "x" * 20} }
|
|
|
|
before { SiteSetting.reserved_usernames = 'a|reserved|b' }
|
|
|
|
after { SiteSetting.reserved_usernames = nil }
|
|
|
|
include_examples 'failed signup'
|
|
|
|
end
|
|
|
|
|
2013-04-13 06:46:55 +08:00
|
|
|
context 'when an Exception is raised' do
|
|
|
|
[ ActiveRecord::StatementInvalid,
|
|
|
|
RestClient::Forbidden ].each do |exception|
|
|
|
|
before { User.any_instance.stubs(:save).raises(exception) }
|
|
|
|
|
|
|
|
let(:create_params) {
|
|
|
|
{ name: @user.name, username: @user.username,
|
|
|
|
password: "strongpassword", email: @user.email}
|
|
|
|
}
|
|
|
|
|
2013-06-29 02:43:35 +08:00
|
|
|
include_examples 'failed signup'
|
2013-03-08 03:56:28 +08:00
|
|
|
end
|
2013-02-13 04:42:04 +08:00
|
|
|
end
|
2013-04-13 06:46:55 +08:00
|
|
|
|
2014-09-27 02:48:34 +08:00
|
|
|
context "with custom fields" do
|
|
|
|
let!(:user_field) { Fabricate(:user_field) }
|
|
|
|
let!(:another_field) { Fabricate(:user_field) }
|
2014-10-09 02:38:18 +08:00
|
|
|
let!(:optional_field) { Fabricate(:user_field, required: false) }
|
2014-09-27 02:48:34 +08:00
|
|
|
|
|
|
|
context "without a value for the fields" do
|
|
|
|
let(:create_params) { {name: @user.name, password: 'watwatwat', username: @user.username, email: @user.email} }
|
|
|
|
include_examples 'failed signup'
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with values for the fields" do
|
|
|
|
let(:create_params) { {
|
|
|
|
name: @user.name,
|
2016-04-01 05:33:25 +08:00
|
|
|
password: 'watwatwatwat',
|
2014-09-27 02:48:34 +08:00
|
|
|
username: @user.username,
|
|
|
|
email: @user.email,
|
|
|
|
user_fields: {
|
|
|
|
user_field.id.to_s => 'value1',
|
|
|
|
another_field.id.to_s => 'value2',
|
|
|
|
}
|
|
|
|
} }
|
|
|
|
|
2014-10-09 02:38:18 +08:00
|
|
|
it "should succeed without the optional field" do
|
2014-09-27 02:48:34 +08:00
|
|
|
xhr :post, :create, create_params
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_success
|
2014-09-27 02:48:34 +08:00
|
|
|
inserted = User.where(email: @user.email).first
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(inserted).to be_present
|
|
|
|
expect(inserted.custom_fields).to be_present
|
|
|
|
expect(inserted.custom_fields["user_field_#{user_field.id}"]).to eq('value1')
|
|
|
|
expect(inserted.custom_fields["user_field_#{another_field.id}"]).to eq('value2')
|
|
|
|
expect(inserted.custom_fields["user_field_#{optional_field.id}"]).to be_blank
|
2014-10-09 02:38:18 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should succeed with the optional field" do
|
|
|
|
create_params[:user_fields][optional_field.id.to_s] = 'value3'
|
|
|
|
xhr :post, :create, create_params.merge(create_params)
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_success
|
2014-10-09 02:38:18 +08:00
|
|
|
inserted = User.where(email: @user.email).first
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(inserted).to be_present
|
|
|
|
expect(inserted.custom_fields).to be_present
|
|
|
|
expect(inserted.custom_fields["user_field_#{user_field.id}"]).to eq('value1')
|
|
|
|
expect(inserted.custom_fields["user_field_#{another_field.id}"]).to eq('value2')
|
|
|
|
expect(inserted.custom_fields["user_field_#{optional_field.id}"]).to eq('value3')
|
2014-09-27 02:48:34 +08:00
|
|
|
end
|
|
|
|
|
2015-02-24 02:02:30 +08:00
|
|
|
it "trims excessively long fields" do
|
|
|
|
create_params[:user_fields][optional_field.id.to_s] = ('x' * 3000)
|
|
|
|
xhr :post, :create, create_params.merge(create_params)
|
|
|
|
expect(response).to be_success
|
|
|
|
inserted = User.where(email: @user.email).first
|
|
|
|
|
|
|
|
val = inserted.custom_fields["user_field_#{optional_field.id}"]
|
|
|
|
expect(val.length).to eq(UserField.max_length)
|
|
|
|
end
|
2014-09-27 02:48:34 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-27 17:48:27 +08:00
|
|
|
context "with only optional custom fields" do
|
|
|
|
let!(:user_field) { Fabricate(:user_field, required: false) }
|
|
|
|
|
|
|
|
context "without values for the fields" do
|
|
|
|
let(:create_params) { {
|
|
|
|
name: @user.name,
|
2016-04-01 05:33:25 +08:00
|
|
|
password: 'watwatwatwat',
|
2015-01-27 17:48:27 +08:00
|
|
|
username: @user.username,
|
|
|
|
email: @user.email,
|
|
|
|
} }
|
|
|
|
|
|
|
|
it "should succeed" do
|
|
|
|
xhr :post, :create, create_params
|
|
|
|
expect(response).to be_success
|
|
|
|
inserted = User.where(email: @user.email).first
|
|
|
|
expect(inserted).to be_present
|
|
|
|
expect(inserted.custom_fields).not_to be_present
|
|
|
|
expect(inserted.custom_fields["user_field_#{user_field.id}"]).to be_blank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-01 05:33:25 +08:00
|
|
|
context "when taking over a staged account" do
|
|
|
|
let!(:staged) { Fabricate(:staged, email: "staged@account.com") }
|
|
|
|
|
|
|
|
it "succeeds" do
|
|
|
|
xhr :post, :create, email: staged.email, username: "zogstrip", password: "P4ssw0rd$$"
|
|
|
|
result = ::JSON.parse(response.body)
|
|
|
|
expect(result["success"]).to eq(true)
|
|
|
|
expect(User.find_by(email: staged.email).staged).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context '.username' do
|
|
|
|
it 'raises an error when not logged in' do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect { xhr :put, :username, username: 'somename' }.to raise_error(Discourse::NotLoggedIn)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'while logged in' do
|
2015-01-17 06:30:46 +08:00
|
|
|
let(:old_username) { "OrigUsrname" }
|
|
|
|
let(:new_username) { "#{old_username}1234" }
|
|
|
|
let(:user) { Fabricate(:user, username: old_username) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
user.username = old_username
|
|
|
|
log_in_user(user)
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
it 'raises an error without a new_username param' do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect { xhr :put, :username, username: user.username }.to raise_error(ActionController::ParameterMissing)
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(user.reload.username).to eq(old_username)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-08-13 02:54:52 +08:00
|
|
|
it 'raises an error when you don\'t have permission to change the username' do
|
|
|
|
Guardian.any_instance.expects(:can_edit_username?).with(user).returns(false)
|
2013-02-06 03:16:51 +08:00
|
|
|
xhr :put, :username, username: user.username, new_username: new_username
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_forbidden
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(user.reload.username).to eq(old_username)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2015-01-17 06:30:46 +08:00
|
|
|
# Bad behavior, this should give a real JSON error, not an InvalidParameters
|
2013-02-06 03:16:51 +08:00
|
|
|
it 'raises an error when change_username fails' do
|
2015-01-17 06:30:46 +08:00
|
|
|
User.any_instance.expects(:save).returns(false)
|
2015-01-10 01:04:02 +08:00
|
|
|
expect { xhr :put, :username, username: user.username, new_username: new_username }.to raise_error(Discourse::InvalidParameters)
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(user.reload.username).to eq(old_username)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2015-01-17 06:30:46 +08:00
|
|
|
it 'should succeed in normal circumstances' do
|
2013-02-06 03:16:51 +08:00
|
|
|
xhr :put, :username, username: user.username, new_username: new_username
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(response).to be_success
|
|
|
|
expect(user.reload.username).to eq(new_username)
|
2015-01-17 06:30:46 +08:00
|
|
|
end
|
|
|
|
|
2015-02-26 20:05:20 +08:00
|
|
|
skip 'should fail if the user is old', 'ensure_can_edit_username! is not throwing' do
|
2015-01-17 06:30:46 +08:00
|
|
|
# Older than the change period and >1 post
|
|
|
|
user.created_at = Time.now - (SiteSetting.username_change_period + 1).days
|
|
|
|
user.stubs(:post_count).returns(200)
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(Guardian.new(user).can_edit_username?(user)).to eq(false)
|
2015-01-17 06:30:46 +08:00
|
|
|
|
|
|
|
xhr :put, :username, username: user.username, new_username: new_username
|
|
|
|
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(response).to be_forbidden
|
|
|
|
expect(user.reload.username).to eq(old_username)
|
2015-01-17 06:30:46 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should create a staff action log when a staff member changes the username' do
|
|
|
|
acting_user = Fabricate(:admin)
|
|
|
|
log_in_user(acting_user)
|
|
|
|
xhr :put, :username, username: user.username, new_username: new_username
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(response).to be_success
|
|
|
|
expect(UserHistory.where(action: UserHistory.actions[:change_username], target_user_id: user.id, acting_user_id: acting_user.id)).to be_present
|
|
|
|
expect(user.reload.username).to eq(new_username)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-12-11 00:43:16 +08:00
|
|
|
it 'should return a JSON response with the updated username' do
|
|
|
|
xhr :put, :username, username: user.username, new_username: new_username
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(::JSON.parse(response.body)['username']).to eq(new_username)
|
2014-12-11 00:43:16 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context '.check_username' do
|
2013-11-20 03:15:05 +08:00
|
|
|
it 'raises an error without any parameters' do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect { xhr :get, :check_username }.to raise_error(ActionController::ParameterMissing)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-07-17 00:25:24 +08:00
|
|
|
shared_examples 'when username is unavailable' do
|
2013-02-06 03:16:51 +08:00
|
|
|
it 'should return success' do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_success
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return available as false in the JSON' do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(::JSON.parse(response.body)['available']).to eq(false)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return a suggested username' do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(::JSON.parse(response.body)['suggestion']).to be_present
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-17 00:25:24 +08:00
|
|
|
shared_examples 'when username is available' do
|
2013-02-06 03:16:51 +08:00
|
|
|
it 'should return success' do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_success
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return available in the JSON' do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(::JSON.parse(response.body)['available']).to eq(true)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-17 00:25:24 +08:00
|
|
|
it 'returns nothing when given an email param but no username' do
|
|
|
|
xhr :get, :check_username, email: 'dood@example.com'
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_success
|
2014-07-17 00:25:24 +08:00
|
|
|
end
|
2013-02-09 03:12:48 +08:00
|
|
|
|
2014-07-17 00:25:24 +08:00
|
|
|
context 'username is available' do
|
|
|
|
before do
|
|
|
|
xhr :get, :check_username, username: 'BruceWayne'
|
2013-02-09 03:12:48 +08:00
|
|
|
end
|
2014-07-17 00:25:24 +08:00
|
|
|
include_examples 'when username is available'
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-07-17 00:25:24 +08:00
|
|
|
context 'username is unavailable' do
|
|
|
|
let!(:user) { Fabricate(:user) }
|
2013-02-06 03:16:51 +08:00
|
|
|
before do
|
2014-07-17 00:25:24 +08:00
|
|
|
xhr :get, :check_username, username: user.username
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2014-07-17 00:25:24 +08:00
|
|
|
include_examples 'when username is unavailable'
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-07-17 00:25:24 +08:00
|
|
|
shared_examples 'checking an invalid username' do
|
|
|
|
it 'should return success' do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_success
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-07-17 00:25:24 +08:00
|
|
|
it 'should not return an available key' do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(::JSON.parse(response.body)['available']).to eq(nil)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-07-17 00:25:24 +08:00
|
|
|
it 'should return an error message' do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(::JSON.parse(response.body)['errors']).not_to be_empty
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2014-07-17 00:25:24 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-07-17 00:25:24 +08:00
|
|
|
context 'has invalid characters' do
|
|
|
|
before do
|
|
|
|
xhr :get, :check_username, username: 'bad username'
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2014-07-17 00:25:24 +08:00
|
|
|
include_examples 'checking an invalid username'
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-07-17 00:25:24 +08:00
|
|
|
it 'should return the invalid characters message' do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(::JSON.parse(response.body)['errors']).to include(I18n.t(:'user.username.characters'))
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-17 00:25:24 +08:00
|
|
|
context 'is too long' do
|
2013-02-06 03:16:51 +08:00
|
|
|
before do
|
2014-07-17 00:25:24 +08:00
|
|
|
xhr :get, :check_username, username: generate_username(User.username_length.last + 1)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2014-07-17 00:25:24 +08:00
|
|
|
include_examples 'checking an invalid username'
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-07-17 00:25:24 +08:00
|
|
|
it 'should return the "too long" message' do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(::JSON.parse(response.body)['errors']).to include(I18n.t(:'user.username.long', max: User.username_length.end))
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
2013-06-29 04:21:46 +08:00
|
|
|
|
|
|
|
describe 'different case of existing username' do
|
|
|
|
context "it's my username" do
|
|
|
|
let!(:user) { Fabricate(:user, username: 'hansolo') }
|
|
|
|
before do
|
|
|
|
log_in_user(user)
|
|
|
|
xhr :get, :check_username, username: 'HanSolo'
|
|
|
|
end
|
2014-07-17 00:25:24 +08:00
|
|
|
include_examples 'when username is available'
|
2013-06-29 04:21:46 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context "it's someone else's username" do
|
|
|
|
let!(:user) { Fabricate(:user, username: 'hansolo') }
|
|
|
|
before do
|
|
|
|
log_in
|
|
|
|
xhr :get, :check_username, username: 'HanSolo'
|
|
|
|
end
|
2014-07-17 00:25:24 +08:00
|
|
|
include_examples 'when username is unavailable'
|
2013-06-29 04:21:46 +08:00
|
|
|
end
|
2013-07-31 02:13:56 +08:00
|
|
|
|
|
|
|
context "an admin changing it for someone else" do
|
|
|
|
let!(:user) { Fabricate(:user, username: 'hansolo') }
|
|
|
|
before do
|
|
|
|
log_in_user(Fabricate(:admin))
|
|
|
|
xhr :get, :check_username, username: 'HanSolo', for_user_id: user.id
|
|
|
|
end
|
2014-07-17 00:25:24 +08:00
|
|
|
include_examples 'when username is available'
|
2013-07-31 02:13:56 +08:00
|
|
|
end
|
2013-06-29 04:21:46 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-11-09 03:11:41 +08:00
|
|
|
describe '#invited' do
|
2013-02-06 03:16:51 +08:00
|
|
|
it 'returns success' do
|
2013-11-09 03:11:41 +08:00
|
|
|
user = Fabricate(:user)
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
xhr :get, :invited, username: user.username
|
2013-11-09 03:11:41 +08:00
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'filters by email' do
|
|
|
|
inviter = Fabricate(:user)
|
|
|
|
invitee = Fabricate(:user)
|
2014-09-25 15:46:51 +08:00
|
|
|
_invite = Fabricate(
|
2013-11-09 03:11:41 +08:00
|
|
|
:invite,
|
|
|
|
email: 'billybob@example.com',
|
|
|
|
invited_by: inviter,
|
|
|
|
user: invitee
|
|
|
|
)
|
|
|
|
Fabricate(
|
|
|
|
:invite,
|
|
|
|
email: 'jimtom@example.com',
|
|
|
|
invited_by: inviter,
|
|
|
|
user: invitee
|
|
|
|
)
|
|
|
|
|
2015-07-11 20:09:12 +08:00
|
|
|
xhr :get, :invited, username: inviter.username, search: 'billybob'
|
2013-11-09 03:11:41 +08:00
|
|
|
|
2014-03-22 02:13:04 +08:00
|
|
|
invites = JSON.parse(response.body)['invites']
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(invites.size).to eq(1)
|
2013-11-09 03:11:41 +08:00
|
|
|
expect(invites.first).to include('email' => 'billybob@example.com')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'filters by username' do
|
|
|
|
inviter = Fabricate(:user)
|
|
|
|
invitee = Fabricate(:user, username: 'billybob')
|
2014-09-25 15:46:51 +08:00
|
|
|
_invite = Fabricate(
|
2013-11-09 03:11:41 +08:00
|
|
|
:invite,
|
|
|
|
invited_by: inviter,
|
|
|
|
email: 'billybob@example.com',
|
|
|
|
user: invitee
|
|
|
|
)
|
|
|
|
Fabricate(
|
|
|
|
:invite,
|
|
|
|
invited_by: inviter,
|
|
|
|
user: Fabricate(:user, username: 'jimtom')
|
|
|
|
)
|
|
|
|
|
2015-07-11 20:09:12 +08:00
|
|
|
xhr :get, :invited, username: inviter.username, search: 'billybob'
|
2013-11-09 03:11:41 +08:00
|
|
|
|
2014-03-22 02:13:04 +08:00
|
|
|
invites = JSON.parse(response.body)['invites']
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(invites.size).to eq(1)
|
2013-11-09 03:11:41 +08:00
|
|
|
expect(invites.first).to include('email' => 'billybob@example.com')
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-11-09 03:11:41 +08:00
|
|
|
context 'with guest' do
|
|
|
|
context 'with pending invites' do
|
|
|
|
it 'does not return invites' do
|
|
|
|
inviter = Fabricate(:user)
|
|
|
|
Fabricate(:invite, invited_by: inviter)
|
|
|
|
|
2015-07-11 20:09:12 +08:00
|
|
|
xhr :get, :invited, username: inviter.username, filter: 'pending'
|
2013-11-09 03:11:41 +08:00
|
|
|
|
2014-03-22 02:13:04 +08:00
|
|
|
invites = JSON.parse(response.body)['invites']
|
2013-11-09 03:11:41 +08:00
|
|
|
expect(invites).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with redeemed invites' do
|
|
|
|
it 'returns invites' do
|
|
|
|
inviter = Fabricate(:user)
|
|
|
|
invitee = Fabricate(:user)
|
|
|
|
invite = Fabricate(:invite, invited_by: inviter, user: invitee)
|
|
|
|
|
|
|
|
xhr :get, :invited, username: inviter.username
|
|
|
|
|
2014-03-22 02:13:04 +08:00
|
|
|
invites = JSON.parse(response.body)['invites']
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(invites.size).to eq(1)
|
2013-11-09 03:11:41 +08:00
|
|
|
expect(invites.first).to include('email' => invite.email)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with authenticated user' do
|
|
|
|
context 'with pending invites' do
|
|
|
|
context 'with permission to see pending invites' do
|
|
|
|
it 'returns invites' do
|
|
|
|
user = log_in
|
|
|
|
inviter = Fabricate(:user)
|
|
|
|
invite = Fabricate(:invite, invited_by: inviter)
|
|
|
|
stub_guardian(user) do |guardian|
|
2014-03-22 02:13:04 +08:00
|
|
|
guardian.stubs(:can_see_invite_details?).
|
2013-11-09 03:11:41 +08:00
|
|
|
with(inviter).returns(true)
|
|
|
|
end
|
|
|
|
|
2015-07-11 20:09:12 +08:00
|
|
|
xhr :get, :invited, username: inviter.username, filter: 'pending'
|
2013-11-09 03:11:41 +08:00
|
|
|
|
2014-03-22 02:13:04 +08:00
|
|
|
invites = JSON.parse(response.body)['invites']
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(invites.size).to eq(1)
|
2013-11-09 03:11:41 +08:00
|
|
|
expect(invites.first).to include("email" => invite.email)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without permission to see pending invites' do
|
|
|
|
it 'does not return invites' do
|
|
|
|
user = log_in
|
|
|
|
inviter = Fabricate(:user)
|
2014-09-25 15:46:51 +08:00
|
|
|
_invitee = Fabricate(:user)
|
2013-11-09 03:11:41 +08:00
|
|
|
Fabricate(:invite, invited_by: inviter)
|
|
|
|
stub_guardian(user) do |guardian|
|
2014-03-22 02:13:04 +08:00
|
|
|
guardian.stubs(:can_see_invite_details?).
|
2013-11-09 03:11:41 +08:00
|
|
|
with(inviter).returns(false)
|
|
|
|
end
|
|
|
|
|
2015-07-11 20:09:12 +08:00
|
|
|
xhr :get, :invited, username: inviter.username, filter: 'pending'
|
2013-11-09 03:11:41 +08:00
|
|
|
|
2014-03-22 02:13:04 +08:00
|
|
|
json = JSON.parse(response.body)['invites']
|
|
|
|
expect(json).to be_empty
|
2013-11-09 03:11:41 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with redeemed invites' do
|
|
|
|
it 'returns invites' do
|
2014-09-25 15:46:51 +08:00
|
|
|
_user = log_in
|
2013-11-09 03:11:41 +08:00
|
|
|
inviter = Fabricate(:user)
|
|
|
|
invitee = Fabricate(:user)
|
|
|
|
invite = Fabricate(:invite, invited_by: inviter, user: invitee)
|
|
|
|
|
|
|
|
xhr :get, :invited, username: inviter.username
|
|
|
|
|
2014-03-22 02:13:04 +08:00
|
|
|
invites = JSON.parse(response.body)['invites']
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(invites.size).to eq(1)
|
2013-11-09 03:11:41 +08:00
|
|
|
expect(invites.first).to include('email' => invite.email)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-11-02 02:12:25 +08:00
|
|
|
describe '#update' do
|
|
|
|
context 'with guest' do
|
|
|
|
it 'raises an error' do
|
2013-04-13 06:46:55 +08:00
|
|
|
expect do
|
2013-11-02 02:12:25 +08:00
|
|
|
xhr :put, :update, username: 'guest'
|
2013-04-13 06:46:55 +08:00
|
|
|
end.to raise_error(Discourse::NotLoggedIn)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-21 03:18:43 +08:00
|
|
|
context "as a staff user" do
|
|
|
|
let!(:user) { log_in(:admin) }
|
|
|
|
|
|
|
|
context "uneditable field" do
|
|
|
|
let!(:user_field) { Fabricate(:user_field, editable: false) }
|
|
|
|
|
|
|
|
it "allows staff to edit the field" do
|
|
|
|
put :update, username: user.username, name: 'Jim Tom', user_fields: { user_field.id.to_s => 'happy' }
|
|
|
|
expect(response).to be_success
|
|
|
|
expect(user.user_fields[user_field.id.to_s]).to eq('happy')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-11-02 02:12:25 +08:00
|
|
|
context 'with authenticated user' do
|
|
|
|
context 'with permission to update' do
|
2014-09-27 02:48:34 +08:00
|
|
|
let!(:user) { log_in(:user) }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-09-27 02:48:34 +08:00
|
|
|
it 'allows the update' do
|
2015-03-24 08:55:22 +08:00
|
|
|
|
|
|
|
user2 = Fabricate(:user)
|
|
|
|
user3 = Fabricate(:user)
|
|
|
|
|
|
|
|
put :update,
|
|
|
|
username: user.username,
|
|
|
|
name: 'Jim Tom',
|
|
|
|
custom_fields: {test: :it},
|
|
|
|
muted_usernames: "#{user2.username},#{user3.username}"
|
|
|
|
|
2013-11-02 02:12:25 +08:00
|
|
|
expect(response).to be_success
|
2014-06-11 13:50:37 +08:00
|
|
|
|
|
|
|
user.reload
|
|
|
|
|
|
|
|
expect(user.name).to eq 'Jim Tom'
|
|
|
|
expect(user.custom_fields['test']).to eq 'it'
|
2015-03-24 08:55:22 +08:00
|
|
|
expect(user.muted_users.pluck(:username).sort).to eq [user2.username,user3.username].sort
|
|
|
|
|
|
|
|
put :update,
|
|
|
|
username: user.username,
|
|
|
|
muted_usernames: ""
|
|
|
|
|
|
|
|
user.reload
|
|
|
|
|
|
|
|
expect(user.muted_users.pluck(:username).sort).to be_empty
|
|
|
|
|
2013-11-02 02:12:25 +08:00
|
|
|
end
|
2013-11-02 02:42:16 +08:00
|
|
|
|
2016-04-01 05:33:25 +08:00
|
|
|
context 'a locale is chosen that differs from I18n.locale' do
|
|
|
|
it "updates the user's locale" do
|
|
|
|
I18n.stubs(:locale).returns('fr')
|
|
|
|
|
|
|
|
put :update,
|
|
|
|
username: user.username,
|
|
|
|
locale: :fa_IR
|
|
|
|
|
|
|
|
expect(User.find_by(username: user.username).locale).to eq('fa_IR')
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2014-09-27 02:48:34 +08:00
|
|
|
context "with user fields" do
|
|
|
|
context "an editable field" do
|
|
|
|
let!(:user_field) { Fabricate(:user_field) }
|
2014-10-09 02:38:18 +08:00
|
|
|
let!(:optional_field) { Fabricate(:user_field, required: false ) }
|
2014-09-27 02:48:34 +08:00
|
|
|
|
|
|
|
it "should update the user field" do
|
|
|
|
put :update, username: user.username, name: 'Jim Tom', user_fields: { user_field.id.to_s => 'happy' }
|
|
|
|
expect(response).to be_success
|
|
|
|
expect(user.user_fields[user_field.id.to_s]).to eq 'happy'
|
|
|
|
end
|
|
|
|
|
|
|
|
it "cannot be updated to blank" do
|
|
|
|
put :update, username: user.username, name: 'Jim Tom', user_fields: { user_field.id.to_s => '' }
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).not_to be_success
|
|
|
|
expect(user.user_fields[user_field.id.to_s]).not_to eq('happy')
|
2014-09-27 02:48:34 +08:00
|
|
|
end
|
2015-02-24 02:02:30 +08:00
|
|
|
|
|
|
|
it "trims excessively large fields" do
|
|
|
|
put :update, username: user.username, name: 'Jim Tom', user_fields: { user_field.id.to_s => ('x' * 3000) }
|
|
|
|
expect(user.user_fields[user_field.id.to_s].size).to eq(UserField.max_length)
|
|
|
|
end
|
2014-09-27 02:48:34 +08:00
|
|
|
end
|
2013-11-02 02:42:16 +08:00
|
|
|
|
2014-09-27 02:48:34 +08:00
|
|
|
context "uneditable field" do
|
|
|
|
let!(:user_field) { Fabricate(:user_field, editable: false) }
|
|
|
|
|
|
|
|
it "does not update the user field" do
|
|
|
|
put :update, username: user.username, name: 'Jim Tom', user_fields: { user_field.id.to_s => 'happy' }
|
|
|
|
expect(response).to be_success
|
|
|
|
expect(user.user_fields[user_field.id.to_s]).to be_blank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns user JSON' do
|
2013-11-02 02:42:16 +08:00
|
|
|
put :update, username: user.username
|
|
|
|
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
expect(json['user']['id']).to eq user.id
|
|
|
|
end
|
2014-09-27 02:48:34 +08:00
|
|
|
|
2013-11-02 02:12:25 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-11-02 05:06:20 +08:00
|
|
|
context 'without permission to update' do
|
2013-11-02 02:12:25 +08:00
|
|
|
it 'does not allow the update' do
|
2013-11-02 02:42:16 +08:00
|
|
|
user = Fabricate(:user, name: 'Billy Bob')
|
|
|
|
log_in_user(user)
|
2016-04-01 05:33:25 +08:00
|
|
|
Guardian.any_instance.expects(:can_edit?).with(user).returns(false)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-11-02 02:12:25 +08:00
|
|
|
put :update, username: user.username, name: 'Jim Tom'
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-11-02 02:12:25 +08:00
|
|
|
expect(response).to be_forbidden
|
|
|
|
expect(user.reload.name).not_to eq 'Jim Tom'
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-21 01:15:58 +08:00
|
|
|
describe "badge_card" do
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:badge) { Fabricate(:badge) }
|
|
|
|
let(:user_badge) { BadgeGranter.grant(badge, user) }
|
|
|
|
|
|
|
|
it "sets the user's card image to the badge" do
|
|
|
|
log_in_user user
|
|
|
|
xhr :put, :update_card_badge, user_badge_id: user_badge.id, username: user.username
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(user.user_profile.reload.card_image_badge_id).to be_blank
|
2014-10-21 01:15:58 +08:00
|
|
|
badge.update_attributes image: "wat.com/wat.jpg"
|
|
|
|
|
|
|
|
xhr :put, :update_card_badge, user_badge_id: user_badge.id, username: user.username
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(user.user_profile.reload.card_image_badge_id).to eq(badge.id)
|
2014-10-21 01:15:58 +08:00
|
|
|
|
|
|
|
# Can set to nothing
|
|
|
|
xhr :put, :update_card_badge, username: user.username
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(user.user_profile.reload.card_image_badge_id).to be_blank
|
2014-10-21 01:15:58 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-26 02:25:29 +08:00
|
|
|
describe "badge_title" do
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:badge) { Fabricate(:badge) }
|
|
|
|
let(:user_badge) { BadgeGranter.grant(badge, user) }
|
|
|
|
|
|
|
|
it "sets the user's title to the badge name if it is titleable" do
|
|
|
|
log_in_user user
|
|
|
|
xhr :put, :badge_title, user_badge_id: user_badge.id, username: user.username
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(user.reload.title).not_to eq(badge.name)
|
2014-04-26 02:25:29 +08:00
|
|
|
badge.update_attributes allow_title: true
|
|
|
|
xhr :put, :badge_title, user_badge_id: user_badge.id, username: user.username
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(user.reload.title).to eq(badge.name)
|
|
|
|
expect(user.user_profile.badge_granted_title).to eq(true)
|
2014-10-08 07:26:18 +08:00
|
|
|
|
|
|
|
user.title = "testing"
|
|
|
|
user.save
|
|
|
|
user.user_profile.reload
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(user.user_profile.badge_granted_title).to eq(false)
|
2014-10-08 07:26:18 +08:00
|
|
|
|
2014-04-26 02:25:29 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-07 19:50:59 +08:00
|
|
|
describe "search_users" do
|
|
|
|
|
|
|
|
let(:topic) { Fabricate :topic }
|
|
|
|
let(:user) { Fabricate :user, username: "joecabot", name: "Lawrence Tierney" }
|
|
|
|
|
|
|
|
before do
|
2014-07-05 07:13:53 +08:00
|
|
|
ActiveRecord::Base.observers.enable :all
|
2013-02-07 19:50:59 +08:00
|
|
|
Fabricate :post, user: user, topic: topic
|
|
|
|
end
|
|
|
|
|
|
|
|
it "searches when provided the term only" do
|
|
|
|
xhr :post, :search_users, term: user.name.split(" ").last
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_success
|
2013-02-07 19:50:59 +08:00
|
|
|
json = JSON.parse(response.body)
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(json["users"].map { |u| u["username"] }).to include(user.username)
|
2013-02-07 19:50:59 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "searches when provided the topic only" do
|
|
|
|
xhr :post, :search_users, topic_id: topic.id
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_success
|
2013-02-07 19:50:59 +08:00
|
|
|
json = JSON.parse(response.body)
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(json["users"].map { |u| u["username"] }).to include(user.username)
|
2013-02-07 19:50:59 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "searches when provided the term and topic" do
|
|
|
|
xhr :post, :search_users, term: user.name.split(" ").last, topic_id: topic.id
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_success
|
2013-02-07 19:50:59 +08:00
|
|
|
json = JSON.parse(response.body)
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(json["users"].map { |u| u["username"] }).to include(user.username)
|
2013-02-07 19:50:59 +08:00
|
|
|
end
|
|
|
|
|
2015-04-13 23:03:13 +08:00
|
|
|
it "searches only for users who have access to private topic" do
|
|
|
|
privileged_user = Fabricate(:user, trust_level: 4, username: "joecabit", name: "Lawrence Tierney")
|
|
|
|
privileged_group = Fabricate(:group)
|
|
|
|
privileged_group.add(privileged_user)
|
|
|
|
privileged_group.save
|
|
|
|
|
|
|
|
category = Fabricate(:category)
|
|
|
|
category.set_permissions(privileged_group => :readonly)
|
|
|
|
category.save
|
|
|
|
|
|
|
|
private_topic = Fabricate(:topic, category: category)
|
|
|
|
|
|
|
|
xhr :post, :search_users, term: user.name.split(" ").last, topic_id: private_topic.id, topic_allowed_users: "true"
|
|
|
|
expect(response).to be_success
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
expect(json["users"].map { |u| u["username"] }).to_not include(user.username)
|
|
|
|
expect(json["users"].map { |u| u["username"] }).to include(privileged_user.username)
|
|
|
|
end
|
|
|
|
|
2013-10-31 03:45:13 +08:00
|
|
|
context "when `enable_names` is true" do
|
|
|
|
before do
|
2014-07-05 07:13:53 +08:00
|
|
|
SiteSetting.enable_names = true
|
2013-10-31 03:45:13 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns names" do
|
|
|
|
xhr :post, :search_users, term: user.name
|
|
|
|
json = JSON.parse(response.body)
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(json["users"].map { |u| u["name"] }).to include(user.name)
|
2013-10-31 03:45:13 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when `enable_names` is false" do
|
|
|
|
before do
|
|
|
|
SiteSetting.stubs(:enable_names?).returns(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns names" do
|
|
|
|
xhr :post, :search_users, term: user.name
|
|
|
|
json = JSON.parse(response.body)
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(json["users"].map { |u| u["name"] }).not_to include(user.name)
|
2013-10-31 03:45:13 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-07 19:50:59 +08:00
|
|
|
end
|
|
|
|
|
2013-02-23 00:49:48 +08:00
|
|
|
describe 'send_activation_email' do
|
|
|
|
context 'for an existing user' do
|
2014-08-29 00:07:13 +08:00
|
|
|
let(:user) { Fabricate(:user, active: false) }
|
2013-02-23 00:49:48 +08:00
|
|
|
|
|
|
|
context 'with a valid email_token' do
|
|
|
|
it 'should send the activation email' do
|
|
|
|
Jobs.expects(:enqueue).with(:user_email, has_entries(type: :signup))
|
2013-07-06 00:26:46 +08:00
|
|
|
xhr :post, :send_activation_email, username: user.username
|
2013-02-23 00:49:48 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without an existing email_token' do
|
|
|
|
before do
|
|
|
|
user.email_tokens.each {|t| t.destroy}
|
|
|
|
user.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should generate a new token' do
|
|
|
|
expect {
|
2013-07-06 00:26:46 +08:00
|
|
|
xhr :post, :send_activation_email, username: user.username
|
2013-02-23 00:49:48 +08:00
|
|
|
}.to change{ user.email_tokens(true).count }.by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should send an email' do
|
|
|
|
Jobs.expects(:enqueue).with(:user_email, has_entries(type: :signup))
|
2013-07-06 00:26:46 +08:00
|
|
|
xhr :post, :send_activation_email, username: user.username
|
2013-02-23 00:49:48 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when username does not exist' do
|
|
|
|
it 'should not send an email' do
|
|
|
|
Jobs.expects(:enqueue).never
|
2013-07-06 00:26:46 +08:00
|
|
|
xhr :post, :send_activation_email, username: 'nopenopenopenope'
|
2013-02-23 00:49:48 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-26 17:46:43 +08:00
|
|
|
describe '.pick_avatar' do
|
2013-08-25 04:37:31 +08:00
|
|
|
|
|
|
|
it 'raises an error when not logged in' do
|
2015-09-18 01:42:44 +08:00
|
|
|
expect {
|
|
|
|
xhr :put, :pick_avatar, username: 'asdf', avatar_id: 1, type: "custom"
|
|
|
|
}.to raise_error(Discourse::NotLoggedIn)
|
2013-08-25 04:37:31 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'while logged in' do
|
|
|
|
|
|
|
|
let!(:user) { log_in }
|
2015-09-18 01:42:44 +08:00
|
|
|
let(:upload) { Fabricate(:upload) }
|
2013-08-25 04:37:31 +08:00
|
|
|
|
2015-09-18 01:42:44 +08:00
|
|
|
it "raises an error when you don't have permission to toggle the avatar" do
|
2014-05-26 17:46:43 +08:00
|
|
|
another_user = Fabricate(:user)
|
2015-09-18 01:42:44 +08:00
|
|
|
xhr :put, :pick_avatar, username: another_user.username, upload_id: upload.id, type: "custom"
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_forbidden
|
2013-08-25 04:37:31 +08:00
|
|
|
end
|
|
|
|
|
2016-04-01 05:33:25 +08:00
|
|
|
it "raises an error when sso_overrides_avatar is disabled" do
|
|
|
|
SiteSetting.stubs(:sso_overrides_avatar).returns(true)
|
|
|
|
xhr :put, :pick_avatar, username: user.username, upload_id: upload.id, type: "custom"
|
|
|
|
expect(response).to_not be_success
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error when selecting the custom/uploaded avatar and allow_uploaded_avatars is disabled" do
|
|
|
|
SiteSetting.stubs(:allow_uploaded_avatars).returns(false)
|
|
|
|
xhr :put, :pick_avatar, username: user.username, upload_id: upload.id, type: "custom"
|
|
|
|
expect(response).to_not be_success
|
|
|
|
end
|
|
|
|
|
2015-09-18 01:42:44 +08:00
|
|
|
it 'can successfully pick the system avatar' do
|
2014-05-30 12:48:07 +08:00
|
|
|
xhr :put, :pick_avatar, username: user.username
|
2015-09-18 01:42:44 +08:00
|
|
|
expect(response).to be_success
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(user.reload.uploaded_avatar_id).to eq(nil)
|
2015-09-18 01:42:44 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'can successfully pick a gravatar' do
|
|
|
|
xhr :put, :pick_avatar, username: user.username, upload_id: upload.id, type: "gravatar"
|
|
|
|
expect(response).to be_success
|
|
|
|
expect(user.reload.uploaded_avatar_id).to eq(upload.id)
|
|
|
|
expect(user.user_avatar.reload.gravatar_upload_id).to eq(upload.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can successfully pick a custom avatar' do
|
|
|
|
xhr :put, :pick_avatar, username: user.username, upload_id: upload.id, type: "custom"
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_success
|
2015-09-18 01:42:44 +08:00
|
|
|
expect(user.reload.uploaded_avatar_id).to eq(upload.id)
|
|
|
|
expect(user.user_avatar.reload.custom_upload_id).to eq(upload.id)
|
2013-08-25 04:37:31 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2014-03-26 12:39:44 +08:00
|
|
|
|
2014-06-28 02:48:39 +08:00
|
|
|
describe '.destroy_user_image' do
|
2014-03-26 12:39:44 +08:00
|
|
|
|
2014-03-01 04:12:51 +08:00
|
|
|
it 'raises an error when not logged in' do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect { xhr :delete, :destroy_user_image, type: 'profile_background', username: 'asdf' }.to raise_error(Discourse::NotLoggedIn)
|
2014-03-01 04:12:51 +08:00
|
|
|
end
|
2014-03-26 12:39:44 +08:00
|
|
|
|
2014-03-01 04:12:51 +08:00
|
|
|
context 'while logged in' do
|
|
|
|
|
|
|
|
let!(:user) { log_in }
|
|
|
|
|
|
|
|
it 'raises an error when you don\'t have permission to clear the profile background' do
|
|
|
|
Guardian.any_instance.expects(:can_edit?).with(user).returns(false)
|
2015-05-20 07:39:58 +08:00
|
|
|
xhr :delete, :destroy_user_image, username: user.username, type: 'profile_background'
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_forbidden
|
2014-03-01 04:12:51 +08:00
|
|
|
end
|
|
|
|
|
2015-05-20 07:39:58 +08:00
|
|
|
it "requires the `type` param" do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect { xhr :delete, :destroy_user_image, username: user.username }.to raise_error(ActionController::ParameterMissing)
|
2014-06-28 02:48:39 +08:00
|
|
|
end
|
|
|
|
|
2015-05-20 07:39:58 +08:00
|
|
|
it "only allows certain `types`" do
|
|
|
|
expect { xhr :delete, :destroy_user_image, username: user.username, type: 'wat' }.to raise_error(Discourse::InvalidParameters)
|
2014-06-28 02:48:39 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'can clear the profile background' do
|
2015-05-20 07:39:58 +08:00
|
|
|
xhr :delete, :destroy_user_image, type: 'profile_background', username: user.username
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(user.reload.user_profile.profile_background).to eq("")
|
|
|
|
expect(response).to be_success
|
2014-03-01 04:12:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
2014-02-14 00:42:35 +08:00
|
|
|
|
|
|
|
describe '.destroy' do
|
|
|
|
it 'raises an error when not logged in' do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect { xhr :delete, :destroy, username: 'nobody' }.to raise_error(Discourse::NotLoggedIn)
|
2014-02-14 00:42:35 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'while logged in' do
|
|
|
|
let!(:user) { log_in }
|
|
|
|
|
|
|
|
it 'raises an error when you cannot delete your account' do
|
|
|
|
Guardian.any_instance.stubs(:can_delete_user?).returns(false)
|
|
|
|
UserDestroyer.any_instance.expects(:destroy).never
|
|
|
|
xhr :delete, :destroy, username: user.username
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_forbidden
|
2014-02-14 00:42:35 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error when you try to delete someone else's account" do
|
|
|
|
UserDestroyer.any_instance.expects(:destroy).never
|
|
|
|
xhr :delete, :destroy, username: Fabricate(:user).username
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_forbidden
|
2014-02-14 00:42:35 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "deletes your account when you're allowed to" do
|
|
|
|
Guardian.any_instance.stubs(:can_delete_user?).returns(true)
|
|
|
|
UserDestroyer.any_instance.expects(:destroy).with(user, anything).returns(user)
|
|
|
|
xhr :delete, :destroy, username: user.username
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_success
|
2014-02-14 00:42:35 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-21 23:52:11 +08:00
|
|
|
describe '.my_redirect' do
|
|
|
|
|
2016-04-01 05:33:25 +08:00
|
|
|
it "redirects if the user is not logged in" do
|
2014-04-21 23:52:11 +08:00
|
|
|
get :my_redirect, path: "wat"
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).not_to be_success
|
2016-04-01 05:33:25 +08:00
|
|
|
expect(response).to be_redirect
|
2014-04-21 23:52:11 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when the user is logged in" do
|
|
|
|
let!(:user) { log_in }
|
|
|
|
|
|
|
|
it "will not redirect to an invalid path" do
|
|
|
|
get :my_redirect, path: "wat/..password.txt"
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).not_to be_redirect
|
2014-04-21 23:52:11 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "will redirect to an valid path" do
|
|
|
|
get :my_redirect, path: "preferences"
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_redirect
|
2014-04-21 23:52:11 +08:00
|
|
|
end
|
2014-06-15 01:51:06 +08:00
|
|
|
|
|
|
|
it "permits forward slashes" do
|
|
|
|
get :my_redirect, path: "activity/posts"
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_redirect
|
2014-06-15 01:51:06 +08:00
|
|
|
end
|
2014-04-21 23:52:11 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-30 04:31:05 +08:00
|
|
|
describe '.check_emails' do
|
|
|
|
|
|
|
|
it 'raises an error when not logged in' do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect { xhr :put, :check_emails, username: 'zogstrip' }.to raise_error(Discourse::NotLoggedIn)
|
2014-09-30 04:31:05 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'while logged in' do
|
|
|
|
let!(:user) { log_in }
|
|
|
|
|
|
|
|
it "raises an error when you aren't allowed to check emails" do
|
|
|
|
Guardian.any_instance.expects(:can_check_emails?).returns(false)
|
2014-09-30 04:41:54 +08:00
|
|
|
xhr :put, :check_emails, username: Fabricate(:user).username
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_forbidden
|
2014-09-30 04:31:05 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns both email and associated_accounts when you're allowed to see them" do
|
|
|
|
Guardian.any_instance.expects(:can_check_emails?).returns(true)
|
2014-09-30 04:41:54 +08:00
|
|
|
xhr :put, :check_emails, username: Fabricate(:user).username
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_success
|
2014-09-30 04:31:05 +08:00
|
|
|
json = JSON.parse(response.body)
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(json["email"]).to be_present
|
|
|
|
expect(json["associated_accounts"]).to be_present
|
2014-09-30 04:31:05 +08:00
|
|
|
end
|
|
|
|
|
2014-10-29 08:06:59 +08:00
|
|
|
it "works on inactive users" do
|
|
|
|
inactive_user = Fabricate(:user, active: false)
|
|
|
|
Guardian.any_instance.expects(:can_check_emails?).returns(true)
|
|
|
|
xhr :put, :check_emails, username: inactive_user.username
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_success
|
2014-10-29 08:06:59 +08:00
|
|
|
json = JSON.parse(response.body)
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(json["email"]).to be_present
|
|
|
|
expect(json["associated_accounts"]).to be_present
|
2014-10-29 08:06:59 +08:00
|
|
|
end
|
|
|
|
|
2014-09-30 04:31:05 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2016-04-01 05:33:25 +08:00
|
|
|
describe ".is_local_username" do
|
|
|
|
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
it "finds the user" do
|
|
|
|
xhr :get, :is_local_username, username: user.username
|
|
|
|
expect(response).to be_success
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
expect(json["valid"][0]).to eq(user.username)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "supports multiples usernames" do
|
|
|
|
xhr :get, :is_local_username, usernames: [user.username, "system"]
|
|
|
|
expect(response).to be_success
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
expect(json["valid"].size).to eq(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "never includes staged accounts" do
|
|
|
|
staged = Fabricate(:user, staged: true)
|
|
|
|
xhr :get, :is_local_username, usernames: [staged.username]
|
|
|
|
expect(response).to be_success
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
expect(json["valid"].size).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
context '#summary' do
|
|
|
|
|
|
|
|
it "generates summary info" do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
create_post(user: user)
|
|
|
|
|
|
|
|
xhr :get, :summary, username: user.username_lower
|
|
|
|
expect(response).to be_success
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(json["user_summary"]["topic_count"]).to eq(1)
|
|
|
|
expect(json["user_summary"]["post_count"]).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|