summaryrefslogtreecommitdiff
path: root/test/models/node_attach_asset_test.rb
blob: cb5b60f5ec8902001bd6f8ca6cb837d73ce3b7aa (plain)
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
require "test_helper"

class NodeAttachAssetTest < ActiveSupport::TestCase

  def setup
    @user  = users(:quentin)
    @other = users(:aaron)
    @node  = Node.root.children.create!(:slug => "attach_asset_test")
    @image = create_image_asset
  end

  test "attaches to a draft-only node" do
    result = @node.attach_asset!(@image, :user => @user)
    assert_equal 1, result[:attached]
    assert_includes @node.draft.assets, @image
  end

  test "attaches to head when no draft is pending" do
    @node.publish_draft!(@user)
    result = @node.attach_asset!(@image, :user => @user)
    assert_equal 1, result[:attached]
    assert_includes @node.head.assets, @image
  end

  test "attaches to head and pending draft alike" do
    @node.publish_draft!(@user)
    @node.lock_for_editing!(@user)
    @node.create_new_draft(@user)
    result = @node.attach_asset!(@image, :user => @user)
    assert_equal 2, result[:attached]
    assert_includes @node.head.assets, @image
    assert_includes @node.draft.assets, @image
  end

  test "attaches to all three lifecycle rows" do
    @node.publish_draft!(@user)
    @node.lock_for_editing!(@user)
    @node.create_new_draft(@user)
    @node.autosave!({ :title => "wip" }, @user)
    result = @node.attach_asset!(@image, :user => @user)
    assert_equal 3, result[:attached]
    [@node.head, @node.draft, @node.autosave].each do |row|
      assert_includes row.assets, @image
    end
  end

  test "skips rows that already carry the asset" do
    @node.draft.related_assets.create!(:asset => @image)
    @node.publish_draft!(@user)
    @node.lock_for_editing!(@user)
    @node.create_new_draft(@user)
    result = @node.attach_asset!(@image, :user => @user)
    assert_equal 0, result[:attached]
    assert_equal 2, result[:already]
    assert_equal 1, @node.draft.related_assets.where(:asset_id => @image.id).count
  end

  test "refuses when another user holds the lock and writes nothing" do
    @node.lock_for_editing!(@other)
    assert_raises(LockedByAnotherUser) { @node.attach_asset!(@image, :user => @user) }
    assert_empty @node.draft.assets.reload
  end

  test "proceeds when the attaching user holds the lock" do
    @node.lock_for_editing!(@user)
    result = @node.attach_asset!(@image, :user => @user)
    assert_equal 1, result[:attached]
  end

  test "sets the headline when none exists" do
    result = @node.attach_asset!(@image, :user => @user, :headline => true)
    assert_equal :set, result[:headline]
    assert_equal @image, @node.draft.headline_asset
  end

  test "keeps an existing headline and reports it" do
    incumbent = create_image_asset
    @node.draft.related_assets.create!(:asset => incumbent, :headline => true)
    result = @node.attach_asset!(@image, :user => @user, :headline => true)
    assert_equal :kept_existing, result[:headline]
    assert_equal incumbent, @node.draft.reload.headline_asset
    assert_includes @node.draft.assets, @image
  end

  test "declines the headline flag for ineligible asset types" do
    plain = create_plain_asset
    result = @node.attach_asset!(plain, :user => @user, :headline => true)
    assert_equal :not_eligible, result[:headline]
    assert_includes @node.draft.assets.reload, plain
    assert_nil @node.draft.headline_asset
  end

  test "refuses nodes in the Trash" do
    @node.trash!(@user)
    assert_raises(ActiveRecord::RecordInvalid) { @node.attach_asset!(@image, :user => @user) }
  end

  private

    def create_image_asset
      Asset.create!(:name => "attach test image", :upload_content_type => "image/png")
    end

    def create_plain_asset
      Asset.create!(:name => "attach test note", :upload_content_type => "text/plain")
    end
end