summaryrefslogtreecommitdiff
path: root/test/models/duplicate_report_test.rb
blob: 3b104b49799e5f3baffade15344b8734cc92081c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# frozen_string_literal: true

# == Schema Information
#
# Table name: duplicate_reports
#
#  id                   :integer          not null, primary key
#  reason               :string
#  state                :string           default("open"), not null
#  created_at           :datetime         not null
#  updated_at           :datetime         not null
#  duplicate_of_post_id :integer          not null
#  modifier_id          :integer
#  post_id              :integer          not null
#  user_id              :integer
#
# Indexes
#
#  index_duplicate_reports_on_created_at            (created_at)
#  index_duplicate_reports_on_duplicate_of_post_id  (duplicate_of_post_id)
#  index_duplicate_reports_on_modifier_id           (modifier_id)
#  index_duplicate_reports_on_post_id               (post_id)
#  index_duplicate_reports_on_state                 (state)
#  index_duplicate_reports_on_user_id               (user_id)
#
# Foreign Keys
#
#  fk_rails_...  (duplicate_of_post_id => posts.id)
#  fk_rails_...  (modifier_id => users.id) ON DELETE => nullify ON UPDATE => cascade
#  fk_rails_...  (post_id => posts.id)
#  fk_rails_...  (user_id => users.id) ON DELETE => nullify ON UPDATE => cascade
#
require 'test_helper'

class DuplicateReportTest < ActiveSupport::TestCase
  include ActiveJob::TestHelper

  setup do
    queue_adapter.perform_enqueued_jobs = true
    @image = create(:dedupe_image_1)
    @image2 = create(:dedupe_image_2)
    [@image, @image2].each(&:reload)
    @report = DuplicateReport.create image: @image, duplicate_of_image: @image2, reason: 'Test'
    @user = create(:user)
  end

  test 'should not submit a report for invalid arguments' do
    refute_validity = ->(attributes) { assert_not DuplicateReport.new(attributes).valid? }

    refute_validity.call(image: @image, duplicate_of_image: @image, reason: 'Test')
    refute_validity.call(image_id: 9_000, duplicate_of_image: @image, reason: 'Test')
    refute_validity.call(image: @image, duplicate_of_image_id: 9_000, reason: 'Test')
  end

  test 'can accept a report' do
    queue_adapter.perform_enqueued_jobs = false
    assert_enqueued_with(job: ImageDuplicateMergeJob, args: [@image.id, @image2.id, @user.id]) do
      assert @report.accept!(@user)
    end
    assert_equal 'accepted', @report.state
  end

  test 'can reject a report' do
    @report.reject!(@user)

    assert_nil @image.duplicate_id
    assert_not @image.hidden_from_users
    assert_equal @user, @report.modifier
    assert_equal 'rejected', @report.state
  end

  test 'rejecting a report should undo accepting it' do
    assert @report.accept!(@user)
    [@image, @image2].each(&:reload)
    assert @image.hidden_from_users
    assert_equal @image2.id, @image.duplicate_id

    assert @report.reject!(@user)
    assert_not @image.hidden_from_users
    assert_nil @image.duplicate_id
  end

  test 'should detect duplicates' do
    assert @image.detect_duplicates(0.25, 0.25).to_a.include? @image2

    @dupes = [create(:dupe_1), create(:dupe_2)]
    assert DuplicateReport.exists? image: @dupes
  end
end