summaryrefslogtreecommitdiff
path: root/test/controllers/posts_controller_test.rb
blob: ec8985c75a02e89b28e52cc2d8a4e32d3bcd8231 (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
# frozen_string_literal: true

require 'test_helper'
require 'rack/test'

class PostsControllerTest < ActionController::TestCase
  setup do
    @post = create(:post)
    @request.cookies['_ses'] = 'c1836832948'
  end

  test 'should get index' do
    get :index
    assert_response :success
    assert assigns(:posts)
  end

  test 'should get show' do
    get :show, params: { id: @post.id }
    assert_response :success
  end

  test 'should get new' do
    get :new
    assert_response :success
  end

  test 'should create a new image' do
    image_file = Rack::Test::UploadedFile.new Rails.root.join('test/fixtures/image_files/dedupe_evade_1.jpeg'), 'image/jpeg'
    assert_difference('Image.count') do
      post :create, params: { image: {
        description: '',
        source_url:  'h',
        tag_input:   'safe, nightmare moon, nightmare dupe',
        image:       image_file
      } }
    end
    assert_redirected_to short_post_path(assigns(:post))
  end

  test 'duplicates should redirect to the target image' do
    @dedupe_1 = create(:dedupe_image_2)
    ImageDuplicateMergeJob.perform_now @post.id, @dedupe_1.id, nil

    get :show, params: { id: @post.id }
    assert_response :redirect
    assert_redirected_to short_post_path(@dedupe_1)
  end

  test 'content destruction of a post should do nothing given a non-hidden post' do
    sign_in create(:moderator)

    delete :destroy, params: { id: @post.id }

    @post.reload
    assert_response :redirect
    assert_redirected_to root_path
    assert_not @post.destroyed_content
  end

  test 'content destruction of a post should remove the file contents' do
    sign_in create(:moderator)
    PostHider.new(@post, reason: 'The era of deletion is upon us.').save

    delete :destroy, params: { id: @post.id }

    @post.reload
    assert @post.destroyed_content
  end
end