Skip to content

Commit

Permalink
Adjusted tests
Browse files Browse the repository at this point in the history
  • Loading branch information
prioux committed May 25, 2024
1 parent 2501302 commit d9dd9cb
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 35 deletions.
5 changes: 5 additions & 0 deletions Bourreau/lib/bourreau_system_checks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,11 @@ def self.z020_start_background_activity_workers #:nodoc:
puts "C> Starting Background Activity Workers..."
#----------------------------------------------------------------------------

if ENV['CBRAIN_NO_BACKGROUND_ACTIVITY_WORKER'].present?
puts "C> \t- NOT started as env variable CBRAIN_NO_BACKGROUND_ACTIVITY_WORKER is set."
return
end

worker_name = 'BourreauActivity'
num_workers = 1 # hardcoded, usually that's enough for a Bourreau

Expand Down
2 changes: 1 addition & 1 deletion BrainPortal/app/controllers/data_providers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ def delete
# Generate a complete response matching the old API
flash[:notice] = ""
flash[:notice] += "Deleting #{exist_files.count} userfile(s) in background.\n" if exist_files.present?
flash[:notice] += "Deleting #{unreg_basenames.count} unregistered files in background.\n" if unreg_basenames.present?
flash[:notice] += "Deleting #{unreg_basenames.count} unregistered file(s) in background.\n" if unreg_basenames.present?
flash[:error] = "No files selected, or that can be deleted." if exist_files.empty? && unreg_basenames.empty?
api_response = generate_register_response.merge(
:num_erased => exist_files.size,
Expand Down
17 changes: 17 additions & 0 deletions BrainPortal/app/models/background_activity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ class BackgroundActivity < ApplicationRecord
validate :repeat_is_correct

before_save :add_empty_options
after_destroy :record_in_rails_log

belongs_to :user
belongs_to :remote_resource
Expand Down Expand Up @@ -554,6 +555,22 @@ def options_custom_filter_name
# before_save callback, adds options={} if options is nil
def add_empty_options #:nodoc:
self.options = {} if self.options.nil?
true
end

# For user activity statistics, record the BAC in the Rails logs.
# Usually invoked from after_destroy callback.
def record_in_rails_log #:nodoc:
status = self.status
return unless status =~ /^(?:
Completed | PartiallyCompleted | Failed | InProgress |
Cancelled | Suspended | InternalError
)/x # we don't want Scheduled etc that never did anything

json_text = self.attributes.to_json
Rails.logger.info "Destroyed BackgroundActivity: #{json_text}"
rescue
Rails.logger.error("Cannot log #{self.type} #{self.id}") rescue nil
end

def status_is_correct #:nodoc:
Expand Down
64 changes: 31 additions & 33 deletions BrainPortal/spec/controllers/data_providers_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -303,77 +303,75 @@
expect(response).to render_template("browse")
end
end

describe "register" do
let(:registered_file) {mock_model(SingleFile, :save => true).as_null_object.as_new_record}

before(:each) do
allow(DataProvider).to receive(:find_accessible_by_user).and_return(data_provider)
allow(data_provider).to receive(:is_browsable?).and_return(true)
allow(CBRAIN).to receive(:spawn_with_active_records)
allow(SingleFile).to receive(:new).and_return(registered_file)
allow(registered_file).to receive(:save).and_return(true)
end

it "should check if the data_provider is browsable" do
expect(data_provider).to receive(:is_browsable?)
post :register, params: {id: 1, basenames: ["a_file"]}
post :register, params: {id: 1, basenames: ["a_file"], filetypes: ["TextFile-a_file"]}
end

context "provider is not browsable" do
before(:each) do
allow(data_provider).to receive(:is_browsable?).and_return(false)
end
it "should display an error message" do
post :register, params: {id: 1, basenames: ["a_file"]}
post :register, params: {id: 1, basenames: ["a_file"], filetypes: ["TextFile-a_file"]}
expect(flash[:error]).not_to be_blank
end
it "should redirect to index" do
post :register, params: {id: 1, basenames: ["a_file"]}
post :register, params: {id: 1, basenames: ["a_file"], filetypes: ["TextFile-a_file"]}
expect(response).to redirect_to(:action => :index)
end
end

context "register only" do
it "should set the sizes in a separate process" do
expect(CBRAIN).to receive(:spawn_with_active_records)
post :register, params: {id: 1, basenames: ["a_file"]}
it "should check for collisions" do
allow(controller).to receive(:userfiles_from_basenames).and_return({"a_file" => TextFile.new})
expect(BackgroundActivity::RegisterFile).not_to receive(:setup!)
post :register, params: {id: 1, basenames: ["a_file"], filetypes: ["TextFile-a_file"], auto_do: ""}
end
it "should display that registration was a success" do
post :register, params: {id: 1, basenames: ["a_file"]}
allow(controller).to receive(:userfiles_from_basenames).and_return({"a_file" => nil})
expect(BackgroundActivity::RegisterFile).to receive(:setup!)
post :register, params: {id: 1, basenames: ["a_file"], filetypes: ["TextFile-a_file"]}
expect(flash[:notice]).to match(/\bRegistering\b/i)
end
it "should redirect to browse" do
post :register, params: {id: 1, basenames: ["a_file"]}
allow(controller).to receive(:userfiles_from_basenames).and_return({"a_file" => nil})
expect(BackgroundActivity::RegisterFile).to receive(:setup!)
post :register, params: {id: 1, basenames: ["a_file"], filetypes: ["TextFile-a_file"]}
expect(response).to redirect_to(:action => :browse)
end
end
context "doing move or copy" do
before(:each) do
allow(CBRAIN).to receive(:spawn_with_active_records).and_yield
it "should setup a background activity for registering" do
allow(controller).to receive(:userfiles_from_basenames).and_return({"a_file" => nil})
expect(BackgroundActivity::RegisterFile).to receive(:setup!)
post :register, params: {id: 1, basenames: ["a_file"], filetypes: ["TextFile-a_file"], auto_do: ""}
end
end

it "should check for collisions" do
expect(Userfile).to receive(:where).and_return(double("registered files").as_null_object)
post :register, params: {id: 1, basenames: ["a_file"],auto_do: "MOVE"}
context "doing move or copy" do
it "should setup a background activity for moving" do
allow(controller).to receive(:userfiles_from_basenames).and_return({"a_file" => nil})
expect(BackgroundActivity::RegisterAndMoveFile).to receive(:setup!)
post :register, params: {id: 1, basenames: ["a_file"], filetypes: ["TextFile-a_file"], auto_do: "MOVE"}
end
it "should spawn a background process for moving" do
expect(CBRAIN).to receive(:spawn_with_active_records)
post :register, params: {id: 1, basenames: ["a_file"],auto_do: "MOVE"}
it "should setup a background activity for moving" do
allow(controller).to receive(:userfiles_from_basenames).and_return({"a_file" => nil})
expect(BackgroundActivity::RegisterAndCopyFile).to receive(:setup!)
post :register, params: {id: 1, basenames: ["a_file"], filetypes: ["TextFile-a_file"], auto_do: "COPY"}
end
it "should redirect to browse" do
post :register, params: {id: 1, basenames: ["a_file"],auto_do: "MOVE"}
allow(controller).to receive(:userfiles_from_basenames).and_return({"a_file" => nil})
expect(BackgroundActivity::RegisterAndMoveFile).to receive(:setup!)
post :register, params: {id: 1, basenames: ["a_file"], filetypes: ["TextFile-a_file"], auto_do: "MOVE"}
expect(response).to redirect_to(:action => :browse)
end
it "should move the file if a move is requested" do
allow(controller).to receive(:userfiles_from_basenames).and_return({"b_file" => nil})
expect(registered_file).to receive(:provider_move_to_otherprovider)
post :register, params: {id: 1, basenames: ["b_file"], auto_do: "MOVE"}
end
it "should copy the file if a copy is requested" do
allow(controller).to receive(:userfiles_from_basenames).and_return({"b_file" => nil})
expect(registered_file).to receive(:provider_copy_to_otherprovider)
post :register, params: {id: 1, basenames: ["b_file"], auto_do: "COPY"}
end
end

end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
200 application/json we_will_delete_all_after_first_comma ,.*
{"notice":"Deleting 2 userfile(s) in background.\n","error":null,"newly_registered_userfiles":[],"previously_registered_userfiles":[],"userfiles_in_transit":[],"num_unregistered":0,"num_erased":2}
{"notice":"Deleting 2 unregistered file(s) in background.\n","error":null,"newly_registered_userfiles":[],"previously_registered_userfiles":[],"userfiles_in_transit":[],"num_unregistered":0,"num_erased":2}

0 comments on commit d9dd9cb

Please sign in to comment.