controllers with rspec3 syntax

This commit is contained in:
Luciano Sousa
2015-01-09 14:04:02 -03:00
parent c96220ca76
commit bc73238c8f
50 changed files with 955 additions and 955 deletions
+181 -181
View File
@@ -7,28 +7,28 @@ describe UsersController do
it 'returns success' do
xhr :get, :show, username: user.username, format: :json
response.should be_success
expect(response).to be_success
json = JSON.parse(response.body)
json["user"]["has_title_badges"].should == false
expect(json["user"]["has_title_badges"]).to eq(false)
end
it "returns not found when the username doesn't exist" do
xhr :get, :show, username: 'madeuppity'
response.should_not be_success
expect(response).not_to be_success
end
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
expect(response).not_to 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
expect(response).to be_forbidden
end
context "fetching a user by external_id" do
@@ -36,33 +36,33 @@ describe UsersController do
it "returns fetch for a matching external_id" do
xhr :get, :show, external_id: '997'
response.should be_success
expect(response).to 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
expect(response).not_to be_success
end
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)
expect { get :user_preferences_redirect }.to 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")
expect(response).to redirect_to("/users/#{user.username_lower}/preferences")
end
end
describe '.authorize_email' do
it 'errors out for invalid tokens' do
get :authorize_email, token: 'asdfasdf'
response.should be_success
flash[:error].should be_present
expect(response).to be_success
expect(flash[:error]).to be_present
end
context 'valid token' do
@@ -71,9 +71,9 @@ describe UsersController do
email_token = user.email_tokens.create(email: user.email)
get :authorize_email, token: email_token.token
response.should be_success
flash[:error].should be_blank
session[:current_user_id].should be_present
expect(response).to be_success
expect(flash[:error]).to be_blank
expect(session[:current_user_id]).to be_present
end
end
end
@@ -90,11 +90,11 @@ describe UsersController do
end
it 'return success' do
response.should be_success
expect(response).to be_success
end
it 'sets a flash error' do
flash[:error].should be_present
expect(flash[:error]).to be_present
end
end
@@ -123,7 +123,7 @@ describe UsersController 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
expect(response).not_to be_success
end
end
@@ -135,19 +135,19 @@ describe UsersController do
end
it 'returns success' do
response.should be_success
expect(response).to be_success
end
it "doesn't set an error" do
flash[:error].should be_blank
expect(flash[:error]).to be_blank
end
it 'logs in as the user' do
session[:current_user_id].should be_present
expect(session[:current_user_id]).to be_present
end
it "doesn't set @needs_approval" do
assigns[:needs_approval].should be_blank
expect(assigns[:needs_approval]).to be_blank
end
end
@@ -159,19 +159,19 @@ describe UsersController do
end
it 'returns success' do
response.should be_success
expect(response).to be_success
end
it 'sets @needs_approval' do
assigns[:needs_approval].should be_present
expect(assigns[:needs_approval]).to be_present
end
it "doesn't set an error" do
flash[:error].should be_blank
expect(flash[:error]).to be_blank
end
it "doesn't log the user in" do
session[:current_user_id].should be_blank
expect(session[:current_user_id]).to be_blank
end
end
@@ -182,30 +182,30 @@ describe UsersController do
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)
expect { xhr :put, :change_email, username: 'asdf', email: new_email }.to raise_error(Discourse::NotLoggedIn)
end
context 'when logged in' do
let!(:user) { log_in }
it 'raises an error without an email parameter' do
lambda { xhr :put, :change_email, username: user.username }.should raise_error(ActionController::ParameterMissing)
expect { xhr :put, :change_email, username: user.username }.to raise_error(ActionController::ParameterMissing)
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)
xhr :put, :change_email, username: user.username, email: new_email
response.should be_forbidden
expect(response).to 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)
expect { xhr :put, :change_email, username: user.username, email: other_user.email }.to 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)
expect { xhr :put, :change_email, username: user.username, email: other_user.email + ' ' }.to raise_error(Discourse::InvalidParameters)
end
end
@@ -213,14 +213,14 @@ describe UsersController 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)
expect { xhr :put, :change_email, username: user.username, email: other_user.email.upcase }.to raise_error(Discourse::InvalidParameters)
end
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)
expect { xhr :put, :change_email, username: user.username, email: new_email }.to change(EmailToken, :count)
end
it 'enqueues an email authorization' do
@@ -239,7 +239,7 @@ describe UsersController do
it "returns success" do
SiteSetting.login_required = true
get :password_reset, token: 'asdfasdf'
response.should be_success
expect(response).to be_success
end
end
@@ -249,10 +249,10 @@ describe UsersController do
end
it 'disallows login' do
flash[:error].should be_present
session[:current_user_id].should be_blank
assigns[:invalid_token].should == nil
response.should be_success
expect(flash[:error]).to be_present
expect(session[:current_user_id]).to be_blank
expect(assigns[:invalid_token]).to eq(nil)
expect(response).to be_success
end
end
@@ -262,10 +262,10 @@ describe UsersController do
end
it 'disallows login' do
flash[:error].should be_present
session[:current_user_id].should be_blank
assigns[:invalid_token].should == true
response.should be_success
expect(flash[:error]).to be_present
expect(session[:current_user_id]).to be_blank
expect(assigns[:invalid_token]).to eq(true)
expect(response).to be_success
end
end
@@ -276,8 +276,8 @@ describe UsersController do
get :password_reset, token: token
put :password_reset, token: token, password: 'newpassword'
response.should be_success
flash[:error].should be_blank
expect(response).to be_success
expect(flash[:error]).to be_blank
end
end
@@ -289,27 +289,27 @@ describe UsersController do
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
expect(assigns(:user).errors).to be_present
expect(session[:current_user_id]).to 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
expect(assigns(:user).errors).to be_present
expect(session[:current_user_id]).to be_blank
end
it "logs in the user" do
put :password_reset, token: token, password: 'newpassword'
assigns(:user).errors.should be_blank
session[:current_user_id].should be_present
expect(assigns(:user).errors).to be_blank
expect(session[:current_user_id]).to 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: token, password: 'newpassword'
assigns(:user).errors.should be_blank
session[:current_user_id].should be_blank
expect(assigns(:user).errors).to be_blank
expect(session[:current_user_id]).to be_blank
end
end
end
@@ -345,8 +345,8 @@ describe UsersController do
SiteSetting.stubs(:allow_new_registrations).returns(false)
post_user
json = JSON.parse(response.body)
json['success'].should == false
json['message'].should be_present
expect(json['success']).to eq(false)
expect(json['message']).to be_present
end
it 'creates a user correctly' do
@@ -358,7 +358,7 @@ describe UsersController do
expect(JSON.parse(response.body)['active']).to be_falsey
# should save user_created_message in session
session["user_created_message"].should be_present
expect(session["user_created_message"]).to be_present
end
context "and 'must approve users' site setting is enabled" do
@@ -394,7 +394,7 @@ describe UsersController do
post_user
# should save user_created_message in session
session["user_created_message"].should be_present
expect(session["user_created_message"]).to be_present
end
it "shows the 'active' message" do
@@ -408,7 +408,7 @@ describe UsersController do
it "should be logged in" do
User.any_instance.expects(:enqueue_welcome_message)
post_user
session[:current_user_id].should be_present
expect(session[:current_user_id]).to be_present
end
it 'indicates the user is active in the response' do
@@ -421,8 +421,8 @@ describe UsersController do
SiteSetting.stubs(:allow_new_registrations).returns(false)
post_user
json = JSON.parse(response.body)
json['success'].should == false
json['message'].should be_present
expect(json['success']).to eq(false)
expect(json['message']).to be_present
end
context 'authentication records for' do
@@ -448,16 +448,16 @@ describe UsersController do
before { post_user }
it 'should succeed' do
should respond_with(:success)
is_expected.to respond_with(:success)
end
it 'has the proper JSON' do
json = JSON::parse(response.body)
json["success"].should == true
expect(json["success"]).to eq(true)
end
it 'should not result in an active account' do
User.find_by(username: @user.username).active.should == false
expect(User.find_by(username: @user.username).active).to eq(false)
end
end
@@ -476,10 +476,10 @@ describe UsersController do
it 'should say it was successful' do
xhr :post, :create, create_params
json = JSON::parse(response.body)
json["success"].should == true
expect(json["success"]).to eq(true)
# should not change the session
session["user_created_message"].should be_blank
expect(session["user_created_message"]).to be_blank
end
end
@@ -520,10 +520,10 @@ describe UsersController do
it 'should report failed' do
xhr :post, :create, create_params
json = JSON::parse(response.body)
json["success"].should_not == true
expect(json["success"]).not_to eq(true)
# should not change the session
session["user_created_message"].should be_blank
expect(session["user_created_message"]).to be_blank
end
end
@@ -580,25 +580,25 @@ describe UsersController do
it "should succeed without the optional field" do
xhr :post, :create, create_params
response.should be_success
expect(response).to 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 be_blank
expect(inserted).to be_present
expect(inserted.custom_fields).to be_present
expect(inserted.custom_fields["user_field_#{user_field.id}"]).to eq('value1')
expect(inserted.custom_fields["user_field_#{another_field.id}"]).to eq('value2')
expect(inserted.custom_fields["user_field_#{optional_field.id}"]).to be_blank
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
expect(response).to 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'
expect(inserted).to be_present
expect(inserted.custom_fields).to be_present
expect(inserted.custom_fields["user_field_#{user_field.id}"]).to eq('value1')
expect(inserted.custom_fields["user_field_#{another_field.id}"]).to eq('value2')
expect(inserted.custom_fields["user_field_#{optional_field.id}"]).to eq('value3')
end
end
@@ -608,7 +608,7 @@ describe UsersController do
context '.username' do
it 'raises an error when not logged in' do
lambda { xhr :put, :username, username: 'somename' }.should raise_error(Discourse::NotLoggedIn)
expect { xhr :put, :username, username: 'somename' }.to raise_error(Discourse::NotLoggedIn)
end
context 'while logged in' do
@@ -616,29 +616,29 @@ describe UsersController do
let(:new_username) { "#{user.username}1234" }
it 'raises an error without a new_username param' do
lambda { xhr :put, :username, username: user.username }.should raise_error(ActionController::ParameterMissing)
expect { xhr :put, :username, username: user.username }.to raise_error(ActionController::ParameterMissing)
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)
xhr :put, :username, username: user.username, new_username: new_username
response.should be_forbidden
expect(response).to be_forbidden
end
it 'raises an error when change_username fails' do
User.any_instance.expects(:change_username).with(new_username).returns(false)
lambda { xhr :put, :username, username: user.username, new_username: new_username }.should raise_error(Discourse::InvalidParameters)
expect { xhr :put, :username, username: user.username, new_username: new_username }.to raise_error(Discourse::InvalidParameters)
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
expect(response).to be_success
end
it 'should return a JSON response with the updated username' do
xhr :put, :username, username: user.username, new_username: new_username
::JSON.parse(response.body)['username'].should == new_username
expect(::JSON.parse(response.body)['username']).to eq(new_username)
end
end
@@ -646,36 +646,36 @@ describe UsersController do
context '.check_username' do
it 'raises an error without any parameters' do
lambda { xhr :get, :check_username }.should raise_error(ActionController::ParameterMissing)
expect { xhr :get, :check_username }.to raise_error(ActionController::ParameterMissing)
end
shared_examples 'when username is unavailable' do
it 'should return success' do
response.should be_success
expect(response).to be_success
end
it 'should return available as false in the JSON' do
::JSON.parse(response.body)['available'].should == false
expect(::JSON.parse(response.body)['available']).to eq(false)
end
it 'should return a suggested username' do
::JSON.parse(response.body)['suggestion'].should be_present
expect(::JSON.parse(response.body)['suggestion']).to be_present
end
end
shared_examples 'when username is available' do
it 'should return success' do
response.should be_success
expect(response).to be_success
end
it 'should return available in the JSON' do
::JSON.parse(response.body)['available'].should == true
expect(::JSON.parse(response.body)['available']).to eq(true)
end
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
expect(response).to be_success
end
context 'username is available' do
@@ -695,15 +695,15 @@ describe UsersController do
shared_examples 'checking an invalid username' do
it 'should return success' do
response.should be_success
expect(response).to be_success
end
it 'should not return an available key' do
::JSON.parse(response.body)['available'].should == nil
expect(::JSON.parse(response.body)['available']).to eq(nil)
end
it 'should return an error message' do
::JSON.parse(response.body)['errors'].should_not be_empty
expect(::JSON.parse(response.body)['errors']).not_to be_empty
end
end
@@ -714,7 +714,7 @@ describe UsersController do
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'))
expect(::JSON.parse(response.body)['errors']).to include(I18n.t(:'user.username.characters'))
end
end
@@ -725,7 +725,7 @@ describe UsersController do
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))
expect(::JSON.parse(response.body)['errors']).to include(I18n.t(:'user.username.long', max: User.username_length.end))
end
end
@@ -787,7 +787,7 @@ describe UsersController do
xhr :get, :invited, username: inviter.username, filter: 'billybob'
invites = JSON.parse(response.body)['invites']
invites.size.should == 1
expect(invites.size).to eq(1)
expect(invites.first).to include('email' => 'billybob@example.com')
end
@@ -809,7 +809,7 @@ describe UsersController do
xhr :get, :invited, username: inviter.username, filter: 'billybob'
invites = JSON.parse(response.body)['invites']
invites.size.should == 1
expect(invites.size).to eq(1)
expect(invites.first).to include('email' => 'billybob@example.com')
end
@@ -835,7 +835,7 @@ describe UsersController do
xhr :get, :invited, username: inviter.username
invites = JSON.parse(response.body)['invites']
invites.size.should == 1
expect(invites.size).to eq(1)
expect(invites.first).to include('email' => invite.email)
end
end
@@ -856,7 +856,7 @@ describe UsersController do
xhr :get, :invited, username: inviter.username
invites = JSON.parse(response.body)['invites']
invites.size.should == 1
expect(invites.size).to eq(1)
expect(invites.first).to include("email" => invite.email)
end
end
@@ -890,7 +890,7 @@ describe UsersController do
xhr :get, :invited, username: inviter.username
invites = JSON.parse(response.body)['invites']
invites.size.should == 1
expect(invites.size).to eq(1)
expect(invites.first).to include('email' => invite.email)
end
end
@@ -933,8 +933,8 @@ describe UsersController do
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'
expect(response).not_to be_success
expect(user.user_fields[user_field.id.to_s]).not_to eq('happy')
end
end
@@ -984,15 +984,15 @@ describe UsersController do
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
expect(user.user_profile.reload.card_image_badge_id).to 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
expect(user.user_profile.reload.card_image_badge_id).to eq(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
expect(user.user_profile.reload.card_image_badge_id).to be_blank
end
end
@@ -1004,16 +1004,16 @@ describe UsersController do
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
expect(user.reload.title).not_to eq(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
user.user_profile.badge_granted_title.should == true
expect(user.reload.title).to eq(badge.name)
expect(user.user_profile.badge_granted_title).to eq(true)
user.title = "testing"
user.save
user.user_profile.reload
user.user_profile.badge_granted_title.should == false
expect(user.user_profile.badge_granted_title).to eq(false)
end
end
@@ -1030,23 +1030,23 @@ describe UsersController do
it "searches when provided the term only" do
xhr :post, :search_users, term: user.name.split(" ").last
response.should be_success
expect(response).to be_success
json = JSON.parse(response.body)
json["users"].map { |u| u["username"] }.should include(user.username)
expect(json["users"].map { |u| u["username"] }).to include(user.username)
end
it "searches when provided the topic only" do
xhr :post, :search_users, topic_id: topic.id
response.should be_success
expect(response).to be_success
json = JSON.parse(response.body)
json["users"].map { |u| u["username"] }.should include(user.username)
expect(json["users"].map { |u| u["username"] }).to 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
expect(response).to be_success
json = JSON.parse(response.body)
json["users"].map { |u| u["username"] }.should include(user.username)
expect(json["users"].map { |u| u["username"] }).to include(user.username)
end
context "when `enable_names` is true" do
@@ -1057,7 +1057,7 @@ describe UsersController do
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)
expect(json["users"].map { |u| u["name"] }).to include(user.name)
end
end
@@ -1069,7 +1069,7 @@ describe UsersController do
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)
expect(json["users"].map { |u| u["name"] }).not_to include(user.name)
end
end
@@ -1116,7 +1116,7 @@ describe UsersController do
describe '.upload_user_image' do
it 'raises an error when not logged in' do
lambda { xhr :put, :upload_user_image, username: 'asdf' }.should raise_error(Discourse::NotLoggedIn)
expect { xhr :put, :upload_user_image, username: 'asdf' }.to raise_error(Discourse::NotLoggedIn)
end
context 'while logged in' do
@@ -1130,7 +1130,7 @@ describe UsersController do
end
it 'raises an error without a image_type param' do
lambda { xhr :put, :upload_user_image, username: user.username }.should raise_error(ActionController::ParameterMissing)
expect { xhr :put, :upload_user_image, username: user.username }.to raise_error(ActionController::ParameterMissing)
end
describe "with uploaded file" do
@@ -1138,19 +1138,19 @@ describe UsersController do
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, image_type: "avatar"
response.should be_forbidden
expect(response).to be_forbidden
end
it 'rejects large images' do
SiteSetting.stubs(:max_image_size_kb).returns(1)
xhr :post, :upload_user_image, username: user.username, file: user_image, image_type: "avatar"
response.status.should eq 422
expect(response.status).to eq 422
end
it 'rejects unauthorized images' do
SiteSetting.stubs(:authorized_extensions).returns(".txt")
xhr :post, :upload_user_image, username: user.username, file: user_image, image_type: "avatar"
response.status.should eq 422
expect(response.status).to eq 422
end
it 'is successful for avatars' do
@@ -1160,10 +1160,10 @@ describe UsersController do
xhr :post, :upload_user_image, username: user.username, file: user_image, image_type: "avatar"
# 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
json['upload_id'].should == upload.id
expect(json['url']).to eq("/uploads/default/1/1234567890123456.png")
expect(json['width']).to eq(100)
expect(json['height']).to eq(200)
expect(json['upload_id']).to eq(upload.id)
end
it 'is successful for profile backgrounds' do
@@ -1172,13 +1172,13 @@ describe UsersController do
xhr :post, :upload_user_image, username: user.username, file: user_image, image_type: "profile_background"
user.reload
user.user_profile.profile_background.should == "/uploads/default/1/1234567890123456.png"
expect(user.user_profile.profile_background).to eq("/uploads/default/1/1234567890123456.png")
# 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
expect(json['url']).to eq("/uploads/default/1/1234567890123456.png")
expect(json['width']).to eq(100)
expect(json['height']).to eq(200)
end
it 'is successful for card backgrounds' do
@@ -1187,13 +1187,13 @@ describe UsersController do
xhr :post, :upload_user_image, username: user.username, file: user_image, image_type: "card_background"
user.reload
user.user_profile.card_background.should == "/uploads/default/1/1234567890123456.png"
expect(user.user_profile.card_background).to eq("/uploads/default/1/1234567890123456.png")
# 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
expect(json['url']).to eq("/uploads/default/1/1234567890123456.png")
expect(json['width']).to eq(100)
expect(json['height']).to eq(200)
end
end
@@ -1210,13 +1210,13 @@ describe UsersController do
it 'rejects large images' do
SiteSetting.stubs(:max_image_size_kb).returns(1)
xhr :post, :upload_user_image, username: user.username, file: user_image_url, image_type: "profile_background"
response.status.should eq 422
expect(response.status).to eq 422
end
it 'rejects unauthorized images' do
SiteSetting.stubs(:authorized_extensions).returns(".txt")
xhr :post, :upload_user_image, username: user.username, file: user_image_url, image_type: "profile_background"
response.status.should eq 422
expect(response.status).to eq 422
end
it 'is successful for avatars' do
@@ -1225,10 +1225,10 @@ describe UsersController do
# enqueues the user_image generator job
xhr :post, :upload_user_image, username: user.username, file: user_image_url, image_type: "avatar"
json = JSON.parse(response.body)
json['url'].should == "/uploads/default/1/1234567890123456.png"
json['width'].should == 100
json['height'].should == 200
json['upload_id'].should == upload.id
expect(json['url']).to eq("/uploads/default/1/1234567890123456.png")
expect(json['width']).to eq(100)
expect(json['height']).to eq(200)
expect(json['upload_id']).to eq(upload.id)
end
it 'is successful for profile backgrounds' do
@@ -1236,13 +1236,13 @@ describe UsersController do
Upload.expects(:create_for).returns(upload)
xhr :post, :upload_user_image, username: user.username, file: user_image_url, image_type: "profile_background"
user.reload
user.user_profile.profile_background.should == "/uploads/default/1/1234567890123456.png"
expect(user.user_profile.profile_background).to eq("/uploads/default/1/1234567890123456.png")
# 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
expect(json['url']).to eq("/uploads/default/1/1234567890123456.png")
expect(json['width']).to eq(100)
expect(json['height']).to eq(200)
end
it 'is successful for card backgrounds' do
@@ -1250,19 +1250,19 @@ describe UsersController do
Upload.expects(:create_for).returns(upload)
xhr :post, :upload_user_image, username: user.username, file: user_image_url, image_type: "card_background"
user.reload
user.user_profile.card_background.should == "/uploads/default/1/1234567890123456.png"
expect(user.user_profile.card_background).to eq("/uploads/default/1/1234567890123456.png")
# 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
expect(json['url']).to eq("/uploads/default/1/1234567890123456.png")
expect(json['width']).to eq(100)
expect(json['height']).to eq(200)
end
end
it "should handle malformed urls" do
xhr :post, :upload_user_image, username: user.username, file: "foobar", image_type: "profile_background"
response.status.should eq 422
expect(response.status).to eq 422
end
end
@@ -1274,7 +1274,7 @@ describe UsersController do
describe '.pick_avatar' do
it 'raises an error when not logged in' do
lambda { xhr :put, :pick_avatar, username: 'asdf', avatar_id: 1}.should raise_error(Discourse::NotLoggedIn)
expect { xhr :put, :pick_avatar, username: 'asdf', avatar_id: 1}.to raise_error(Discourse::NotLoggedIn)
end
context 'while logged in' do
@@ -1284,25 +1284,25 @@ describe UsersController do
it 'raises an error when you don\'t have permission to toggle the avatar' do
another_user = Fabricate(:user)
xhr :put, :pick_avatar, username: another_user.username, upload_id: 1
response.should be_forbidden
expect(response).to be_forbidden
end
it 'it successful' do
xhr :put, :pick_avatar, username: user.username, upload_id: 111
user.reload.uploaded_avatar_id.should == 111
response.should be_success
expect(user.reload.uploaded_avatar_id).to eq(111)
expect(response).to be_success
xhr :put, :pick_avatar, username: user.username
user.reload.uploaded_avatar_id.should == nil
response.should be_success
expect(user.reload.uploaded_avatar_id).to eq(nil)
expect(response).to be_success
end
it 'returns success' do
xhr :put, :pick_avatar, username: user.username, upload_id: 111
user.reload.uploaded_avatar_id.should == 111
response.should be_success
expect(user.reload.uploaded_avatar_id).to eq(111)
expect(response).to be_success
json = ::JSON.parse(response.body)
json['success'].should == "OK"
expect(json['success']).to eq("OK")
end
end
@@ -1311,7 +1311,7 @@ describe UsersController do
describe '.destroy_user_image' do
it 'raises an error when not logged in' do
lambda { xhr :delete, :destroy_user_image, type: 'profile_background', username: 'asdf' }.should raise_error(Discourse::NotLoggedIn)
expect { xhr :delete, :destroy_user_image, type: 'profile_background', username: 'asdf' }.to raise_error(Discourse::NotLoggedIn)
end
context 'while logged in' do
@@ -1321,21 +1321,21 @@ describe UsersController do
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 :delete, :destroy_user_image, username: user.username, image_type: 'profile_background'
response.should be_forbidden
expect(response).to be_forbidden
end
it "requires the `image_type` param" do
-> { xhr :delete, :destroy_user_image, username: user.username }.should raise_error(ActionController::ParameterMissing)
expect { xhr :delete, :destroy_user_image, username: user.username }.to raise_error(ActionController::ParameterMissing)
end
it "only allows certain `image_types`" do
-> { xhr :delete, :destroy_user_image, username: user.username, image_type: 'wat' }.should raise_error(Discourse::InvalidParameters)
expect { xhr :delete, :destroy_user_image, username: user.username, image_type: 'wat' }.to raise_error(Discourse::InvalidParameters)
end
it 'can clear the profile background' do
xhr :delete, :destroy_user_image, image_type: 'profile_background', username: user.username
user.reload.user_profile.profile_background.should == ""
response.should be_success
expect(user.reload.user_profile.profile_background).to eq("")
expect(response).to be_success
end
end
@@ -1343,7 +1343,7 @@ describe UsersController do
describe '.destroy' do
it 'raises an error when not logged in' do
lambda { xhr :delete, :destroy, username: 'nobody' }.should raise_error(Discourse::NotLoggedIn)
expect { xhr :delete, :destroy, username: 'nobody' }.to raise_error(Discourse::NotLoggedIn)
end
context 'while logged in' do
@@ -1353,20 +1353,20 @@ describe UsersController 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
expect(response).to 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
expect(response).to 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
expect(response).to be_success
end
end
end
@@ -1375,8 +1375,8 @@ describe UsersController 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
expect(response).not_to be_success
expect(response).not_to be_redirect
end
context "when the user is logged in" do
@@ -1384,17 +1384,17 @@ describe UsersController do
it "will not redirect to an invalid path" do
get :my_redirect, path: "wat/..password.txt"
response.should_not be_redirect
expect(response).not_to be_redirect
end
it "will redirect to an valid path" do
get :my_redirect, path: "preferences"
response.should be_redirect
expect(response).to be_redirect
end
it "permits forward slashes" do
get :my_redirect, path: "activity/posts"
response.should be_redirect
expect(response).to be_redirect
end
end
end
@@ -1402,7 +1402,7 @@ describe UsersController do
describe '.check_emails' do
it 'raises an error when not logged in' do
lambda { xhr :put, :check_emails, username: 'zogstrip' }.should raise_error(Discourse::NotLoggedIn)
expect { xhr :put, :check_emails, username: 'zogstrip' }.to raise_error(Discourse::NotLoggedIn)
end
context 'while logged in' do
@@ -1411,26 +1411,26 @@ describe UsersController do
it "raises an error when you aren't allowed to check emails" do
Guardian.any_instance.expects(:can_check_emails?).returns(false)
xhr :put, :check_emails, username: Fabricate(:user).username
response.should be_forbidden
expect(response).to 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)
xhr :put, :check_emails, username: Fabricate(:user).username
response.should be_success
expect(response).to be_success
json = JSON.parse(response.body)
json["email"].should be_present
json["associated_accounts"].should be_present
expect(json["email"]).to be_present
expect(json["associated_accounts"]).to be_present
end
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
expect(response).to be_success
json = JSON.parse(response.body)
json["email"].should be_present
json["associated_accounts"].should be_present
expect(json["email"]).to be_present
expect(json["associated_accounts"]).to be_present
end
end