2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
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
|
|
|
|
2015-10-28 14:56:08 -04:00
|
|
|
context "anon" do
|
2014-07-09 01:31:49 -04:00
|
|
|
|
2015-10-28 14:56:08 -04:00
|
|
|
let(:user) { Discourse.system_user }
|
2014-07-09 01:31:49 -04:00
|
|
|
|
2015-10-28 14:56:08 -04:00
|
|
|
it "returns success" do
|
2017-08-31 00:06:56 -04:00
|
|
|
get :show, params: { username: user.username }, format: :json
|
2015-10-28 14:56:08 -04:00
|
|
|
expect(response).to be_success
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2017-04-20 03:30:45 -04:00
|
|
|
it "should redirect to login page for anonymous user when profiles are hidden" do
|
2017-02-13 14:31:15 -05:00
|
|
|
SiteSetting.hide_user_profiles_from_public = true
|
2017-08-31 00:06:56 -04:00
|
|
|
get :show, params: { username: user.username }, format: :json
|
2017-04-20 03:30:45 -04:00
|
|
|
expect(response).to redirect_to '/login'
|
2015-10-28 14:56:08 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-08-13 13:30:25 -04:00
|
|
|
end
|
|
|
|
|
2015-10-28 14:56:08 -04:00
|
|
|
context "logged in" do
|
|
|
|
|
|
|
|
let(:user) { log_in }
|
2014-06-18 14:40:15 -04:00
|
|
|
|
2015-10-28 14:56:08 -04:00
|
|
|
it 'returns success' do
|
2017-08-31 00:06:56 -04:00
|
|
|
get :show, params: { username: user.username, format: :json }, format: :json
|
2015-10-28 14:56:08 -04:00
|
|
|
expect(response).to be_success
|
|
|
|
json = JSON.parse(response.body)
|
2015-09-14 03:51:17 -04:00
|
|
|
|
2015-10-28 14:56:08 -04:00
|
|
|
expect(json["user"]["has_title_badges"]).to eq(false)
|
2015-09-14 03:51:17 -04:00
|
|
|
end
|
|
|
|
|
2015-10-28 14:56:08 -04:00
|
|
|
it "returns not found when the username doesn't exist" do
|
2017-08-31 00:06:56 -04:00
|
|
|
get :show, params: { username: 'madeuppity' }, format: :json
|
2015-10-28 14:56:08 -04:00
|
|
|
expect(response).not_to be_success
|
2015-09-14 03:51:17 -04:00
|
|
|
end
|
|
|
|
|
2015-10-28 14:56:08 -04:00
|
|
|
it 'returns not found when the user is inactive' do
|
|
|
|
inactive = Fabricate(:user, active: false)
|
2017-08-31 00:06:56 -04:00
|
|
|
get :show, params: { username: inactive.username }, format: :json
|
2015-10-28 14:56:08 -04:00
|
|
|
expect(response).not_to be_success
|
2015-09-14 03:51:17 -04:00
|
|
|
end
|
|
|
|
|
2017-12-07 01:23:27 -05:00
|
|
|
it 'returns success when show_inactive_accounts is true and user is logged in' do
|
|
|
|
SiteSetting.show_inactive_accounts = true
|
|
|
|
log_in_user(user)
|
|
|
|
inactive = Fabricate(:user, active: false)
|
|
|
|
get :show, params: { username: inactive.username }, format: :json
|
|
|
|
expect(response).to be_success
|
|
|
|
end
|
|
|
|
|
2015-10-28 14:56:08 -04:00
|
|
|
it "raises an error on invalid access" do
|
|
|
|
Guardian.any_instance.expects(:can_see?).with(user).returns(false)
|
2017-08-31 00:06:56 -04:00
|
|
|
get :show, params: { username: user.username }, format: :json
|
2015-10-28 14:56:08 -04:00
|
|
|
expect(response).to be_forbidden
|
2015-09-14 03:51:17 -04:00
|
|
|
end
|
|
|
|
|
2015-10-28 14:56:08 -04:00
|
|
|
describe "user profile views" do
|
|
|
|
let(:other_user) { Fabricate(:user) }
|
2014-06-18 14:40:15 -04:00
|
|
|
|
2015-10-28 14:56:08 -04:00
|
|
|
it "should track a user profile view for a signed in user" do
|
|
|
|
UserProfileView.expects(:add).with(other_user.user_profile.id, request.remote_ip, user.id)
|
2017-08-31 00:06:56 -04:00
|
|
|
get :show, params: { username: other_user.username }, format: :json
|
2015-10-28 14:56:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should not track a user profile view for a user viewing his own profile" do
|
|
|
|
UserProfileView.expects(:add).never
|
2017-08-31 00:06:56 -04:00
|
|
|
get :show, params: { username: user.username }, format: :json
|
2015-10-28 14:56:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should track a user profile view for an anon user" do
|
|
|
|
UserProfileView.expects(:add).with(other_user.user_profile.id, request.remote_ip, nil)
|
2017-08-31 00:06:56 -04:00
|
|
|
get :show, params: { username: other_user.username }, format: :json
|
2015-10-28 14:56:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "skips tracking" do
|
|
|
|
UserProfileView.expects(:add).never
|
2017-08-31 00:06:56 -04:00
|
|
|
get :show, params: { username: user.username, skip_track_visit: true }, format: :json
|
2015-10-28 14:56:08 -04:00
|
|
|
end
|
2014-06-18 14:40:15 -04:00
|
|
|
end
|
|
|
|
|
2015-10-28 14:56:08 -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
|
2017-08-31 00:06:56 -04:00
|
|
|
get :show, params: { external_id: '997' }, format: :json
|
2015-10-28 14:56:08 -04:00
|
|
|
expect(response).to be_success
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns not found when external_id doesn't match" do
|
2017-08-31 00:06:56 -04:00
|
|
|
get :show, params: { external_id: '99' }, format: :json
|
2015-10-28 14:56:08 -04:00
|
|
|
expect(response).not_to be_success
|
|
|
|
end
|
2014-06-18 14:40:15 -04:00
|
|
|
end
|
2015-10-28 14:56:08 -04:00
|
|
|
|
2017-09-14 14:08:16 -04:00
|
|
|
describe "include_post_count_for" do
|
|
|
|
|
|
|
|
let(:admin) { Fabricate(:admin) }
|
|
|
|
let(:topic) { Fabricate(:topic) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
Fabricate(:post, user: user, topic: topic)
|
|
|
|
Fabricate(:post, user: admin, topic: topic)
|
|
|
|
Fabricate(:post, user: admin, topic: topic, post_type: Post.types[:whisper])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "includes only visible posts" do
|
2017-08-31 00:06:56 -04:00
|
|
|
get :show,
|
|
|
|
params: { username: admin.username, include_post_count_for: topic.id },
|
|
|
|
format: :json
|
|
|
|
|
2017-09-14 14:08:16 -04:00
|
|
|
topic_post_count = JSON.parse(response.body).dig("user", "topic_post_count")
|
|
|
|
expect(topic_post_count[topic.id.to_s]).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "includes all post types for staff members" do
|
|
|
|
log_in_user(admin)
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
get :show,
|
|
|
|
params: { username: admin.username, include_post_count_for: topic.id },
|
|
|
|
format: :json
|
|
|
|
|
2017-09-14 14:08:16 -04:00
|
|
|
topic_post_count = JSON.parse(response.body).dig("user", "topic_post_count")
|
|
|
|
expect(topic_post_count[topic.id.to_s]).to eq(2)
|
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
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
|
|
|
|
|
|
|
|
it 'return success' do
|
2015-02-19 18:28:38 -05:00
|
|
|
EmailToken.expects(:confirm).with('asdfasdf').returns(nil)
|
2017-08-31 00:06:56 -04:00
|
|
|
put :perform_account_activation, params: { token: 'asdfasdf' }
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
|
|
|
expect(flash[:error]).to 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')
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
put :perform_account_activation, params: { 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
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
put :perform_account_activation, params: { 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)
|
2017-08-31 00:06:56 -04:00
|
|
|
put :perform_account_activation, params: { token: 'asdfasdf' }, format: :json
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).not_to be_success
|
2014-07-15 14:07:19 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-03-26 00:39:44 -04:00
|
|
|
context 'response' do
|
2017-08-31 00:06:56 -04:00
|
|
|
render_views
|
|
|
|
|
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)
|
|
|
|
end
|
|
|
|
|
2017-01-31 17:21:37 -05:00
|
|
|
it 'correctly logs on user' do
|
2017-06-01 04:19:42 -04:00
|
|
|
events = DiscourseEvent.track_events do
|
2017-08-31 00:06:56 -04:00
|
|
|
put :perform_account_activation, params: { token: 'asdfasdf' }
|
2017-06-01 04:19:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
expect(events.map { |event| event[:event_name] }).to include(
|
|
|
|
:user_logged_in, :user_first_logged_in
|
|
|
|
)
|
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
|
|
|
expect(flash[:error]).to be_blank
|
|
|
|
expect(session[:current_user_id]).to be_present
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
|
|
|
|
expect(CGI.unescapeHTML(response.body))
|
|
|
|
.to_not include(I18n.t('activation.approval_required'))
|
2013-02-07 06:50:59 -05:00
|
|
|
end
|
2017-01-31 17:21:37 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-04-03 12:23:28 -04:00
|
|
|
context 'user is not approved' do
|
2017-08-31 00:06:56 -04:00
|
|
|
render_views
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
before do
|
2017-09-12 15:04:39 -04:00
|
|
|
SiteSetting.must_approve_users = true
|
2013-02-05 14:16:51 -05:00
|
|
|
EmailToken.expects(:confirm).with('asdfasdf').returns(user)
|
2017-08-31 00:06:56 -04:00
|
|
|
put :perform_account_activation, params: { token: 'asdfasdf' }
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
it 'should return the right response' do
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
expect(CGI.unescapeHTML(response.body))
|
|
|
|
.to include(I18n.t('activation.approval_required'))
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
expect(response.body).to_not have_tag(:script, with: {
|
|
|
|
src: '/assets/application.js'
|
|
|
|
})
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
expect(flash[:error]).to be_blank
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(session[:current_user_id]).to be_blank
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-15 21:48:51 -04:00
|
|
|
describe '#perform_account_activation' do
|
|
|
|
describe 'when cookies contains a destination URL' do
|
|
|
|
let(:token) { 'asdadwewq' }
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
UsersController.any_instance.stubs(:honeypot_or_challenge_fails?).returns(false)
|
|
|
|
EmailToken.expects(:confirm).with(token).returns(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should redirect to the URL' do
|
|
|
|
destination_url = 'http://thisisasite.com/somepath'
|
|
|
|
request.cookies[:destination_url] = destination_url
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
put :perform_account_activation, params: { token: token }
|
2017-03-15 21:48:51 -04:00
|
|
|
|
|
|
|
expect(response).to redirect_to(destination_url)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
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
|
2017-08-31 00:06:56 -04:00
|
|
|
get :password_reset, params: { token: 'asdfasdf' }
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
2013-07-15 12:12:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-25 15:30:52 -04:00
|
|
|
context 'missing token' do
|
2017-08-31 00:06:56 -04:00
|
|
|
render_views
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
before do
|
2017-08-31 00:06:56 -04:00
|
|
|
get :password_reset, params: { token: SecureRandom.hex }
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-07-01 23:06:55 -04:00
|
|
|
it 'disallows login' do
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
expect(CGI.unescapeHTML(response.body))
|
|
|
|
.to include(I18n.t('password_reset.no_token'))
|
|
|
|
|
|
|
|
expect(response.body).to_not have_tag(:script, with: {
|
|
|
|
src: '/assets/application.js'
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(session[:current_user_id]).to be_blank
|
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
|
2017-08-31 00:06:56 -04:00
|
|
|
render_views
|
|
|
|
|
2018-01-09 13:01:59 -05:00
|
|
|
it 'disallows login' do
|
2017-08-31 00:06:56 -04:00
|
|
|
get :password_reset, params: { token: "evil_trout!" }
|
2014-08-25 15:30:52 -04:00
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
expect(CGI.unescapeHTML(response.body))
|
|
|
|
.to include(I18n.t('password_reset.no_token'))
|
|
|
|
|
|
|
|
expect(response.body).to_not have_tag(:script, with: {
|
|
|
|
src: '/assets/application.js'
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(session[:current_user_id]).to be_blank
|
2014-08-25 15:30:52 -04:00
|
|
|
end
|
2018-01-09 13:01:59 -05:00
|
|
|
|
|
|
|
it "responds with proper error message" do
|
|
|
|
put :password_reset, params: {
|
|
|
|
token: "evil_trout!", password: "awesomeSecretPassword"
|
|
|
|
}, format: :json
|
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
expect(JSON.parse(response.body)["message"]).to eq(I18n.t('password_reset.no_token'))
|
|
|
|
expect(session[:current_user_id]).to be_blank
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'valid token' do
|
2017-08-31 00:06:56 -04:00
|
|
|
render_views
|
2016-12-18 19:01:58 -05:00
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
context 'when rendered' do
|
2016-12-18 19:01:58 -05:00
|
|
|
it 'renders referrer never on get requests' do
|
2017-01-31 17:21:37 -05:00
|
|
|
user = Fabricate(:user)
|
2016-12-18 19:01:58 -05:00
|
|
|
token = user.email_tokens.create(email: user.email).token
|
2017-08-31 00:06:56 -04:00
|
|
|
get :password_reset, params: { token: token }
|
2016-12-18 19:01:58 -05:00
|
|
|
|
|
|
|
expect(response.body).to include('<meta name="referrer" content="never">')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
it 'returns success' do
|
2017-01-31 17:21:37 -05:00
|
|
|
user = Fabricate(:user)
|
|
|
|
user_auth_token = UserAuthToken.generate!(user_id: user.id)
|
2014-07-01 23:06:55 -04:00
|
|
|
token = user.email_tokens.create(email: user.email).token
|
2017-08-31 00:06:56 -04:00
|
|
|
get :password_reset, params: { token: token }
|
2017-06-01 04:19:42 -04:00
|
|
|
|
|
|
|
events = DiscourseEvent.track_events do
|
2017-08-31 00:06:56 -04:00
|
|
|
put :password_reset,
|
|
|
|
params: { token: token, password: 'hg9ow8yhg98o' }
|
2017-06-01 04:19:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
expect(events.map { |event| event[:event_name] }).to include(
|
|
|
|
:user_logged_in, :user_first_logged_in
|
|
|
|
)
|
2017-01-31 17:21:37 -05:00
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
2017-12-21 20:18:12 -05:00
|
|
|
expect(response.body).to include('{"is_developer":false,"admin":false,"second_factor_required":false}')
|
2015-04-14 18:57:43 -04:00
|
|
|
|
|
|
|
user.reload
|
2017-01-31 17:21:37 -05:00
|
|
|
|
2016-09-30 12:20:02 -04:00
|
|
|
expect(session["password-#{token}"]).to be_blank
|
2017-01-31 17:21:37 -05:00
|
|
|
expect(UserAuthToken.where(id: user_auth_token.id).count).to eq(0)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2016-01-04 11:48:54 -05:00
|
|
|
|
2016-12-19 01:15:20 -05:00
|
|
|
it 'disallows double password reset' do
|
2017-01-31 17:21:37 -05:00
|
|
|
user = Fabricate(:user)
|
2016-12-19 01:15:20 -05:00
|
|
|
token = user.email_tokens.create(email: user.email).token
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
get :password_reset, params: { token: token }
|
|
|
|
|
|
|
|
put :password_reset,
|
|
|
|
params: { token: token, password: 'hg9ow8yHG32O' }
|
|
|
|
|
|
|
|
put :password_reset,
|
|
|
|
params: { token: token, password: 'test123987AsdfXYZ' }
|
2016-12-19 01:15:20 -05:00
|
|
|
|
|
|
|
user.reload
|
2017-02-10 10:38:17 -05:00
|
|
|
expect(user.confirm_password?('hg9ow8yHG32O')).to eq(true)
|
2017-01-31 17:21:37 -05:00
|
|
|
|
|
|
|
# logged in now
|
|
|
|
expect(user.user_auth_tokens.count).to eq(1)
|
2016-12-19 01:15:20 -05:00
|
|
|
end
|
|
|
|
|
2017-06-07 12:36:52 -04:00
|
|
|
it "doesn't redirect to wizard on get" do
|
|
|
|
user = Fabricate(:admin)
|
|
|
|
UserAuthToken.generate!(user_id: user.id)
|
|
|
|
|
|
|
|
token = user.email_tokens.create(email: user.email).token
|
2017-08-31 00:06:56 -04:00
|
|
|
get :password_reset, params: { token: token }, format: :json
|
2017-06-07 12:36:52 -04:00
|
|
|
expect(response).not_to redirect_to(wizard_path)
|
|
|
|
end
|
|
|
|
|
2016-10-18 11:44:25 -04:00
|
|
|
it "redirects to the wizard if you're the first admin" do
|
2017-01-31 17:21:37 -05:00
|
|
|
user = Fabricate(:admin)
|
2017-06-07 12:36:52 -04:00
|
|
|
UserAuthToken.generate!(user_id: user.id)
|
|
|
|
|
2016-10-18 11:44:25 -04:00
|
|
|
token = user.email_tokens.create(email: user.email).token
|
2017-08-31 00:06:56 -04:00
|
|
|
get :password_reset, params: { token: token }
|
|
|
|
|
|
|
|
put :password_reset, params: {
|
|
|
|
token: token, password: 'hg9ow8yhg98oadminlonger'
|
|
|
|
}
|
|
|
|
|
2017-06-07 12:36:52 -04:00
|
|
|
expect(response).to redirect_to(wizard_path)
|
2016-10-18 11:44:25 -04:00
|
|
|
end
|
|
|
|
|
2016-01-04 11:48:54 -05:00
|
|
|
it "doesn't invalidate the token when loading the page" do
|
2017-01-31 17:21:37 -05:00
|
|
|
user = Fabricate(:user)
|
|
|
|
user_token = UserAuthToken.generate!(user_id: user.id)
|
|
|
|
|
2016-01-04 11:48:54 -05:00
|
|
|
email_token = user.email_tokens.create(email: user.email)
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
get :password_reset, params: { token: email_token.token }, format: :json
|
2016-01-04 11:48:54 -05:00
|
|
|
|
|
|
|
email_token.reload
|
2017-01-31 17:21:37 -05:00
|
|
|
|
2016-01-04 11:48:54 -05:00
|
|
|
expect(email_token.confirmed).to eq(false)
|
2017-01-31 17:21:37 -05:00
|
|
|
expect(UserAuthToken.where(id: user_token.id).count).to eq(1)
|
2016-01-04 11:48:54 -05:00
|
|
|
end
|
2017-12-21 20:18:12 -05:00
|
|
|
|
2018-02-20 01:44:51 -05:00
|
|
|
context '2 factor authentication required' do
|
|
|
|
let!(:second_factor) { Fabricate(:user_second_factor, user: user) }
|
2017-12-21 20:18:12 -05:00
|
|
|
|
|
|
|
it 'does not change with an invalid token' do
|
2018-02-20 01:44:51 -05:00
|
|
|
token = user.email_tokens.create!(email: user.email).token
|
2017-12-21 20:18:12 -05:00
|
|
|
|
|
|
|
get :password_reset, params: { token: token }
|
|
|
|
|
|
|
|
expect(response.body).to include('{"is_developer":false,"admin":false,"second_factor_required":true}')
|
|
|
|
|
|
|
|
put :password_reset,
|
|
|
|
params: { token: token, password: 'hg9ow8yHG32O', second_factor_token: '000000' }
|
|
|
|
|
|
|
|
expect(response.body).to include(I18n.t("login.invalid_second_factor_code"))
|
|
|
|
|
|
|
|
user.reload
|
|
|
|
expect(user.confirm_password?('hg9ow8yHG32O')).not_to eq(true)
|
|
|
|
expect(user.user_auth_tokens.count).not_to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'changes password with valid 2-factor tokens' do
|
|
|
|
token = user.email_tokens.create(email: user.email).token
|
|
|
|
|
|
|
|
get :password_reset, params: { token: token }
|
|
|
|
|
2018-02-20 01:44:51 -05:00
|
|
|
put :password_reset, params: {
|
|
|
|
token: token,
|
|
|
|
password: 'hg9ow8yHG32O',
|
|
|
|
second_factor_token: ROTP::TOTP.new(second_factor.data).now
|
|
|
|
}
|
2017-12-21 20:18:12 -05:00
|
|
|
|
|
|
|
user.reload
|
|
|
|
expect(user.confirm_password?('hg9ow8yHG32O')).to eq(true)
|
|
|
|
expect(user.user_auth_tokens.count).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'submit change' do
|
2014-08-25 15:30:52 -04:00
|
|
|
let(:token) { EmailToken.generate_token }
|
2017-08-31 00:06:56 -04:00
|
|
|
|
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
|
2017-08-31 00:06:56 -04:00
|
|
|
put :password_reset, params: {
|
|
|
|
token: token, password: ''
|
|
|
|
}, format: :json
|
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
expect(JSON.parse(response.body)["errors"]).to be_present
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(session[:current_user_id]).to be_blank
|
2014-09-11 15:22:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails when the password is too long" do
|
2017-08-31 00:06:56 -04:00
|
|
|
put :password_reset, params: {
|
|
|
|
token: token, password: ('x' * (User.max_password_length + 1))
|
|
|
|
}, format: :json
|
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
expect(JSON.parse(response.body)["errors"]).to be_present
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(session[:current_user_id]).to be_blank
|
2014-09-11 15:22:11 -04:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
it "logs in the user" do
|
2017-08-31 00:06:56 -04:00
|
|
|
put :password_reset, params: {
|
|
|
|
token: token, password: 'ksjafh928r'
|
|
|
|
}, format: :json
|
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
expect(JSON.parse(response.body)["errors"]).to be_blank
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(session[:current_user_id]).to be_present
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't log in the user when not approved" do
|
2017-02-13 14:31:15 -05:00
|
|
|
SiteSetting.must_approve_users = true
|
2017-08-31 00:06:56 -04:00
|
|
|
put :password_reset, params: {
|
|
|
|
token: token, password: 'ksjafh928r'
|
|
|
|
}, format: :json
|
|
|
|
|
|
|
|
expect(JSON.parse(response.body)["errors"]).to be_blank
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(session[:current_user_id]).to be_blank
|
2013-02-07 06:50:59 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
2016-01-04 11:48:54 -05:00
|
|
|
|
|
|
|
describe '.confirm_email_token' do
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
it "token doesn't match any records" do
|
|
|
|
email_token = user.email_tokens.create(email: user.email)
|
2017-08-31 00:06:56 -04:00
|
|
|
get :confirm_email_token, params: { token: SecureRandom.hex }, format: :json
|
2016-01-04 11:48:54 -05:00
|
|
|
expect(response).to be_success
|
|
|
|
expect(email_token.reload.confirmed).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "token matches" do
|
|
|
|
email_token = user.email_tokens.create(email: user.email)
|
2017-08-31 00:06:56 -04:00
|
|
|
get :confirm_email_token, params: { token: email_token.token }, format: :json
|
2016-01-04 11:48:54 -05:00
|
|
|
expect(response).to be_success
|
|
|
|
expect(email_token.reload.confirmed).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2018-02-20 01:44:51 -05:00
|
|
|
describe '#admin_login' do
|
2015-04-27 06:59:48 -04:00
|
|
|
let(:admin) { Fabricate(:admin) }
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
context 'enqueues mail' do
|
|
|
|
it 'enqueues mail with admin email and sso enabled' do
|
2016-04-07 00:38:43 -04:00
|
|
|
Jobs.expects(:enqueue).with(:critical_user_email, has_entries(type: :admin_login, user_id: admin.id))
|
2017-08-31 00:06:56 -04:00
|
|
|
put :admin_login, params: { email: admin.email }
|
2015-04-27 06:59:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-22 22:05:39 -05:00
|
|
|
context 'when email is incorrect' do
|
|
|
|
render_views
|
|
|
|
|
|
|
|
it 'should return the right response' do
|
2018-02-22 22:31:10 -05:00
|
|
|
put :admin_login, params: { email: 'random' }
|
2018-02-22 22:05:39 -05:00
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
response_body = response.body
|
|
|
|
|
|
|
|
expect(response_body).to match(I18n.t("admin_login.errors.unknown_email_address"))
|
|
|
|
expect(response_body).to_not match(I18n.t("login.second_factor_description"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-27 06:59:48 -04:00
|
|
|
context 'logs in admin' do
|
|
|
|
it 'does not log in admin with invalid token' do
|
2017-12-23 03:46:48 -05:00
|
|
|
SiteSetting.sso_url = "https://www.example.com/sso"
|
2015-04-27 06:59:48 -04:00
|
|
|
SiteSetting.enable_sso = true
|
2017-08-31 00:06:56 -04:00
|
|
|
get :admin_login, params: { token: "invalid" }
|
2015-04-27 06:59:48 -04:00
|
|
|
expect(session[:current_user_id]).to be_blank
|
|
|
|
end
|
|
|
|
|
2017-06-01 04:19:42 -04:00
|
|
|
context 'valid token' do
|
|
|
|
it 'does log in admin with SSO disabled' do
|
|
|
|
SiteSetting.enable_sso = false
|
|
|
|
token = admin.email_tokens.create(email: admin.email).token
|
2015-04-27 06:59:48 -04:00
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
get :admin_login, params: { token: token }
|
2017-06-01 04:19:42 -04:00
|
|
|
expect(response).to redirect_to('/')
|
|
|
|
expect(session[:current_user_id]).to eq(admin.id)
|
|
|
|
end
|
2015-04-27 06:59:48 -04:00
|
|
|
|
2017-06-01 04:19:42 -04:00
|
|
|
it 'logs in admin with SSO enabled' do
|
2017-12-23 03:46:48 -05:00
|
|
|
SiteSetting.sso_url = "https://www.example.com/sso"
|
2017-06-01 04:19:42 -04:00
|
|
|
SiteSetting.enable_sso = true
|
|
|
|
token = admin.email_tokens.create(email: admin.email).token
|
2015-04-27 06:59:48 -04:00
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
get :admin_login, params: { token: token }
|
2017-06-01 04:19:42 -04:00
|
|
|
expect(response).to redirect_to('/')
|
|
|
|
expect(session[:current_user_id]).to eq(admin.id)
|
|
|
|
end
|
2015-04-27 06:59:48 -04:00
|
|
|
end
|
2017-12-21 20:18:12 -05:00
|
|
|
|
2018-02-20 01:44:51 -05:00
|
|
|
describe 'when 2 factor authentication is enabled' do
|
|
|
|
let(:second_factor) { Fabricate(:user_second_factor, user: admin) }
|
2018-02-21 20:45:57 -05:00
|
|
|
let(:email_token) { Fabricate(:email_token, user: admin) }
|
2017-12-21 20:18:12 -05:00
|
|
|
render_views
|
|
|
|
|
|
|
|
it 'does not log in when token required' do
|
2018-02-20 01:44:51 -05:00
|
|
|
second_factor
|
2018-02-21 20:45:57 -05:00
|
|
|
get :admin_login, params: { token: email_token.token }
|
2017-12-21 20:18:12 -05:00
|
|
|
expect(response).not_to redirect_to('/')
|
|
|
|
expect(session[:current_user_id]).not_to eq(admin.id)
|
|
|
|
expect(response.body).to include(I18n.t('login.second_factor_description'));
|
|
|
|
end
|
|
|
|
|
2018-02-21 20:45:57 -05:00
|
|
|
describe 'invalid 2 factor token' do
|
|
|
|
it 'should display the right error' do
|
|
|
|
second_factor
|
|
|
|
|
|
|
|
put :admin_login, params: {
|
|
|
|
token: email_token.token,
|
|
|
|
second_factor_token: '13213'
|
|
|
|
}
|
2018-02-20 01:44:51 -05:00
|
|
|
|
2018-02-21 20:45:57 -05:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.body).to include(I18n.t('login.second_factor_description'));
|
|
|
|
expect(response.body).to include(I18n.t('login.invalid_second_factor_code'));
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'logs in when a valid 2-factor token is given' do
|
2018-02-20 01:44:51 -05:00
|
|
|
put :admin_login, params: {
|
2018-02-21 20:45:57 -05:00
|
|
|
token: email_token.token,
|
2018-02-20 01:44:51 -05:00
|
|
|
second_factor_token: ROTP::TOTP.new(second_factor.data).now
|
|
|
|
}
|
|
|
|
|
2017-12-21 20:18:12 -05:00
|
|
|
expect(response).to redirect_to('/')
|
|
|
|
expect(session[:current_user_id]).to eq(admin.id)
|
|
|
|
end
|
|
|
|
end
|
2015-04-27 06:59:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-07 04:02:10 -04:00
|
|
|
describe '#toggle_anon' do
|
|
|
|
it 'allows you to toggle anon if enabled' do
|
|
|
|
SiteSetting.allow_anonymous_posting = true
|
|
|
|
|
|
|
|
user = log_in
|
|
|
|
user.trust_level = 1
|
|
|
|
user.save
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
post :toggle_anon, format: :json
|
2015-04-07 04:02:10 -04:00
|
|
|
expect(response).to be_success
|
|
|
|
expect(session[:current_user_id]).to eq(AnonymousShadowCreator.get(user).id)
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
post :toggle_anon, format: :json
|
2015-04-07 04:02:10 -04:00
|
|
|
expect(response).to be_success
|
|
|
|
expect(session[:current_user_id]).to eq(user.id)
|
|
|
|
|
|
|
|
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)
|
2017-02-13 14:31:15 -05:00
|
|
|
SiteSetting.allow_new_registrations = 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
|
|
|
|
|
2016-08-05 11:57:13 -04:00
|
|
|
let(:post_user_params) do
|
|
|
|
{ name: @user.name,
|
2013-06-05 20:09:22 -04:00
|
|
|
username: @user.username,
|
|
|
|
password: "strongpassword",
|
2016-08-05 11:57:13 -04:00
|
|
|
email: @user.email }
|
|
|
|
end
|
|
|
|
|
|
|
|
def post_user
|
2017-08-31 00:06:56 -04:00
|
|
|
post :create, params: post_user_params, format: :json
|
2013-06-05 20:09:22 -04:00
|
|
|
end
|
|
|
|
|
2017-06-12 04:48:32 -04:00
|
|
|
context 'when email params is missing' do
|
|
|
|
it 'should raise the right error' do
|
|
|
|
expect do
|
2017-08-31 00:06:56 -04:00
|
|
|
post :create, params: {
|
2017-06-12 04:48:32 -04:00
|
|
|
name: @user.name,
|
|
|
|
username: @user.username,
|
|
|
|
passsword: 'tesing12352343'
|
2017-08-31 00:06:56 -04:00
|
|
|
}, format: :json
|
2017-06-12 04:48:32 -04:00
|
|
|
end.to raise_error(ActionController::ParameterMissing)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-06 14:49:39 -05:00
|
|
|
context 'when creating a user' do
|
|
|
|
it 'sets the user locale to I18n.locale' do
|
2017-02-13 14:31:15 -05:00
|
|
|
SiteSetting.default_locale = 'en'
|
2016-02-06 14:49:39 -05:00
|
|
|
I18n.stubs(:locale).returns(:fr)
|
|
|
|
post_user
|
|
|
|
expect(User.find_by(username: @user.username).locale).to eq('fr')
|
|
|
|
end
|
|
|
|
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
|
2017-02-13 14:31:15 -05:00
|
|
|
SiteSetting.enable_local_logins = 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
|
2017-02-13 14:31:15 -05:00
|
|
|
SiteSetting.allow_new_registrations = false
|
2014-07-14 15:42:14 -04:00
|
|
|
post_user
|
|
|
|
json = JSON.parse(response.body)
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(json['success']).to eq(false)
|
|
|
|
expect(json['message']).to be_present
|
2014-07-14 15:42:14 -04:00
|
|
|
end
|
|
|
|
|
2014-03-26 00:39:44 -04:00
|
|
|
it 'creates a user correctly' do
|
2016-04-07 00:38:43 -04:00
|
|
|
Jobs.expects(:enqueue).with(:critical_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
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(session["user_created_message"]).to be_present
|
2017-05-02 15:57:55 -04:00
|
|
|
expect(session[SessionController::ACTIVATE_USER_KEY]).to be_present
|
2013-02-07 06:50:59 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2017-09-12 15:04:39 -04:00
|
|
|
context "`must approve users` site setting is enabled" do
|
2017-02-13 14:31:15 -05:00
|
|
|
before { SiteSetting.must_approve_users = true }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2017-09-12 15:04:39 -04:00
|
|
|
it 'creates a user correctly' do
|
|
|
|
Jobs.expects(:enqueue).with(:critical_user_email, has_entries(type: :signup))
|
|
|
|
User.any_instance.expects(:enqueue_welcome_message).with('welcome_user').never
|
2013-06-05 21:19:27 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
2017-09-12 15:04:39 -04:00
|
|
|
# should save user_created_message in session
|
|
|
|
expect(session["user_created_message"]).to be_present
|
|
|
|
expect(session[SessionController::ACTIVATE_USER_KEY]).to be_present
|
2017-10-03 14:08:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'users already exists with given email' do
|
|
|
|
let!(:existing) { Fabricate(:user, email: post_user_params[:email]) }
|
|
|
|
|
2017-10-03 15:28:15 -04:00
|
|
|
it 'returns an error if hide_email_address_taken is disabled' do
|
|
|
|
SiteSetting.hide_email_address_taken = false
|
2017-10-03 14:08:37 -04:00
|
|
|
post_user
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
expect(json['success']).to eq(false)
|
|
|
|
expect(json['message']).to be_present
|
|
|
|
end
|
|
|
|
|
2017-10-03 15:28:15 -04:00
|
|
|
it 'returns success if hide_email_address_taken is enabled' do
|
|
|
|
SiteSetting.hide_email_address_taken = true
|
2017-10-03 14:08:37 -04:00
|
|
|
expect {
|
|
|
|
post_user
|
|
|
|
}.to_not change { User.count }
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
expect(json['active']).to be_falsey
|
|
|
|
expect(session["user_created_message"]).to be_present
|
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
|
|
|
|
|
2016-08-05 11:57:13 -04:00
|
|
|
context "creating as active" do
|
|
|
|
it "won't create the user as active" do
|
2017-08-31 00:06:56 -04:00
|
|
|
post :create, params: post_user_params.merge(active: true), format: :json
|
2016-08-05 11:57:13 -04:00
|
|
|
expect(JSON.parse(response.body)['active']).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with a regular api key" do
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:api_key) { Fabricate(:api_key, user: user) }
|
|
|
|
|
|
|
|
it "won't create the user as active with a regular key" do
|
2017-08-31 00:06:56 -04:00
|
|
|
post :create,
|
|
|
|
params: post_user_params.merge(active: true, api_key: api_key.key),
|
|
|
|
format: :json
|
|
|
|
|
2016-08-05 11:57:13 -04:00
|
|
|
expect(JSON.parse(response.body)['active']).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with an admin api key" do
|
2017-08-28 15:36:46 -04:00
|
|
|
let(:admin) { Fabricate(:admin) }
|
|
|
|
let(:api_key) { Fabricate(:api_key, user: admin) }
|
2016-08-05 11:57:13 -04:00
|
|
|
|
|
|
|
it "creates the user as active with a regular key" do
|
2017-08-28 15:36:46 -04:00
|
|
|
SiteSetting.queue_jobs = true
|
|
|
|
SiteSetting.send_welcome_message = true
|
|
|
|
SiteSetting.must_approve_users = true
|
|
|
|
|
|
|
|
Sidekiq::Client.expects(:enqueue).never
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
post :create,
|
|
|
|
params: post_user_params.merge(approved: true, active: true, api_key: api_key.key),
|
|
|
|
format: :json
|
|
|
|
|
2017-08-28 15:36:46 -04:00
|
|
|
json = JSON.parse(response.body)
|
|
|
|
|
|
|
|
new_user = User.find(json["user_id"])
|
|
|
|
|
|
|
|
expect(json['active']).to be_truthy
|
|
|
|
|
|
|
|
expect(new_user.active).to eq(true)
|
|
|
|
expect(new_user.approved).to eq(true)
|
|
|
|
expect(new_user.approved_by_id).to eq(admin.id)
|
|
|
|
expect(new_user.approved_at).to_not eq(nil)
|
2016-08-05 11:57:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "won't create the developer as active" do
|
|
|
|
UsernameCheckerService.expects(:is_developer?).returns(true)
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
post :create,
|
|
|
|
params: post_user_params.merge(active: true, api_key: api_key.key),
|
|
|
|
format: :json
|
|
|
|
|
2016-08-05 11:57:13 -04:00
|
|
|
expect(JSON.parse(response.body)['active']).to be_falsy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "creating as staged" do
|
|
|
|
it "won't create the user as staged" do
|
2017-08-31 00:06:56 -04:00
|
|
|
post :create,
|
|
|
|
params: post_user_params.merge(staged: true),
|
|
|
|
format: :json
|
|
|
|
|
2016-08-05 11:57:13 -04:00
|
|
|
new_user = User.where(username: post_user_params[:username]).first
|
|
|
|
expect(new_user.staged?).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with a regular api key" do
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:api_key) { Fabricate(:api_key, user: user) }
|
|
|
|
|
|
|
|
it "won't create the user as staged with a regular key" do
|
2017-08-31 00:06:56 -04:00
|
|
|
post :create,
|
|
|
|
params: post_user_params.merge(staged: true, api_key: api_key.key),
|
|
|
|
format: :json
|
|
|
|
|
2016-08-05 11:57:13 -04:00
|
|
|
new_user = User.where(username: post_user_params[:username]).first
|
|
|
|
expect(new_user.staged?).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with an admin api key" do
|
|
|
|
let(:user) { Fabricate(:admin) }
|
|
|
|
let(:api_key) { Fabricate(:api_key, user: user) }
|
|
|
|
|
|
|
|
it "creates the user as staged with a regular key" do
|
2017-08-31 00:06:56 -04:00
|
|
|
post :create,
|
|
|
|
params: post_user_params.merge(staged: true, api_key: api_key.key),
|
|
|
|
format: :json
|
2016-08-05 11:57:13 -04:00
|
|
|
|
|
|
|
new_user = User.where(username: post_user_params[:username]).first
|
|
|
|
expect(new_user.staged?).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "won't create the developer as staged" do
|
|
|
|
UsernameCheckerService.expects(:is_developer?).returns(true)
|
2017-08-31 00:06:56 -04:00
|
|
|
post :create,
|
|
|
|
params: post_user_params.merge(staged: true, api_key: api_key.key),
|
|
|
|
format: :json
|
2016-08-05 11:57:13 -04:00
|
|
|
|
|
|
|
new_user = User.where(username: post_user_params[:username]).first
|
|
|
|
expect(new_user.staged?).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-05 21:19:27 -04:00
|
|
|
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
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(session["user_created_message"]).to be_present
|
2017-05-02 15:57:55 -04:00
|
|
|
expect(session[SessionController::ACTIVATE_USER_KEY]).to 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
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(session[:current_user_id]).to be_present
|
2013-02-05 14:16:51 -05:00
|
|
|
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
|
2017-02-13 14:31:15 -05:00
|
|
|
SiteSetting.allow_new_registrations = false
|
|
|
|
|
2014-07-14 15:42:14 -04:00
|
|
|
post_user
|
2017-02-13 14:31:15 -05:00
|
|
|
|
2014-07-14 15:42:14 -04:00
|
|
|
json = JSON.parse(response.body)
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(json['success']).to eq(false)
|
|
|
|
expect(json['message']).to be_present
|
2014-07-14 15:42:14 -04:00
|
|
|
end
|
|
|
|
|
2013-04-12 18:46:55 -04:00
|
|
|
context 'authentication records for' do
|
|
|
|
|
2013-08-25 21:04:16 -04:00
|
|
|
it 'should create twitter user info if required' do
|
2017-02-13 14:31:15 -05:00
|
|
|
SiteSetting.must_approve_users = true
|
|
|
|
SiteSetting.enable_twitter_logins = 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
|
2017-02-13 14:31:15 -05:00
|
|
|
|
2013-06-05 20:09:22 -04:00
|
|
|
post_user
|
2017-02-14 09:17:52 -05:00
|
|
|
|
|
|
|
expect(TwitterUserInfo.count).to eq(1)
|
2013-04-12 18:46:55 -04:00
|
|
|
end
|
|
|
|
end
|
2017-10-16 13:51:35 -04:00
|
|
|
|
|
|
|
it "returns an error when email has been changed from the validated email address" do
|
|
|
|
auth = session[:authentication] = {}
|
|
|
|
auth[:email_valid] = 'true'
|
|
|
|
auth[:email] = 'therealone@gmail.com'
|
|
|
|
post_user
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
expect(json['success']).to eq(false)
|
|
|
|
expect(json['message']).to be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
it "will create the user successfully if email validation is required" do
|
|
|
|
auth = session[:authentication] = {}
|
|
|
|
auth[:email] = post_user_params[:email]
|
|
|
|
post_user
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
expect(json['success']).to eq(true)
|
|
|
|
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
|
2015-01-09 12:04:02 -05:00
|
|
|
is_expected.to respond_with(:success)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'has the proper JSON' do
|
|
|
|
json = JSON::parse(response.body)
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(json["success"]).to eq(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
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(User.find_by(username: @user.username).active).to eq(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 {
|
2017-08-31 00:06:56 -04:00
|
|
|
post :create, params: create_params, format: :json
|
2013-02-06 19:25:21 -05:00
|
|
|
}.to_not change { User.count }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not send an email' do
|
|
|
|
User.any_instance.expects(:enqueue_welcome_message).never
|
2017-08-31 00:06:56 -04:00
|
|
|
post :create, params: create_params, format: :json
|
2013-02-06 19:25:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should say it was successful' do
|
2017-08-31 00:06:56 -04:00
|
|
|
post :create, params: create_params, format: :json
|
2013-02-06 19:25:21 -05:00
|
|
|
json = JSON::parse(response.body)
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(json["success"]).to eq(true)
|
2014-10-01 13:33:49 -04:00
|
|
|
|
|
|
|
# should not change the session
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(session["user_created_message"]).to be_blank
|
2017-05-02 15:57:55 -04:00
|
|
|
expect(session[SessionController::ACTIVATE_USER_KEY]).to 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
|
2017-07-27 21:20:09 -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
|
2017-07-27 21:20:09 -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
|
2017-02-13 14:31:15 -05:00
|
|
|
before { SiteSetting.invite_only = true }
|
2013-06-05 14:08:21 -04:00
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
let(:create_params) { {
|
2013-06-05 14:08:21 -04:00
|
|
|
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
|
2017-08-31 00:06:56 -04:00
|
|
|
expect { post :create, params: create_params, format: :json }.to_not change { User.count }
|
2013-02-12 15:42:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should report failed' do
|
2017-08-31 00:06:56 -04:00
|
|
|
post :create, params: create_params, format: :json
|
2013-02-12 15:42:04 -05:00
|
|
|
json = JSON::parse(response.body)
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(json["success"]).not_to eq(true)
|
2014-10-28 20:06:59 -04:00
|
|
|
|
2014-10-01 13:33:49 -04:00
|
|
|
# should not change the session
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(session["user_created_message"]).to be_blank
|
2017-05-02 15:57:55 -04:00
|
|
|
expect(session[SessionController::ACTIVATE_USER_KEY]).to be_blank
|
2013-02-12 15:42:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when password is blank' do
|
2017-07-27 21:20:09 -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
|
2017-07-27 21:20:09 -04:00
|
|
|
let(:create_params) { { name: @user.name, username: @user.username, password: "x" * (User.max_password_length + 1), email: @user.email } }
|
2014-09-11 15:22:11 -04:00
|
|
|
include_examples 'failed signup'
|
|
|
|
end
|
|
|
|
|
2013-02-12 15:42:04 -05:00
|
|
|
context 'when password param is missing' do
|
2017-07-27 21:20:09 -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
|
|
|
|
|
2015-07-01 16:44:53 -04:00
|
|
|
context 'with a reserved username' do
|
2017-07-27 21:20:09 -04:00
|
|
|
let(:create_params) { { name: @user.name, username: 'Reserved', email: @user.email, password: "x" * 20 } }
|
2015-07-01 16:44:53 -04:00
|
|
|
before { SiteSetting.reserved_usernames = 'a|reserved|b' }
|
|
|
|
after { SiteSetting.reserved_usernames = nil }
|
|
|
|
include_examples 'failed signup'
|
|
|
|
end
|
|
|
|
|
2013-04-12 18:46:55 -04:00
|
|
|
context 'when an Exception is raised' do
|
2017-05-01 14:53:00 -04:00
|
|
|
before { User.any_instance.stubs(:save).raises(ActiveRecord::StatementInvalid.new('Oh no')) }
|
2013-04-12 18:46:55 -04:00
|
|
|
|
2017-05-01 14:53:00 -04:00
|
|
|
let(:create_params) {
|
|
|
|
{ name: @user.name, username: @user.username,
|
2017-07-27 21:20:09 -04:00
|
|
|
password: "strongpassword", email: @user.email }
|
2017-05-01 14:53:00 -04:00
|
|
|
}
|
2013-04-12 18:46:55 -04:00
|
|
|
|
2017-05-01 14:53:00 -04:00
|
|
|
include_examples 'failed signup'
|
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
|
2017-07-27 21:20:09 -04:00
|
|
|
let(:create_params) { { name: @user.name, password: 'watwatwat', username: @user.username, email: @user.email } }
|
2014-09-26 14:48:34 -04:00
|
|
|
include_examples 'failed signup'
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with values for the fields" do
|
|
|
|
let(:create_params) { {
|
|
|
|
name: @user.name,
|
2017-02-09 15:00:22 -05:00
|
|
|
password: 'suChS3cuRi7y',
|
2014-09-26 14:48:34 -04:00
|
|
|
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
|
2017-08-31 00:06:56 -04:00
|
|
|
post :create, params: create_params, format: :json
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
2017-04-26 14:47:36 -04:00
|
|
|
inserted = User.find_by_email(@user.email)
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(inserted).to be_present
|
|
|
|
expect(inserted.custom_fields).to be_present
|
|
|
|
expect(inserted.custom_fields["user_field_#{user_field.id}"]).to eq('value1')
|
|
|
|
expect(inserted.custom_fields["user_field_#{another_field.id}"]).to eq('value2')
|
|
|
|
expect(inserted.custom_fields["user_field_#{optional_field.id}"]).to be_blank
|
2014-10-08 14:38:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should succeed with the optional field" do
|
|
|
|
create_params[:user_fields][optional_field.id.to_s] = 'value3'
|
2017-08-31 00:06:56 -04:00
|
|
|
post :create, params: create_params.merge(create_params), format: :json
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
2017-04-26 14:47:36 -04:00
|
|
|
inserted = User.find_by_email(@user.email)
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(inserted).to be_present
|
|
|
|
expect(inserted.custom_fields).to be_present
|
|
|
|
expect(inserted.custom_fields["user_field_#{user_field.id}"]).to eq('value1')
|
|
|
|
expect(inserted.custom_fields["user_field_#{another_field.id}"]).to eq('value2')
|
|
|
|
expect(inserted.custom_fields["user_field_#{optional_field.id}"]).to eq('value3')
|
2014-09-26 14:48:34 -04:00
|
|
|
end
|
|
|
|
|
2015-02-23 13:02:30 -05:00
|
|
|
it "trims excessively long fields" do
|
|
|
|
create_params[:user_fields][optional_field.id.to_s] = ('x' * 3000)
|
2017-08-31 00:06:56 -04:00
|
|
|
post :create, params: create_params.merge(create_params), format: :json
|
2015-02-23 13:02:30 -05:00
|
|
|
expect(response).to be_success
|
2017-04-26 14:47:36 -04:00
|
|
|
inserted = User.find_by_email(@user.email)
|
2015-02-23 13:02:30 -05:00
|
|
|
|
|
|
|
val = inserted.custom_fields["user_field_#{optional_field.id}"]
|
|
|
|
expect(val.length).to eq(UserField.max_length)
|
|
|
|
end
|
2014-09-26 14:48:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-27 04:48:27 -05:00
|
|
|
context "with only optional custom fields" do
|
|
|
|
let!(:user_field) { Fabricate(:user_field, required: false) }
|
|
|
|
|
|
|
|
context "without values for the fields" do
|
|
|
|
let(:create_params) { {
|
|
|
|
name: @user.name,
|
2017-02-09 15:00:22 -05:00
|
|
|
password: 'suChS3cuRi7y',
|
2015-01-27 04:48:27 -05:00
|
|
|
username: @user.username,
|
|
|
|
email: @user.email,
|
|
|
|
} }
|
|
|
|
|
|
|
|
it "should succeed" do
|
2017-08-31 00:06:56 -04:00
|
|
|
post :create, params: create_params, format: :json
|
2015-01-27 04:48:27 -05:00
|
|
|
expect(response).to be_success
|
2017-04-26 14:47:36 -04:00
|
|
|
inserted = User.find_by_email(@user.email)
|
2015-01-27 04:48:27 -05:00
|
|
|
expect(inserted).to be_present
|
|
|
|
expect(inserted.custom_fields).not_to be_present
|
|
|
|
expect(inserted.custom_fields["user_field_#{user_field.id}"]).to be_blank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2017-06-06 21:45:27 -04:00
|
|
|
context '#username' do
|
2013-02-05 14:16:51 -05:00
|
|
|
it 'raises an error when not logged in' do
|
2018-01-11 22:15:10 -05:00
|
|
|
put :username, params: { username: 'somename' }, format: :json
|
|
|
|
expect(response.status).to eq(403)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'while logged in' do
|
2015-01-16 17:30:46 -05:00
|
|
|
let(:old_username) { "OrigUsrname" }
|
|
|
|
let(:new_username) { "#{old_username}1234" }
|
|
|
|
let(:user) { Fabricate(:user, username: old_username) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
user.username = old_username
|
|
|
|
log_in_user(user)
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
it 'raises an error without a new_username param' do
|
2017-08-31 00:06:56 -04:00
|
|
|
expect do
|
|
|
|
put :username, params: { username: user.username }, format: :json
|
|
|
|
end.to raise_error(ActionController::ParameterMissing)
|
|
|
|
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(user.reload.username).to eq(old_username)
|
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)
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
put :username, params: {
|
|
|
|
username: user.username, new_username: new_username
|
|
|
|
}, format: :json
|
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_forbidden
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(user.reload.username).to eq(old_username)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error when change_username fails' do
|
2017-08-31 00:06:56 -04:00
|
|
|
put :username,
|
|
|
|
params: { username: user.username, new_username: '@' },
|
|
|
|
format: :json
|
2017-06-06 21:45:27 -04:00
|
|
|
|
|
|
|
expect(response).to_not be_success
|
|
|
|
|
|
|
|
body = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(body['errors'].first).to include(I18n.t(
|
|
|
|
'user.username.short', min: User.username_length.begin
|
|
|
|
))
|
|
|
|
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(user.reload.username).to eq(old_username)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2015-01-16 17:30:46 -05:00
|
|
|
it 'should succeed in normal circumstances' do
|
2017-08-31 00:06:56 -04:00
|
|
|
put :username,
|
|
|
|
params: { username: user.username, new_username: new_username },
|
|
|
|
format: :json
|
|
|
|
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(response).to be_success
|
|
|
|
expect(user.reload.username).to eq(new_username)
|
2015-01-16 17:30:46 -05:00
|
|
|
end
|
|
|
|
|
2017-07-24 07:46:44 -04:00
|
|
|
it 'should fail if the user is old' do
|
2015-01-16 17:30:46 -05:00
|
|
|
# Older than the change period and >1 post
|
|
|
|
user.created_at = Time.now - (SiteSetting.username_change_period + 1).days
|
2017-07-24 07:46:44 -04:00
|
|
|
PostCreator.new(user,
|
|
|
|
title: 'This is a test topic',
|
|
|
|
raw: 'This is a test this is a test'
|
|
|
|
).create
|
2015-01-16 17:30:46 -05:00
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
put :username, params: {
|
|
|
|
username: user.username, new_username: new_username
|
|
|
|
}, format: :json
|
2015-01-16 17:30:46 -05:00
|
|
|
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(response).to be_forbidden
|
|
|
|
expect(user.reload.username).to eq(old_username)
|
2015-01-16 17:30:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should create a staff action log when a staff member changes the username' do
|
|
|
|
acting_user = Fabricate(:admin)
|
|
|
|
log_in_user(acting_user)
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
put :username, params: {
|
|
|
|
username: user.username, new_username: new_username
|
|
|
|
}, format: :json
|
|
|
|
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(response).to be_success
|
|
|
|
expect(UserHistory.where(action: UserHistory.actions[:change_username], target_user_id: user.id, acting_user_id: acting_user.id)).to be_present
|
|
|
|
expect(user.reload.username).to eq(new_username)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-12-10 11:43:16 -05:00
|
|
|
it 'should return a JSON response with the updated username' do
|
2017-08-31 00:06:56 -04:00
|
|
|
put :username, params: {
|
|
|
|
username: user.username, new_username: new_username
|
|
|
|
}, format: :json
|
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(::JSON.parse(response.body)['username']).to eq(new_username)
|
2014-12-10 11:43:16 -05:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context '.check_username' do
|
2013-11-19 14:15:05 -05:00
|
|
|
it 'raises an error without any parameters' do
|
2017-08-31 00:06:56 -04:00
|
|
|
expect do
|
|
|
|
get :check_username, format: :json
|
|
|
|
end.to 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
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return available as false in the JSON' do
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(::JSON.parse(response.body)['available']).to eq(false)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return a suggested username' do
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(::JSON.parse(response.body)['suggestion']).to be_present
|
2013-02-05 14:16:51 -05:00
|
|
|
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
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return available in the JSON' do
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(::JSON.parse(response.body)['available']).to eq(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
|
2017-08-31 00:06:56 -04:00
|
|
|
get :check_username, params: { email: 'dood@example.com' }, format: :json
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
2014-07-16 12:25:24 -04:00
|
|
|
end
|
2013-02-08 14:12:48 -05:00
|
|
|
|
2014-07-16 12:25:24 -04:00
|
|
|
context 'username is available' do
|
|
|
|
before do
|
2017-08-31 00:06:56 -04:00
|
|
|
get :check_username, params: { username: 'BruceWayne' }, format: :json
|
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
|
2017-08-31 00:06:56 -04:00
|
|
|
get :check_username, params: { username: user.username }, format: :json
|
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
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to 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
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(::JSON.parse(response.body)['available']).to eq(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
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(::JSON.parse(response.body)['errors']).not_to 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
|
2017-08-31 00:06:56 -04:00
|
|
|
get :check_username, params: {
|
|
|
|
username: 'bad username'
|
|
|
|
}, format: :json
|
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
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(::JSON.parse(response.body)['errors']).to 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
|
2017-08-31 00:06:56 -04:00
|
|
|
get :check_username, params: {
|
|
|
|
username: generate_username(User.username_length.last + 1)
|
|
|
|
}, format: :json
|
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
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(::JSON.parse(response.body)['errors']).to 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)
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
get :check_username, params: {
|
|
|
|
username: 'HanSolo'
|
|
|
|
}, format: :json
|
2013-06-28 16:21:46 -04:00
|
|
|
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
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
get :check_username, params: {
|
|
|
|
username: 'HanSolo'
|
|
|
|
}, format: :json
|
2013-06-28 16:21:46 -04:00
|
|
|
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))
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
get :check_username, params: {
|
|
|
|
username: 'HanSolo', for_user_id: user.id
|
|
|
|
}, format: :json
|
2013-07-30 14:13:56 -04:00
|
|
|
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)
|
2017-08-31 00:06:56 -04:00
|
|
|
get :invited, params: { username: user.username }, format: :json
|
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
|
|
|
|
)
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
get :invited, params: {
|
|
|
|
username: inviter.username, search: 'billybob'
|
|
|
|
}, format: :json
|
2013-11-08 14:11:41 -05:00
|
|
|
|
2014-03-21 14:13:04 -04:00
|
|
|
invites = JSON.parse(response.body)['invites']
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(invites.size).to eq(1)
|
2013-11-08 14:11:41 -05:00
|
|
|
expect(invites.first).to include('email' => 'billybob@example.com')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'filters by username' do
|
|
|
|
inviter = Fabricate(:user)
|
|
|
|
invitee = Fabricate(:user, username: 'billybob')
|
2014-09-25 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')
|
|
|
|
)
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
get :invited, params: {
|
|
|
|
username: inviter.username, search: 'billybob'
|
|
|
|
}, format: :json
|
2013-11-08 14:11:41 -05:00
|
|
|
|
2014-03-21 14:13:04 -04:00
|
|
|
invites = JSON.parse(response.body)['invites']
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(invites.size).to eq(1)
|
2013-11-08 14:11:41 -05:00
|
|
|
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)
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
get :invited,
|
|
|
|
params: { username: inviter.username, filter: 'pending' },
|
|
|
|
format: :json
|
2013-11-08 14:11:41 -05:00
|
|
|
|
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)
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
get :invited,
|
|
|
|
params: { username: inviter.username },
|
|
|
|
format: :json
|
2013-11-08 14:11:41 -05:00
|
|
|
|
2014-03-21 14:13:04 -04:00
|
|
|
invites = JSON.parse(response.body)['invites']
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(invites.size).to eq(1)
|
2013-11-08 14:11:41 -05:00
|
|
|
expect(invites.first).to include('email' => invite.email)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with authenticated user' do
|
|
|
|
context 'with pending invites' do
|
|
|
|
context 'with permission to see pending invites' do
|
|
|
|
it 'returns invites' do
|
|
|
|
user = log_in
|
|
|
|
inviter = Fabricate(:user)
|
|
|
|
invite = Fabricate(:invite, invited_by: inviter)
|
|
|
|
stub_guardian(user) do |guardian|
|
2014-03-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
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
get :invited, params: {
|
|
|
|
username: inviter.username, filter: 'pending'
|
|
|
|
}, format: :json
|
2013-11-08 14:11:41 -05:00
|
|
|
|
2014-03-21 14:13:04 -04:00
|
|
|
invites = JSON.parse(response.body)['invites']
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(invites.size).to eq(1)
|
2013-11-08 14:11:41 -05:00
|
|
|
expect(invites.first).to include("email" => invite.email)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without permission to see pending invites' do
|
|
|
|
it 'does not return invites' do
|
|
|
|
user = log_in
|
|
|
|
inviter = Fabricate(:user)
|
2014-09-25 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
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
get :invited, params: {
|
|
|
|
username: inviter.username, filter: 'pending'
|
|
|
|
}, format: :json
|
2013-11-08 14:11:41 -05:00
|
|
|
|
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)
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
get :invited, params: { username: inviter.username }, format: :json
|
2013-11-08 14:11:41 -05:00
|
|
|
|
2014-03-21 14:13:04 -04:00
|
|
|
invites = JSON.parse(response.body)['invites']
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(invites.size).to eq(1)
|
2013-11-08 14:11:41 -05:00
|
|
|
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
|
2018-01-11 22:15:10 -05:00
|
|
|
put :update, params: { username: 'guest' }, format: :json
|
|
|
|
expect(response.status).to eq(403)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-20 15:18:43 -04:00
|
|
|
context "as a staff user" do
|
|
|
|
let!(:user) { log_in(:admin) }
|
|
|
|
|
|
|
|
context "uneditable field" do
|
|
|
|
let!(:user_field) { Fabricate(:user_field, editable: false) }
|
|
|
|
|
|
|
|
it "allows staff to edit the field" do
|
2017-08-31 00:06:56 -04:00
|
|
|
put :update, params: {
|
|
|
|
username: user.username,
|
|
|
|
name: 'Jim Tom',
|
2017-09-26 14:13:24 -04:00
|
|
|
title: "foobar",
|
2017-08-31 00:06:56 -04:00
|
|
|
user_fields: { user_field.id.to_s => 'happy' }
|
|
|
|
}, format: :json
|
|
|
|
|
2015-03-20 15:18:43 -04:00
|
|
|
expect(response).to be_success
|
2017-09-26 14:13:24 -04:00
|
|
|
|
|
|
|
user.reload
|
|
|
|
|
2015-03-20 15:18:43 -04:00
|
|
|
expect(user.user_fields[user_field.id.to_s]).to eq('happy')
|
2017-09-26 14:13:24 -04:00
|
|
|
expect(user.title).to eq("foobar")
|
2015-03-20 15:18:43 -04:00
|
|
|
end
|
|
|
|
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
|
2015-03-23 20:55:22 -04:00
|
|
|
user2 = Fabricate(:user)
|
|
|
|
user3 = Fabricate(:user)
|
2018-03-29 15:08:22 -04:00
|
|
|
tags = [Fabricate(:tag), Fabricate(:tag)]
|
2015-03-23 20:55:22 -04:00
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
put :update, params: {
|
|
|
|
username: user.username,
|
|
|
|
name: 'Jim Tom',
|
|
|
|
custom_fields: { test: :it },
|
2018-03-29 15:08:22 -04:00
|
|
|
muted_usernames: "#{user2.username},#{user3.username}",
|
|
|
|
watched_tags: "#{tags[0].name},#{tags[1].name}"
|
2017-08-31 00:06:56 -04:00
|
|
|
}, format: :json
|
2015-03-23 20:55:22 -04:00
|
|
|
|
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'
|
2017-07-27 21:20:09 -04:00
|
|
|
expect(user.muted_users.pluck(:username).sort).to eq [user2.username, user3.username].sort
|
2018-03-29 15:08:22 -04:00
|
|
|
expect(TagUser.where(
|
|
|
|
user: user,
|
|
|
|
notification_level: TagUser.notification_levels[:watching]
|
|
|
|
).pluck(:tag_id)).to contain_exactly(tags[0].id, tags[1].id)
|
2015-03-23 20:55:22 -04:00
|
|
|
|
2017-05-12 12:41:26 -04:00
|
|
|
theme = Theme.create(name: "test", user_selectable: true, user_id: -1)
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
put :update, params: {
|
|
|
|
username: user.username,
|
|
|
|
muted_usernames: "",
|
2017-09-26 14:00:10 -04:00
|
|
|
theme_key: theme.key,
|
|
|
|
email_direct: false
|
2017-08-31 00:06:56 -04:00
|
|
|
}, format: :json
|
2015-03-23 20:55:22 -04:00
|
|
|
|
|
|
|
user.reload
|
|
|
|
|
|
|
|
expect(user.muted_users.pluck(:username).sort).to be_empty
|
2017-05-12 12:41:26 -04:00
|
|
|
expect(user.user_option.theme_key).to eq(theme.key)
|
2017-09-26 14:00:10 -04:00
|
|
|
expect(user.user_option.email_direct).to eq(false)
|
2013-11-01 14:12:25 -04:00
|
|
|
end
|
2013-11-01 14:42:16 -04:00
|
|
|
|
2016-02-06 14:49:39 -05:00
|
|
|
context 'a locale is chosen that differs from I18n.locale' do
|
|
|
|
it "updates the user's locale" do
|
|
|
|
I18n.stubs(:locale).returns('fr')
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
put :update, params: {
|
|
|
|
username: user.username,
|
|
|
|
locale: :fa_IR
|
|
|
|
}, format: :json
|
2016-02-06 14:49:39 -05:00
|
|
|
|
|
|
|
expect(User.find_by(username: user.username).locale).to eq('fa_IR')
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2014-09-26 14:48:34 -04:00
|
|
|
context "with user fields" do
|
|
|
|
context "an editable field" do
|
|
|
|
let!(:user_field) { Fabricate(:user_field) }
|
2017-07-27 21:20:09 -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
|
2017-08-31 00:06:56 -04:00
|
|
|
put :update, params: {
|
|
|
|
username: user.username, name: 'Jim Tom', user_fields: { user_field.id.to_s => 'happy' }
|
|
|
|
}, format: :json
|
|
|
|
|
2014-09-26 14:48:34 -04:00
|
|
|
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
|
2017-08-31 00:06:56 -04:00
|
|
|
put :update, params: {
|
|
|
|
username: user.username, name: 'Jim Tom', user_fields: { user_field.id.to_s => '' }
|
|
|
|
}, format: :json
|
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).not_to be_success
|
|
|
|
expect(user.user_fields[user_field.id.to_s]).not_to eq('happy')
|
2014-09-26 14:48:34 -04:00
|
|
|
end
|
2015-02-23 13:02:30 -05:00
|
|
|
|
|
|
|
it "trims excessively large fields" do
|
2017-08-31 00:06:56 -04:00
|
|
|
put :update, params: {
|
|
|
|
username: user.username, name: 'Jim Tom', user_fields: { user_field.id.to_s => ('x' * 3000) }
|
|
|
|
}, format: :json
|
|
|
|
|
2015-02-23 13:02:30 -05:00
|
|
|
expect(user.user_fields[user_field.id.to_s].size).to eq(UserField.max_length)
|
|
|
|
end
|
2017-12-15 21:16:22 -05:00
|
|
|
|
|
|
|
it "should retain existing user fields" do
|
|
|
|
put :update, params: {
|
|
|
|
username: user.username, name: 'Jim Tom', user_fields: { user_field.id.to_s => 'happy', optional_field.id.to_s => 'feet' }
|
|
|
|
}, format: :json
|
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
expect(user.user_fields[user_field.id.to_s]).to eq('happy')
|
|
|
|
expect(user.user_fields[optional_field.id.to_s]).to eq('feet')
|
|
|
|
|
|
|
|
put :update, params: {
|
|
|
|
username: user.username, name: 'Jim Tom', user_fields: { user_field.id.to_s => 'sad' }
|
|
|
|
}, format: :json
|
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
|
|
|
|
user.reload
|
|
|
|
|
|
|
|
expect(user.user_fields[user_field.id.to_s]).to eq('sad')
|
|
|
|
expect(user.user_fields[optional_field.id.to_s]).to eq('feet')
|
|
|
|
end
|
2014-09-26 14:48:34 -04:00
|
|
|
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
|
2017-08-31 00:06:56 -04:00
|
|
|
put :update, params: {
|
|
|
|
username: user.username, name: 'Jim Tom', user_fields: { user_field.id.to_s => 'happy' }
|
|
|
|
}, format: :json
|
|
|
|
|
2014-09-26 14:48:34 -04:00
|
|
|
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
|
2017-08-31 00:06:56 -04:00
|
|
|
put :update, params: { username: user.username }, format: :json
|
2013-11-01 14:42:16 -04:00
|
|
|
|
|
|
|
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)
|
2016-02-24 05:39:39 -05:00
|
|
|
Guardian.any_instance.expects(:can_edit?).with(user).returns(false)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
put :update,
|
|
|
|
params: { username: user.username, name: 'Jim Tom' },
|
|
|
|
format: :json
|
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-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
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
put :badge_title, params: {
|
|
|
|
user_badge_id: user_badge.id, username: user.username
|
|
|
|
}, format: :json
|
|
|
|
|
2018-01-05 10:58:15 -05:00
|
|
|
expect(user.reload.title).not_to eq(badge.display_name)
|
2014-04-25 14:25:29 -04:00
|
|
|
badge.update_attributes allow_title: true
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
put :badge_title, params: {
|
|
|
|
user_badge_id: user_badge.id, username: user.username
|
|
|
|
}, format: :json
|
|
|
|
|
2018-01-05 10:58:15 -05:00
|
|
|
expect(user.reload.title).to eq(badge.display_name)
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(user.user_profile.badge_granted_title).to eq(true)
|
2014-10-07 19:26:18 -04:00
|
|
|
|
|
|
|
user.title = "testing"
|
|
|
|
user.save
|
|
|
|
user.user_profile.reload
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(user.user_profile.badge_granted_title).to eq(false)
|
2014-10-07 19:26:18 -04:00
|
|
|
|
2014-04-25 14:25:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-05 10:58:15 -05:00
|
|
|
describe "badge_title with overrided name" do
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:badge) { Fabricate(:badge, name: 'Demogorgon', allow_title: true) }
|
|
|
|
let(:user_badge) { BadgeGranter.grant(badge, user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
TranslationOverride.upsert!('en', 'badges.demogorgon.name', 'Boss')
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
TranslationOverride.revert!('en', ['badges.demogorgon.name'])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "uses the badge display name as user title" do
|
|
|
|
log_in_user user
|
|
|
|
|
|
|
|
put :badge_title, params: {
|
|
|
|
user_badge_id: user_badge.id, username: user.username
|
|
|
|
}, format: :json
|
|
|
|
|
|
|
|
expect(user.reload.title).to eq(badge.display_name)
|
|
|
|
end
|
|
|
|
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
|
|
|
|
2017-03-20 05:40:48 -04:00
|
|
|
context 'for an activated account with email confirmed' do
|
2017-03-13 07:19:42 -04:00
|
|
|
it 'fails' do
|
|
|
|
active_user = Fabricate(:user, active: true)
|
2017-03-20 05:40:48 -04:00
|
|
|
email_token = active_user.email_tokens.create(email: active_user.email).token
|
|
|
|
EmailToken.confirm(email_token)
|
2017-03-13 08:20:25 -04:00
|
|
|
session[SessionController::ACTIVATE_USER_KEY] = active_user.id
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
post :send_activation_email, params: {
|
|
|
|
username: active_user.username
|
|
|
|
}, format: :json
|
2017-03-13 07:19:42 -04:00
|
|
|
|
|
|
|
expect(response.status).to eq(409)
|
|
|
|
|
|
|
|
expect(JSON.parse(response.body)['errors']).to include(I18n.t(
|
|
|
|
'activation.activated'
|
|
|
|
))
|
|
|
|
|
2017-03-20 05:40:48 -04:00
|
|
|
expect(session[SessionController::ACTIVATE_USER_KEY]).to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for an activated account with unconfirmed email' do
|
|
|
|
it 'should send an email' do
|
|
|
|
unconfirmed_email_user = Fabricate(:user, active: true)
|
|
|
|
unconfirmed_email_user.email_tokens.create(email: unconfirmed_email_user.email)
|
|
|
|
session[SessionController::ACTIVATE_USER_KEY] = unconfirmed_email_user.id
|
2017-07-31 13:27:39 -04:00
|
|
|
Jobs.expects(:enqueue).with(:critical_user_email, has_entries(type: :signup, to_address: unconfirmed_email_user.email))
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
post :send_activation_email, params: {
|
|
|
|
username: unconfirmed_email_user.username
|
|
|
|
}, format: :json
|
2017-03-20 05:40:48 -04:00
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
2017-03-13 07:19:42 -04:00
|
|
|
expect(session[SessionController::ACTIVATE_USER_KEY]).to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-25 15:28:39 -04:00
|
|
|
context "approval is enabled" do
|
|
|
|
before do
|
|
|
|
SiteSetting.must_approve_users = true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should raise an error" do
|
|
|
|
unconfirmed_email_user = Fabricate(:user, active: true)
|
|
|
|
unconfirmed_email_user.email_tokens.create(email: unconfirmed_email_user.email)
|
|
|
|
session[SessionController::ACTIVATE_USER_KEY] = unconfirmed_email_user.id
|
2017-08-31 00:06:56 -04:00
|
|
|
post :send_activation_email, params: {
|
|
|
|
username: unconfirmed_email_user.username
|
|
|
|
}, format: :json
|
|
|
|
|
2017-05-25 15:28:39 -04:00
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-13 08:20:25 -04:00
|
|
|
describe 'when user does not have a valid session' do
|
|
|
|
it 'should not be valid' do
|
|
|
|
user = Fabricate(:user)
|
2017-08-31 00:06:56 -04:00
|
|
|
post :send_activation_email, params: {
|
|
|
|
username: user.username
|
|
|
|
}, format: :json
|
|
|
|
|
2017-03-13 08:20:25 -04:00
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
2017-03-13 10:32:24 -04:00
|
|
|
|
|
|
|
it 'should allow staff regardless' do
|
|
|
|
log_in :admin
|
|
|
|
user = Fabricate(:user, active: false)
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
post :send_activation_email, params: {
|
|
|
|
username: user.username
|
|
|
|
}, format: :json
|
|
|
|
|
2017-03-13 10:32:24 -04:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
2017-03-13 08:20:25 -04:00
|
|
|
end
|
|
|
|
|
2013-02-22 11:49:48 -05:00
|
|
|
context 'with a valid email_token' do
|
|
|
|
it 'should send the activation email' do
|
2017-03-13 08:20:25 -04:00
|
|
|
session[SessionController::ACTIVATE_USER_KEY] = user.id
|
2016-04-07 00:38:43 -04:00
|
|
|
Jobs.expects(:enqueue).with(:critical_user_email, has_entries(type: :signup))
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
post :send_activation_email, params: {
|
|
|
|
username: user.username
|
|
|
|
}, format: :json
|
2017-03-13 07:19:42 -04:00
|
|
|
|
|
|
|
expect(session[SessionController::ACTIVATE_USER_KEY]).to eq(nil)
|
2013-02-22 11:49:48 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without an existing email_token' do
|
|
|
|
before do
|
2017-07-27 21:20:09 -04:00
|
|
|
user.email_tokens.each { |t| t.destroy }
|
2013-02-22 11:49:48 -05:00
|
|
|
user.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should generate a new token' do
|
|
|
|
expect {
|
2017-03-13 08:20:25 -04:00
|
|
|
session[SessionController::ACTIVATE_USER_KEY] = user.id
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
post :send_activation_email,
|
|
|
|
params: { username: user.username },
|
|
|
|
format: :json
|
|
|
|
}.to change { user.reload.email_tokens.count }.by(1)
|
2013-02-22 11:49:48 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should send an email' do
|
2017-03-13 08:20:25 -04:00
|
|
|
session[SessionController::ACTIVATE_USER_KEY] = user.id
|
2016-04-07 00:38:43 -04:00
|
|
|
Jobs.expects(:enqueue).with(:critical_user_email, has_entries(type: :signup))
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
post :send_activation_email,
|
|
|
|
params: { username: user.username },
|
|
|
|
format: :json
|
2017-03-13 07:19:42 -04:00
|
|
|
|
|
|
|
expect(session[SessionController::ACTIVATE_USER_KEY]).to eq(nil)
|
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
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
post :send_activation_email,
|
|
|
|
params: { username: 'nopenopenopenope' },
|
|
|
|
format: :json
|
2013-02-22 11:49:48 -05: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
|
2018-01-11 22:15:10 -05:00
|
|
|
put :pick_avatar, params: {
|
|
|
|
username: 'asdf', avatar_id: 1, type: "custom"
|
|
|
|
}, format: :json
|
|
|
|
expect(response.status).to eq(403)
|
2013-08-24 16:37:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'while logged in' do
|
|
|
|
|
|
|
|
let!(:user) { log_in }
|
2015-09-17 13:42:44 -04:00
|
|
|
let(:upload) { Fabricate(:upload) }
|
2013-08-24 16:37:31 -04:00
|
|
|
|
2015-09-17 13:42:44 -04:00
|
|
|
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)
|
2017-08-31 00:06:56 -04:00
|
|
|
put :pick_avatar, params: {
|
|
|
|
username: another_user.username, upload_id: upload.id, type: "custom"
|
|
|
|
}, format: :json
|
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_forbidden
|
2013-08-24 16:37:31 -04:00
|
|
|
end
|
|
|
|
|
2015-11-12 04:26:45 -05:00
|
|
|
it "raises an error when sso_overrides_avatar is disabled" do
|
2017-02-13 14:31:15 -05:00
|
|
|
SiteSetting.sso_overrides_avatar = true
|
2017-08-31 00:06:56 -04:00
|
|
|
put :pick_avatar, params: {
|
|
|
|
username: user.username, upload_id: upload.id, type: "custom"
|
|
|
|
}, format: :json
|
|
|
|
|
2015-11-12 04:26:45 -05:00
|
|
|
expect(response).to_not be_success
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error when selecting the custom/uploaded avatar and allow_uploaded_avatars is disabled" do
|
2017-02-13 14:31:15 -05:00
|
|
|
SiteSetting.allow_uploaded_avatars = false
|
2017-08-31 00:06:56 -04:00
|
|
|
put :pick_avatar, params: {
|
|
|
|
username: user.username, upload_id: upload.id, type: "custom"
|
|
|
|
}, format: :json
|
|
|
|
|
2015-11-12 04:26:45 -05:00
|
|
|
expect(response).to_not be_success
|
|
|
|
end
|
|
|
|
|
2015-09-17 13:42:44 -04:00
|
|
|
it 'can successfully pick the system avatar' do
|
2017-08-31 00:06:56 -04:00
|
|
|
put :pick_avatar, params: {
|
|
|
|
username: user.username
|
|
|
|
}, format: :json
|
|
|
|
|
2015-09-17 13:42:44 -04:00
|
|
|
expect(response).to be_success
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(user.reload.uploaded_avatar_id).to eq(nil)
|
2015-09-17 13:42:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'can successfully pick a gravatar' do
|
2017-08-31 00:06:56 -04:00
|
|
|
put :pick_avatar, params: {
|
|
|
|
username: user.username, upload_id: upload.id, type: "gravatar"
|
|
|
|
}, format: :json
|
|
|
|
|
2015-09-17 13:42:44 -04:00
|
|
|
expect(response).to be_success
|
|
|
|
expect(user.reload.uploaded_avatar_id).to eq(upload.id)
|
|
|
|
expect(user.user_avatar.reload.gravatar_upload_id).to eq(upload.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can successfully pick a custom avatar' do
|
2017-08-31 00:06:56 -04:00
|
|
|
put :pick_avatar, params: {
|
|
|
|
username: user.username, upload_id: upload.id, type: "custom"
|
|
|
|
}, format: :json
|
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
2015-09-17 13:42:44 -04:00
|
|
|
expect(user.reload.uploaded_avatar_id).to eq(upload.id)
|
|
|
|
expect(user.user_avatar.reload.custom_upload_id).to eq(upload.id)
|
2013-08-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
|
2018-01-11 22:15:10 -05:00
|
|
|
delete :destroy_user_image,
|
|
|
|
params: { type: 'profile_background', username: 'asdf' },
|
|
|
|
format: :json
|
|
|
|
expect(response.status).to eq(403)
|
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)
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
delete :destroy_user_image,
|
|
|
|
params: { username: user.username, type: 'profile_background' },
|
|
|
|
format: :json
|
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_forbidden
|
2014-02-28 15:12:51 -05:00
|
|
|
end
|
|
|
|
|
2015-05-19 19:39:58 -04:00
|
|
|
it "requires the `type` param" do
|
2017-08-31 00:06:56 -04:00
|
|
|
expect do
|
|
|
|
delete :destroy_user_image, params: { username: user.username }, format: :json
|
|
|
|
end.to raise_error(ActionController::ParameterMissing)
|
2014-06-27 14:48:39 -04:00
|
|
|
end
|
|
|
|
|
2015-05-19 19:39:58 -04:00
|
|
|
it "only allows certain `types`" do
|
2018-01-11 22:15:10 -05:00
|
|
|
delete :destroy_user_image,
|
|
|
|
params: { username: user.username, type: 'wat' },
|
|
|
|
format: :json
|
|
|
|
expect(response.status).to eq(400)
|
2014-06-27 14:48:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'can clear the profile background' do
|
2017-08-31 00:06:56 -04:00
|
|
|
delete :destroy_user_image, params: {
|
|
|
|
type: 'profile_background', username: user.username
|
|
|
|
}, format: :json
|
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(user.reload.user_profile.profile_background).to eq("")
|
|
|
|
expect(response).to be_success
|
2014-02-28 15:12:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
2014-02-13 11:42:35 -05:00
|
|
|
|
|
|
|
describe '.destroy' do
|
|
|
|
it 'raises an error when not logged in' do
|
2018-01-11 22:15:10 -05:00
|
|
|
delete :destroy, params: { username: 'nobody' }, format: :json
|
|
|
|
expect(response.status).to eq(403)
|
2014-02-13 11:42:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'while logged in' do
|
|
|
|
let!(:user) { log_in }
|
|
|
|
|
|
|
|
it 'raises an error when you cannot delete your account' do
|
|
|
|
Guardian.any_instance.stubs(:can_delete_user?).returns(false)
|
|
|
|
UserDestroyer.any_instance.expects(:destroy).never
|
2017-08-31 00:06:56 -04:00
|
|
|
delete :destroy, params: { username: user.username }, format: :json
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_forbidden
|
2014-02-13 11:42:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error when you try to delete someone else's account" do
|
|
|
|
UserDestroyer.any_instance.expects(:destroy).never
|
2017-08-31 00:06:56 -04:00
|
|
|
delete :destroy, params: { username: Fabricate(:user).username }, format: :json
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_forbidden
|
2014-02-13 11:42:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "deletes your account when you're allowed to" do
|
|
|
|
Guardian.any_instance.stubs(:can_delete_user?).returns(true)
|
|
|
|
UserDestroyer.any_instance.expects(:destroy).with(user, anything).returns(user)
|
2017-08-31 00:06:56 -04:00
|
|
|
delete :destroy, params: { username: user.username }, format: :json
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
2014-02-13 11:42:35 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-21 11:52:11 -04:00
|
|
|
describe '.my_redirect' do
|
|
|
|
|
2015-10-14 13:39:31 -04:00
|
|
|
it "redirects if the user is not logged in" do
|
2017-08-31 00:06:56 -04:00
|
|
|
get :my_redirect, params: { path: "wat" }, format: :json
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).not_to be_success
|
2015-10-14 13:39:31 -04:00
|
|
|
expect(response).to be_redirect
|
2014-04-21 11:52:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when the user is logged in" do
|
|
|
|
let!(:user) { log_in }
|
|
|
|
|
|
|
|
it "will not redirect to an invalid path" do
|
2017-08-31 00:06:56 -04:00
|
|
|
get :my_redirect, params: { path: "wat/..password.txt" }, format: :json
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).not_to be_redirect
|
2014-04-21 11:52:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "will redirect to an valid path" do
|
2017-08-31 00:06:56 -04:00
|
|
|
get :my_redirect, params: { path: "preferences" }, format: :json
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_redirect
|
2014-04-21 11:52:11 -04:00
|
|
|
end
|
2014-06-14 13:51:06 -04:00
|
|
|
|
|
|
|
it "permits forward slashes" do
|
2017-08-31 00:06:56 -04:00
|
|
|
get :my_redirect, params: { path: "activity/posts" }, format: :json
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_redirect
|
2014-06-14 13:51:06 -04:00
|
|
|
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
|
2018-01-11 22:15:10 -05:00
|
|
|
put :check_emails, params: { username: 'zogstrip' }, format: :json
|
|
|
|
expect(response.status).to eq(403)
|
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)
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
put :check_emails,
|
|
|
|
params: { username: Fabricate(:user).username },
|
|
|
|
format: :json
|
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_forbidden
|
2014-09-29 16:31:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns both email and associated_accounts when you're allowed to see them" do
|
|
|
|
Guardian.any_instance.expects(:can_check_emails?).returns(true)
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
put :check_emails,
|
|
|
|
params: { username: Fabricate(:user).username },
|
|
|
|
format: :json
|
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
2014-09-29 16:31:05 -04:00
|
|
|
json = JSON.parse(response.body)
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(json["email"]).to be_present
|
|
|
|
expect(json["associated_accounts"]).to be_present
|
2014-09-29 16:31:05 -04:00
|
|
|
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)
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
put :check_emails, params: {
|
|
|
|
username: inactive_user.username
|
|
|
|
}, format: :json
|
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
2014-10-28 20:06:59 -04:00
|
|
|
json = JSON.parse(response.body)
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(json["email"]).to be_present
|
|
|
|
expect(json["associated_accounts"]).to be_present
|
2014-10-28 20:06:59 -04:00
|
|
|
end
|
|
|
|
|
2014-09-29 16:31:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-11-27 12:16:50 -05:00
|
|
|
describe ".is_local_username" do
|
|
|
|
|
|
|
|
let(:user) { Fabricate(:user) }
|
2016-05-22 09:01:46 -04:00
|
|
|
let(:group) { Fabricate(:group, name: "Discourse") }
|
2016-11-14 22:03:16 -05:00
|
|
|
let(:topic) { Fabricate(:topic) }
|
|
|
|
let(:allowed_user) { Fabricate(:user) }
|
|
|
|
let(:private_topic) { Fabricate(:private_message_topic, user: allowed_user) }
|
2015-11-27 12:16:50 -05:00
|
|
|
|
|
|
|
it "finds the user" do
|
2017-08-31 00:06:56 -04:00
|
|
|
get :is_local_username, params: {
|
|
|
|
username: user.username
|
|
|
|
}, format: :json
|
|
|
|
|
2015-11-27 12:16:50 -05:00
|
|
|
expect(response).to be_success
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
expect(json["valid"][0]).to eq(user.username)
|
|
|
|
end
|
|
|
|
|
2016-05-22 09:01:46 -04:00
|
|
|
it "finds the group" do
|
2017-08-31 00:06:56 -04:00
|
|
|
get :is_local_username, params: {
|
|
|
|
username: group.name
|
|
|
|
}, format: :json
|
|
|
|
|
2016-05-22 09:01:46 -04:00
|
|
|
expect(response).to be_success
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
expect(json["valid_groups"][0]).to eq(group.name)
|
|
|
|
end
|
|
|
|
|
2015-11-27 12:16:50 -05:00
|
|
|
it "supports multiples usernames" do
|
2017-08-31 00:06:56 -04:00
|
|
|
get :is_local_username, params: {
|
|
|
|
usernames: [user.username, "system"]
|
|
|
|
}, format: :json
|
|
|
|
|
2015-11-27 12:16:50 -05:00
|
|
|
expect(response).to be_success
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
expect(json["valid"].size).to eq(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "never includes staged accounts" do
|
|
|
|
staged = Fabricate(:user, staged: true)
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
get :is_local_username, params: {
|
|
|
|
usernames: [staged.username]
|
|
|
|
}, format: :json
|
|
|
|
|
2015-11-27 12:16:50 -05:00
|
|
|
expect(response).to be_success
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
expect(json["valid"].size).to eq(0)
|
|
|
|
end
|
|
|
|
|
2016-11-14 22:03:16 -05:00
|
|
|
it "returns user who cannot see topic" do
|
|
|
|
Guardian.any_instance.expects(:can_see?).with(topic).returns(false)
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
get :is_local_username, params: {
|
|
|
|
usernames: [user.username], topic_id: topic.id
|
|
|
|
}, format: :json
|
|
|
|
|
2016-11-14 22:03:16 -05:00
|
|
|
expect(response).to be_success
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
expect(json["cannot_see"].size).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "never returns a user who can see the topic" do
|
|
|
|
Guardian.any_instance.expects(:can_see?).with(topic).returns(true)
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
get :is_local_username, params: {
|
|
|
|
usernames: [user.username], topic_id: topic.id
|
|
|
|
}, format: :json
|
|
|
|
|
2016-11-14 22:03:16 -05:00
|
|
|
expect(response).to be_success
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
expect(json["cannot_see"].size).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns user who cannot see a private topic" do
|
|
|
|
Guardian.any_instance.expects(:can_see?).with(private_topic).returns(false)
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
get :is_local_username, params: {
|
|
|
|
usernames: [user.username], topic_id: private_topic.id
|
|
|
|
}, format: :json
|
|
|
|
|
2016-11-14 22:03:16 -05:00
|
|
|
expect(response).to be_success
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
expect(json["cannot_see"].size).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "never returns a user who can see the topic" do
|
|
|
|
Guardian.any_instance.expects(:can_see?).with(private_topic).returns(true)
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
get :is_local_username, params: {
|
|
|
|
usernames: [allowed_user.username], topic_id: private_topic.id
|
|
|
|
}, format: :json
|
|
|
|
|
2016-11-14 22:03:16 -05:00
|
|
|
expect(response).to be_success
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
expect(json["cannot_see"].size).to eq(0)
|
|
|
|
end
|
|
|
|
|
2015-11-27 12:16:50 -05:00
|
|
|
end
|
|
|
|
|
2016-08-12 02:35:10 -04:00
|
|
|
describe '.topic_tracking_state' do
|
2017-07-27 21:20:09 -04:00
|
|
|
let(:user) { Fabricate(:user) }
|
2016-08-12 02:35:10 -04:00
|
|
|
|
|
|
|
context 'anon' do
|
|
|
|
it "raises an error on anon for topic_tracking_state" do
|
2018-01-11 22:15:10 -05:00
|
|
|
get :topic_tracking_state, params: { username: user.username }, format: :json
|
|
|
|
expect(response.status).to eq(403)
|
2016-08-12 02:35:10 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'logged on' do
|
|
|
|
it "detects new topic" do
|
|
|
|
log_in_user(user)
|
|
|
|
|
|
|
|
topic = Fabricate(:topic)
|
2017-08-31 00:06:56 -04:00
|
|
|
get :topic_tracking_state, params: { username: user.username }, format: :json
|
2016-08-12 02:35:10 -04:00
|
|
|
|
|
|
|
states = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(states[0]["topic_id"]).to eq(topic.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.summary' do
|
2016-01-19 23:11:52 -05:00
|
|
|
|
|
|
|
it "generates summary info" do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
create_post(user: user)
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
get :summary, params: { username: user.username_lower }, format: :json
|
2016-01-19 23:11:52 -05:00
|
|
|
expect(response).to be_success
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(json["user_summary"]["topic_count"]).to eq(1)
|
2017-11-09 18:05:53 -05:00
|
|
|
expect(json["user_summary"]["post_count"]).to eq(0)
|
2016-01-19 23:11:52 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-04 13:59:22 -04:00
|
|
|
describe ".confirm_admin" do
|
|
|
|
it "fails without a valid token" do
|
|
|
|
expect {
|
2017-08-31 00:06:56 -04:00
|
|
|
get :confirm_admin, params: { token: 'invalid-token' }, format: :json
|
2017-04-04 13:59:22 -04:00
|
|
|
}.to raise_error(ActionController::UrlGenerationError)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "fails with a missing token" do
|
2017-08-31 00:06:56 -04:00
|
|
|
get :confirm_admin, params: { token: 'a0a0a0a0a0' }, format: :json
|
2017-04-04 13:59:22 -04:00
|
|
|
expect(response).to_not be_success
|
|
|
|
end
|
|
|
|
|
|
|
|
it "succeeds with a valid code as anonymous" do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
ac = AdminConfirmation.new(user, Fabricate(:admin))
|
|
|
|
ac.create_confirmation
|
2017-08-31 00:06:56 -04:00
|
|
|
get :confirm_admin, params: { token: ac.token }
|
2017-04-04 13:59:22 -04:00
|
|
|
expect(response).to be_success
|
|
|
|
|
|
|
|
user.reload
|
|
|
|
expect(user.admin?).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "succeeds with a valid code when logged in as that user" do
|
|
|
|
admin = log_in(:admin)
|
|
|
|
user = Fabricate(:user)
|
|
|
|
|
|
|
|
ac = AdminConfirmation.new(user, admin)
|
|
|
|
ac.create_confirmation
|
2017-08-31 00:06:56 -04:00
|
|
|
get :confirm_admin, params: { token: ac.token }
|
2017-04-04 13:59:22 -04:00
|
|
|
expect(response).to be_success
|
|
|
|
|
|
|
|
user.reload
|
|
|
|
expect(user.admin?).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "fails if you're logged in as a different account" do
|
|
|
|
log_in(:admin)
|
|
|
|
user = Fabricate(:user)
|
|
|
|
|
|
|
|
ac = AdminConfirmation.new(user, Fabricate(:admin))
|
|
|
|
ac.create_confirmation
|
2017-08-31 00:06:56 -04:00
|
|
|
get :confirm_admin, params: { token: ac.token }, format: :json
|
2017-04-04 13:59:22 -04:00
|
|
|
expect(response).to_not be_success
|
|
|
|
|
|
|
|
user.reload
|
|
|
|
expect(user.admin?).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "post" do
|
|
|
|
it "gives the user admin access when POSTed" do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
ac = AdminConfirmation.new(user, Fabricate(:admin))
|
|
|
|
ac.create_confirmation
|
2017-08-31 00:06:56 -04:00
|
|
|
post :confirm_admin, params: { token: ac.token }
|
2017-04-04 13:59:22 -04:00
|
|
|
expect(response).to be_success
|
|
|
|
|
|
|
|
user.reload
|
|
|
|
expect(user.admin?).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2017-04-05 16:14:22 -04:00
|
|
|
describe '.update_activation_email' do
|
|
|
|
|
2017-05-02 15:57:55 -04:00
|
|
|
context "with a session variable" do
|
2017-04-05 16:14:22 -04:00
|
|
|
|
2017-05-02 15:57:55 -04:00
|
|
|
it "raises an error with an invalid session value" do
|
|
|
|
session[SessionController::ACTIVATE_USER_KEY] = 1234
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
put :update_activation_email, params: {
|
|
|
|
email: 'updatedemail@example.com'
|
|
|
|
}, format: :json
|
|
|
|
|
2017-05-02 15:57:55 -04:00
|
|
|
expect(response).to_not be_success
|
|
|
|
end
|
2017-04-05 16:14:22 -04:00
|
|
|
|
2017-05-02 15:57:55 -04:00
|
|
|
it "raises an error for an active user" do
|
|
|
|
user = Fabricate(:walter_white)
|
|
|
|
session[SessionController::ACTIVATE_USER_KEY] = user.id
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
put :update_activation_email, params: {
|
|
|
|
email: 'updatedemail@example.com'
|
|
|
|
}, format: :json
|
|
|
|
|
2017-05-02 15:57:55 -04:00
|
|
|
expect(response).to_not be_success
|
|
|
|
end
|
2017-04-05 16:14:22 -04:00
|
|
|
|
2017-05-02 15:57:55 -04:00
|
|
|
it "raises an error when logged in" do
|
|
|
|
moderator = log_in(:moderator)
|
|
|
|
session[SessionController::ACTIVATE_USER_KEY] = moderator.id
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
put :update_activation_email, params: {
|
|
|
|
email: 'updatedemail@example.com'
|
|
|
|
}, format: :json
|
|
|
|
|
2017-05-02 15:57:55 -04:00
|
|
|
expect(response).to_not be_success
|
|
|
|
end
|
2017-04-05 16:14:22 -04:00
|
|
|
|
2017-05-02 15:57:55 -04:00
|
|
|
it "raises an error when the new email is taken" do
|
|
|
|
active_user = Fabricate(:user)
|
|
|
|
user = Fabricate(:inactive_user)
|
|
|
|
session[SessionController::ACTIVATE_USER_KEY] = user.id
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
put :update_activation_email, params: {
|
|
|
|
email: active_user.email
|
|
|
|
}, format: :json
|
|
|
|
|
2017-05-02 15:57:55 -04:00
|
|
|
expect(response).to_not be_success
|
|
|
|
end
|
|
|
|
|
2017-09-12 10:03:33 -04:00
|
|
|
it "raises an error when the email is blacklisted" do
|
|
|
|
user = Fabricate(:inactive_user)
|
|
|
|
SiteSetting.email_domains_blacklist = 'example.com'
|
|
|
|
session[SessionController::ACTIVATE_USER_KEY] = user.id
|
2017-08-31 00:06:56 -04:00
|
|
|
put :update_activation_email, params: { email: 'test@example.com' }, format: :json
|
2017-09-12 10:03:33 -04:00
|
|
|
expect(response).to_not be_success
|
|
|
|
end
|
|
|
|
|
2017-05-02 15:57:55 -04:00
|
|
|
it "can be updated" do
|
|
|
|
user = Fabricate(:inactive_user)
|
|
|
|
token = user.email_tokens.first
|
|
|
|
|
|
|
|
session[SessionController::ACTIVATE_USER_KEY] = user.id
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
put :update_activation_email, params: {
|
|
|
|
email: 'updatedemail@example.com'
|
|
|
|
}, format: :json
|
2017-05-02 15:57:55 -04:00
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
|
|
|
|
user.reload
|
|
|
|
expect(user.email).to eq('updatedemail@example.com')
|
|
|
|
expect(user.email_tokens.where(email: 'updatedemail@example.com', expired: false)).to be_present
|
|
|
|
|
|
|
|
token.reload
|
|
|
|
expect(token.expired?).to eq(true)
|
|
|
|
end
|
2017-04-05 16:14:22 -04:00
|
|
|
end
|
|
|
|
|
2017-05-02 15:57:55 -04:00
|
|
|
context "with a username and password" do
|
|
|
|
it "raises an error with an invalid username" do
|
2017-08-31 00:06:56 -04:00
|
|
|
put :update_activation_email, params: {
|
|
|
|
username: 'eviltrout',
|
|
|
|
password: 'invalid-password',
|
|
|
|
email: 'updatedemail@example.com'
|
|
|
|
}, format: :json
|
|
|
|
|
2017-05-02 15:57:55 -04:00
|
|
|
expect(response).to_not be_success
|
|
|
|
end
|
2017-04-05 16:14:22 -04:00
|
|
|
|
2017-05-02 15:57:55 -04:00
|
|
|
it "raises an error with an invalid password" do
|
2017-08-31 00:06:56 -04:00
|
|
|
put :update_activation_email, params: {
|
|
|
|
username: Fabricate(:inactive_user).username,
|
|
|
|
password: 'invalid-password',
|
|
|
|
email: 'updatedemail@example.com'
|
|
|
|
}, format: :json
|
|
|
|
|
2017-05-02 15:57:55 -04:00
|
|
|
expect(response).to_not be_success
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error for an active user" do
|
2017-08-31 00:06:56 -04:00
|
|
|
put :update_activation_email, params: {
|
|
|
|
username: Fabricate(:walter_white).username,
|
|
|
|
password: 'letscook',
|
|
|
|
email: 'updatedemail@example.com'
|
|
|
|
}, format: :json
|
|
|
|
|
2017-05-02 15:57:55 -04:00
|
|
|
expect(response).to_not be_success
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error when logged in" do
|
|
|
|
log_in(:moderator)
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
put :update_activation_email, params: {
|
|
|
|
username: Fabricate(:inactive_user).username,
|
|
|
|
password: 'qwerqwer123',
|
|
|
|
email: 'updatedemail@example.com'
|
|
|
|
}, format: :json
|
|
|
|
|
2017-05-02 15:57:55 -04:00
|
|
|
expect(response).to_not be_success
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error when the new email is taken" do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
put :update_activation_email, params: {
|
|
|
|
username: Fabricate(:inactive_user).username,
|
|
|
|
password: 'qwerqwer123',
|
|
|
|
email: user.email
|
|
|
|
}, format: :json
|
2017-04-26 14:47:36 -04:00
|
|
|
|
2017-05-02 15:57:55 -04:00
|
|
|
expect(response).to_not be_success
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can be updated" do
|
|
|
|
user = Fabricate(:inactive_user)
|
|
|
|
token = user.email_tokens.first
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
put :update_activation_email, params: {
|
2017-08-08 22:56:08 -04:00
|
|
|
username: user.username,
|
|
|
|
password: 'qwerqwer123',
|
|
|
|
email: 'updatedemail@example.com'
|
2017-08-31 00:06:56 -04:00
|
|
|
}, format: :json
|
2017-05-02 15:57:55 -04:00
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
|
|
|
|
user.reload
|
|
|
|
expect(user.email).to eq('updatedemail@example.com')
|
|
|
|
expect(user.email_tokens.where(email: 'updatedemail@example.com', expired: false)).to be_present
|
|
|
|
|
|
|
|
token.reload
|
|
|
|
expect(token.expired?).to eq(true)
|
|
|
|
end
|
2017-04-05 16:14:22 -04:00
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|