Skip to content

Commit

Permalink
Merge pull request #23 from coopdevs/dont-serialize-file-contents
Browse files Browse the repository at this point in the history
Serialize file path instead of its contents
  • Loading branch information
sauloperez committed May 28, 2021
2 parents a6f4dcb + 5cdd5d4 commit 9e30bad
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ def call
attr_reader :form, :file, :organization, :user, :action

def register_users_async
RegisterUsersJob.perform_later(file.read, organization, user)
RegisterUsersJob.perform_later(file.path, organization, user)
end

def revoke_users_async
RevokeUsersJob.perform_later(file.read, organization, user)
RevokeUsersJob.perform_later(file.path, organization, user)
end

def authorize_users_async
AuthorizeUsersJob.perform_later(file.read, organization, user)
AuthorizeUsersJob.perform_later(file.path, organization, user)
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion app/jobs/decidim/direct_verifications/base_import_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ module DirectVerifications
class BaseImportJob < ApplicationJob
queue_as :default

def perform(userslist, organization, current_user)
def perform(path, organization, current_user)
userslist = File.read(path)
@emails = Verification::MetadataParser.new(userslist).to_h
@organization = organization
@current_user = current_user
Expand Down
36 changes: 26 additions & 10 deletions spec/jobs/decidim/direct_verifications/register_users_job_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "spec_helper"
require "tempfile"

module Decidim
module DirectVerifications
Expand All @@ -22,24 +23,39 @@ module DirectVerifications
end

it "creates the user" do
expect { described_class.perform_later(userslist, organization, current_user) }
.to change(Decidim::User, :count).from(1).to(2)
Tempfile.create("import.csv") do |file|
file.write(userslist)
file.rewind

user = Decidim::User.find_by(email: "brandy@example.com")
expect(user.name).to eq("brandy")
expect { described_class.perform_later(file.path, organization, current_user) }
.to change(Decidim::User, :count).from(1).to(2)

user = Decidim::User.find_by(email: "brandy@example.com")
expect(user.name).to eq("brandy")
end
end

it "does not authorize the user" do
described_class.perform_later(userslist, organization, current_user)
Tempfile.create("import.csv") do |file|
file.write(userslist)
file.rewind

described_class.perform_later(file.path, organization, current_user)

user = Decidim::User.find_by(email: "brandy@example.com")
authorization = Decidim::Authorization.find_by(decidim_user_id: user.id)
expect(authorization).to be_nil
user = Decidim::User.find_by(email: "brandy@example.com")
authorization = Decidim::Authorization.find_by(decidim_user_id: user.id)
expect(authorization).to be_nil
end
end

it "notifies the result by email" do
described_class.perform_later(userslist, organization, current_user)
expect(mailer).to have_received(:deliver_now)
Tempfile.create("import.csv") do |file|
file.write(userslist)
file.rewind

described_class.perform_later(file.path, organization, current_user)
expect(mailer).to have_received(:deliver_now)
end
end
end
end
Expand Down
18 changes: 14 additions & 4 deletions spec/jobs/decidim/direct_verifications/revoke_users_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,23 @@ module DirectVerifications
end

it "deletes the user authorization" do
described_class.perform_later(userslist, organization, current_user)
expect(Decidim::Authorization.find_by(id: authorization.id)).to be_nil
Tempfile.create("import.csv") do |file|
file.write(userslist)
file.rewind

described_class.perform_later(file.path, organization, current_user)
expect(Decidim::Authorization.find_by(id: authorization.id)).to be_nil
end
end

it "notifies the result by email" do
described_class.perform_later(userslist, organization, current_user)
expect(mailer).to have_received(:deliver_now)
Tempfile.create("import.csv") do |file|
file.write(userslist)
file.rewind

described_class.perform_later(file.path, organization, current_user)
expect(mailer).to have_received(:deliver_now)
end
end
end
end
Expand Down

0 comments on commit 9e30bad

Please sign in to comment.