summaryrefslogtreecommitdiff
path: root/test/controllers/assets_controller_test.rb
blob: 67f629d902d766eca1c7937f2c92c0ae5ce62f6b (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
require 'test_helper'

class AssetsControllerTest < ActionController::TestCase

  def setup
    login_as :quentin
    @existing_asset_ids = Asset.pluck(:id)
  end

  def teardown
    (Asset.pluck(:id) - @existing_asset_ids).each do |id|
      dir = Asset.upload_root.join(id.to_s)
      raise "Refusing to delete #{dir} -- outside tmp/, Rails.env.test? may be false" unless
        dir.to_s.start_with?(Rails.root.join("tmp").to_s)
      FileUtils.rm_rf(dir)
    end
  end

  # --- index ---

  test "get index" do
    get :index
    assert_response :success
  end

  # --- show ---

  test "show existing asset" do
    asset = Asset.create!(
      name: 'Test asset',
      upload_file_name: 'test_image.png',
      upload_content_type: 'image/png',
      upload_file_size: 49854,
      upload_updated_at: Time.current
    )
    get :show, params: { id: asset.id }
    assert_response :success
  end

  # --- new ---

  test "get new" do
    get :new
    assert_response :success
  end

  # --- create with image ---

  test "create asset with image upload generates variants" do
    uploaded = Rack::Test::UploadedFile.new(
      Rails.root.join('test', 'fixtures', 'files', 'test_image.png'),
      'image/png'
    )
    assert_difference 'Asset.count', 1 do
      post :create, params: { asset: { name: 'Logo', upload: uploaded } }
    end
    assert_response :redirect

    asset = Asset.last
    assert_equal 'test_image.png', asset.upload_file_name
    assert_equal 'image/png',      asset.upload_content_type
    assert asset.upload_file_size > 0

    # original and all four variants should exist on disk
    %w[original medium thumb headline large].each do |style|
      path = asset.send(:file_path, style)
      assert File.exist?(path), "Expected #{style} variant at #{path}"
    end
  end

  # --- create with PDF ---

  test "create asset with PDF upload generates rasterized variants" do
    uploaded = Rack::Test::UploadedFile.new(
      Rails.root.join('test', 'fixtures', 'files', 'test_document.pdf'),
      'application/pdf'
    )
    assert_difference 'Asset.count', 1 do
      post :create, params: { asset: { name: 'Document', upload: uploaded } }
    end
    assert_response :redirect

    asset = Asset.last
    original_path = asset.send(:file_path, :original)
    assert File.exist?(original_path), "Expected original at #{original_path}"
    assert_equal 'test_document.pdf', File.basename(original_path)

    %w[medium thumb headline large].each do |style|
      path = asset.send(:file_path, style)
      assert File.exist?(path), "Expected a #{style} variant at #{path}"
      assert_equal '.png', File.extname(path), "Expected #{style} variant to be a PNG, not a PDF"
    end
  end

  # --- create with attach ---

  test "create with node_id attaches the asset to an existing draft" do
    node = Node.root.children.create!(:slug => "asset_attach_target")

    post :create, params: { asset: { name: 'Attach me' }, node_id: node.id }

    assert_response :redirect
    asset = Asset.last
    assert_includes node.draft.assets.reload, asset
    assert_equal [I18n.t("flash.assets.created"),
              I18n.t("flash.assets.attached_to_draft", :title => node.title)].join(" "),
             flash[:notice]
  end

  test "create with node_id creates a draft when none is pending" do
    node = Node.root.children.create!(:slug => "asset_attach_no_draft")
    node.publish_draft!(users(:quentin))
    assert_nil node.reload.draft

    post :create, params: { asset: { name: 'Attach me too' }, node_id: node.id }

    node.reload
    assert_includes node.draft.assets.reload, Asset.last
    assert_empty node.head.assets.reload
    assert_equal users(:quentin), node.draft.editor
    assert_equal [I18n.t("flash.assets.created"),
              I18n.t("flash.assets.attached_new_draft", :title => node.title)].join(" "),
             flash[:notice]
  end

  test "create against a foreign-locked node keeps the asset but refuses the attach" do
    node = Node.root.children.create!(:slug => "asset_attach_locked")
    node.lock_for_editing!(users(:aaron))

    assert_difference 'Asset.count', 1 do
      post :create, params: { asset: { name: 'Orphaned for now' }, node_id: node.id }
    end

    assert_empty node.draft.assets.reload
    assert_equal node_path(node), flash[:locked_node_path]
    assert_equal "aaron", flash[:locked_by]
  end

  test "create with headline against a page that has one keeps the incumbent and warns" do
    node = Node.root.children.create!(:slug => "asset_attach_headline")
    incumbent = Asset.create!(:name => 'Incumbent', :upload_content_type => 'image/png')
    node.draft.related_assets.create!(:asset => incumbent, :headline => true)

    uploaded = Rack::Test::UploadedFile.new(
      Rails.root.join('test', 'fixtures', 'files', 'test_image.png'), 'image/png')
    post :create, params: { asset: { name: 'Challenger', upload: uploaded },
                            node_id: node.id, headline: "1" }

    assert_includes node.draft.assets.reload, Asset.last
    assert_equal incumbent, node.draft.reload.headline_asset
    assert_equal node_path(node), flash[:headline_kept_path]
  end

  test "create with node_id writes only an asset_create entry" do
    node = Node.root.children.create!(:slug => "asset_log_single")

    assert_difference 'NodeAction.count', 1 do
      post :create, params: { asset: { name: 'Logged once' }, node_id: node.id }
    end

    action = NodeAction.last
    assert_equal "asset_create", action.action
    assert_equal users(:quentin), action.user
  end

  # --- edit ---

  test "get edit" do
    asset = Asset.create!(
      name: 'Edit me',
      upload_file_name: 'test_image.png',
      upload_content_type: 'image/png',
      upload_file_size: 49854,
      upload_updated_at: Time.current
    )
    get :edit, params: { id: asset.id }
    assert_response :success
  end

  # --- update ---

  test "update asset name" do
    asset = Asset.create!(
      name: 'Old name',
      upload_file_name: 'test_image.png',
      upload_content_type: 'image/png',
      upload_file_size: 49854,
      upload_updated_at: Time.current
    )
    put :update, params: { id: asset.id, asset: { name: 'New name' } }
    assert_response :redirect
    assert_equal 'New name', asset.reload.name
  end

  # --- destroy ---

  test "destroy asset removes record and files" do
    # Create a real upload so there are files to delete
    uploaded = Rack::Test::UploadedFile.new(
      Rails.root.join('test', 'fixtures', 'files', 'test_image.png'),
      'image/png'
    )
    post :create, params: { asset: { name: 'To be deleted', upload: uploaded } }
    asset = Asset.last
    upload_dir = asset.send(:upload_root).join(asset.id.to_s)
    assert Dir.exist?(upload_dir), "Upload directory should exist before destroy"

    assert_difference 'Asset.count', -1 do
      delete :destroy, params: { id: asset.id }
    end
    assert_response :redirect
    assert !Dir.exist?(upload_dir), "Upload directory should be removed after destroy"
  end

  test "destroy is witnessed in the action log with the current user" do
    asset = Asset.create!(:name => 'Witness me',
                           :upload_file_name => 'w.png',
                           :upload_content_type => 'image/png')
    assert_difference 'NodeAction.where(:action => "asset_destroy").count' do
      delete :destroy, params: { id: asset.id }
    end
    assert_equal users(:quentin), NodeAction.last.user
  end

  test "attach_to_node attaches the asset to the node's draft" do
    node  = Node.root.children.create!(:slug => "asset_show_attach")
    asset = Asset.create!(:name => "From the asset page", :upload_content_type => "image/png")

    post :attach_to_node, params: { id: asset.id, node_id: node.id }

    assert_redirected_to asset_path(asset)
    assert_includes node.draft.assets.reload, asset
    assert_equal I18n.t("flash.assets.attached_to_draft", :title => node.title), flash[:notice]
  end

  test "attach_to_node without a node says so and attaches nothing" do
    asset = Asset.create!(:name => "Unpicked", :upload_content_type => "image/png")

    post :attach_to_node, params: { id: asset.id }

    assert_redirected_to asset_path(asset)
    assert_equal I18n.t("flash.assets.attach_no_node"), flash[:error]
    assert_empty asset.related_assets.reload
  end

  # --- URL helpers ---

  test "upload url returns correct path for original" do
    asset = Asset.create!(
      name: 'URL test',
      upload_file_name: 'logo.png',
      upload_content_type: 'image/png',
      upload_file_size: 1000,
      upload_updated_at: Time.current
    )
    assert_equal "/system/uploads/#{asset.id}/original/logo.png", asset.upload.url
    assert_equal "/system/uploads/#{asset.id}/thumb/logo.png",    asset.upload.url(:thumb)
    assert_equal "/system/uploads/#{asset.id}/medium/logo.png",   asset.upload.url(:medium)
    assert_equal "/system/uploads/#{asset.id}/headline/logo.png", asset.upload.url(:headline)
    assert_equal "/system/uploads/#{asset.id}/large/logo.png",    asset.upload.url(:large)
  end

  # --- login required ---

  test "index requires login" do
    session[:user_id] = nil
    @controller.instance_variable_set(:@current_user, nil)
    get :index
    assert_response :redirect
  end
end