Skip to content

Commit

Permalink
Add route and controller for allowing sysadmins to import projects fr…
Browse files Browse the repository at this point in the history
…om Mediaflux (#1294)
  • Loading branch information
carolyncole authored Feb 7, 2025
1 parent bf736f7 commit e5079c7
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 11 deletions.
11 changes: 11 additions & 0 deletions app/controllers/project_import_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true
class ProjectImportController < ApplicationController
def run
if current_user.eligible_sysadmin?
ProjectImport.run_with_report(mediaflux_session: current_user.mediaflux_session)
else
flash[:alert] = I18n.t(:access_denied)
redirect_to root_path
end
end
end
4 changes: 2 additions & 2 deletions app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def index
if current_user.eligible_sysadmin?
@projects = Project.all
else
flash[:alert] = "Access Denied"
flash[:alert] = I18n.t(:access_denied)
redirect_to root_path
end
end
Expand Down Expand Up @@ -272,7 +272,7 @@ def project
if project.user_has_access?(user: current_user)
project
else
flash[:alert] = "Access Denied"
flash[:alert] = I18n.t(:access_denied)
redirect_to root_path
nil
end
Expand Down
10 changes: 10 additions & 0 deletions app/services/project_import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,20 @@ def initialize(csv_data, test_run: false)
@test_run = test_run
end

def self.run_with_report(mediaflux_session:)
report = Mediaflux::ProjectReport.new(session_token: mediaflux_session)
importer = self.new(report.csv_data)
importer.run
end

def run
output = []
mediaflux_projects = CSV.new(csv_data, headers: true)
mediaflux_projects.each do |project_metadata|
# skip projects not part of the current namespace in dev & test mode since we have both mediaflux instances in one server
if Rails.env.development? || Rails.env.test?
next unless project_metadata["path"].starts_with?(Rails.configuration.mediaflux["api_root_collection_namespace"])
end
project_id = project_metadata["projectID"]
existing_project = Project.where("metadata_json @> ?", JSON.dump(project_id:))
if existing_project.count > 0
Expand Down
2 changes: 1 addition & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@
# available at https://guides.rubyonrails.org/i18n.html.

en:
hello: "Hello world"
access_denied: "Access Denied"
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,6 @@
mount ActionCable.server => "/cable"
get "mediaflux_extra", to: "users/mediaflux_callbacks#cas", as: :mediaflux_extra
get "mediaflux_passthru", to: "users/mediaflux_callbacks#passthru", as: :mediaflux_passthru

put "project_import", to: "project_import#run"
end
2 changes: 1 addition & 1 deletion spec/controllers/projects_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@
# Views are stubbed by default for rspec-rails
# https://rspec.info/features/6-0/rspec-rails/controller-specs/isolation-from-views/
render_views
let(:current_user) { FactoryBot.create(:user, uid: "pul123") }
let(:current_user) { FactoryBot.create(:project_sponsor_and_data_manager, uid: "pul123") }
let(:project) { FactoryBot.create(:project, data_sponsor: current_user.uid, data_manager: current_user.uid, title: "project 111") }

before do
Expand Down
6 changes: 3 additions & 3 deletions spec/fixtures/files/project_report.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
asset,path,creatorDomain,creatorUser,createdOn,quota,store,projectDirectory,title,description,dataSponsor,dataManager,dataUser,department,projectID
4894926,/td-staging-001/tigerdata/lynchSample-09497,princeton,tigerdataapp,13-Nov-2024 14:04:17,500000000000,data,/td-staging-001/tigerdataNS/lynchSample-09497,"Project lynchSample 09497","Description of project lynchSample 09497",uid1,uid2,"uid3,uid4","23100",10.00000/1234-abcd
4894935,/td-staging-001/tigerdata/GeophysicsSample2-01014,princeton,tigerdataapp,13-Nov-2024 14:06:26,500000000000,data,/td-staging-001/tigerdataNS/GeophysicsSample2-01014,"Project GeophysicsSample2 01014","Description of project GeophysicsSample2 01014",uid3,uid1,"","10003",10.00000/1234-efgh
4897938,/td-staging-001/tigerdata/data-test-03857,princeton,tigerdataapp,24-Jan-2025 09:34:55,500000000000,data,/td-staging-001/tigerdataNS/data-test-03857,"Project data-test 03857","Description of project data-test 03857",uid4,uid5,"uid1,uid2","10003",10.00000/1234-ijkl
4894926,/td-test-001/test/tigerdata/lynchSample-09497,princeton,tigerdataapp,13-Nov-2024 14:04:17,500000000000,data,//td-test-001/test/tigerdataNS/lynchSample-09497,"Project lynchSample 09497","Description of project lynchSample 09497",uid1,uid2,"uid3,uid4","23100",10.00000/1234-abcd
4894935,/td-test-001/test/tigerdata/GeophysicsSample2-01014,princeton,tigerdataapp,13-Nov-2024 14:06:26,500000000000,data,//td-test-001/test/tigerdataNS/GeophysicsSample2-01014,"Project GeophysicsSample2 01014","Description of project GeophysicsSample2 01014",uid3,uid1,"","10003",10.00000/1234-efgh
4897938,/td-test-001/test/tigerdata/data-test-03857,princeton,tigerdataapp,24-Jan-2025 09:34:55,500000000000,data,//td-test-001/test/tigerdataNS/data-test-03857,"Project data-test 03857","Description of project data-test 03857",uid4,uid5,"uid1,uid2","10003",10.00000/1234-ijkl
44 changes: 44 additions & 0 deletions spec/requests/project_import_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require 'rails_helper'

RSpec.describe "ProjectImports", type: :request do
describe "POST /index" do
it "redirects to sign in" do
put project_import_path
expect(response).to redirect_to new_user_session_path
expect(flash[:alert]).to eq("You need to sign in or sign up before continuing.")
end

context "a signed in user" do
let(:user) { FactoryBot.create :user }

before do
sign_in(user)
end

it "renders a successful response" do
put project_import_path
expect(response).to redirect_to(root_path)
expect(flash[:alert]).to eq("Access Denied")
end
end

context "a sysadmin user" do
let(:user) { FactoryBot.create :sysadmin, mediaflux_session: SystemUser.mediaflux_session}

before do
sign_in(user)
end

it "renders a successful response" do
new_project = FactoryBot.create(:approved_project, project_directory: "test-request")
new_project.mediaflux_id = nil
ProjectMediaflux.create!(project: new_project, user:)
new_project.destroy

expect{ put project_import_path }.to change { Project.count }.by(1)
expect(response).not_to redirect_to(root_path)
expect(flash[:alert]).to be_blank
end
end
end
end
13 changes: 13 additions & 0 deletions spec/services/project_import_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,17 @@
end
end
end

describe "##run_with_report" do
let (:user) {FactoryBot.create :sysadmin, mediaflux_session: SystemUser.mediaflux_session}
it "creates projects for project in Mediaflux" do
new_project = FactoryBot.create(:approved_project, project_directory: "test-request-service")
new_project.mediaflux_id = nil
ProjectMediaflux.create!(project: new_project, user:)
new_project.destroy

expect{ described_class.run_with_report(mediaflux_session: user.mediaflux_session) }.to change { Project.count }.by(1)
end

end
end
6 changes: 2 additions & 4 deletions spec/support/connect_to_mediaflux.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ def create_test_root_namespace(user)

# When connect_to_mediaflux is true reset the mediaflux server and make sure it is setup for the tests
RSpec.configure do |config|
config.before(:each) do |ex|
if ex.metadata[:connect_to_mediaflux]
reset_mediaflux_root
end
config.before(:each) do |_ex|
reset_mediaflux_root
rescue StandardError => namespace_error
message = "Bypassing pre-test cleanup error, #{namespace_error.message}"
puts message # allow the message to show in CI output
Expand Down

0 comments on commit e5079c7

Please sign in to comment.