Files
sure/test/models/transaction_attachment_validation_test.rb
Ellion Blessan 98ae6782dc feat(transaction): add support for file attachments using Active Storage (#713)
* feat(transaction): add support for file attachments using Active Storage

* feat(attachments): implement transaction attachments with upload, show, and delete functionality

* feat(attachments): enhance attachment upload functionality to support multiple files and improved error handling

* feat(attachments): add attachment upload form and display functionality in transaction views

* feat(attachments): implement attachment validation for count, size, and content type; enhance upload form with validation hints

* fix(attachments): use correct UI components

* feat(attachments): Implement Turbo Stream responses for creating and deleting transaction attachments.

* fix(attachments): include auth in activestorage controller

* test(attachments): add test coverage for turbostream and auth

* feat(attachments): extract strings to i18n

* fix(attachments): ensure only newly added attachments are purged when transaction validation fails.

* fix(attachments): validate attachment params

* refactor(attachments): use stimulus declarative actions

* fix(attachments): add auth for other representations

* refactor(attachments): use Browse component for attachment uploads

* fix(attachments): reject empty values on attachment upload

* fix(attachments): hide the upload form if reached max uploads

* fix(attachments): correctly purge only newly added attachments on upload failure

* fix(attachments): ensure attachment count limit is respected within a transaction lock

* fix(attachments): update attachment parameter handling to avoid `ParameterMissing` errors.

* fix(components): adjust icon_only logic for buttonish

---------

Signed-off-by: Juan José Mata <juanjo.mata@gmail.com>
Co-authored-by: Juan José Mata <juanjo.mata@gmail.com>
2026-03-14 23:56:27 +01:00

62 lines
1.8 KiB
Ruby

require "test_helper"
class TransactionAttachmentValidationTest < ActiveSupport::TestCase
setup do
@transaction = transactions(:one)
end
test "should validate attachment content types" do
# Valid content type should pass
@transaction.attachments.attach(
io: StringIO.new("valid content"),
filename: "test.pdf",
content_type: "application/pdf"
)
assert @transaction.valid?
# Invalid content type should fail
@transaction.attachments.attach(
io: StringIO.new("invalid content"),
filename: "test.txt",
content_type: "text/plain"
)
assert_not @transaction.valid?
assert_includes @transaction.errors.full_messages_for(:attachments).join, "unsupported format"
end
test "should validate attachment count limit" do
# Fill up to the limit
Transaction::MAX_ATTACHMENTS_PER_TRANSACTION.times do |i|
@transaction.attachments.attach(
io: StringIO.new("content #{i}"),
filename: "file#{i}.pdf",
content_type: "application/pdf"
)
end
assert @transaction.valid?
# Exceeding the limit should fail
@transaction.attachments.attach(
io: StringIO.new("extra content"),
filename: "extra.pdf",
content_type: "application/pdf"
)
assert_not @transaction.valid?
assert_includes @transaction.errors.full_messages_for(:attachments).join, "cannot exceed"
end
test "should validate attachment file size" do
# Create a mock large attachment
large_content = "x" * (Transaction::MAX_ATTACHMENT_SIZE + 1)
@transaction.attachments.attach(
io: StringIO.new(large_content),
filename: "large.pdf",
content_type: "application/pdf"
)
assert_not @transaction.valid?
assert_includes @transaction.errors.full_messages_for(:attachments).join, "too large"
end
end