Skip to content

Commit

Permalink
Allow exporting the private user when the user exports themselves
Browse files Browse the repository at this point in the history
The `.user_collection` method needs to search through the entire
collection for it to work properly for the private user exports.

Also some minor refactoring in the spec and user extensions
concern.
  • Loading branch information
ahukkanen committed Dec 11, 2023
1 parent d67073c commit dcaa83d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 15 deletions.
11 changes: 9 additions & 2 deletions app/models/concerns/decidim/privacy/user_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Decidim
module Privacy
module UserExtensions
extend ActiveSupport::Concern

included do
before_update :update_followers_count

Expand All @@ -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?

Expand Down
57 changes: 44 additions & 13 deletions spec/models/decidim/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit dcaa83d

Please sign in to comment.