From fa7c2aeb3d7218f0c35ba2a2a3dba74642a30d6f Mon Sep 17 00:00:00 2001 From: stephanie rousset Date: Fri, 9 Aug 2024 14:29:58 +0200 Subject: [PATCH] refactor: update and add tests --- ...y_participatory_space_private_user_spec.rb | 56 +++++-------------- .../destroy_private_users_follows_job_spec.rb | 56 +++++++++++++++++++ 2 files changed, 71 insertions(+), 41 deletions(-) create mode 100644 decidim-admin/spec/jobs/destroy_private_users_follows_job_spec.rb diff --git a/decidim-admin/spec/commands/decidim/admin/destroy_participatory_space_private_user_spec.rb b/decidim-admin/spec/commands/decidim/admin/destroy_participatory_space_private_user_spec.rb index c1d254caa58a7..47998b6ed1df4 100644 --- a/decidim-admin/spec/commands/decidim/admin/destroy_participatory_space_private_user_spec.rb +++ b/decidim-admin/spec/commands/decidim/admin/destroy_participatory_space_private_user_spec.rb @@ -38,34 +38,25 @@ module Decidim::Admin expect(action_log.version).to be_nil end - context "when assembly is not transparent" do + context "when assembly is private and user follows assembly" do let(:normal_user) { create(:user, organization:) } - let(:assembly) { create(:assembly, :private, :opaque, :published, organization: user.organization) } + let(:assembly) { create(:assembly, :private, :published, organization: user.organization) } let!(:participatory_space_private_user) { create(:participatory_space_private_user, user: normal_user, privatable_to: assembly) } + let!(:follow) { create(:follow, followable: assembly, user: normal_user) } - context "and user follows assembly" do - let!(:follow) { create(:follow, followable: assembly, user: normal_user) } - - it "destroys the follow" do + context "and assembly is transparent" do + it "does not enqueue a job" do + assembly.update(is_transparent: true) expect(Decidim::Follow.where(user: normal_user).count).to eq(1) - expect do - subject.call - end.to change(Decidim::Follow, :count).by(-1) + expect { subject.call }.not_to have_enqueued_job(DestroyPrivateUsersFollowsJob) end + end - context "and user follows meeting belonging to assembly" do - let(:meetings_component) { create(:component, manifest_name: "meetings", participatory_space: assembly) } - - it "destroys all follows" do - meeting = Decidim::Meetings::Meeting.create!(title: generate_localized_title(:meeting_title, skip_injection: false), - description: generate_localized_description(:meeting_description, skip_injection: false), - component: meetings_component, author: user) - create(:follow, followable: meeting, user: normal_user) - expect(Decidim::Follow.where(user: normal_user).count).to eq(2) - expect do - subject.call - end.to change(Decidim::Follow, :count).by(-2) - end + context "when assembly is not transparent" do + it "enqueues a job" do + assembly.update(is_transparent: false) + expect(Decidim::Follow.where(user: normal_user).count).to eq(1) + expect { subject.call }.to have_enqueued_job(DestroyPrivateUsersFollowsJob) end end end @@ -78,26 +69,9 @@ module Decidim::Admin context "and user follows process" do let!(:follow) { create(:follow, followable: participatory_process, user: normal_user) } - it "destroys the follow" do + it "enqueues a job" do expect(Decidim::Follow.where(user: normal_user).count).to eq(1) - expect do - subject.call - end.to change(Decidim::Follow, :count).by(-1) - end - - context "and user follows meeting belonging to process" do - let(:meetings_component) { create(:component, manifest_name: "meetings", participatory_space: participatory_process) } - - it "destroys all follows" do - meeting = Decidim::Meetings::Meeting.create!(title: generate_localized_title(:meeting_title, skip_injection: false), - description: generate_localized_description(:meeting_description, skip_injection: false), - component: meetings_component, author: user) - create(:follow, followable: meeting, user: normal_user) - expect(Decidim::Follow.where(user: normal_user).count).to eq(2) - expect do - subject.call - end.to change(Decidim::Follow, :count).by(-2) - end + expect { subject.call }.to have_enqueued_job(DestroyPrivateUsersFollowsJob) end end end diff --git a/decidim-admin/spec/jobs/destroy_private_users_follows_job_spec.rb b/decidim-admin/spec/jobs/destroy_private_users_follows_job_spec.rb new file mode 100644 index 0000000000000..61a9db5ea7d39 --- /dev/null +++ b/decidim-admin/spec/jobs/destroy_private_users_follows_job_spec.rb @@ -0,0 +1,56 @@ +# frozen_string_literal: true + +require "spec_helper" + +module Decidim + module Admin + describe DestroyPrivateUsersFollowsJob do + let(:organization) { create(:organization) } + let(:user) { create(:user, :admin, :confirmed, organization:) } + let(:participatory_space_private_user) { create(:participatory_space_private_user, user:) } + + context "when assembly is not transparent and user follows assembly and one meeting belonging to assembly" do + let(:normal_user) { create(:user, organization:) } + let(:assembly) { create(:assembly, :private, :opaque, :published, organization: user.organization) } + let!(:participatory_space_private_user) { create(:participatory_space_private_user, user: normal_user, privatable_to: assembly) } + let!(:follow) { create(:follow, followable: assembly, user: normal_user) } + let(:meetings_component) { create(:component, manifest_name: "meetings", participatory_space: assembly) } + + it "destroys 2 follows" do + meeting = Decidim::Meetings::Meeting.create!(title: generate_localized_title(:meeting_title, skip_injection: false), + description: generate_localized_description(:meeting_description, skip_injection: false), + component: meetings_component, author: user) + create(:follow, followable: meeting, user: normal_user) + + expect(Decidim::Follow.where(user: normal_user).count).to eq(2) + expect do + described_class.perform_now(normal_user.id, "Decidim::Assembly", assembly) + end.to change(Decidim::Follow, :count).by(-2) + end + end + + context "when participatory process is private" do + let(:normal_user) { create(:user, organization:) } + let(:participatory_process) { create(:participatory_process, :private, :published, organization: user.organization) } + let!(:participatory_space_private_user) { create(:participatory_space_private_user, user: normal_user, privatable_to: participatory_process) } + + context "and user follows process and follows meeting belonging to process" do + let!(:follow) { create(:follow, followable: participatory_process, user: normal_user) } + let(:meetings_component) { create(:component, manifest_name: "meetings", participatory_space: participatory_process) } + + it "destroys 2 follows" do + meeting = Decidim::Meetings::Meeting.create!(title: generate_localized_title(:meeting_title, skip_injection: false), + description: generate_localized_description(:meeting_description, skip_injection: false), + component: meetings_component, author: user) + create(:follow, followable: meeting, user: normal_user) + + expect(Decidim::Follow.where(user: normal_user).count).to eq(2) + expect do + described_class.perform_now(normal_user.id, "Decidim::ParticipatoryProcess", participatory_process) + end.to change(Decidim::Follow, :count).by(-2) + end + end + end + end + end +end