discourse/spec/controllers/users_controller_spec.rb

1333 lines
42 KiB
Ruby
Raw Normal View History

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
before do
UsersController.any_instance.stubs(:honeypot_value).returns(nil)
UsersController.any_instance.stubs(:challenge_value).returns(nil)
end
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
xhr :get, :show, username: user.username
response.should be_success
end
it "returns not found when the username doesn't exist" do
xhr :get, :show, username: 'madeuppity'
response.should_not be_success
end
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
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
context 'invalid token' do
before do
EmailToken.expects(:confirm).with('asdfasdf').returns(nil)
get :activate_account, token: 'asdfasdf'
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')
get :activate_account, token: 'asdfasdf'
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
get :activate_account, token: 'asdfasdf'
end
end
context 'response' do
2013-02-05 14:16:51 -05:00
before do
Guardian.any_instance.expects(:can_access_forum?).returns(true)
2013-02-05 14:16:51 -05:00
EmailToken.expects(:confirm).with('asdfasdf').returns(user)
get :activate_account, token: 'asdfasdf'
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
context 'user is not approved' do
2013-02-05 14:16:51 -05:00
before do
Guardian.any_instance.expects(:can_access_forum?).returns(false)
2013-02-05 14:16:51 -05:00
EmailToken.expects(:confirm).with('asdfasdf').returns(user)
get :activate_account, token: 'asdfasdf'
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
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
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
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) }
context "you can view it even if login is required" do
before do
SiteSetting.stubs(:login_required).returns(true)
get :password_reset, token: 'asdfasdf'
end
it "returns success" do
response.should be_success
end
end
2013-02-05 14:16:51 -05:00
context 'invalid token' do
before do
EmailToken.expects(:confirm).with('asdfasdf').returns(nil)
get :password_reset, token: 'asdfasdf'
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
it "doesn't log in the user" do
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
context 'valid token' do
before do
EmailToken.expects(:confirm).with('asdfasdf').returns(user)
put :password_reset, token: 'asdfasdf', password: 'newpassword'
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
end
context 'submit change' do
before do
EmailToken.expects(:confirm).with('asdfasdf').returns(user)
end
it "logs in the user" do
put :password_reset, token: 'asdfasdf', password: 'newpassword'
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)
put :password_reset, token: 'asdfasdf', password: 'newpassword'
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
describe '#create' do
2013-02-07 06:50:59 -05:00
before do
2013-02-05 14:16:51 -05:00
@user = Fabricate.build(:user)
2013-02-07 06:50:59 -05:00
@user.password = "strongpassword"
DiscourseHub.stubs(:register_username).returns([true, nil])
2013-02-05 14:16:51 -05:00
end
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
it 'returns a 500 when local logins are disabled' do
SiteSetting.expects(:enable_local_logins).returns(false)
post_user
expect(response.status).to eq(500)
2013-02-05 14:16:51 -05:00
end
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
post_user
expect(JSON.parse(response.body)['active']).to be_false
2013-02-07 06:50:59 -05:00
end
2013-02-05 14:16:51 -05: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
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
expect(JSON.parse(response.body)['active']).to be_false
end
it "shows the 'waiting approval' message" do
post_user
expect(JSON.parse(response.body)['message']).to eq(
I18n.t 'login.wait_approval'
)
end
2013-02-05 14:16:51 -05:00
end
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
it 'enqueues a welcome email' do
2013-02-05 14:16:51 -05:00
User.any_instance.expects(:enqueue_welcome_message).with('welcome_user')
post_user
end
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)
post_user
2013-02-05 14:16:51 -05:00
session[:current_user_id].should be_present
end
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)
post_user
expect(JSON.parse(response.body)['active']).to be_true
2013-02-05 14:16:51 -05:00
end
context 'authentication records for' do
before do
SiteSetting.expects(:must_approve_users).returns(true)
end
it 'should create twitter user info if required' do
SiteSetting.stubs(:enable_twitter_logins?).returns(true)
twitter_auth = { twitter_user_id: 42, twitter_screen_name: "bruce" }
auth = session[:authentication] = {}
auth[:authenticator_name] = 'twitter'
auth[:extra_data] = twitter_auth
TwitterUserInfo.expects(:create)
post_user
end
end
2013-02-05 14:16:51 -05:00
end
context 'after success' do
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)
2013-02-07 06:50:59 -05:00
json["success"].should be_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
2013-02-05 14:16:51 -05:00
User.where(username: @user.username).first.active.should be_false
end
end
2013-06-28 14:43:35 -04:00
shared_examples 'honeypot fails' do
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)
json["success"].should be_true
end
end
context 'when honeypot value is wrong' do
before do
UsersController.any_instance.stubs(:honeypot_value).returns('abc')
end
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'
end
context 'when challenge answer is wrong' do
before do
UsersController.any_instance.stubs(:challenge_value).returns('abc')
end
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'
end
2013-02-12 15:42:04 -05: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'
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)
json["success"].should_not be_true
end
end
context 'when password is blank' do
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
context 'when password param is missing' do
let(:create_params) { {name: @user.name, username: @user.username, email: @user.email} }
2013-06-28 14:43:35 -04:00
include_examples 'failed signup'
end
context 'when username is unavailable in DiscourseHub' do
before do
SiteSetting.stubs(:call_discourse_hub?).returns(true)
DiscourseHub.stubs(:register_username).raises(DiscourseHub::UsernameUnavailable.new(@user.name))
end
let(:create_params) {{
name: @user.name,
username: @user.username,
password: 'strongpassword',
email: @user.email
}}
include_examples 'failed signup'
end
context 'when an Exception is raised' do
[ ActiveRecord::StatementInvalid,
DiscourseHub::UsernameUnavailable,
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'
end
2013-02-12 15:42:04 -05:00
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
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
before do
DiscourseHub.stubs(:username_available?).returns([true, nil])
2013-02-05 14:16:51 -05:00
end
it 'raises an error without any parameters' do
lambda { xhr :get, :check_username }.should raise_error(ActionController::ParameterMissing)
2013-02-05 14:16:51 -05:00
end
2013-06-28 14:43:35 -04:00
shared_examples 'when username is unavailable locally' 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
::JSON.parse(response.body)['available'].should be_false
end
it 'should return a suggested username' do
::JSON.parse(response.body)['suggestion'].should be_present
end
end
2013-06-28 14:43:35 -04:00
shared_examples 'when username is available everywhere' 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
::JSON.parse(response.body)['available'].should be_true
end
end
context 'when call_discourse_hub is disabled' do
2013-02-05 14:16:51 -05:00
before do
SiteSetting.stubs(:call_discourse_hub?).returns(false)
DiscourseHub.expects(:username_available?).never
DiscourseHub.expects(:username_match?).never
2013-02-05 14:16:51 -05:00
end
it 'returns nothing when given an email param but no username' do
xhr :get, :check_username, email: 'dood@example.com'
response.should be_success
2013-02-05 14:16:51 -05:00
end
context 'username is available' do
2013-02-05 14:16:51 -05:00
before do
xhr :get, :check_username, username: 'BruceWayne'
end
2013-06-28 14:43:35 -04:00
include_examples 'when username is available everywhere'
2013-02-05 14:16:51 -05:00
end
context 'username is unavailable' do
2013-02-05 14:16:51 -05:00
let!(:user) { Fabricate(:user) }
before do
xhr :get, :check_username, username: user.username
end
2013-06-28 14:43:35 -04:00
include_examples 'when username is unavailable locally'
2013-02-05 14:16:51 -05:00
end
2013-06-28 14:43:35 -04:00
shared_examples 'checking an invalid username' do
it 'should return success' do
response.should be_success
end
it 'should not return an available key' do
::JSON.parse(response.body)['available'].should be_nil
end
it 'should return an error message' do
::JSON.parse(response.body)['errors'].should_not be_empty
end
end
context 'has invalid characters' do
before do
xhr :get, :check_username, username: 'bad username'
end
2013-06-28 14:43:35 -04:00
include_examples 'checking an invalid username'
it 'should return the invalid characters message' do
::JSON.parse(response.body)['errors'].should include(I18n.t(:'user.username.characters'))
end
end
context 'is too long' do
before do
xhr :get, :check_username, username: generate_username(User.username_length.last + 1)
end
2013-06-28 14:43:35 -04:00
include_examples 'checking an invalid username'
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))
end
end
2013-02-05 14:16:51 -05:00
end
context 'when call_discourse_hub is enabled' do
2013-02-05 14:16:51 -05:00
before do
SiteSetting.stubs(:call_discourse_hub?).returns(true)
2013-02-05 14:16:51 -05:00
end
context 'available locally and globally' do
before do
DiscourseHub.stubs(:username_available?).returns([true, nil])
DiscourseHub.stubs(:username_match?).returns([false, true, nil]) # match = false, available = true, suggestion = nil
2013-02-05 14:16:51 -05:00
end
shared_examples 'check_username when username is available everywhere' 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
::JSON.parse(response.body)['available'].should be_true
end
it 'should return global_match false in the JSON' do
::JSON.parse(response.body)['global_match'].should be_false
end
end
context 'and email is not given' do
before do
xhr :get, :check_username, username: 'BruceWayne'
end
include_examples 'check_username when username is available everywhere'
2013-02-05 14:16:51 -05:00
end
context 'both username and email is given' do
2013-02-05 14:16:51 -05:00
before do
xhr :get, :check_username, username: 'BruceWayne', email: 'brucie@gmail.com'
end
include_examples 'check_username when username is available everywhere'
2013-02-05 14:16:51 -05:00
end
context 'only email is given' do
it "should check for a matching username" do
UsernameCheckerService.any_instance.expects(:check_username).with(nil, 'brucie@gmail.com').returns({json: 'blah'})
xhr :get, :check_username, email: 'brucie@gmail.com'
response.should be_success
end
end
2013-02-05 14:16:51 -05:00
end
shared_examples 'when email is needed to check username match' 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
::JSON.parse(response.body)['available'].should be_false
end
it 'should not return a suggested username' do
::JSON.parse(response.body)['suggestion'].should_not be_present
end
end
context 'available locally but not globally' do
before do
DiscourseHub.stubs(:username_available?).returns([false, 'suggestion'])
2013-02-05 14:16:51 -05:00
end
context 'email param is not given' do
before do
xhr :get, :check_username, username: 'BruceWayne'
end
include_examples 'when email is needed to check username match'
2013-02-05 14:16:51 -05:00
end
context 'email param is an empty string' do
before do
xhr :get, :check_username, username: 'BruceWayne', email: ''
end
include_examples 'when email is needed to check username match'
2013-02-05 14:16:51 -05:00
end
context 'email matches global username' do
2013-02-05 14:16:51 -05:00
before do
DiscourseHub.stubs(:username_match?).returns([true, false, nil])
2013-02-05 14:16:51 -05:00
xhr :get, :check_username, username: 'BruceWayne', email: 'brucie@example.com'
end
2013-06-28 14:43:35 -04:00
include_examples 'when username is available everywhere'
2013-02-05 14:16:51 -05:00
it 'should indicate a global match' do
::JSON.parse(response.body)['global_match'].should be_true
end
end
context 'email does not match global username' do
2013-02-05 14:16:51 -05:00
before do
DiscourseHub.stubs(:username_match?).returns([false, false, 'suggestion'])
2013-02-05 14:16:51 -05:00
xhr :get, :check_username, username: 'BruceWayne', email: 'brucie@example.com'
end
2013-06-28 14:43:35 -04:00
include_examples 'when username is unavailable locally'
2013-02-05 14:16:51 -05:00
it 'should not indicate a global match' do
::JSON.parse(response.body)['global_match'].should be_false
end
end
end
context 'unavailable locally and globally' do
let!(:user) { Fabricate(:user) }
before do
DiscourseHub.stubs(:username_available?).returns([false, 'suggestion'])
2013-02-05 14:16:51 -05:00
xhr :get, :check_username, username: user.username
end
2013-06-28 14:43:35 -04:00
include_examples 'when username is unavailable locally'
2013-02-05 14:16:51 -05:00
end
context 'unavailable locally and available globally' do
let!(:user) { Fabricate(:user) }
before do
DiscourseHub.stubs(:username_available?).returns([true, nil])
2013-02-05 14:16:51 -05:00
xhr :get, :check_username, username: user.username
end
2013-06-28 14:43:35 -04:00
include_examples 'when username is unavailable locally'
2013-02-05 14:16:51 -05:00
end
end
context 'when discourse_org_access_key is wrong' do
before do
SiteSetting.stubs(:call_discourse_hub?).returns(true)
DiscourseHub.stubs(:username_available?).raises(RestClient::Forbidden)
DiscourseHub.stubs(:username_match?).raises(RestClient::Forbidden)
2013-02-05 14:16:51 -05:00
end
it 'should return an error message' do
xhr :get, :check_username, username: 'horsie'
json = JSON.parse(response.body)
json['errors'].should_not be_nil
json['errors'][0].should_not be_nil
end
end
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
include_examples 'when username is available everywhere'
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
include_examples 'when username is unavailable locally'
end
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
include_examples 'when username is available everywhere'
end
end
2013-02-05 14:16:51 -05:00
end
describe '#invited' do
2013-02-05 14:16:51 -05:00
it 'returns success' do
user = Fabricate(:user)
2013-02-05 14:16:51 -05:00
xhr :get, :invited, username: user.username
expect(response).to be_success
end
it 'filters by email' do
inviter = Fabricate(:user)
invitee = Fabricate(:user)
invite = Fabricate(
: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'
invites = JSON.parse(response.body)['invites']
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')
invite = Fabricate(
: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'
invites = JSON.parse(response.body)['invites']
expect(invites).to have(1).item
expect(invites.first).to include('email' => 'billybob@example.com')
2013-02-05 14:16:51 -05:00
end
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
invites = JSON.parse(response.body)['invites']
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
invites = JSON.parse(response.body)['invites']
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|
guardian.stubs(:can_see_invite_details?).
with(inviter).returns(true)
end
xhr :get, :invited, username: inviter.username
invites = JSON.parse(response.body)['invites']
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)
invitee = Fabricate(:user)
Fabricate(:invite, invited_by: inviter)
stub_guardian(user) do |guardian|
guardian.stubs(:can_see_invite_details?).
with(inviter).returns(false)
end
xhr :get, :invited, username: inviter.username
json = JSON.parse(response.body)['invites']
expect(json).to be_empty
end
end
end
context 'with redeemed invites' do
it 'returns invites' do
user = log_in
inviter = Fabricate(:user)
invitee = Fabricate(:user)
invite = Fabricate(:invite, invited_by: inviter, user: invitee)
xhr :get, :invited, username: inviter.username
invites = JSON.parse(response.body)['invites']
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
describe '#update' do
context 'with guest' do
it 'raises an error' do
expect do
xhr :put, :update, username: 'guest'
end.to raise_error(Discourse::NotLoggedIn)
2013-02-05 14:16:51 -05:00
end
end
context 'with authenticated user' do
context 'with permission to update' do
it 'allows the update' do
2013-11-01 14:42:16 -04:00
user = Fabricate(:user, name: 'Billy Bob')
log_in_user(user)
2013-02-05 14:16:51 -05:00
put :update, username: user.username, name: 'Jim Tom'
2013-02-05 14:16:51 -05:00
expect(response).to be_success
expect(user.reload.name).to eq 'Jim Tom'
end
2013-11-01 14:42:16 -04:00
it 'returns user JSON' do
user = log_in
put :update, username: user.username
json = JSON.parse(response.body)
expect(json['user']['id']).to eq user.id
end
end
2013-02-05 14:16:51 -05:00
context 'without permission to update' do
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
put :update, username: user.username, name: 'Jim Tom'
2013-02-05 14:16:51 -05: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
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
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
context "when `enable_names` is true" do
before do
SiteSetting.stubs(:enable_names?).returns(true)
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
describe 'send_activation_email' do
context 'for an existing user' do
let(:user) { Fabricate(:user) }
before do
UsersController.any_instance.stubs(:fetch_user_from_params).returns(user)
end
context 'with a valid email_token' do
it 'should send the activation email' do
Jobs.expects(:enqueue).with(:user_email, has_entries(type: :signup))
xhr :post, :send_activation_email, username: user.username
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 {
xhr :post, :send_activation_email, username: user.username
}.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))
xhr :post, :send_activation_email, username: user.username
end
end
end
context 'when username does not exist' do
it 'should not send an email' do
Jobs.expects(:enqueue).never
xhr :post, :send_activation_email, username: 'nopenopenopenope'
end
end
end
describe '.upload_user_image' do
2013-08-24 16:37:31 -04:00
it 'raises an error when not logged in' do
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-04-14 16:55:57 -04:00
let(:logo) { File.new("#{Rails.root}/spec/fixtures/images/logo.png") }
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
it 'raises an error without a user_image_type param' do
lambda { xhr :put, :upload_user_image, username: user.username }.should raise_error(ActionController::ParameterMissing)
end
2013-08-24 16:37:31 -04:00
describe "with uploaded file" do
2014-04-14 16:55:57 -04:00
it 'raises an error when you don\'t have permission to upload an user image' do
Guardian.any_instance.expects(:can_edit?).with(user).returns(false)
xhr :post, :upload_user_image, username: user.username, user_image_type: "avatar"
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)
xhr :post, :upload_user_image, username: user.username, file: user_image, user_image_type: "avatar"
2014-04-14 16:55:57 -04:00
response.status.should eq 422
end
it 'rejects unauthorized images' do
2014-04-14 16:55:57 -04:00
SiteSetting.stubs(:authorized_extensions).returns(".txt")
xhr :post, :upload_user_image, username: user.username, file: user_image, user_image_type: "avatar"
response.status.should eq 422
end
it 'is successful for avatars' do
upload = Fabricate(:upload)
Upload.expects(:create_for).returns(upload)
# enqueues the user_image generator job
Jobs.expects(:enqueue).with(:generate_avatars, { user_id: user.id, upload_id: upload.id })
xhr :post, :upload_user_image, username: user.username, file: user_image, user_image_type: "avatar"
user.reload
# erase the previous template
user.uploaded_avatar_template.should == nil
# link to the right upload
user.uploaded_avatar.id.should == upload.id
# automatically set "use_uploaded_user_image"
user.use_uploaded_avatar.should == true
# 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"
json['width'].should == 100
json['height'].should == 200
end
2014-04-14 16:55:57 -04:00
it 'is successful for profile backgrounds' do
upload = Fabricate(:upload)
Upload.expects(:create_for).returns(upload)
xhr :post, :upload_user_image, username: user.username, file: user_image, user_image_type: "profile_background"
user.reload
2014-04-14 16:55:57 -04:00
user.profile_background.should == "/uploads/default/1/1234567890123456.png"
# 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"
json['width'].should == 100
json['height'].should == 200
end
2014-04-14 16:55:57 -04:00
2013-08-24 16:37:31 -04:00
end
describe "with url" do
let(:user_image_url) { "http://cdn.discourse.org/assets/logo.png" }
2014-04-14 16:55:57 -04:00
before { UsersController.any_instance.stubs(:is_api?).returns(true) }
describe "correct urls" do
2014-04-14 16:55:57 -04:00
before { FileHelper.stubs(:download).returns(logo) }
it 'rejects large images' do
2014-04-14 16:55:57 -04:00
SiteSetting.stubs(:max_image_size_kb).returns(1)
xhr :post, :upload_user_image, username: user.username, file: user_image_url, user_image_type: "profile_background"
2014-04-14 16:55:57 -04:00
response.status.should eq 422
end
it 'rejects unauthorized images' do
2014-04-14 16:55:57 -04:00
SiteSetting.stubs(:authorized_extensions).returns(".txt")
xhr :post, :upload_user_image, username: user.username, file: user_image_url, user_image_type: "profile_background"
response.status.should eq 422
end
it 'is successful for avatars' do
upload = Fabricate(:upload)
Upload.expects(:create_for).returns(upload)
# enqueues the user_image generator job
Jobs.expects(:enqueue).with(:generate_avatars, { user_id: user.id, upload_id: upload.id })
xhr :post, :upload_avatar, username: user.username, file: user_image_url, user_image_type: "avatar"
user.reload
# erase the previous template
user.uploaded_avatar_template.should == nil
# link to the right upload
user.uploaded_avatar.id.should == upload.id
# automatically set "use_uploaded_user_image"
user.use_uploaded_avatar.should == true
# 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"
json['width'].should == 100
json['height'].should == 200
end
it 'is successful for profile backgrounds' do
upload = Fabricate(:upload)
Upload.expects(:create_for).returns(upload)
xhr :post, :upload_user_image, username: user.username, file: user_image_url, user_image_type: "profile_background"
user.reload
2014-04-14 16:55:57 -04:00
user.profile_background.should == "/uploads/default/1/1234567890123456.png"
# 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"
json['width'].should == 100
json['height'].should == 200
end
end
it "should handle malformed urls" do
xhr :post, :upload_user_image, username: user.username, file: "foobar", user_image_type: "profile_background"
response.status.should eq 422
end
2013-08-24 16:37:31 -04:00
end
end
end
describe '.toggle_avatar' do
it 'raises an error when not logged in' do
lambda { xhr :put, :toggle_avatar, username: 'asdf' }.should raise_error(Discourse::NotLoggedIn)
end
context 'while logged in' do
let!(:user) { log_in }
it 'raises an error without a use_uploaded_avatar param' do
lambda { xhr :put, :toggle_avatar, username: user.username }.should raise_error(ActionController::ParameterMissing)
end
it 'raises an error when you don\'t have permission to toggle the avatar' do
Guardian.any_instance.expects(:can_edit?).with(user).returns(false)
xhr :put, :toggle_avatar, username: user.username, use_uploaded_avatar: "true"
response.should be_forbidden
end
it 'it successful' do
xhr :put, :toggle_avatar, username: user.username, use_uploaded_avatar: "false"
user.reload.use_uploaded_avatar.should == false
response.should be_success
end
end
end
describe '.clear_profile_background' do
it 'raises an error when not logged in' do
lambda { xhr :put, :clear_profile_background, username: 'asdf' }.should raise_error(Discourse::NotLoggedIn)
end
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)
xhr :put, :clear_profile_background, username: user.username
response.should be_forbidden
end
it 'it successful' do
xhr :put, :clear_profile_background, username: user.username
user.reload.profile_background.should == ""
response.should be_success
end
end
end
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
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
end
end
2013-02-05 14:16:51 -05:00
end