-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create relation between location and rescan_runner * Edit method to use instance's location * Create method to schedule all runners * tmp rollback * Migrate and annotate * Fix wrong method name * Simplify schedule_all and make run private * Rework Controller and routes * Don't schedule new job while running * Update model tests * Rename controller to RescansController * Add tests for new controller * Fix routes * Fix policy * Update way we test schedule methods * Dont' set finished_at, since this is done in SQL * Remove unused method * Update rake task * Explicitly test that RescanRunner is create in callback * Include id in serializer * Update seeds * Update test * Make count explicit * Fix test Co-authored-by: Charlotte Van Petegem <[email protected]>
- Loading branch information
Showing
16 changed files
with
255 additions
and
148 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
class RescansController < ApplicationController | ||
before_action :set_rescan, only: %i[show start] | ||
|
||
def index | ||
authorize RescanRunner | ||
@rescans = policy_scope(RescanRunner).paginate(page: params[:page], per_page: params[:per_page]) | ||
add_pagination_headers(@rescans) | ||
render json: @rescans | ||
end | ||
|
||
def show | ||
render json: @rescan | ||
end | ||
|
||
def start | ||
@rescan.schedule | ||
render json: @rescan | ||
end | ||
|
||
def start_all | ||
authorize RescanRunner | ||
RescanRunner.schedule_all | ||
end | ||
|
||
private | ||
|
||
def set_rescan | ||
@rescan = RescanRunner.find(params[:id]) | ||
authorize @rescan | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,23 @@ | ||
class RescanRunnerPolicy < ApplicationPolicy | ||
class Scope < Scope | ||
def resolve | ||
scope.all if user&.moderator? | ||
end | ||
end | ||
|
||
def index? | ||
user&.moderator? | ||
end | ||
|
||
def show? | ||
user&.moderator? | ||
end | ||
|
||
def start? | ||
user&.moderator? | ||
end | ||
|
||
def start_all? | ||
user&.moderator? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
db/migrate/20210905090539_add_location_to_rescan_runner.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class AddLocationToRescanRunner < ActiveRecord::Migration[6.1] | ||
def change | ||
RescanRunner.destroy_all | ||
|
||
add_reference :rescan_runners, :location, null: false, foreign_key: true | ||
|
||
Location.find_each do |l| | ||
RescanRunner.create(finished_at: Date.new, location: l) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
namespace :rescan do | ||
task start: :environment do | ||
RescanRunner.instance.schedule | ||
RescanRunner.schedule_all | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
require 'test_helper' | ||
|
||
class RescansControllerTest < ActionDispatch::IntegrationTest | ||
setup do | ||
@runner = create(:rescan_runner) | ||
@moderator = create(:moderator) | ||
@user = create(:user) | ||
sign_in_as(@user) | ||
end | ||
|
||
test 'should get not index for user' do | ||
get rescans_url | ||
assert_response :forbidden | ||
end | ||
|
||
test 'should get index for moderator' do | ||
sign_in_as(@moderator) | ||
get rescans_url | ||
assert_response :success | ||
end | ||
|
||
test 'should get not show for user' do | ||
get rescan_url(@runner) | ||
assert_response :forbidden | ||
end | ||
|
||
test 'should get show for moderator' do | ||
sign_in_as(@moderator) | ||
get rescan_url(@runner) | ||
assert_response :success | ||
end | ||
|
||
test 'should not start rescan for user' do | ||
post rescan_url(@runner) | ||
assert_response :forbidden | ||
end | ||
|
||
test 'should start rescan' do | ||
sign_in_as(@moderator) | ||
prev = @runner.finished_at | ||
post rescan_url(@runner) | ||
assert_response :success | ||
@runner.reload | ||
assert_not_equal prev, @runner.finished_at | ||
end | ||
|
||
test 'should not start all rescans for user' do | ||
post rescans_url | ||
assert_response :forbidden | ||
end | ||
|
||
test 'should start all rescans' do | ||
sign_in_as(@moderator) | ||
prev = @runner.finished_at | ||
post rescans_url | ||
assert_response :success | ||
@runner.reload | ||
assert_not_equal prev, @runner.finished_at | ||
end | ||
end |
Oops, something went wrong.