Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change user for dataset #609

Merged
merged 11 commits into from
Sep 20, 2017
3 changes: 1 addition & 2 deletions app/api/octopub/datasets/files/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Create < Grape::API
params.files = [params.file]
process_file(params.file)

job = UpdateDataset.perform_async(@dataset.id, current_user.id, {}, params.file)
job = UpdateDataset.perform_async(@dataset.id, {}, params.file)

status 202
{
Expand All @@ -43,4 +43,3 @@ class Create < Grape::API
end
end
end

2 changes: 1 addition & 1 deletion app/api/octopub/datasets/files/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Update < Grape::API

process_file(params.file)

job = UpdateDataset.perform_async(@dataset.id, current_user.id, {}, params.file)
job = UpdateDataset.perform_async(@dataset.id, {}, params.file)

status 202
{
Expand Down
2 changes: 1 addition & 1 deletion app/api/octopub/datasets/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Update < Grape::API
end
end
put 'datasets/:id' do
job = UpdateDataset.perform_async(@dataset.id, current_user.id, (params.dataset || {}), [])
job = UpdateDataset.perform_async(@dataset.id, (params.dataset || {}), [])

status 202
{
Expand Down
5 changes: 3 additions & 2 deletions app/controllers/datasets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def create
def edit
render_404 and return if @dataset.nil?
@dataset_file_schemas = available_schemas
@users = User.all
@default_schema = @dataset.dataset_files.first.try(:dataset_file_schema_id)
end

Expand All @@ -83,7 +84,7 @@ def show
def update
logger.info "DatasetsController: In update"
files_array = get_files_as_array_for_serialisation
UpdateDataset.perform_async(params["id"], current_user.id, dataset_update_params.to_h, files_array, channel_id: params[:channel_id])
UpdateDataset.perform_async(params["id"], dataset_update_params.to_h, files_array, channel_id: params[:channel_id])

if params[:async]
head :accepted
Expand Down Expand Up @@ -120,7 +121,7 @@ def check_publisher
end

def dataset_update_params
params[:dataset].try(:permit, [:description, :publisher_name, :publisher_url, :license, :frequency, :schema, :schema_name, :schema_description, :dataset_file_schema_id, :publishing_method])
params[:dataset].try(:permit, [:user_id, :description, :publisher_name, :publisher_url, :license, :frequency, :schema, :schema_name, :schema_description, :dataset_file_schema_id, :publishing_method])
end

def set_direct_post
Expand Down
14 changes: 12 additions & 2 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def organization_options
}
end

def user_option
def user_organization_option
[
current_user.github_username,
current_user.github_username,
Expand All @@ -27,7 +27,17 @@ def user_option
end

def organization_select_options
organization_options.unshift(user_option)
organization_options.unshift(user_organization_option)
end

def user_select_options(users)
users.collect do |user|
[
user.github_username,
user.id,
{ 'data-content' => "<img src='#{current_user.github_user.avatar_url}' height='20' width='20' /> #{current_user.github_username}" }
]
end
end

class CodeRayify < Redcarpet::Render::HTML
Expand Down
18 changes: 15 additions & 3 deletions app/jobs/update_dataset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,27 @@ class UpdateDataset
include Sidekiq::Worker
sidekiq_options retry: false

def perform(id, user_id, dataset_params, files, options = {})
def perform(id, dataset_params, files, options = {})
files = [files] if files.class == Hash

@user = User.find(user_id)
dataset_params = ActiveSupport::HashWithIndifferentAccess.new(
dataset_params.merge(job_id: self.jid)
)

@dataset = get_dataset(id)

user_id = dataset_params[:user_id].try(:to_i)
# Give new user access to repo
if @dataset.publishing_method != 'local_private' &&
user_id &&
user_id != @dataset.user_id
new_user = User.find(user_id)
if new_user
@dataset.user.octokit_client.add_collaborator(@dataset.full_name, new_user.github_username)
end
end

# Update
@dataset.assign_attributes(dataset_params) if dataset_params

handle_files(files)
Expand Down
11 changes: 11 additions & 0 deletions app/views/datasets/_edit_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@
<%= bs_button_to 'Add another data file', '#', style: "success", id: "clone", icon: "glyphicon glyphicon-plus", class: "btn-xs" %>
</p>

<% if dataset.id %>
<div class='panel panel-danger'>
<div class="panel-heading">
<h3 class="panel-title">Danger Zone</h3>
</div>
<div class="panel-body">
<%= f.select "dataset[user_id]", user_select_options(@users), { label: "Transfer dataset to another user" }, { 'data-live-search': "true", class: "selectpicker form-control show-tick" } %>
<span id="transferHelpBlock" class="help-block">Selecting another user may make it impossible for you to edit this dataset further. Only change it if you know what you are doing!</span>
</div>
</div>
<% end %>

<button type="submit" class="btn btn-primary btn-lg btn-block">
Submit <i class="fa fa-refresh fa-spin hidden" id="spinner"></i>
Expand Down
1 change: 0 additions & 1 deletion spec/api/create_files_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,3 @@
# end

end

1 change: 1 addition & 0 deletions spec/api/update_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
it 'updates a dataset sucessfully' do
license = Octopub::API_LICENCES.sample
frequency = Octopub::PUBLICATION_FREQUENCIES.sample

put "/api/datasets/#{@dataset.id}", params:
{
dataset: {
Expand Down
6 changes: 6 additions & 0 deletions spec/controllers/datasets/edit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
expect(assigns(:dataset_file_schemas).count).to eq(2)
end

it "lists available users" do
sign_in @user
dataset = create(:dataset, name: "Dataset", user: @user)
get :edit, params: { id: dataset.id }
expect(assigns(:users).count).to eq(User.count)
end

# TODO can't see how this ever works without organizations!

Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/datasets/update_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@
}
]

expect(UpdateDataset).to receive(:perform_async).with(@dataset.id.to_s, @user.id, @dataset_hash.stringify_keys!, [
expect(UpdateDataset).to receive(:perform_async).with(@dataset.id.to_s, @dataset_hash.stringify_keys!, [
{
"id" => @dataset_file.id.to_s,
"title" => "New title",
Expand Down
18 changes: 14 additions & 4 deletions spec/features/edit_dataset_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
let(:dataset_file_description) { Faker::Lorem.sentence }

before(:each) do
allow(UpdateDataset).to receive(:perform_async) do |a,b,c,d,e|
UpdateDataset.new.perform(a,b,c,d,e)
allow(UpdateDataset).to receive(:perform_async) do |a,b,c,d|
UpdateDataset.new.perform(a,b,c,d)
end
@another_user = create(:user, name: "A. N. Other", name: "another-username")
good_schema_url = url_with_stubbed_get_for_fixture_file('schemas/good-schema.json')
@dataset_file_schema = create(:dataset_file_schema, url_in_repo: good_schema_url, name: 'good schema', description: 'good schema description', user: @user)
@dataset = create(:dataset, name: dataset_name, user: @user, license: "CC-BY-4.0", description: dataset_description)
@dataset = create(:dataset, name: dataset_name, user: @user, license: "CC-BY-4.0", description: dataset_description, owner: "owner", repo: "repo")
file = create(:dataset_file, dataset_file_schema: @dataset_file_schema,
filename: "example.csv",
title: dataset_file_name,
Expand Down Expand Up @@ -58,6 +59,16 @@
expect(@dataset.description).to eq new_description
end

scenario "can change the user" do
expect_any_instance_of(Octokit::Client).to receive(:add_collaborator).with("owner/repo", "another-username").once
# select by ID, because of bootstrap selects hiding the text itself at this level
select @another_user.github_username, from: '_dataset[user_id]'
click_on 'Submit'
expect(page).to have_content 'Your edits have been queued for creation'
@dataset.reload
expect(@dataset.user).to eq @another_user
end

scenario "can edit the file description" do
new_description = Faker::Lorem.sentence
within 'div.visible' do
Expand All @@ -78,4 +89,3 @@

end
end

10 changes: 5 additions & 5 deletions spec/features/update_dataset_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
expect(page.all('table.table tr').count).to be Dataset.count + 1
page.find("tr[data-dataset-id='#{@dataset.id}']").click_link('Edit')
# Bypass sidekiq completely
allow(UpdateDataset).to receive(:perform_async) do |a,b,c,d,e|
UpdateDataset.new.perform(a,b,c,d,e)
end
allow(UpdateDataset).to receive(:perform_async) do |a,b,c,d|
UpdateDataset.new.perform(a,b,c,d)
end
allow_any_instance_of(UpdateDataset).to receive(:handle_files) do |a,b|
{}
end
Expand Down Expand Up @@ -74,8 +74,8 @@
expect(page.all('table.table tr').count).to be Dataset.count + 1
page.find("tr[data-dataset-id='#{@dataset.id}']").click_link('Edit')
# Bypass sidekiq completely
allow(UpdateDataset).to receive(:perform_async) do |a,b,c,d,e|
UpdateDataset.new.perform(a,b,c,d,e)
allow(UpdateDataset).to receive(:perform_async) do |a,b,c,d|
UpdateDataset.new.perform(a,b,c,d)
end
allow_any_instance_of(UpdateDataset).to receive(:handle_files) do |a,b|
{}
Expand Down
4 changes: 2 additions & 2 deletions spec/helpers/application_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@
)
end

it 'gets user option' do
expect(helper.user_option).to eq(
it 'gets user organization option' do
expect(helper.user_organization_option).to eq(
[
@user.github_username,
@user.github_username,
Expand Down
10 changes: 5 additions & 5 deletions spec/jobs/update_dataset_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
expect_any_instance_of(JekyllService).to receive(:add_jekyll_to_github)
expect_any_instance_of(JekyllService).to receive(:push_to_github)

@worker.perform(@dataset.id, @user.id, @dataset_params, @files, "channel_id" => "beep-beep")
@worker.perform(@dataset.id, @dataset_params, @files, "channel_id" => "beep-beep")

expect(@dataset.job_id).to eq("84855ffe6a7e1d6dacf6685e")
end
Expand All @@ -61,7 +61,7 @@
expect_any_instance_of(JekyllService).to receive(:add_jekyll_to_github)
expect_any_instance_of(JekyllService).to receive(:push_to_github)

dataset = @worker.perform(@dataset.id, @user.id, @dataset_params, @files, "channel_id" => "beep-beep")
dataset = @worker.perform(@dataset.id, @dataset_params, @files, "channel_id" => "beep-beep")
end

it "reports success and doesn't push to GitHub it private" do
Expand All @@ -71,7 +71,7 @@
expect_any_instance_of(JekyllService).to_not receive(:add_jekyll_to_github)
expect_any_instance_of(JekyllService).to_not receive(:push_to_github)

dataset = @worker.perform(@dataset.id, @user.id, @dataset_params, @files, "channel_id" => "beep-beep")
dataset = @worker.perform(@dataset.id, @dataset_params, @files, "channel_id" => "beep-beep")
end

context 'reports errors' do
Expand All @@ -97,12 +97,12 @@
expect(mock_client).to receive(:trigger).with('dataset_failed', instance_of(Array))
expect_any_instance_of(JekyllService).to receive(:push_to_github)

@worker.perform(@dataset.id, @user.id, @dataset_params, @bad_files, "channel_id" => "beep-beep")
@worker.perform(@dataset.id, @dataset_params, @bad_files, "channel_id" => "beep-beep")
end

it 'to the database' do
expect_any_instance_of(JekyllService).to receive(:push_to_github)
@worker.perform(@dataset.id, @user.id, @dataset_params, @bad_files)
@worker.perform(@dataset.id, @dataset_params, @bad_files)

expect(Error.count).to eq(1)
expect(Error.first.messages).to eq(["Dataset files is invalid", "Your file 'My File' does not appear to be a valid CSV. Please check your file and try again."])
Expand Down