A platform for community discussion. Free, open, simple.
tgxworld feedback
OsamaSayegh0347c9752023
diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rbindex 8414e90a0d6..43c344ed567 100644--- a/spec/requests/users_controller_spec.rb+++ b/spec/requests/users_controller_spec.rb@@ -3,40 +3,45 @@ require 'rails_helper'describe UsersController dolet(:user) { Fabricate(:user) }- describe '#activate_account' do- let(:token) { "asdfasdf" }+ describe '#perform_account_activation' do+ let(:token) do+ return @token if @token.present?+ email_token = EmailToken.create!(expired: false, confirmed: false, user: user, email: user.email)+ @token = email_token.token+ @token+ end+before doUsersController.any_instance.stubs(:honeypot_or_challenge_fails?).returns(false)endcontext 'invalid token' doit 'return success' do- put "/u/activate-account/#{token}"- expect(response).to be_success+ put "/u/activate-account/invalid-tooken"+ expect(response.status).to eq(200)expect(flash[:error]).to be_presentendendcontext 'valid token' do- let(:user) { Fabricate(:user) }-context 'welcome message' dobefore do- EmailToken.expects(:confirm).with("#{token}").returns(user)+ SiteSetting.queue_jobs = trueendit 'enqueues a welcome message if the user object indicates so' do- user.send_welcome_message = true- user.expects(:enqueue_welcome_message).with('welcome_user')-+ user.update(active: false)put "/u/activate-account/#{token}"+ expect(response.status).to eq(200)+ expect(Jobs::SendSystemMessage.jobs.size).to eq(1)+ expect(Jobs::SendSystemMessage.jobs.first["args"].first["message_type"]).to eq("welcome_user")endit "doesn't enqueue the welcome message if the object returns false" do- user.send_welcome_message = false- user.expects(:enqueue_welcome_message).with('welcome_user').never-+ user.update(active: true)put "/u/activate-account/#{token}"+ expect(response.status).to eq(200)+ expect(Jobs::SendSystemMessage.jobs.size).to eq(0)endend@@ -44,7 +49,7 @@ describe UsersController doit "raises an error if the honeypot is invalid" doUsersController.any_instance.stubs(:honeypot_or_challenge_fails?).returns(true)put "/u/activate-account/#{token}"- expect(response).not_to be_success+ expect(response.status).to eq(403)endend@@ -59,7 +64,7 @@ describe UsersController doput "/u/activate-account/#{token}"end- expect(events.map { |event| event[:event_name] }).to include(+ expect(events.map { |event| event[:event_name] }).to contain_exactly(:user_logged_in, :user_first_logged_in)@@ -67,8 +72,6 @@ describe UsersController doexpect(flash[:error]).to be_blankexpect(session[:current_user_id]).to be_present- expect(response).to be_success-expect(CGI.unescapeHTML(response.body)).to_not include(I18n.t('activation.approval_required'))end@@ -96,18 +99,8 @@ describe UsersController doendendend- end-- 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+ context 'when cookies contains a destination URL' doit 'should redirect to the URL' dodestination_url = 'http://thisisasite.com/somepath'cookies[:destination_url] = destination_url@@ -128,6 +121,7 @@ describe UsersController doSiteSetting.login_required = trueget "/u/password-reset/#{token}"expect(response).to be_success+ expect(CGI.unescapeHTML(response.body)).to include(I18n.t('password_reset.no_token'))endend@@ -181,7 +175,7 @@ describe UsersController douser = Fabricate(:user)token = user.email_tokens.create(email: user.email).tokenget "/u/password-reset/#{token}"-+ expect(response.status).to eq(200)expect(response.body).to include('<meta name="referrer" content="never">')endend@@ -190,21 +184,18 @@ describe UsersController douser = Fabricate(:user)user_auth_token = UserAuthToken.generate!(user_id: user.id)token = user.email_tokens.create(email: user.email).token- get "/u/password-reset/#{token}"events = DiscourseEvent.track_events doput "/u/password-reset/#{token}", params: { password: 'hg9ow8yhg98o' }end- expect(events.map { |event| event[:event_name] }).to include(+ expect(events.map { |event| event[:event_name] }).to contain_exactly(:user_logged_in, :user_first_logged_in)expect(response).to be_successexpect(response.body).to include('{"is_developer":false,"admin":false,"second_factor_required":false}')- user.reload-expect(session["password-#{token}"]).to be_blankexpect(UserAuthToken.where(id: user_auth_token.id).count).to eq(0)end@@ -213,8 +204,6 @@ describe UsersController douser = Fabricate(:user)token = user.email_tokens.create(email: user.email).token- get "/u/password-reset/#{token}"-put "/u/password-reset/#{token}", params: { password: 'hg9ow8yHG32O' }put "/u/password-reset/#{token}", params: { password: 'test123987AsdfXYZ' }@@ -254,6 +243,7 @@ describe UsersController doemail_token = user.email_tokens.create(email: user.email)get "/u/password-reset/#{email_token.token}.json"+ expect(response.status).to eq(200)email_token.reload@@ -291,6 +281,7 @@ describe UsersController do}user.reload+ expect(response.status).to eq(200)expect(user.confirm_password?('hg9ow8yHG32O')).to eq(true)expect(user.user_auth_tokens.count).to eq(1)end@@ -330,8 +321,8 @@ describe UsersController doit "doesn't log in the user when not approved" doSiteSetting.must_approve_users = true- put "/u/password-reset/#{token}.json", params: { password: 'ksjafh928r' }+ put "/u/password-reset/#{token}.json", params: { password: 'ksjafh928r' }expect(JSON.parse(response.body)["errors"]).to be_blankexpect(session[:current_user_id]).to be_blankend@@ -362,8 +353,12 @@ describe UsersController docontext 'enqueues mail' doit 'enqueues mail with admin email and sso enabled' do- Jobs.expects(:enqueue).with(:critical_user_email, has_entries(type: :admin_login, user_id: admin.id))+ SiteSetting.queue_jobs = trueput "/u/admin-login", params: { email: admin.email }+ expect(response.status).to eq(200)+ expect(Jobs::CriticalUserEmail.jobs.size).to eq(1)+ args = Jobs::CriticalUserEmail.jobs.first["args"].first+ expect(args["user_id"]).to eq(admin.id)endend@@ -510,48 +505,62 @@ describe UsersController doendcontext 'when creating a non active user (unconfirmed email)' do- it 'returns a 500 when local logins are disabled' do+ it 'returns 403 forbidden when local logins are disabled' doSiteSetting.enable_local_logins = falsepost_user- expect(response.status).to eq(500)+ expect(response.status).to eq(403)endit 'returns an error when new registrations are disabled' doSiteSetting.allow_new_registrations = false+post_user+ expect(response.status).to eq(200)+json = JSON.parse(response.body)expect(json['success']).to eq(false)expect(json['message']).to be_presentendit '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+ SiteSetting.queue_jobs = truepost_user-+ expect(response.status).to eq(200)expect(JSON.parse(response.body)['active']).to be_falsey# should save user_created_message in sessionexpect(session["user_created_message"]).to be_presentexpect(session[SessionController::ACTIVATE_USER_KEY]).to be_present++ expect(Jobs::SendSystemMessage.jobs.size).to eq(0)++ expect(Jobs::CriticalUserEmail.jobs.size).to eq(1)+ args = Jobs::CriticalUserEmail.jobs.first["args"].first+ expect(args["type"]).to eq("signup")endcontext "`must approve users` site setting is enabled" dobefore { SiteSetting.must_approve_users = true }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+ SiteSetting.queue_jobs = truepost_user+ expect(response.status).to eq(200)expect(JSON.parse(response.body)['active']).to be_falsey# should save user_created_message in sessionexpect(session["user_created_message"]).to be_presentexpect(session[SessionController::ACTIVATE_USER_KEY]).to be_present++ expect(Jobs::SendSystemMessage.jobs.size).to eq(0)++ expect(Jobs::CriticalUserEmail.jobs.size).to eq(1)+ args = Jobs::CriticalUserEmail.jobs.first["args"].first+ expect(args["type"]).to eq("signup")endend@@ -560,7 +569,10 @@ describe UsersController doit 'returns an error if hide_email_address_taken is disabled' doSiteSetting.hide_email_address_taken = false+post_user+ expect(response.status).to eq(200)+json = JSON.parse(response.body)expect(json['success']).to eq(false)expect(json['message']).to be_present@@ -571,9 +583,13 @@ describe UsersController doexpect {post_user}.to_not change { User.count }++ expect(response.status).to eq(200)+ expect(session["user_created_message"]).to be_present+json = JSON.parse(response.body)expect(json['active']).to be_falsey- expect(session["user_created_message"]).to be_present+ expect(json['message']).to eq(I18n.t("login.activate_email", email: post_user_params[:email]))endendend@@ -581,6 +597,7 @@ describe UsersController docontext "creating as active" doit "won't create the user as active" dopost "/u.json", params: post_user_params.merge(active: true)+ expect(response.status).to eq(200)expect(JSON.parse(response.body)['active']).to be_falseyend@@ -592,6 +609,7 @@ describe UsersController dopost "/u.json",params: post_user_params.merge(active: true, api_key: api_key.key)+ expect(response.status).to eq(200)expect(JSON.parse(response.body)['active']).to be_falseyendend@@ -605,10 +623,13 @@ describe UsersController doSiteSetting.send_welcome_message = trueSiteSetting.must_approve_users = true- Sidekiq::Client.expects(:enqueue).never-+ #Sidekiq::Client.expects(:enqueue).neverpost "/u.json", params: post_user_params.merge(approved: true, active: true, api_key: api_key.key)+ expect(Jobs::CriticalUserEmail.jobs.size).to eq(0)+ expect(Jobs::SendSystemMessage.jobs.size).to eq(0)++ expect(response.status).to eq(200)json = JSON.parse(response.body)new_user = User.find(json["user_id"])@@ -625,7 +646,7 @@ describe UsersController doUsernameCheckerService.expects(:is_developer?).returns(true)post "/u.json", params: post_user_params.merge(active: true, api_key: api_key.key)-+ expect(response.status).to eq(200)expect(JSON.parse(response.body)['active']).to be_falsyendend@@ -634,7 +655,7 @@ describe UsersController docontext "creating as staged" doit "won't create the user as staged" dopost "/u.json", params: post_user_params.merge(staged: true)-+ expect(response.status).to eq(200)new_user = User.where(username: post_user_params[:username]).firstexpect(new_user.staged?).to eq(false)end@@ -645,6 +666,7 @@ describe UsersController doit "won't create the user as staged with a regular key" dopost "/u.json", params: post_user_params.merge(staged: true, api_key: api_key.key)+ expect(response.status).to eq(200)new_user = User.where(username: post_user_params[:username]).firstexpect(new_user.staged?).to eq(false)@@ -657,6 +679,7 @@ describe UsersController doit "creates the user as staged with a regular key" dopost "/u.json", params: post_user_params.merge(staged: true, api_key: api_key.key)+ expect(response.status).to eq(200)new_user = User.where(username: post_user_params[:username]).firstexpect(new_user.staged?).to eq(true)@@ -665,6 +688,7 @@ describe UsersController doit "won't create the developer as staged" doUsernameCheckerService.expects(:is_developer?).returns(true)post "/u.json", params: post_user_params.merge(staged: true, api_key: api_key.key)+ expect(response.status).to eq(200)new_user = User.where(username: post_user_params[:username]).firstexpect(new_user.staged?).to eq(false)@@ -676,8 +700,11 @@ describe UsersController dobefore { User.any_instance.stubs(:active?).returns(true) }it 'enqueues a welcome email' do+ SiteSetting.queue_jobs = trueUser.any_instance.expects(:enqueue_welcome_message).with('welcome_user')+post_user+ expect(response.status).to eq(200)# should save user_created_message in sessionexpect(session["user_created_message"]).to be_present@@ -687,6 +714,7 @@ describe UsersController doit "shows the 'active' message" doUser.any_instance.expects(:enqueue_welcome_message)post_user+ expect(response.status).to eq(200)expect(JSON.parse(response.body)['message']).to eq(I18n.t 'login.active')@@ -695,19 +723,22 @@ describe UsersController doit "should be logged in" doUser.any_instance.expects(:enqueue_welcome_message)post_user+ expect(response.status).to eq(200)expect(session[:current_user_id]).to be_presentendit 'indicates the user is active in the response' doUser.any_instance.expects(:enqueue_welcome_message)post_user+ expect(response.status).to eq(200)expect(JSON.parse(response.body)['active']).to be_truthyend- it 'returns 500 status when new registrations are disabled' do+ it 'doesn\'t succeed when new registrations are disabled' doSiteSetting.allow_new_registrations = falsepost_user+ expect(response.status).to eq(200)json = JSON.parse(response.body)expect(json['success']).to eq(false)@@ -739,6 +770,7 @@ describe UsersController doemail: "[email protected]"}+ expect(response.status).to eq(200)expect(TwitterUserInfo.count).to eq(1)end@@ -750,6 +782,7 @@ describe UsersController doemail: "[email protected]"}+ expect(response.status).to eq(200)json = JSON.parse(response.body)expect(json['success']).to eq(false)expect(json['message']).to be_present@@ -763,28 +796,19 @@ describe UsersController doemail: "[email protected]"}+ expect(response.status).to eq(200)json = JSON.parse(response.body)expect(json['success']).to eq(true)endendend- context 'after success' do- before { post_user }-- it 'should succeed' do- expect(response).to be_success- #is_expected.to respond_with(:success)- end-- it 'has the proper JSON' do- json = JSON::parse(response.body)- expect(json["success"]).to eq(true)- end-- it 'should not result in an active account' do- expect(User.find_by(username: @user.username).active).to eq(false)- end+ it "creates user successfully but doesn't activate the account" do+ post_user+ expect(response.status).to eq(200)+ json = JSON::parse(response.body)+ expect(json["success"]).to eq(true)+ expect(User.find_by(username: @user.username).active).to eq(false)endshared_examples 'honeypot fails' do@@ -792,16 +816,19 @@ describe UsersController doexpect {post "/u.json", params: create_params}.to_not change { User.count }+ expect(response.status).to eq(200)endit 'should not send an email' doUser.any_instance.expects(:enqueue_welcome_message).neverpost "/u.json", params: create_params+ expect(response.status).to eq(200)endit 'should say it was successful' dopost "/u.json", params: create_paramsjson = JSON::parse(response.body)+ expect(response.status).to eq(200)expect(json["success"]).to eq(true)# should not change the session@@ -873,7 +900,6 @@ describe UsersController docontext 'with a reserved username' dolet(:create_params) { { name: @user.name, username: 'Reserved', email: @user.email, password: "x" * 20 } }before { SiteSetting.reserved_usernames = 'a|reserved|b' }- after { SiteSetting.reserved_usernames = nil }include_examples 'failed signup'end@@ -998,7 +1024,7 @@ describe UsersController docreated_user.reloadexpect(created_user.email).to eq("[email protected]")- expect(response.status).not_to eq(200)+ expect(response.status).to eq(403)endendend@@ -1021,7 +1047,7 @@ describe UsersController doit 'raises an error without a new_username param' doput "/u/#{user.username}/preferences/username.json", params: { username: user.username }- expect(response).not_to be_success+ expect(response.status).to eq(400)expect(user.reload.username).to eq(old_username)end@@ -1037,7 +1063,7 @@ describe UsersController doit 'raises an error when change_username fails' doput "/u/#{user.username}/preferences/username.json", params: { new_username: '@' }- expect(response).to_not be_success+ expect(response.status).to eq(422)body = JSON.parse(response.body)@@ -1091,36 +1117,27 @@ describe UsersController dodescribe '#check_username' doit 'raises an error without any parameters' doget "/u/check_username.json"- expect(response).not_to be_success+ expect(response.status).to eq(400)endshared_examples 'when username is unavailable' do- it 'should return success' do- expect(response).to be_success- end-- it 'should return available as false in the JSON' do+ it 'should return available as false in the JSON and return a suggested username' do+ expect(response.status).to eq(200)expect(::JSON.parse(response.body)['available']).to eq(false)- end-- it 'should return a suggested username' doexpect(::JSON.parse(response.body)['suggestion']).to be_presentendendshared_examples 'when username is available' do- it 'should return success' do- expect(response).to be_success- end-it 'should return available in the JSON' do+ expect(response.status).to eq(200)expect(::JSON.parse(response.body)['available']).to eq(true)endendit 'returns nothing when given an email param but no username' doget "/u/check_username.json", params: { email: '[email protected]' }- expect(response).to be_success+ expect(response.status).to eq(200)endcontext 'username is available' do@@ -1139,16 +1156,10 @@ describe UsersController doendshared_examples 'checking an invalid username' do- it 'should return success' do- expect(response).to be_success- end-- it 'should not return an available key' do+ it 'should not return an available key but should return an error message' do+ expect(response.status).to eq(200)expect(::JSON.parse(response.body)['available']).to eq(nil)- end-- it 'should return an error message' do- expect(::JSON.parse(response.body)['errors']).not_to be_empty+ expect(::JSON.parse(response.body)['errors']).to be_presentendend@@ -1159,6 +1170,7 @@ describe UsersController doinclude_examples 'checking an invalid username'it 'should return the invalid characters message' do+ expect(response.status).to eq(200)expect(::JSON.parse(response.body)['errors']).to include(I18n.t(:'user.username.characters'))endend@@ -1170,6 +1182,7 @@ describe UsersController doinclude_examples 'checking an invalid username'it 'should return the "too long" message' do+ expect(response.status).to eq(200)expect(::JSON.parse(response.body)['errors']).to include(I18n.t(:'user.username.long', max: User.username_length.end))endend@@ -1218,7 +1231,7 @@ describe UsersController doit 'filters by email' doinviter = Fabricate(:user)invitee = Fabricate(:user)- _invite = Fabricate(+ Fabricate(:invite,email: '[email protected]',invited_by: inviter,@@ -1458,7 +1471,7 @@ describe UsersController doit "cannot be updated to blank" doput "/u/#{user.username}.json", params: { name: 'Jim Tom', user_fields: { user_field.id.to_s => '' } }- expect(response).not_to be_success+ expect(response.status).to eq(422)expect(user.user_fields[user_field.id.to_s]).not_to eq('happy')end@@ -1510,7 +1523,6 @@ describe UsersController doit 'does not allow the update' douser = Fabricate(:user, name: 'Billy Bob')sign_in(Fabricate(:user))- #Guardian.any_instance.expects(:can_edit?).with(user).returns(false)put "/u/#{user.username}.json", params: { name: 'Jim Tom' }@@ -1678,8 +1690,10 @@ describe UsersController doendit 'should send an email' do- Jobs.expects(:enqueue).with(:critical_user_email, has_entries(type: :signup))+ SiteSetting.queue_jobs = truepost "/u/action/send_activation_email.json", params: { username: user.username }++ expect(Jobs::CriticalUserEmail.jobs.size).to eq(1)expect(session[SessionController::ACTIVATE_USER_KEY]).to eq(nil)endend@@ -1687,9 +1701,10 @@ describe UsersController docontext 'when username does not exist' doit 'should not send an email' do- Jobs.expects(:enqueue).never-+ SiteSetting.queue_jobs = truepost "/u/action/send_activation_email.json", params: { username: 'nopenopenopenope' }+ expect(response.status).to eq(404)+ expect(Jobs::CriticalUserEmail.jobs.size).to eq(0)endendend@@ -1720,7 +1735,7 @@ describe UsersController doupload_id: upload.id, type: "custom"}- expect(response).to_not be_success+ expect(response.status).to eq(422)endit "raises an error when selecting the custom/uploaded avatar and allow_uploaded_avatars is disabled" do@@ -1729,7 +1744,7 @@ describe UsersController doupload_id: upload.id, type: "custom"}- expect(response).to_not be_success+ expect(response.status).to eq(422)endit 'can successfully pick the system avatar' do@@ -1838,7 +1853,6 @@ describe UsersController dodescribe '#my_redirect' doit "redirects if the user is not logged in" doget "/my/wat.json"- expect(response).not_to be_successexpect(response).to be_redirectend@@ -1912,7 +1926,7 @@ describe UsersController doit "finds the user" doget "/u/is_local_username.json", params: { username: user.username }- expect(response).to be_success+ expect(response.status).to eq(200)json = JSON.parse(response.body)expect(json["valid"][0]).to eq(user.username)end@@ -1920,7 +1934,7 @@ describe UsersController doit "finds the group" doget "/u/is_local_username.json", params: { username: group.name }- expect(response).to be_success+ expect(response.status).to eq(200)json = JSON.parse(response.body)expect(json["valid_groups"][0]).to eq(group.name)end@@ -1928,7 +1942,7 @@ describe UsersController doit "supports multiples usernames" doget "/u/is_local_username.json", params: { usernames: [user.username, "system"] }- expect(response).to be_success+ expect(response.status).to eq(200)json = JSON.parse(response.body)expect(json["valid"].size).to eq(2)end@@ -1938,7 +1952,7 @@ describe UsersController doget "/u/is_local_username.json", params: { usernames: [staged.username] }- expect(response).to be_success+ expect(response.status).to eq(200)json = JSON.parse(response.body)expect(json["valid"].size).to eq(0)end@@ -1950,7 +1964,7 @@ describe UsersController dousernames: [user.username], topic_id: topic.id}- expect(response).to be_success+ expect(response.status).to eq(200)json = JSON.parse(response.body)expect(json["cannot_see"].size).to eq(1)end@@ -1962,7 +1976,7 @@ describe UsersController dousernames: [user.username], topic_id: topic.id}- expect(response).to be_success+ expect(response.status).to eq(200)json = JSON.parse(response.body)expect(json["cannot_see"].size).to eq(0)end@@ -1974,7 +1988,7 @@ describe UsersController dousernames: [user.username], topic_id: private_topic.id}- expect(response).to be_success+ expect(response.status).to eq(200)json = JSON.parse(response.body)expect(json["cannot_see"].size).to eq(1)end@@ -1986,7 +2000,7 @@ describe UsersController dousernames: [allowed_user.username], topic_id: private_topic.id}- expect(response).to be_success+ expect(response.status).to eq(200)json = JSON.parse(response.body)expect(json["cannot_see"].size).to eq(0)end@@ -2009,6 +2023,7 @@ describe UsersController dotopic = Fabricate(:topic)get "/u/#{user.username}/topic-tracking-state.json"+ expect(response.status).to eq(200)states = JSON.parse(response.body)expect(states[0]["topic_id"]).to eq(topic.id)end@@ -2432,7 +2447,7 @@ describe UsersController doendend- describe '#account_created' do+ describe "#account_created" doit "returns a message when no session is present" doget "/u/account-created"