Files
nairobi/spec/shared/models/image_validations.rb
Javi Martín 4f232c3a25 Use the file_fixture helper in tests
This way we don't have to write `"spec/fixtures/files"` every time.

Note this method isn't included in factories. We could include it like
so:

```
FactoryBot::SyntaxRunner.class_eval do
  include ActiveSupport::Testing::FileFixtures
  self.file_fixture_path = RSpec.configuration.file_fixture_path
end
```

However, I'm not sure about the possible side effects, and since we only
use attachments in a few factories, there isn't much gain in applying
the monkey-patch.
2022-02-23 18:43:48 +01:00

67 lines
1.7 KiB
Ruby

shared_examples "image validations" do |imageable_factory|
let!(:image) { build(:image, imageable_factory.to_sym) }
let!(:acceptedcontenttypes) { Image.accepted_content_types }
it "is valid" do
expect(image).to be_valid
end
it "is not valid without a title" do
image.title = nil
expect(image).not_to be_valid
end
it "is not valid without an attachment" do
image.attachment = nil
expect(image).not_to be_valid
end
it "is valid for all accepted content types" do
acceptedcontenttypes.each do |content_type|
extension = content_type.split("/").last
image.attachment = File.new(file_fixture("clippy.#{extension}"))
expect(image).to be_valid
end
end
it "is not valid for png and gif image content types" do
["gif", "png"].each do |content_type|
extension = content_type.split("/").last
image.attachment = File.new(file_fixture("clippy.#{extension}"))
expect(image).not_to be_valid
end
end
it "is not valid for attachments larger than imageable max_file_size definition" do
larger_size = Setting["uploads.images.max_size"].to_i.megabytes + 1.byte
allow(image.attachment).to receive(:byte_size).and_return(larger_size)
expect(image).not_to be_valid
expect(image.errors[:attachment]).to include "must be in between 0 Bytes and 1 MB"
end
it "is not valid without a user_id" do
image.user_id = nil
expect(image).not_to be_valid
end
it "is not valid without a imageable_id" do
image.save!
image.imageable_id = nil
expect(image).not_to be_valid
end
it "is not valid without a imageable_type" do
image.save!
image.imageable_type = nil
expect(image).not_to be_valid
end
end