# 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