2013-02-05 14:16:51 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2013-02-07 06:50:59 -05:00
|
|
|
describe UsersController do
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-07 06:50:59 -05:00
|
|
|
describe '.show' do
|
2013-02-05 14:16:51 -05:00
|
|
|
let!(:user) { log_in }
|
|
|
|
|
|
|
|
it 'returns success' do
|
2014-07-09 01:31:49 -04:00
|
|
|
xhr :get, :show, username: user.username, format: :json
|
2013-02-05 14:16:51 -05:00
|
|
|
response.should be_success
|
2014-07-09 01:31:49 -04:00
|
|
|
json = JSON.parse(response.body)
|
|
|
|
|
|
|
|
json["user"]["has_title_badges"].should == false
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns not found when the username doesn't exist" do
|
|
|
|
xhr :get, :show, username: 'madeuppity'
|
|
|
|
response.should_not be_success
|
|
|
|
end
|
|
|
|
|
2014-08-13 13:30:25 -04:00
|
|
|
it 'returns not found when the user is inactive' do
|
|
|
|
inactive = Fabricate(:user, active: false)
|
|
|
|
xhr :get, :show, username: inactive.username
|
|
|
|
response.should_not be_success
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05: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
|
|
|
|
response.should be_forbidden
|
|
|
|
end
|
2014-06-18 14:40:15 -04:00
|
|
|
|
|
|
|
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'
|
|
|
|
response.should be_success
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns not found when external_id doesn't match" do
|
|
|
|
xhr :get, :show, external_id: '99'
|
|
|
|
response.should_not be_success
|
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '.user_preferences_redirect' do
|
|
|
|
it 'requires the user to be logged in' do
|
|
|
|
lambda { get :user_preferences_redirect }.should raise_error(Discourse::NotLoggedIn)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "redirects to their profile when logged in" do
|
|
|
|
user = log_in
|
|
|
|
get :user_preferences_redirect
|
|
|
|
response.should redirect_to("/users/#{user.username_lower}/preferences")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.authorize_email' do
|
2014-04-27 10:34:55 -04:00
|
|
|
it 'errors out for invalid tokens' do
|
|
|
|
get :authorize_email, token: 'asdfasdf'
|
|
|
|
response.should be_success
|
|
|
|
flash[:error].should be_present
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'valid token' do
|
2014-04-27 10:34:55 -04:00
|
|
|
it 'authorizes with a correct token' do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
email_token = user.email_tokens.create(email: user.email)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-04-27 10:34:55 -04:00
|
|
|
get :authorize_email, token: email_token.token
|
2013-02-05 14:16:51 -05:00
|
|
|
response.should be_success
|
|
|
|
flash[:error].should be_blank
|
|
|
|
session[:current_user_id].should be_present
|
|
|
|
end
|
2013-02-07 06:50:59 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '.activate_account' do
|
2014-07-15 14:07:19 -04:00
|
|
|
before do
|
|
|
|
UsersController.any_instance.stubs(:honeypot_or_challenge_fails?).returns(false)
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
context 'invalid token' do
|
|
|
|
before do
|
|
|
|
EmailToken.expects(:confirm).with('asdfasdf').returns(nil)
|
2014-07-14 12:25:42 -04:00
|
|
|
put :perform_account_activation, token: 'asdfasdf'
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'return success' do
|
|
|
|
response.should be_success
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets a flash error' do
|
|
|
|
flash[:error].should be_present
|
2013-02-07 06:50:59 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05: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 06:50:59 -05:00
|
|
|
user.send_welcome_message = true
|
2013-02-05 14:16:51 -05:00
|
|
|
user.expects(:enqueue_welcome_message).with('welcome_user')
|
2014-07-14 12:25:42 -04:00
|
|
|
put :perform_account_activation, token: 'asdfasdf'
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't enqueue the welcome message if the object returns false" do
|
2013-02-07 06:50:59 -05:00
|
|
|
user.send_welcome_message = false
|
2013-02-05 14:16:51 -05:00
|
|
|
user.expects(:enqueue_welcome_message).with('welcome_user').never
|
2014-07-14 12:25:42 -04:00
|
|
|
put :perform_account_activation, token: 'asdfasdf'
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2014-07-15 14:07:19 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-07-15 14:07:19 -04: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'
|
|
|
|
response.should_not be_success
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-03-26 00:39:44 -04:00
|
|
|
context 'response' do
|
2013-02-05 14:16:51 -05:00
|
|
|
before do
|
2013-04-03 12:23:28 -04:00
|
|
|
Guardian.any_instance.expects(:can_access_forum?).returns(true)
|
2013-02-05 14:16:51 -05:00
|
|
|
EmailToken.expects(:confirm).with('asdfasdf').returns(user)
|
2014-07-14 12:25:42 -04:00
|
|
|
put :perform_account_activation, token: 'asdfasdf'
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns success' do
|
|
|
|
response.should be_success
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't set an error" do
|
|
|
|
flash[:error].should be_blank
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'logs in as the user' do
|
|
|
|
session[:current_user_id].should be_present
|
2013-02-07 06:50:59 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
it "doesn't set @needs_approval" do
|
|
|
|
assigns[:needs_approval].should be_blank
|
2013-02-07 06:50:59 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-04-03 12:23:28 -04:00
|
|
|
context 'user is not approved' do
|
2013-02-05 14:16:51 -05:00
|
|
|
before do
|
2013-04-03 12:23:28 -04:00
|
|
|
Guardian.any_instance.expects(:can_access_forum?).returns(false)
|
2013-02-05 14:16:51 -05:00
|
|
|
EmailToken.expects(:confirm).with('asdfasdf').returns(user)
|
2014-07-14 12:25:42 -04:00
|
|
|
put :perform_account_activation, token: 'asdfasdf'
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns success' do
|
|
|
|
response.should be_success
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets @needs_approval' do
|
|
|
|
assigns[:needs_approval].should be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't set an error" do
|
|
|
|
flash[:error].should be_blank
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't log the user in" do
|
|
|
|
session[:current_user_id].should be_blank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-07 06:50:59 -05:00
|
|
|
describe '.change_email' do
|
2013-02-05 14:16:51 -05:00
|
|
|
let(:new_email) { 'bubblegum@adventuretime.ooo' }
|
|
|
|
|
|
|
|
it "requires you to be logged in" do
|
|
|
|
lambda { xhr :put, :change_email, username: 'asdf', email: new_email }.should raise_error(Discourse::NotLoggedIn)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when logged in' do
|
|
|
|
let!(:user) { log_in }
|
|
|
|
|
|
|
|
it 'raises an error without an email parameter' do
|
2013-06-28 14:43:35 -04:00
|
|
|
lambda { xhr :put, :change_email, username: user.username }.should raise_error(ActionController::ParameterMissing)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-09-07 22:42:41 -04:00
|
|
|
it "raises an error if you can't edit the user's email" do
|
|
|
|
Guardian.any_instance.expects(:can_edit_email?).with(user).returns(false)
|
2013-02-05 14:16:51 -05:00
|
|
|
xhr :put, :change_email, username: user.username, email: new_email
|
|
|
|
response.should be_forbidden
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the new email address is taken' do
|
|
|
|
let!(:other_user) { Fabricate(:coding_horror) }
|
|
|
|
it 'raises an error' do
|
|
|
|
lambda { xhr :put, :change_email, username: user.username, email: other_user.email }.should raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
2013-04-27 23:02:23 -04:00
|
|
|
|
|
|
|
it 'raises an error if there is whitespace too' do
|
|
|
|
lambda { xhr :put, :change_email, username: user.username, email: other_user.email + ' ' }.should raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-07-14 10:16:24 -04:00
|
|
|
context 'when new email is different case of existing email' do
|
|
|
|
let!(:other_user) { Fabricate(:user, email: 'case.insensitive@gmail.com')}
|
|
|
|
|
|
|
|
it 'raises an error' do
|
|
|
|
lambda { xhr :put, :change_email, username: user.username, email: other_user.email.upcase }.should raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
context 'success' do
|
|
|
|
|
|
|
|
it 'has an email token' do
|
|
|
|
lambda { xhr :put, :change_email, username: user.username, email: new_email }.should change(EmailToken, :count)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'enqueues an email authorization' do
|
|
|
|
Jobs.expects(:enqueue).with(:user_email, has_entries(type: :authorize_email, user_id: user.id, to_address: new_email))
|
|
|
|
xhr :put, :change_email, username: user.username, email: new_email
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.password_reset' do
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
|
2013-07-15 12:12:54 -04:00
|
|
|
context "you can view it even if login is required" do
|
|
|
|
it "returns success" do
|
2014-07-01 23:06:55 -04:00
|
|
|
SiteSetting.login_required = true
|
|
|
|
get :password_reset, token: 'asdfasdf'
|
2013-07-15 12:12:54 -04:00
|
|
|
response.should be_success
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-25 15:30:52 -04:00
|
|
|
context 'missing token' do
|
2013-02-05 14:16:51 -05:00
|
|
|
before do
|
2014-07-01 23:06:55 -04:00
|
|
|
get :password_reset, token: SecureRandom.hex
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-07-01 23:06:55 -04:00
|
|
|
it 'disallows login' do
|
2013-02-05 14:16:51 -05:00
|
|
|
flash[:error].should be_present
|
|
|
|
session[:current_user_id].should be_blank
|
2014-09-25 11:44:48 -04:00
|
|
|
assigns[:invalid_token].should == nil
|
2014-07-01 23:06:55 -04:00
|
|
|
response.should be_success
|
2013-02-07 06:50:59 -05:00
|
|
|
end
|
2014-08-25 15:30:52 -04:00
|
|
|
end
|
2014-07-01 23:06:55 -04:00
|
|
|
|
2014-08-25 15:30:52 -04:00
|
|
|
context 'invalid token' do
|
|
|
|
before do
|
|
|
|
get :password_reset, token: "evil_trout!"
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'disallows login' do
|
|
|
|
flash[:error].should be_present
|
|
|
|
session[:current_user_id].should be_blank
|
2014-09-25 11:44:48 -04:00
|
|
|
assigns[:invalid_token].should == true
|
2014-08-25 15:30:52 -04:00
|
|
|
response.should be_success
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'valid token' do
|
|
|
|
it 'returns success' do
|
2014-07-01 23:06:55 -04:00
|
|
|
user = Fabricate(:user)
|
|
|
|
token = user.email_tokens.create(email: user.email).token
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-07-01 23:06:55 -04:00
|
|
|
get :password_reset, token: token
|
|
|
|
put :password_reset, token: token, password: 'newpassword'
|
|
|
|
response.should be_success
|
2013-02-05 14:16:51 -05:00
|
|
|
flash[:error].should be_blank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'submit change' do
|
2014-08-25 15:30:52 -04:00
|
|
|
let(:token) { EmailToken.generate_token }
|
2013-02-05 14:16:51 -05:00
|
|
|
before do
|
2014-08-25 15:30:52 -04:00
|
|
|
EmailToken.expects(:confirm).with(token).returns(user)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-09-11 15:22:11 -04:00
|
|
|
it "fails when the password is blank" do
|
|
|
|
put :password_reset, token: token, password: ''
|
|
|
|
assigns(:user).errors.should be_present
|
|
|
|
session[:current_user_id].should be_blank
|
|
|
|
end
|
|
|
|
|
|
|
|
it "fails when the password is too long" do
|
|
|
|
put :password_reset, token: token, password: ('x' * (User.max_password_length + 1))
|
|
|
|
assigns(:user).errors.should be_present
|
|
|
|
session[:current_user_id].should be_blank
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
it "logs in the user" do
|
2014-08-25 15:30:52 -04:00
|
|
|
put :password_reset, token: token, password: 'newpassword'
|
2014-09-11 15:22:11 -04:00
|
|
|
assigns(:user).errors.should be_blank
|
2013-02-05 14:16:51 -05:00
|
|
|
session[:current_user_id].should be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't log in the user when not approved" do
|
|
|
|
SiteSetting.expects(:must_approve_users?).returns(true)
|
2014-08-25 15:30:52 -04:00
|
|
|
put :password_reset, token: token, password: 'newpassword'
|
2014-09-11 15:22:11 -04:00
|
|
|
assigns(:user).errors.should be_blank
|
2013-02-05 14:16:51 -05:00
|
|
|
session[:current_user_id].should be_blank
|
2013-02-07 06:50:59 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-12 17:37:38 -05:00
|
|
|
describe '#create' do
|
2014-07-15 14:07:19 -04:00
|
|
|
|
2013-02-07 06:50:59 -05:00
|
|
|
before do
|
2014-07-15 14:07:19 -04:00
|
|
|
UsersController.any_instance.stubs(:honeypot_value).returns(nil)
|
|
|
|
UsersController.any_instance.stubs(:challenge_value).returns(nil)
|
2014-07-14 15:42:14 -04:00
|
|
|
SiteSetting.stubs(:allow_new_registrations).returns(true)
|
2013-02-05 14:16:51 -05:00
|
|
|
@user = Fabricate.build(:user)
|
2013-02-07 06:50:59 -05:00
|
|
|
@user.password = "strongpassword"
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-06-05 20:09:22 -04:00
|
|
|
def post_user
|
|
|
|
xhr :post, :create,
|
|
|
|
name: @user.name,
|
|
|
|
username: @user.username,
|
|
|
|
password: "strongpassword",
|
|
|
|
email: @user.email
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
context 'when creating a non active user (unconfirmed email)' do
|
2014-03-26 00:39:44 -04:00
|
|
|
|
|
|
|
it 'returns a 500 when local logins are disabled' do
|
|
|
|
SiteSetting.expects(:enable_local_logins).returns(false)
|
2013-06-05 20:09:22 -04:00
|
|
|
post_user
|
2014-03-26 00:39:44 -04:00
|
|
|
|
|
|
|
expect(response.status).to eq(500)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-07-14 15:42:14 -04: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)
|
2014-09-25 11:44:48 -04:00
|
|
|
json['success'].should == false
|
2014-07-14 15:42:14 -04:00
|
|
|
json['message'].should be_present
|
|
|
|
end
|
|
|
|
|
2014-03-26 00:39:44 -04:00
|
|
|
it 'creates a user correctly' do
|
|
|
|
Jobs.expects(:enqueue).with(:user_email, has_entries(type: :signup))
|
2013-02-05 14:16:51 -05:00
|
|
|
User.any_instance.expects(:enqueue_welcome_message).with('welcome_user').never
|
2013-06-05 20:09:22 -04:00
|
|
|
|
2013-06-05 21:19:27 -04:00
|
|
|
post_user
|
2014-03-26 00:39:44 -04:00
|
|
|
|
2014-09-25 11:44:48 -04:00
|
|
|
expect(JSON.parse(response.body)['active']).to be_falsey
|
2014-10-01 13:33:49 -04:00
|
|
|
|
2014-11-04 15:47:32 -05:00
|
|
|
# should save user_created_message in session
|
|
|
|
session["user_created_message"].should be_present
|
2013-02-07 06:50:59 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-06-05 21:19:27 -04:00
|
|
|
context "and 'must approve users' site setting is enabled" do
|
|
|
|
before { SiteSetting.expects(:must_approve_users).returns(true) }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-06-05 21:19:27 -04: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 11:44:48 -04:00
|
|
|
expect(JSON.parse(response.body)['active']).to be_falsey
|
2013-06-05 21:19:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "shows the 'waiting approval' message" do
|
|
|
|
post_user
|
2014-09-25 11:44:48 -04:00
|
|
|
expect(JSON.parse(response.body)['message']).to eq(I18n.t 'login.wait_approval')
|
2013-06-05 21:19:27 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-06-05 21:19:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when creating an active user (confirmed email)' do
|
|
|
|
before { User.any_instance.stubs(:active?).returns(true) }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-06-05 19:50:24 -04:00
|
|
|
it 'enqueues a welcome email' do
|
2013-02-05 14:16:51 -05:00
|
|
|
User.any_instance.expects(:enqueue_welcome_message).with('welcome_user')
|
2013-06-05 20:09:22 -04:00
|
|
|
post_user
|
2014-10-01 13:33:49 -04:00
|
|
|
|
2014-11-04 15:47:32 -05:00
|
|
|
# should save user_created_message in session
|
|
|
|
session["user_created_message"].should be_present
|
2013-06-05 20:09:22 -04:00
|
|
|
end
|
2013-06-05 21:19:27 -04: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 06:50:59 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
it "should be logged in" do
|
|
|
|
User.any_instance.expects(:enqueue_welcome_message)
|
2013-06-05 20:09:22 -04:00
|
|
|
post_user
|
2013-02-05 14:16:51 -05:00
|
|
|
session[:current_user_id].should be_present
|
|
|
|
end
|
|
|
|
|
2013-06-05 21:19:27 -04:00
|
|
|
it 'indicates the user is active in the response' do
|
2013-02-05 14:16:51 -05:00
|
|
|
User.any_instance.expects(:enqueue_welcome_message)
|
2013-06-05 20:09:22 -04:00
|
|
|
post_user
|
2014-09-25 11:44:48 -04:00
|
|
|
expect(JSON.parse(response.body)['active']).to be_truthy
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-07-14 15:42:14 -04: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)
|
2014-09-25 11:44:48 -04:00
|
|
|
json['success'].should == false
|
2014-07-14 15:42:14 -04:00
|
|
|
json['message'].should be_present
|
|
|
|
end
|
|
|
|
|
2013-04-12 18:46:55 -04:00
|
|
|
context 'authentication records for' do
|
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.expects(:must_approve_users).returns(true)
|
|
|
|
end
|
|
|
|
|
2013-08-25 21:04:16 -04:00
|
|
|
it 'should create twitter user info if required' do
|
|
|
|
SiteSetting.stubs(:enable_twitter_logins?).returns(true)
|
2013-04-12 18:46:55 -04:00
|
|
|
twitter_auth = { twitter_user_id: 42, twitter_screen_name: "bruce" }
|
2013-08-25 21:04:16 -04:00
|
|
|
auth = session[:authentication] = {}
|
|
|
|
auth[:authenticator_name] = 'twitter'
|
|
|
|
auth[:extra_data] = twitter_auth
|
2013-04-12 18:46:55 -04:00
|
|
|
TwitterUserInfo.expects(:create)
|
|
|
|
|
2013-06-05 20:09:22 -04:00
|
|
|
post_user
|
2013-04-12 18:46:55 -04:00
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'after success' do
|
2013-06-05 20:09:22 -04:00
|
|
|
before { post_user }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-07 06:50:59 -05:00
|
|
|
it 'should succeed' do
|
2013-02-05 14:16:51 -05:00
|
|
|
should respond_with(:success)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has the proper JSON' do
|
|
|
|
json = JSON::parse(response.body)
|
2014-09-25 11:44:48 -04:00
|
|
|
json["success"].should == true
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-07 06:50:59 -05:00
|
|
|
it 'should not result in an active account' do
|
2014-09-25 11:44:48 -04:00
|
|
|
User.find_by(username: @user.username).active.should == false
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
2013-02-06 19:25:21 -05:00
|
|
|
|
2013-06-28 14:43:35 -04:00
|
|
|
shared_examples 'honeypot fails' do
|
2013-02-06 19:25:21 -05: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)
|
2014-09-25 11:44:48 -04:00
|
|
|
json["success"].should == true
|
2014-10-01 13:33:49 -04:00
|
|
|
|
|
|
|
# should not change the session
|
2014-11-04 15:47:32 -05:00
|
|
|
session["user_created_message"].should be_blank
|
2013-02-06 19:25:21 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when honeypot value is wrong' do
|
|
|
|
before do
|
|
|
|
UsersController.any_instance.stubs(:honeypot_value).returns('abc')
|
|
|
|
end
|
2013-03-23 11:02:59 -04:00
|
|
|
let(:create_params) { {name: @user.name, username: @user.username, password: "strongpassword", email: @user.email, password_confirmation: 'wrong'} }
|
2013-06-28 14:43:35 -04:00
|
|
|
include_examples 'honeypot fails'
|
2013-02-06 19:25:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when challenge answer is wrong' do
|
|
|
|
before do
|
|
|
|
UsersController.any_instance.stubs(:challenge_value).returns('abc')
|
|
|
|
end
|
2013-03-23 11:02:59 -04:00
|
|
|
let(:create_params) { {name: @user.name, username: @user.username, password: "strongpassword", email: @user.email, challenge: 'abc'} }
|
2013-06-28 14:43:35 -04:00
|
|
|
include_examples 'honeypot fails'
|
2013-02-06 19:25:21 -05:00
|
|
|
end
|
2013-02-12 15:42:04 -05:00
|
|
|
|
2013-06-05 14:08:21 -04: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-28 14:43:35 -04:00
|
|
|
include_examples 'honeypot fails'
|
2013-06-05 14:08:21 -04:00
|
|
|
end
|
|
|
|
|
2013-06-28 14:43:35 -04:00
|
|
|
shared_examples 'failed signup' do
|
2013-02-12 15:42:04 -05: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)
|
2014-09-25 11:44:48 -04:00
|
|
|
json["success"].should_not == true
|
2014-10-28 20:06:59 -04:00
|
|
|
|
2014-10-01 13:33:49 -04:00
|
|
|
# should not change the session
|
2014-11-04 15:47:32 -05:00
|
|
|
session["user_created_message"].should be_blank
|
2013-02-12 15:42:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when password is blank' do
|
2013-03-23 11:02:59 -04:00
|
|
|
let(:create_params) { {name: @user.name, username: @user.username, password: "", email: @user.email} }
|
2013-06-28 14:43:35 -04:00
|
|
|
include_examples 'failed signup'
|
2013-02-12 15:42:04 -05:00
|
|
|
end
|
|
|
|
|
2014-09-11 15:22:11 -04: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-12 15:42:04 -05:00
|
|
|
context 'when password param is missing' do
|
2013-03-23 11:02:59 -04:00
|
|
|
let(:create_params) { {name: @user.name, username: @user.username, email: @user.email} }
|
2013-06-28 14:43:35 -04:00
|
|
|
include_examples 'failed signup'
|
2013-03-07 14:56:28 -05:00
|
|
|
end
|
|
|
|
|
2013-04-12 18:46:55 -04: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-28 14:43:35 -04:00
|
|
|
include_examples 'failed signup'
|
2013-03-07 14:56:28 -05:00
|
|
|
end
|
2013-02-12 15:42:04 -05:00
|
|
|
end
|
2013-04-12 18:46:55 -04:00
|
|
|
|
2014-09-26 14:48:34 -04:00
|
|
|
context "with custom fields" do
|
|
|
|
let!(:user_field) { Fabricate(:user_field) }
|
|
|
|
let!(:another_field) { Fabricate(:user_field) }
|
2014-10-08 14:38:18 -04:00
|
|
|
let!(:optional_field) { Fabricate(:user_field, required: false) }
|
2014-09-26 14:48:34 -04: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,
|
|
|
|
password: 'watwatwat',
|
|
|
|
username: @user.username,
|
|
|
|
email: @user.email,
|
|
|
|
user_fields: {
|
|
|
|
user_field.id.to_s => 'value1',
|
|
|
|
another_field.id.to_s => 'value2',
|
|
|
|
}
|
|
|
|
} }
|
|
|
|
|
2014-10-08 14:38:18 -04:00
|
|
|
it "should succeed without the optional field" do
|
2014-09-26 14:48:34 -04:00
|
|
|
xhr :post, :create, create_params
|
|
|
|
response.should be_success
|
|
|
|
inserted = User.where(email: @user.email).first
|
|
|
|
inserted.should be_present
|
|
|
|
inserted.custom_fields.should be_present
|
|
|
|
inserted.custom_fields["user_field_#{user_field.id}"].should == 'value1'
|
|
|
|
inserted.custom_fields["user_field_#{another_field.id}"].should == 'value2'
|
2014-10-08 14:38:18 -04:00
|
|
|
inserted.custom_fields["user_field_#{optional_field.id}"].should be_blank
|
|
|
|
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)
|
|
|
|
response.should be_success
|
|
|
|
inserted = User.where(email: @user.email).first
|
|
|
|
inserted.should be_present
|
|
|
|
inserted.custom_fields.should be_present
|
|
|
|
inserted.custom_fields["user_field_#{user_field.id}"].should == 'value1'
|
|
|
|
inserted.custom_fields["user_field_#{another_field.id}"].should == 'value2'
|
|
|
|
inserted.custom_fields["user_field_#{optional_field.id}"].should == 'value3'
|
2014-09-26 14:48:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context '.username' do
|
|
|
|
it 'raises an error when not logged in' do
|
|
|
|
lambda { xhr :put, :username, username: 'somename' }.should raise_error(Discourse::NotLoggedIn)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'while logged in' do
|
|
|
|
let!(:user) { log_in }
|
|
|
|
let(:new_username) { "#{user.username}1234" }
|
|
|
|
|
|
|
|
it 'raises an error without a new_username param' do
|
2013-06-28 14:43:35 -04:00
|
|
|
lambda { xhr :put, :username, username: user.username }.should raise_error(ActionController::ParameterMissing)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-08-12 14:54:52 -04: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-05 14:16:51 -05:00
|
|
|
xhr :put, :username, username: user.username, new_username: new_username
|
|
|
|
response.should be_forbidden
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error when change_username fails' do
|
|
|
|
User.any_instance.expects(:change_username).with(new_username).returns(false)
|
2013-02-07 06:50:59 -05:00
|
|
|
lambda { xhr :put, :username, username: user.username, new_username: new_username }.should raise_error(Discourse::InvalidParameters)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should succeed when the change_username returns true' do
|
|
|
|
User.any_instance.expects(:change_username).with(new_username).returns(true)
|
|
|
|
xhr :put, :username, username: user.username, new_username: new_username
|
|
|
|
response.should be_success
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context '.check_username' do
|
2013-11-19 14:15:05 -05:00
|
|
|
it 'raises an error without any parameters' do
|
2013-06-06 03:14:32 -04:00
|
|
|
lambda { xhr :get, :check_username }.should raise_error(ActionController::ParameterMissing)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-07-16 12:25:24 -04:00
|
|
|
shared_examples 'when username is unavailable' do
|
2013-02-05 14:16:51 -05:00
|
|
|
it 'should return success' do
|
|
|
|
response.should be_success
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return available as false in the JSON' do
|
2014-09-25 11:44:48 -04:00
|
|
|
::JSON.parse(response.body)['available'].should == false
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return a suggested username' do
|
|
|
|
::JSON.parse(response.body)['suggestion'].should be_present
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-16 12:25:24 -04:00
|
|
|
shared_examples 'when username is available' do
|
2013-02-05 14:16:51 -05:00
|
|
|
it 'should return success' do
|
|
|
|
response.should be_success
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return available in the JSON' do
|
2014-09-25 11:44:48 -04:00
|
|
|
::JSON.parse(response.body)['available'].should == true
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-16 12:25:24 -04:00
|
|
|
it 'returns nothing when given an email param but no username' do
|
|
|
|
xhr :get, :check_username, email: 'dood@example.com'
|
|
|
|
response.should be_success
|
|
|
|
end
|
2013-02-08 14:12:48 -05:00
|
|
|
|
2014-07-16 12:25:24 -04:00
|
|
|
context 'username is available' do
|
|
|
|
before do
|
|
|
|
xhr :get, :check_username, username: 'BruceWayne'
|
2013-02-08 14:12:48 -05:00
|
|
|
end
|
2014-07-16 12:25:24 -04:00
|
|
|
include_examples 'when username is available'
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-07-16 12:25:24 -04:00
|
|
|
context 'username is unavailable' do
|
|
|
|
let!(:user) { Fabricate(:user) }
|
2013-02-05 14:16:51 -05:00
|
|
|
before do
|
2014-07-16 12:25:24 -04:00
|
|
|
xhr :get, :check_username, username: user.username
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2014-07-16 12:25:24 -04:00
|
|
|
include_examples 'when username is unavailable'
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-07-16 12:25:24 -04:00
|
|
|
shared_examples 'checking an invalid username' do
|
|
|
|
it 'should return success' do
|
|
|
|
response.should be_success
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-07-16 12:25:24 -04:00
|
|
|
it 'should not return an available key' do
|
2014-09-25 11:44:48 -04:00
|
|
|
::JSON.parse(response.body)['available'].should == nil
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-07-16 12:25:24 -04:00
|
|
|
it 'should return an error message' do
|
|
|
|
::JSON.parse(response.body)['errors'].should_not be_empty
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2014-07-16 12:25:24 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-07-16 12:25:24 -04:00
|
|
|
context 'has invalid characters' do
|
|
|
|
before do
|
|
|
|
xhr :get, :check_username, username: 'bad username'
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2014-07-16 12:25:24 -04:00
|
|
|
include_examples 'checking an invalid username'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-07-16 12:25:24 -04:00
|
|
|
it 'should return the invalid characters message' do
|
|
|
|
::JSON.parse(response.body)['errors'].should include(I18n.t(:'user.username.characters'))
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-16 12:25:24 -04:00
|
|
|
context 'is too long' do
|
2013-02-05 14:16:51 -05:00
|
|
|
before do
|
2014-07-16 12:25:24 -04:00
|
|
|
xhr :get, :check_username, username: generate_username(User.username_length.last + 1)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2014-07-16 12:25:24 -04:00
|
|
|
include_examples 'checking an invalid username'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-07-16 12:25:24 -04:00
|
|
|
it 'should return the "too long" message' do
|
|
|
|
::JSON.parse(response.body)['errors'].should include(I18n.t(:'user.username.long', max: User.username_length.end))
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
2013-06-28 16:21:46 -04: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-16 12:25:24 -04:00
|
|
|
include_examples 'when username is available'
|
2013-06-28 16:21:46 -04: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-16 12:25:24 -04:00
|
|
|
include_examples 'when username is unavailable'
|
2013-06-28 16:21:46 -04:00
|
|
|
end
|
2013-07-30 14:13:56 -04: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-16 12:25:24 -04:00
|
|
|
include_examples 'when username is available'
|
2013-07-30 14:13:56 -04:00
|
|
|
end
|
2013-06-28 16:21:46 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-11-08 14:11:41 -05:00
|
|
|
describe '#invited' do
|
2013-02-05 14:16:51 -05:00
|
|
|
it 'returns success' do
|
2013-11-08 14:11:41 -05:00
|
|
|
user = Fabricate(:user)
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
xhr :get, :invited, username: user.username
|
2013-11-08 14:11:41 -05:00
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'filters by email' do
|
|
|
|
inviter = Fabricate(:user)
|
|
|
|
invitee = Fabricate(:user)
|
2014-09-25 03:46:51 -04:00
|
|
|
_invite = Fabricate(
|
2013-11-08 14:11:41 -05:00
|
|
|
:invite,
|
|
|
|
email: 'billybob@example.com',
|
|
|
|
invited_by: inviter,
|
|
|
|
user: invitee
|
|
|
|
)
|
|
|
|
Fabricate(
|
|
|
|
:invite,
|
|
|
|
email: 'jimtom@example.com',
|
|
|
|
invited_by: inviter,
|
|
|
|
user: invitee
|
|
|
|
)
|
|
|
|
|
|
|
|
xhr :get, :invited, username: inviter.username, filter: 'billybob'
|
|
|
|
|
2014-03-21 14:13:04 -04:00
|
|
|
invites = JSON.parse(response.body)['invites']
|
2013-11-08 14:11:41 -05:00
|
|
|
expect(invites).to have(1).item
|
|
|
|
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 03:46:51 -04:00
|
|
|
_invite = Fabricate(
|
2013-11-08 14:11:41 -05:00
|
|
|
:invite,
|
|
|
|
invited_by: inviter,
|
|
|
|
email: 'billybob@example.com',
|
|
|
|
user: invitee
|
|
|
|
)
|
|
|
|
Fabricate(
|
|
|
|
:invite,
|
|
|
|
invited_by: inviter,
|
|
|
|
user: Fabricate(:user, username: 'jimtom')
|
|
|
|
)
|
|
|
|
|
|
|
|
xhr :get, :invited, username: inviter.username, filter: 'billybob'
|
|
|
|
|
2014-03-21 14:13:04 -04:00
|
|
|
invites = JSON.parse(response.body)['invites']
|
2013-11-08 14:11:41 -05:00
|
|
|
expect(invites).to have(1).item
|
|
|
|
expect(invites.first).to include('email' => 'billybob@example.com')
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-11-08 14:11:41 -05:00
|
|
|
context 'with guest' do
|
|
|
|
context 'with pending invites' do
|
|
|
|
it 'does not return invites' do
|
|
|
|
inviter = Fabricate(:user)
|
|
|
|
Fabricate(:invite, invited_by: inviter)
|
|
|
|
|
|
|
|
xhr :get, :invited, username: inviter.username
|
|
|
|
|
2014-03-21 14:13:04 -04:00
|
|
|
invites = JSON.parse(response.body)['invites']
|
2013-11-08 14:11:41 -05: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-21 14:13:04 -04:00
|
|
|
invites = JSON.parse(response.body)['invites']
|
2013-11-08 14:11:41 -05:00
|
|
|
expect(invites).to have(1).item
|
|
|
|
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-21 14:13:04 -04:00
|
|
|
guardian.stubs(:can_see_invite_details?).
|
2013-11-08 14:11:41 -05:00
|
|
|
with(inviter).returns(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
xhr :get, :invited, username: inviter.username
|
|
|
|
|
2014-03-21 14:13:04 -04:00
|
|
|
invites = JSON.parse(response.body)['invites']
|
2013-11-08 14:11:41 -05:00
|
|
|
expect(invites).to have(1).item
|
|
|
|
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 03:46:51 -04:00
|
|
|
_invitee = Fabricate(:user)
|
2013-11-08 14:11:41 -05:00
|
|
|
Fabricate(:invite, invited_by: inviter)
|
|
|
|
stub_guardian(user) do |guardian|
|
2014-03-21 14:13:04 -04:00
|
|
|
guardian.stubs(:can_see_invite_details?).
|
2013-11-08 14:11:41 -05:00
|
|
|
with(inviter).returns(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
xhr :get, :invited, username: inviter.username
|
|
|
|
|
2014-03-21 14:13:04 -04:00
|
|
|
json = JSON.parse(response.body)['invites']
|
|
|
|
expect(json).to be_empty
|
2013-11-08 14:11:41 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with redeemed invites' do
|
|
|
|
it 'returns invites' do
|
2014-09-25 03:46:51 -04:00
|
|
|
_user = log_in
|
2013-11-08 14:11:41 -05:00
|
|
|
inviter = Fabricate(:user)
|
|
|
|
invitee = Fabricate(:user)
|
|
|
|
invite = Fabricate(:invite, invited_by: inviter, user: invitee)
|
|
|
|
|
|
|
|
xhr :get, :invited, username: inviter.username
|
|
|
|
|
2014-03-21 14:13:04 -04:00
|
|
|
invites = JSON.parse(response.body)['invites']
|
2013-11-08 14:11:41 -05:00
|
|
|
expect(invites).to have(1).item
|
|
|
|
expect(invites.first).to include('email' => invite.email)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-11-01 14:12:25 -04:00
|
|
|
describe '#update' do
|
|
|
|
context 'with guest' do
|
|
|
|
it 'raises an error' do
|
2013-04-12 18:46:55 -04:00
|
|
|
expect do
|
2013-11-01 14:12:25 -04:00
|
|
|
xhr :put, :update, username: 'guest'
|
2013-04-12 18:46:55 -04:00
|
|
|
end.to raise_error(Discourse::NotLoggedIn)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-01 14:12:25 -04:00
|
|
|
context 'with authenticated user' do
|
|
|
|
context 'with permission to update' do
|
2014-09-26 14:48:34 -04:00
|
|
|
let!(:user) { log_in(:user) }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-09-26 14:48:34 -04:00
|
|
|
it 'allows the update' do
|
2014-06-11 01:50:37 -04:00
|
|
|
put :update, username: user.username, name: 'Jim Tom', custom_fields: {test: :it}
|
2013-11-01 14:12:25 -04:00
|
|
|
expect(response).to be_success
|
2014-06-11 01:50:37 -04:00
|
|
|
|
|
|
|
user.reload
|
|
|
|
|
|
|
|
expect(user.name).to eq 'Jim Tom'
|
|
|
|
expect(user.custom_fields['test']).to eq 'it'
|
2013-11-01 14:12:25 -04:00
|
|
|
end
|
2013-11-01 14:42:16 -04:00
|
|
|
|
2014-09-26 14:48:34 -04:00
|
|
|
context "with user fields" do
|
|
|
|
context "an editable field" do
|
|
|
|
let!(:user_field) { Fabricate(:user_field) }
|
2014-10-08 14:38:18 -04:00
|
|
|
let!(:optional_field) { Fabricate(:user_field, required: false ) }
|
2014-09-26 14:48:34 -04: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 => '' }
|
|
|
|
response.should_not be_success
|
|
|
|
user.user_fields[user_field.id.to_s].should_not == 'happy'
|
|
|
|
end
|
|
|
|
end
|
2013-11-01 14:42:16 -04:00
|
|
|
|
2014-09-26 14:48:34 -04: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-01 14:42:16 -04:00
|
|
|
put :update, username: user.username
|
|
|
|
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
expect(json['user']['id']).to eq user.id
|
|
|
|
end
|
2014-09-26 14:48:34 -04:00
|
|
|
|
2013-11-01 14:12:25 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-11-01 17:06:20 -04:00
|
|
|
context 'without permission to update' do
|
2013-11-01 14:12:25 -04:00
|
|
|
it 'does not allow the update' do
|
2013-11-01 14:42:16 -04:00
|
|
|
user = Fabricate(:user, name: 'Billy Bob')
|
|
|
|
log_in_user(user)
|
|
|
|
guardian = Guardian.new(user)
|
|
|
|
guardian.stubs(:ensure_can_edit!).with(user).raises(Discourse::InvalidAccess.new)
|
|
|
|
Guardian.stubs(new: guardian).with(user)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-11-01 14:12:25 -04:00
|
|
|
put :update, username: user.username, name: 'Jim Tom'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-11-01 14:12:25 -04:00
|
|
|
expect(response).to be_forbidden
|
|
|
|
expect(user.reload.name).not_to eq 'Jim Tom'
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-20 13:15:58 -04: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
|
|
|
|
user.user_profile.reload.card_image_badge_id.should be_blank
|
|
|
|
badge.update_attributes image: "wat.com/wat.jpg"
|
|
|
|
|
|
|
|
xhr :put, :update_card_badge, user_badge_id: user_badge.id, username: user.username
|
|
|
|
user.user_profile.reload.card_image_badge_id.should == badge.id
|
|
|
|
|
|
|
|
# Can set to nothing
|
|
|
|
xhr :put, :update_card_badge, username: user.username
|
|
|
|
user.user_profile.reload.card_image_badge_id.should be_blank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-25 14:25:29 -04: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
|
|
|
|
user.reload.title.should_not == badge.name
|
|
|
|
badge.update_attributes allow_title: true
|
|
|
|
xhr :put, :badge_title, user_badge_id: user_badge.id, username: user.username
|
|
|
|
user.reload.title.should == badge.name
|
2014-10-07 19:26:18 -04:00
|
|
|
user.user_profile.badge_granted_title.should == true
|
|
|
|
|
|
|
|
user.title = "testing"
|
|
|
|
user.save
|
|
|
|
user.user_profile.reload
|
|
|
|
user.user_profile.badge_granted_title.should == false
|
|
|
|
|
2014-04-25 14:25:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-07 06:50:59 -05:00
|
|
|
describe "search_users" do
|
|
|
|
|
|
|
|
let(:topic) { Fabricate :topic }
|
|
|
|
let(:user) { Fabricate :user, username: "joecabot", name: "Lawrence Tierney" }
|
|
|
|
|
|
|
|
before do
|
2014-07-04 19:13:53 -04:00
|
|
|
ActiveRecord::Base.observers.enable :all
|
2013-02-07 06:50:59 -05: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
|
|
|
|
response.should be_success
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
json["users"].map { |u| u["username"] }.should include(user.username)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "searches when provided the topic only" do
|
|
|
|
xhr :post, :search_users, topic_id: topic.id
|
|
|
|
response.should be_success
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
json["users"].map { |u| u["username"] }.should include(user.username)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "searches when provided the term and topic" do
|
|
|
|
xhr :post, :search_users, term: user.name.split(" ").last, topic_id: topic.id
|
|
|
|
response.should be_success
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
json["users"].map { |u| u["username"] }.should include(user.username)
|
|
|
|
end
|
|
|
|
|
2013-10-30 15:45:13 -04:00
|
|
|
context "when `enable_names` is true" do
|
|
|
|
before do
|
2014-07-04 19:13:53 -04:00
|
|
|
SiteSetting.enable_names = true
|
2013-10-30 15:45:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns names" do
|
|
|
|
xhr :post, :search_users, term: user.name
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
json["users"].map { |u| u["name"] }.should include(user.name)
|
|
|
|
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)
|
|
|
|
json["users"].map { |u| u["name"] }.should_not include(user.name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-07 06:50:59 -05:00
|
|
|
end
|
|
|
|
|
2013-02-22 11:49:48 -05:00
|
|
|
describe 'send_activation_email' do
|
|
|
|
context 'for an existing user' do
|
2014-08-28 12:07:13 -04:00
|
|
|
let(:user) { Fabricate(:user, active: false) }
|
2013-02-22 11:49:48 -05: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-05 12:26:46 -04:00
|
|
|
xhr :post, :send_activation_email, username: user.username
|
2013-02-22 11:49:48 -05: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-05 12:26:46 -04:00
|
|
|
xhr :post, :send_activation_email, username: user.username
|
2013-02-22 11:49:48 -05: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-05 12:26:46 -04:00
|
|
|
xhr :post, :send_activation_email, username: user.username
|
2013-02-22 11:49:48 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when username does not exist' do
|
|
|
|
it 'should not send an email' do
|
|
|
|
Jobs.expects(:enqueue).never
|
2013-07-05 12:26:46 -04:00
|
|
|
xhr :post, :send_activation_email, username: 'nopenopenopenope'
|
2013-02-22 11:49:48 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-28 15:12:51 -05:00
|
|
|
describe '.upload_user_image' do
|
2013-08-24 16:37:31 -04:00
|
|
|
|
|
|
|
it 'raises an error when not logged in' do
|
2014-02-28 15:12:51 -05:00
|
|
|
lambda { xhr :put, :upload_user_image, username: 'asdf' }.should raise_error(Discourse::NotLoggedIn)
|
2013-08-24 16:37:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'while logged in' do
|
|
|
|
|
|
|
|
let!(:user) { log_in }
|
|
|
|
|
2014-07-14 11:34:23 -04:00
|
|
|
let(:logo) { file_from_fixtures("logo.png") }
|
2014-04-14 16:55:57 -04:00
|
|
|
|
2014-02-28 15:12:51 -05:00
|
|
|
let(:user_image) do
|
2014-04-14 16:55:57 -04:00
|
|
|
ActionDispatch::Http::UploadedFile.new({ filename: 'logo.png', tempfile: logo })
|
2013-08-24 16:37:31 -04:00
|
|
|
end
|
2014-04-14 16:55:57 -04:00
|
|
|
|
2014-06-26 11:43:44 -04:00
|
|
|
it 'raises an error without a image_type param' do
|
2014-02-28 15:12:51 -05:00
|
|
|
lambda { xhr :put, :upload_user_image, username: user.username }.should raise_error(ActionController::ParameterMissing)
|
|
|
|
end
|
2013-08-24 16:37:31 -04:00
|
|
|
|
2013-10-18 10:33:19 -04:00
|
|
|
describe "with uploaded file" do
|
2014-04-14 16:55:57 -04:00
|
|
|
|
2014-02-28 15:12:51 -05:00
|
|
|
it 'raises an error when you don\'t have permission to upload an user image' do
|
2013-10-18 10:33:19 -04:00
|
|
|
Guardian.any_instance.expects(:can_edit?).with(user).returns(false)
|
2014-06-26 11:43:44 -04:00
|
|
|
xhr :post, :upload_user_image, username: user.username, image_type: "avatar"
|
2013-10-18 10:33:19 -04:00
|
|
|
response.should be_forbidden
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'rejects large images' do
|
2014-04-14 16:55:57 -04:00
|
|
|
SiteSetting.stubs(:max_image_size_kb).returns(1)
|
2014-06-26 11:43:44 -04:00
|
|
|
xhr :post, :upload_user_image, username: user.username, file: user_image, image_type: "avatar"
|
2014-04-14 16:55:57 -04:00
|
|
|
response.status.should eq 422
|
2013-10-18 10:33:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'rejects unauthorized images' do
|
2014-04-14 16:55:57 -04:00
|
|
|
SiteSetting.stubs(:authorized_extensions).returns(".txt")
|
2014-06-26 11:43:44 -04:00
|
|
|
xhr :post, :upload_user_image, username: user.username, file: user_image, image_type: "avatar"
|
2014-02-28 15:12:51 -05:00
|
|
|
response.status.should eq 422
|
|
|
|
end
|
2013-10-18 10:33:19 -04:00
|
|
|
|
2014-02-28 15:12:51 -05:00
|
|
|
it 'is successful for avatars' do
|
2013-10-18 10:33:19 -04:00
|
|
|
upload = Fabricate(:upload)
|
|
|
|
Upload.expects(:create_for).returns(upload)
|
2014-02-28 15:12:51 -05:00
|
|
|
# enqueues the user_image generator job
|
2014-06-26 11:43:44 -04:00
|
|
|
xhr :post, :upload_user_image, username: user.username, file: user_image, image_type: "avatar"
|
2013-10-18 10:33:19 -04:00
|
|
|
# returns the url, width and height of the uploaded image
|
|
|
|
json = JSON.parse(response.body)
|
2014-04-14 16:55:57 -04:00
|
|
|
json['url'].should == "/uploads/default/1/1234567890123456.png"
|
2013-10-18 10:33:19 -04:00
|
|
|
json['width'].should == 100
|
|
|
|
json['height'].should == 200
|
2014-05-26 05:46:43 -04:00
|
|
|
json['upload_id'].should == upload.id
|
2013-10-18 10:33:19 -04:00
|
|
|
end
|
2014-04-14 16:55:57 -04:00
|
|
|
|
2014-02-28 15:12:51 -05:00
|
|
|
it 'is successful for profile backgrounds' do
|
|
|
|
upload = Fabricate(:upload)
|
|
|
|
Upload.expects(:create_for).returns(upload)
|
2014-06-26 11:43:44 -04:00
|
|
|
xhr :post, :upload_user_image, username: user.username, file: user_image, image_type: "profile_background"
|
2014-02-28 15:12:51 -05:00
|
|
|
user.reload
|
2014-04-14 16:55:57 -04:00
|
|
|
|
2014-06-11 21:52:50 -04:00
|
|
|
user.user_profile.profile_background.should == "/uploads/default/1/1234567890123456.png"
|
2014-04-14 16:55:57 -04:00
|
|
|
|
2014-02-28 15:12:51 -05:00
|
|
|
# returns the url, width and height of the uploaded image
|
|
|
|
json = JSON.parse(response.body)
|
2014-04-14 16:55:57 -04:00
|
|
|
json['url'].should == "/uploads/default/1/1234567890123456.png"
|
2014-02-28 15:12:51 -05:00
|
|
|
json['width'].should == 100
|
|
|
|
json['height'].should == 200
|
|
|
|
end
|
2014-04-14 16:55:57 -04:00
|
|
|
|
2014-10-20 12:11:36 -04:00
|
|
|
it 'is successful for card backgrounds' do
|
2014-10-16 15:05:36 -04:00
|
|
|
upload = Fabricate(:upload)
|
|
|
|
Upload.expects(:create_for).returns(upload)
|
2014-10-20 12:11:36 -04:00
|
|
|
xhr :post, :upload_user_image, username: user.username, file: user_image, image_type: "card_background"
|
2014-10-16 15:05:36 -04:00
|
|
|
user.reload
|
|
|
|
|
2014-10-20 12:11:36 -04:00
|
|
|
user.user_profile.card_background.should == "/uploads/default/1/1234567890123456.png"
|
2014-10-16 15:05:36 -04:00
|
|
|
|
|
|
|
# returns the url, width and height of the uploaded image
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
json['url'].should == "/uploads/default/1/1234567890123456.png"
|
|
|
|
json['width'].should == 100
|
|
|
|
json['height'].should == 200
|
|
|
|
end
|
|
|
|
|
2013-08-24 16:37:31 -04:00
|
|
|
end
|
|
|
|
|
2013-10-18 10:33:19 -04:00
|
|
|
describe "with url" do
|
2014-02-28 15:12:51 -05:00
|
|
|
let(:user_image_url) { "http://cdn.discourse.org/assets/logo.png" }
|
2013-10-18 10:33:19 -04:00
|
|
|
|
2014-04-14 16:55:57 -04:00
|
|
|
before { UsersController.any_instance.stubs(:is_api?).returns(true) }
|
2013-10-18 10:33:19 -04:00
|
|
|
|
|
|
|
describe "correct urls" do
|
2014-04-14 16:55:57 -04:00
|
|
|
|
|
|
|
before { FileHelper.stubs(:download).returns(logo) }
|
|
|
|
|
2013-10-18 10:33:19 -04:00
|
|
|
it 'rejects large images' do
|
2014-04-14 16:55:57 -04:00
|
|
|
SiteSetting.stubs(:max_image_size_kb).returns(1)
|
2014-06-26 11:43:44 -04:00
|
|
|
xhr :post, :upload_user_image, username: user.username, file: user_image_url, image_type: "profile_background"
|
2014-04-14 16:55:57 -04:00
|
|
|
response.status.should eq 422
|
2013-10-18 10:33:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'rejects unauthorized images' do
|
2014-04-14 16:55:57 -04:00
|
|
|
SiteSetting.stubs(:authorized_extensions).returns(".txt")
|
2014-06-26 11:43:44 -04:00
|
|
|
xhr :post, :upload_user_image, username: user.username, file: user_image_url, image_type: "profile_background"
|
2014-02-28 15:12:51 -05:00
|
|
|
response.status.should eq 422
|
|
|
|
end
|
2014-03-26 00:39:44 -04:00
|
|
|
|
2014-02-28 15:12:51 -05:00
|
|
|
it 'is successful for avatars' do
|
2013-10-18 10:33:19 -04:00
|
|
|
upload = Fabricate(:upload)
|
|
|
|
Upload.expects(:create_for).returns(upload)
|
2014-02-28 15:12:51 -05:00
|
|
|
# enqueues the user_image generator job
|
2014-06-26 11:43:44 -04:00
|
|
|
xhr :post, :upload_avatar, username: user.username, file: user_image_url, image_type: "avatar"
|
2013-10-18 10:33:19 -04:00
|
|
|
json = JSON.parse(response.body)
|
2014-04-14 16:55:57 -04:00
|
|
|
json['url'].should == "/uploads/default/1/1234567890123456.png"
|
2013-10-18 10:33:19 -04:00
|
|
|
json['width'].should == 100
|
|
|
|
json['height'].should == 200
|
2014-05-26 05:46:43 -04:00
|
|
|
json['upload_id'].should == upload.id
|
2013-10-18 10:33:19 -04:00
|
|
|
end
|
2014-03-26 00:39:44 -04:00
|
|
|
|
2014-02-28 15:12:51 -05:00
|
|
|
it 'is successful for profile backgrounds' do
|
|
|
|
upload = Fabricate(:upload)
|
|
|
|
Upload.expects(:create_for).returns(upload)
|
2014-06-26 11:43:44 -04:00
|
|
|
xhr :post, :upload_user_image, username: user.username, file: user_image_url, image_type: "profile_background"
|
2014-02-28 15:12:51 -05:00
|
|
|
user.reload
|
2014-06-11 21:52:50 -04:00
|
|
|
user.user_profile.profile_background.should == "/uploads/default/1/1234567890123456.png"
|
2014-03-26 00:39:44 -04:00
|
|
|
|
2014-02-28 15:12:51 -05:00
|
|
|
# returns the url, width and height of the uploaded image
|
|
|
|
json = JSON.parse(response.body)
|
2014-04-14 16:55:57 -04:00
|
|
|
json['url'].should == "/uploads/default/1/1234567890123456.png"
|
2014-02-28 15:12:51 -05:00
|
|
|
json['width'].should == 100
|
|
|
|
json['height'].should == 200
|
|
|
|
end
|
2014-10-16 15:05:36 -04:00
|
|
|
|
2014-10-20 12:11:36 -04:00
|
|
|
it 'is successful for card backgrounds' do
|
2014-10-16 15:05:36 -04:00
|
|
|
upload = Fabricate(:upload)
|
|
|
|
Upload.expects(:create_for).returns(upload)
|
2014-10-20 12:11:36 -04:00
|
|
|
xhr :post, :upload_user_image, username: user.username, file: user_image_url, image_type: "card_background"
|
2014-10-16 15:05:36 -04:00
|
|
|
user.reload
|
2014-10-20 12:11:36 -04:00
|
|
|
user.user_profile.card_background.should == "/uploads/default/1/1234567890123456.png"
|
2014-10-16 15:05:36 -04:00
|
|
|
|
|
|
|
# returns the url, width and height of the uploaded image
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
json['url'].should == "/uploads/default/1/1234567890123456.png"
|
|
|
|
json['width'].should == 100
|
|
|
|
json['height'].should == 200
|
|
|
|
end
|
2013-10-18 10:33:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should handle malformed urls" do
|
2014-06-26 11:43:44 -04:00
|
|
|
xhr :post, :upload_user_image, username: user.username, file: "foobar", image_type: "profile_background"
|
2013-10-18 10:33:19 -04:00
|
|
|
response.status.should eq 422
|
|
|
|
end
|
|
|
|
|
2013-08-24 16:37:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2014-05-26 05:46:43 -04:00
|
|
|
describe '.pick_avatar' do
|
2013-08-24 16:37:31 -04:00
|
|
|
|
|
|
|
it 'raises an error when not logged in' do
|
2014-05-26 05:46:43 -04:00
|
|
|
lambda { xhr :put, :pick_avatar, username: 'asdf', avatar_id: 1}.should raise_error(Discourse::NotLoggedIn)
|
2013-08-24 16:37:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'while logged in' do
|
|
|
|
|
|
|
|
let!(:user) { log_in }
|
|
|
|
|
|
|
|
it 'raises an error when you don\'t have permission to toggle the avatar' do
|
2014-05-26 05:46:43 -04:00
|
|
|
another_user = Fabricate(:user)
|
|
|
|
xhr :put, :pick_avatar, username: another_user.username, upload_id: 1
|
2013-08-24 16:37:31 -04:00
|
|
|
response.should be_forbidden
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'it successful' do
|
2014-05-26 05:46:43 -04:00
|
|
|
xhr :put, :pick_avatar, username: user.username, upload_id: 111
|
|
|
|
user.reload.uploaded_avatar_id.should == 111
|
2013-08-24 16:37:31 -04:00
|
|
|
response.should be_success
|
2014-05-30 00:48:07 -04:00
|
|
|
|
|
|
|
xhr :put, :pick_avatar, username: user.username
|
|
|
|
user.reload.uploaded_avatar_id.should == nil
|
|
|
|
response.should be_success
|
2013-08-24 16:37:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2014-03-26 00:39:44 -04:00
|
|
|
|
2014-06-27 14:48:39 -04:00
|
|
|
describe '.destroy_user_image' do
|
2014-03-26 00:39:44 -04:00
|
|
|
|
2014-02-28 15:12:51 -05:00
|
|
|
it 'raises an error when not logged in' do
|
2014-06-27 15:35:25 -04:00
|
|
|
lambda { xhr :delete, :destroy_user_image, type: 'profile_background', username: 'asdf' }.should raise_error(Discourse::NotLoggedIn)
|
2014-02-28 15:12:51 -05:00
|
|
|
end
|
2014-03-26 00:39:44 -04:00
|
|
|
|
2014-02-28 15:12:51 -05: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)
|
2014-06-27 15:35:25 -04:00
|
|
|
xhr :delete, :destroy_user_image, username: user.username, image_type: 'profile_background'
|
2014-02-28 15:12:51 -05:00
|
|
|
response.should be_forbidden
|
|
|
|
end
|
|
|
|
|
2014-06-27 14:48:39 -04:00
|
|
|
it "requires the `image_type` param" do
|
2014-06-27 15:35:25 -04:00
|
|
|
-> { xhr :delete, :destroy_user_image, username: user.username }.should raise_error(ActionController::ParameterMissing)
|
2014-06-27 14:48:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "only allows certain `image_types`" do
|
2014-06-27 15:35:25 -04:00
|
|
|
-> { xhr :delete, :destroy_user_image, username: user.username, image_type: 'wat' }.should raise_error(Discourse::InvalidParameters)
|
2014-06-27 14:48:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'can clear the profile background' do
|
2014-06-27 15:35:25 -04:00
|
|
|
xhr :delete, :destroy_user_image, image_type: 'profile_background', username: user.username
|
2014-06-11 21:52:50 -04:00
|
|
|
user.reload.user_profile.profile_background.should == ""
|
2014-02-28 15:12:51 -05:00
|
|
|
response.should be_success
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
2014-02-13 11:42:35 -05:00
|
|
|
|
|
|
|
describe '.destroy' do
|
|
|
|
it 'raises an error when not logged in' do
|
|
|
|
lambda { xhr :delete, :destroy, username: 'nobody' }.should raise_error(Discourse::NotLoggedIn)
|
|
|
|
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
|
|
|
|
response.should be_forbidden
|
|
|
|
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
|
|
|
|
response.should be_forbidden
|
|
|
|
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
|
|
|
|
response.should be_success
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-21 11:52:11 -04:00
|
|
|
describe '.my_redirect' do
|
|
|
|
|
|
|
|
it "returns 404 if the user is not logged in" do
|
|
|
|
get :my_redirect, path: "wat"
|
|
|
|
response.should_not be_success
|
|
|
|
response.should_not be_redirect
|
|
|
|
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"
|
|
|
|
response.should_not be_redirect
|
|
|
|
end
|
|
|
|
|
|
|
|
it "will redirect to an valid path" do
|
|
|
|
get :my_redirect, path: "preferences"
|
|
|
|
response.should be_redirect
|
|
|
|
end
|
2014-06-14 13:51:06 -04:00
|
|
|
|
|
|
|
it "permits forward slashes" do
|
|
|
|
get :my_redirect, path: "activity/posts"
|
|
|
|
response.should be_redirect
|
|
|
|
end
|
2014-04-21 11:52:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-29 16:31:05 -04:00
|
|
|
describe '.check_emails' do
|
|
|
|
|
|
|
|
it 'raises an error when not logged in' do
|
2014-09-29 16:41:54 -04:00
|
|
|
lambda { xhr :put, :check_emails, username: 'zogstrip' }.should raise_error(Discourse::NotLoggedIn)
|
2014-09-29 16:31:05 -04: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-29 16:41:54 -04:00
|
|
|
xhr :put, :check_emails, username: Fabricate(:user).username
|
2014-09-29 16:31:05 -04:00
|
|
|
response.should be_forbidden
|
|
|
|
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-29 16:41:54 -04:00
|
|
|
xhr :put, :check_emails, username: Fabricate(:user).username
|
2014-09-29 16:31:05 -04:00
|
|
|
response.should be_success
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
json["email"].should be_present
|
|
|
|
json["associated_accounts"].should be_present
|
|
|
|
end
|
|
|
|
|
2014-10-28 20:06:59 -04: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
|
|
|
|
response.should be_success
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
json["email"].should be_present
|
|
|
|
json["associated_accounts"].should be_present
|
|
|
|
end
|
|
|
|
|
2014-09-29 16:31:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|