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

Fix filtering on new saved annotation page #5838

Merged
merged 6 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions app/controllers/saved_annotations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ def new
authorize SavedAnnotation
@annotations = AnnotationPolicy::Scope.new(current_user, Annotation.all).resolve
@annotations = @annotations.where(saved_annotation_id: nil).where(user_id: current_user.id)
@courses = Course.where(id: @annotations.joins(:submission).pluck('submissions.course_id').uniq)
@exercises = Activity.where(id: @annotations.joins(:submission).pluck('submissions.exercise_id').uniq)
submissions = Submission.where(id: @annotations.pluck(:submission_id).uniq)
@courses = Course.where(id: submissions.pluck(:course_id).uniq)
@exercises = Activity.where(id: submissions.pluck(:exercise_id).uniq)
@annotations = apply_scopes(@annotations.order_by_created_at(:DESC))
.includes(:course).includes(:user).includes(:submission)
.paginate(page: parse_pagination_param(params[:page]), per_page: parse_pagination_param(params[:per_page]))
Expand Down
2 changes: 1 addition & 1 deletion app/policies/annotation_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def resolve
scope.all
elsif user
common = scope.joins(:submission)
common.released.where(submissions: { user: user }).or(common.where(submissions: { course_id: user.administrating_courses.map(&:id) }))
common.released.where(submission: { user: user }).or(common.where(submission: { course_id: user.administrating_courses.map(&:id) }))
else
scope.none
end
Expand Down
21 changes: 21 additions & 0 deletions test/controllers/saved_annotation_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,25 @@ def setup

assert_response :forbidden
end

test 'staff should be able to filter existing annotations on new page' do
get new_saved_annotation_path

assert_response :success

get new_saved_annotation_path, params: { exercise_id: 1 }

assert_response :success
end
Comment on lines +53 to +61
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance test coverage for staff filtering

The test correctly checks if staff can access the new saved annotation page with and without filtering. However, it could be improved to verify the actual filtering functionality.

Consider the following improvements:

  1. Add setup code to create test data (e.g., saved annotations with different exercise_ids).
  2. Verify the content of the response, not just the status code.
  3. Assert that the filtered results only contain annotations for the specified exercise_id.

Here's a suggested improvement:

test 'staff should be able to filter existing annotations on new page' do
  exercise1 = create(:exercise)
  exercise2 = create(:exercise)
  annotation1 = create(:saved_annotation, user: @user, course: @course, exercise: exercise1)
  annotation2 = create(:saved_annotation, user: @user, course: @course, exercise: exercise2)

  get new_saved_annotation_path
  assert_response :success
  assert_select 'your-selector-for-annotation', 2

  get new_saved_annotation_path, params: { exercise_id: exercise1.id }
  assert_response :success
  assert_select 'your-selector-for-annotation', 1
  assert_select 'your-selector-for-annotation-title', annotation1.title
end

Replace 'your-selector-for-annotation' with the actual CSS selector used in your view to represent annotations.


test 'zeus should be able to filter existing annotations on new page' do
sign_in users(:zeus)
get new_saved_annotation_path

assert_response :success

get new_saved_annotation_path, params: { exercise_id: 1 }

assert_response :success
end
Comment on lines +63 to +72
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Refactor zeus test and enhance coverage

The zeus test is very similar to the staff test, which suggests an opportunity for refactoring to reduce code duplication. Additionally, it has the same limitations in terms of verifying the actual filtering functionality.

Consider the following improvements:

  1. Refactor the common testing logic into a shared method.
  2. Enhance the test to verify the content of the response, similar to the suggestions for the staff test.
  3. Ensure that zeus users can only see their own annotations in the filtered results.

Here's a suggested refactoring and improvement:

def assert_can_filter_annotations(user)
  sign_in user
  exercise1 = create(:exercise)
  exercise2 = create(:exercise)
  annotation1 = create(:saved_annotation, user: user, course: @course, exercise: exercise1)
  annotation2 = create(:saved_annotation, user: user, course: @course, exercise: exercise2)

  get new_saved_annotation_path
  assert_response :success
  assert_select 'your-selector-for-annotation', 2

  get new_saved_annotation_path, params: { exercise_id: exercise1.id }
  assert_response :success
  assert_select 'your-selector-for-annotation', 1
  assert_select 'your-selector-for-annotation-title', annotation1.title
end

test 'staff should be able to filter existing annotations on new page' do
  assert_can_filter_annotations(@user)
end

test 'zeus should be able to filter existing annotations on new page' do
  assert_can_filter_annotations(users(:zeus))
end

This refactoring reduces duplication and ensures consistent testing across user types. Adjust the selectors and assertions as needed to match your actual view structure.

end