diff --git a/app/models/concerns/decidim/privacy/user_extensions.rb b/app/models/concerns/decidim/privacy/user_extensions.rb index 4289121..9dedd62 100644 --- a/app/models/concerns/decidim/privacy/user_extensions.rb +++ b/app/models/concerns/decidim/privacy/user_extensions.rb @@ -4,6 +4,7 @@ module Decidim module Privacy module UserExtensions extend ActiveSupport::Concern + included do before_update :update_followers_count @@ -28,16 +29,22 @@ module UserExtensions index_on_create: ->(user) { !user.deleted? && user.public? }, index_on_update: ->(user) { !user.deleted? && user.public? }) - # we need to remove the default scope for the registeration, so as to check the uniqueness of + # We need to remove the default scope for the registeration, so as to check the uniqueness of # accounts through all of the accounts def self.find_for_authentication(warden_conditions) organization = warden_conditions.dig(:env, "decidim.current_organization") - unscoped.find_by( + entire_collection.find_by( email: warden_conditions[:email].to_s.downcase, decidim_organization_id: organization.id ) end + # This method is used to export the user record so it also needs to + # search through the entire collection. + def self.user_collection(user) + entire_collection.where(id: user.id) + end + def public? return false if blocked? diff --git a/spec/models/decidim/user_spec.rb b/spec/models/decidim/user_spec.rb index a892790..6a8a68f 100644 --- a/spec/models/decidim/user_spec.rb +++ b/spec/models/decidim/user_spec.rb @@ -9,27 +9,58 @@ let!(:published_user) { create(:user, :confirmed, :published, organization: organization) } let!(:private_user) { create(:user, :confirmed, organization: organization) } - describe "#default_scope" do - it "returns published users by default" do - result = subject.all - expect(result).to include(published_user) - expect(result).not_to include(private_user) + describe ".default_scope" do + subject { described_class.all } + + it "returns only published users by default" do + expect(subject).to include(published_user) + expect(subject).not_to include(private_user) end end - describe "#entire_collection" do + describe ".entire_collection" do + subject { described_class.entire_collection } + it "rerutns entire_collection when scoped" do - result = subject.entire_collection.all - expect(result).to include(published_user) - expect(result).to include(private_user) + expect(subject).to include(published_user) + expect(subject).to include(private_user) + end + end + + describe ".profile_published" do + subject { described_class.profile_published } + + it "returns the published users only" do + expect(subject).to include(published_user) + expect(subject).not_to include(private_user) end end - describe "#profile_private" do + describe ".profile_private" do + subject { described_class.profile_private } + it "returns private when scoped" do - result = subject.profile_private.all - expect(result).not_to include(published_user) - expect(result).to include(private_user) + expect(subject).not_to include(published_user) + expect(subject).to include(private_user) + end + end + + describe ".find_for_authentication" do + subject { described_class.find_for_authentication(conditions) } + + let(:conditions) { { email: private_user.email, env: { "decidim.current_organization" => organization } } } + + it "finds the private user for authentication" do + expect(subject).to eq(private_user) + end + end + + describe ".user_collection" do + subject { described_class.user_collection(private_user) } + + it "finds the private user for export" do + expect(subject.count).to eq(1) + expect(subject).to include(private_user) end end