From 1b063065129f4d49306cf864bea693cca6828496 Mon Sep 17 00:00:00 2001 From: Vasili Kachalko Date: Wed, 22 May 2024 18:11:50 +0200 Subject: [PATCH] add specs on form/account controllers --- spec/controllers/accounts_controller_spec.rb | 60 ++++++++++++++++++++ spec/controllers/form_controller_spec.rb | 24 ++++++++ 2 files changed, 84 insertions(+) diff --git a/spec/controllers/accounts_controller_spec.rb b/spec/controllers/accounts_controller_spec.rb index e69de29bb2..4dd1ec55a4 100644 --- a/spec/controllers/accounts_controller_spec.rb +++ b/spec/controllers/accounts_controller_spec.rb @@ -0,0 +1,60 @@ +require "rails_helper" + +describe AccountsController do + let(:old_password) { "^#ur9EkLm@1W+OaDvgTT" } + let(:new_password) { "^#ur9EkLm@1W+OaDvg" } + + let(:user) { create(:user, :completed_profile, password: "^#ur9EkLm@1W+OaDvgTT", password_confirmation: "^#ur9EkLm@1W+OaDvgTT") } + + before do + sign_in user + end + + describe "PATCH #update_password_settings" do + context "with valid params" do + it "updates the password and redirects to the dashboard with a success message" do + patch :update_password_settings, params: { + user: { + current_password: old_password, + password: new_password, + password_confirmation: new_password, + }, + } + + user.reload + expect(user.valid_password?(new_password)).to be_truthy + expect(flash[:notice]).to eq("Your account details were successfully saved") + expect(response).to redirect_to(dashboard_path) + end + end + + context "with invalid params" do + it "does not update the password and renders the password_settings template with an error message" do + patch :update_password_settings, params: { + user: { + current_password: "wrong_password", + password: new_password, + password_confirmation: new_password, + }, + } + + user.reload + expect(user.valid_password?(new_password)).to be_falsey + expect(flash[:alert]).to eq("Error updating your password") + expect(response).to render_template(:password_settings) + end + + it "sets @active_step to 5" do + patch :update_password_settings, params: { + user: { + current_password: "wrong_password", + password: new_password, + password_confirmation: new_password, + }, + } + + expect(assigns(:active_step)).to eq(5) + end + end + end +end diff --git a/spec/controllers/form_controller_spec.rb b/spec/controllers/form_controller_spec.rb index 3d1e9ef102..d0e0d6d3f4 100644 --- a/spec/controllers/form_controller_spec.rb +++ b/spec/controllers/form_controller_spec.rb @@ -100,4 +100,28 @@ end end end + + describe "#add_attachment" do + let(:file) { Rack::Test::UploadedFile.new(Rails.root.join("spec/fixtures/cat.jpg"), "image/jpeg") } + + it "adds attachment to the form answer" do + expect { + post :add_attachment, params: { + form: { + file: file, + }, + id: form_answer.id, + question_key: "org_chart", + } + }.to change { + form_answer.reload.form_answer_attachments.count + }.by(1) + + expect(response).to have_http_status(:created) + + id = JSON.parse(response.body)["id"] + attachment = FormAnswerAttachment.find_by(id: id) + expect(attachment).to be_present + end + end end