summaryrefslogtreecommitdiff
path: root/test/models/image_test.rb
blob: 6ea68bbc886c51fac064c05725053e38ab677e5c (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# frozen_string_literal: true

# == Schema Information
#
# Table name: images
#
#  id                          :integer          not null, primary key
#  anonymous                   :boolean          default(FALSE)
#  average_intensity           :float
#  commenting_allowed          :boolean          default(TRUE), not null
#  comments_count              :integer          default(0), not null
#  deletion_reason             :string
#  description                 :string           default(""), not null
#  description_editing_allowed :boolean          default(TRUE), not null
#  destroyed_content           :boolean          default(FALSE), not null
#  downvotes_count             :integer          default(0), not null
#  duplication_checked         :boolean          default(FALSE), not null
#  duration                    :float            default(0.0)
#  faves_count                 :integer          default(0), not null
#  file_name_cache             :string
#  first_seen_at               :datetime         not null
#  hidden_from_users           :boolean          default(FALSE), not null
#  hidden_image_key            :string
#  hides_count                 :integer          default(0), not null
#  image                       :string
#  image_aspect_ratio          :float
#  image_format                :string
#  image_height                :integer
#  image_mime_type             :string
#  image_name                  :string
#  image_orig_sha512_hash      :string
#  image_sha512_hash           :string
#  image_size                  :integer
#  image_width                 :integer
#  ip                          :inet
#  is_animated                 :boolean          not null
#  ne_intensity                :float
#  nw_intensity                :float
#  processed                   :boolean          default(FALSE), not null
#  referrer                    :string           default("")
#  score                       :integer          default(0), not null
#  scratchpad                  :string
#  se_intensity                :float
#  source_url                  :string
#  sw_intensity                :float
#  tag_editing_allowed         :boolean          default(TRUE), not null
#  tag_ids                     :integer          default([]), not null, is an Array
#  tag_list_cache              :string
#  tag_list_plus_alias_cache   :string
#  thumbnails_generated        :boolean          default(FALSE), not null
#  tripcode                    :text
#  upvotes_count               :integer          default(0), not null
#  user_agent                  :string           default("")
#  watcher_count               :integer          default(0), not null
#  watcher_ids                 :integer          default([]), not null, is an Array
#  created_at                  :datetime         not null
#  updated_at                  :datetime         not null
#  deleted_by_id               :integer
#  duplicate_id                :integer
#  user_id                     :integer
#
# Indexes
#
#  index_images_on_created_at              (created_at)
#  index_images_on_deleted_by_id           (deleted_by_id) WHERE (deleted_by_id IS NOT NULL)
#  index_images_on_duplicate_id            (duplicate_id) WHERE (duplicate_id IS NOT NULL)
#  index_images_on_first_seen_at           (first_seen_at)
#  index_images_on_image_orig_sha512_hash  (image_orig_sha512_hash)
#  index_images_on_tag_ids                 (tag_ids) USING gin
#  index_images_on_updated_at              (updated_at)
#  index_images_on_user_id                 (user_id)
#  intensities_index                       (se_intensity,sw_intensity,ne_intensity,nw_intensity,average_intensity)
#
# Foreign Keys
#
#  fk_rails_...  (deleted_by_id => users.id) ON DELETE => nullify ON UPDATE => cascade
#  fk_rails_...  (duplicate_id => images.id) ON DELETE => nullify ON UPDATE => cascade
#  fk_rails_...  (user_id => users.id) ON DELETE => nullify ON UPDATE => cascade
#
require 'test_helper'

class ImageTest < ActiveSupport::TestCase
  setup do
    @image = create(:post)
  end

  test 'should be valid' do
    assert @image.valid?
  end

  test 'should raise a validation error on exact dupes' do
    @image.save!
    assert @image.valid?

    @image2 = build(:post)
    assert_not @image2.valid?
  end

  test 'should set first_seen_at to created_at' do
    @image.save!
    assert_equal @image.created_at, @image.first_seen_at
  end

  def setup_tags
    @safe = Tag.find_tag_by_name('safe')
    @tags = create_list :tag, Booru::CONFIG.settings[:tags][:min_count]
  end

  test 'tag addition should be validated' do
    setup_tags

    @image.add_tags @tags + [@safe]
    assert @image.save

    assert_includes @image.tag_list, @tags.first.name
    assert_includes @image.tags.to_a, @safe

    @image.remove_tags [@safe]
    assert_not @image.save
    @image.remove_tags [@tags.first]
    assert @image.save

    assert_not_includes @image.tag_list, @tags.first.name
    assert_not_includes @image.tags.to_a, @tags.first
  end

  test '#hide! should prevent users from seeing things' do
    setup_test_users

    @image.save!
    assert_not @image.hidden_from_users
    assert @image.visible_to?(@user)
    assert @image.visible_to?(@dupe_assistant)
    assert @image.visible_to?(@image_assistant)
    assert @image.visible_to?(@comment_assistant)
    assert @image.visible_to?(@tag_assistant)
    assert @image.visible_to?(@admin)

    PostHider.new(@image, reason: 'The age of deletion is upon us.').save
    assert @image.hidden_from_users
    assert_not @image.visible_to?(@user)
    assert_not @image.visible_to?(@dupe_assistant)
    assert @image.visible_to?(@image_assistant)
    assert_not @image.visible_to?(@comment_assistant)
    assert_not @image.visible_to?(@tag_assistant)
    assert @image.visible_to?(@admin)

    PostUnhider.new(@image).save
    assert_not @image.hidden_from_users
    assert @image.visible_to?(@user)
    assert @image.visible_to?(@dupe_assistant)
    assert @image.visible_to?(@image_assistant)
    assert @image.visible_to?(@comment_assistant)
    assert @image.visible_to?(@tag_assistant)
    assert @image.visible_to?(@admin)
  end

  def hide_and_destroy_image_content
    @image.save!
    PostHider.new(@image, reason: 'Because they felt like it.').save
    ImageDestroyer.new(@image).save
  end

  test '#destroy_content! should set maintain model validity.' do
    hide_and_destroy_image_content

    assert @image.valid?
  end

  test '#destroy_content! should update attributes appropriately.' do
    hide_and_destroy_image_content

    @image.reload
    assert @image.read_attribute(:image).nil?
    assert @image.destroyed_content
  end

  test 'destroying a hidden record should not decrement tag counters' do
    @image.save!
    tag = @image.tags.first
    assert_difference(-> { tag.reload.images_count }, -1) { PostHider.new(@image, reason: 'Just because.').save }
    assert_difference(-> { tag.reload.images_count }, 0) { ImageDestroyer.new(@image).save }
  end
end