cirandas.net

ref: master

test/unit/image_test.rb


  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
require_relative "../test_helper"

class ImageTest < ActiveSupport::TestCase
  fixtures :images

  def setup
    @profile = create_user('testinguser').person
  end
  attr_reader :profile

  should 'have thumbnails options' do
    [:big, :thumb, :portrait, :minor, :icon].each do |option|
      assert Image.attachment_options[:thumbnails].include?(option), "should have #{option}"
    end
  end

  should 'match max_size in validates message of size field' do
    image = build(Image, :filename => 'fake_filename.png')
    image.valid?

    assert_match /#{Image.max_size.to_humanreadable}/, image.errors[:size].first
  end

  should 'create thumbnails after processing jobs' do
    file = create(Image, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'))
    profile.update_attribute(:image_id, file.id)

    process_delayed_job_queue
    Image.attachment_options[:thumbnails].each do |suffix, size|
      assert File.exists?(Image.find(file.id).public_filename(suffix))
    end
    file.destroy
  end

  should 'set thumbnails_processed to true after creating thumbnails' do
    file = create(Image, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'))
    profile.update_attribute(:image_id, file.id)

    process_delayed_job_queue

    assert Image.find(file.id).thumbnails_processed
    file.destroy
  end

  should 'have thumbnails_processed attribute' do
    assert Image.new.respond_to?(:thumbnails_processed)
  end

  should 'return false by default in thumbnails_processed' do
    refute Image.new.thumbnails_processed
  end

  should 'set thumbnails_processed to true' do
    file = Image.new
    file.thumbnails_processed = true

    assert file.thumbnails_processed
  end

  should 'have a default image if thumbnails were not processed' do
    file = Image.new
    file.expects(:thumbnailable?).returns(true)
    assert_equal '/images/icons-app/image-loading-thumb.png', file.public_filename(:thumb)
  end

  should 'use origin image if thumbnails were not processed and fallback is enabled' do
    NOOSFERO_CONF.expects(:[]).with('delayed_attachment_fallback_original_image').returns(true).at_least_once
    file = create(Image, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'))
    profile.update_attribute(:image_id, file.id)

    assert_match(/rails.png/, Image.find(file.id).public_filename(:thumb))

    file.destroy
  end

  should 'return image thumbnail if thumbnails were processed' do
    file = create(Image, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'))
    profile.update_attribute(:image_id, file.id)
    process_delayed_job_queue

    assert_match(/rails_thumb.png/, Image.find(file.id).public_filename(:thumb))

    file.destroy
  end

  should 'store width and height after processing' do
    file = create(Image, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'))
    profile.update_attribute(:image_id, file.id)
    file.create_thumbnails

    file = Image.find(file.id)
    assert_equal [50, 64], [file.width, file.height]
  end

  should 'have a loading image to each size of thumbnails' do
    Image.attachment_options[:thumbnails].each do |suffix, size|
      image = Rails.root.join("public/images/icons-app/image-loading-#{suffix}.png")
      assert File.exists?(image), "#{image} should exist."
    end
  end

  should 'not create a background job for an image that is not thumbnailable' do
    # this test verifies whether it created background jobs also for the
    # thumbnails!
    assert_no_difference 'Delayed::Job.count' do
      image = build(Image, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'))
      image.stubs(:thumbnailable?).returns(false)
      image.save!
    end
  end

  should 'upload to a folder with same name as the schema if database is postgresql' do
    uses_postgresql
    file = create(Image, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'))
    profile.update_attribute(:image_id, file.id)
    process_delayed_job_queue
    assert_match(/images\/test_schema\/\d{4}\/\d{4}\/rails.png/, Image.find(file.id).public_filename)
    file.destroy
    uses_sqlite
  end

  should 'upload to path prefix folder if database is not postgresql' do
    uses_sqlite
    file = create(Image, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'))
    profile.update_attribute(:image_id, file.id)
    process_delayed_job_queue
    assert_match(/images\/\d{4}\/\d{4}\/rails.png/, Image.find(file.id).public_filename)
    file.destroy
  end

  should 'not allow script files to be uploaded without append .txt in the end' do
    file = create(Image, :uploaded_data => fixture_file_upload('files/hello_world.php', 'image/png'))
    assert_equal 'hello_world.php.txt', file.filename
  end

  should 'have an owner' do
    owner = fast_create(Block)
    file = create(Image, uploaded_data: fixture_file_upload('/files/rails.png', 'image/png'), owner: owner)
    assert_equal owner, file.owner
  end

end