Add arguments to documentable concern to make it configurable for any recipient model.

This commit is contained in:
Senén Rodero Rodríguez
2017-07-21 19:48:57 +02:00
parent 62372aaee3
commit 38d4d59241
11 changed files with 78 additions and 22 deletions

View File

@@ -1,6 +1,8 @@
shared_examples "document validations" do |documentable_factory|
let(:document) { build(:document, documentable_factory.to_sym) }
let(:document) { build(:document, documentable_factory.to_sym) }
let(:max_file_size) { document.documentable.class.max_file_size }
let(:accepted_content_types) { document.documentable.class.accepted_content_types }
it "should be valid" do
expect(document).to be_valid
@@ -18,17 +20,20 @@ shared_examples "document validations" do |documentable_factory|
expect(document).to_not be_valid
end
it "should not be valid for attachment images" do
document.attachment = File.new("spec/fixtures/files/logo_header.png")
it "should be valid for all accepted content types" do
accepted_content_types.each do |content_type|
extension = content_type.split("/").last
document.attachment = File.new("spec/fixtures/files/empty.#{extension}")
expect(document).to_not be_valid
expect(document).to be_valid
end
end
it "should not be valid for attachment 3MB" do
document.stub(:attachment_file_size).and_return(3.1.megabytes)
it "should not be valid for attachments larger than documentable max_file_size definition" do
document.stub(:attachment_file_size).and_return(max_file_size.bytes + 1.byte)
document.should_not be_valid
expect(document.errors[:attachment]).to include "must be in between 0 Bytes and 3 MB"
expect(document).to_not be_valid
expect(document.errors[:attachment]).to include "must be in between 0 Bytes and #{bytesToMeg(max_file_size)} MB"
end
it "should not be valid without a user_id" do
@@ -50,3 +55,7 @@ shared_examples "document validations" do |documentable_factory|
end
end
def bytesToMeg(bytes)
bytes / (1024.0 * 1024.0)
end