|
| 1 | +require "test_helper" |
| 2 | + |
| 3 | +class Card::StorageTrackingTest < ActiveSupport::TestCase |
| 4 | + setup do |
| 5 | + Current.session = sessions(:david) |
| 6 | + @account = Current.account |
| 7 | + @board = boards(:writebook) |
| 8 | + end |
| 9 | + |
| 10 | + test "tracks storage when creating card with rich text attachment" do |
| 11 | + expected_bytes = active_storage_blobs(:hello_txt).byte_size |
| 12 | + |
| 13 | + assert_difference -> { @board.reload.bytes_used }, expected_bytes do |
| 14 | + assert_difference -> { @account.reload.bytes_used }, expected_bytes do |
| 15 | + perform_enqueued_jobs only: Account::AdjustStorageJob do |
| 16 | + @board.cards.create!(title: "Test", description: attachment_html(active_storage_blobs(:hello_txt)), status: :published) |
| 17 | + end |
| 18 | + end |
| 19 | + end |
| 20 | + end |
| 21 | + |
| 22 | + test "tracks storage delta when updating card with different attachment" do |
| 23 | + perform_enqueued_jobs only: Account::AdjustStorageJob do |
| 24 | + card = @board.cards.create!(title: "Test", description: attachment_html(active_storage_blobs(:hello_txt)), status: :published) |
| 25 | + |
| 26 | + expected_delta = active_storage_blobs(:list_pdf).byte_size - active_storage_blobs(:hello_txt).byte_size |
| 27 | + assert_difference -> { @board.reload.bytes_used }, expected_delta do |
| 28 | + assert_difference -> { @account.reload.bytes_used }, expected_delta do |
| 29 | + card.update!(description: attachment_html(active_storage_blobs(:list_pdf))) |
| 30 | + end |
| 31 | + end |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + test "tracks negative delta when removing attachment from card" do |
| 36 | + perform_enqueued_jobs only: Account::AdjustStorageJob do |
| 37 | + card = @board.cards.create!(title: "Test", description: attachment_html(active_storage_blobs(:hello_txt)), status: :published) |
| 38 | + |
| 39 | + expected_delta = -active_storage_blobs(:hello_txt).byte_size |
| 40 | + assert_difference -> { @board.reload.bytes_used }, expected_delta do |
| 41 | + assert_difference -> { @account.reload.bytes_used }, expected_delta do |
| 42 | + card.update!(description: "No attachments") |
| 43 | + end |
| 44 | + end |
| 45 | + end |
| 46 | + end |
| 47 | + |
| 48 | + test "tracks negative storage when destroying card with attachment" do |
| 49 | + perform_enqueued_jobs only: Account::AdjustStorageJob do |
| 50 | + card = @board.cards.create!(title: "Test", description: attachment_html(active_storage_blobs(:hello_txt)), status: :published) |
| 51 | + |
| 52 | + expected_delta = -active_storage_blobs(:hello_txt).byte_size |
| 53 | + assert_difference -> { @board.reload.bytes_used }, expected_delta do |
| 54 | + assert_difference -> { @account.reload.bytes_used }, expected_delta do |
| 55 | + card.destroy! |
| 56 | + end |
| 57 | + end |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + test "does not change storage when no attachments change" do |
| 62 | + assert_no_difference -> { @board.reload.bytes_used } do |
| 63 | + assert_no_difference -> { @account.reload.bytes_used } do |
| 64 | + perform_enqueued_jobs only: Account::AdjustStorageJob do |
| 65 | + @board.cards.create!(title: "Test", description: "Plain text", status: :published) |
| 66 | + end |
| 67 | + end |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + test "does not change storage when updating title on card with attachment" do |
| 72 | + perform_enqueued_jobs only: Account::AdjustStorageJob do |
| 73 | + card = @board.cards.create!(title: "Test", description: attachment_html(active_storage_blobs(:hello_txt)), status: :published) |
| 74 | + |
| 75 | + assert_no_difference -> { @board.reload.bytes_used } do |
| 76 | + assert_no_difference -> { @account.reload.bytes_used } do |
| 77 | + card.update!(title: "New title") |
| 78 | + end |
| 79 | + end |
| 80 | + end |
| 81 | + end |
| 82 | + |
| 83 | + test "does not change storage when updating description text but keeping same attachment" do |
| 84 | + perform_enqueued_jobs only: Account::AdjustStorageJob do |
| 85 | + card = @board.cards.create!(title: "Test", description: "Some text #{attachment_html(active_storage_blobs(:hello_txt))}", status: :published) |
| 86 | + |
| 87 | + assert_no_difference -> { @board.reload.bytes_used } do |
| 88 | + assert_no_difference -> { @account.reload.bytes_used } do |
| 89 | + card.update!(description: "Different text #{attachment_html(active_storage_blobs(:hello_txt))}") |
| 90 | + end |
| 91 | + end |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + test "tracks storage separately for each board" do |
| 96 | + other_board = boards(:private) |
| 97 | + |
| 98 | + perform_enqueued_jobs only: Account::AdjustStorageJob do |
| 99 | + @board.cards.create!(title: "Card 1", description: attachment_html(active_storage_blobs(:hello_txt)), status: :published) |
| 100 | + other_board.cards.create!(title: "Card 2", description: attachment_html(active_storage_blobs(:list_pdf)), status: :published) |
| 101 | + |
| 102 | + assert_equal active_storage_blobs(:hello_txt).byte_size, @board.reload.bytes_used |
| 103 | + assert_equal active_storage_blobs(:list_pdf).byte_size, other_board.reload.bytes_used |
| 104 | + assert_equal active_storage_blobs(:hello_txt).byte_size + active_storage_blobs(:list_pdf).byte_size, @account.reload.bytes_used |
| 105 | + end |
| 106 | + end |
| 107 | +end |
0 commit comments