cirandas.net

ref: master

test/unit/gallery_test.rb


  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
require_relative "../test_helper"

class GalleryTest < ActiveSupport::TestCase

  should 'be an article' do
    assert_kind_of Article, Gallery.new
  end

  should 'provide proper description' do
    assert_kind_of String, Gallery.description
  end

  should 'provide proper short description' do
    assert_kind_of String, Gallery.short_description
  end

  should 'provide own icon name' do
    assert_not_equal Article.icon_name, Gallery.icon_name
  end

  should 'provide gallery as icon name' do
    assert_not_equal Article.icon_name, Gallery.icon_name
  end

  should 'identify as folder' do
    assert Folder.new.folder?, 'gallery must identity itself as folder'
  end

  should 'identify as gallery' do
    assert Gallery.new.gallery?, 'gallery must identity itself as gallery'
  end

  should 'can display hits' do
    profile = create_user('testuser').person
    a = fast_create(Gallery, :profile_id => profile.id)
    assert_equal false, a.can_display_hits?
  end

  should 'have images that are only images or other galleries' do
    p = create_user('test_user').person
    f = fast_create(Gallery, :profile_id => p.id)
    file = create(UploadedFile, :uploaded_data => fixture_file_upload('/files/test.txt', 'text/plain'), :parent => f, :profile => p)
    image = create(UploadedFile, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :parent => f, :profile => p)
    gallery = fast_create(Gallery, :profile_id => p.id, :parent_id => f.id)

    assert_equivalent [gallery, image], f.images
  end

  should 'bring galleries first in alpha order in images listing' do
    p = create_user('test_user').person
    f = fast_create(Gallery, :profile_id => p.id)
    gallery1 = fast_create(Gallery, :name => 'b', :profile_id => p.id, :parent_id => f.id)
    image = create(UploadedFile, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :parent => f, :profile => p)
    gallery2 = fast_create(Gallery, :name => 'c', :profile_id => p.id, :parent_id => f.id)
    gallery3 = fast_create(Gallery, :name => 'a', :profile_id => p.id, :parent_id => f.id)

    assert_equal [gallery3.id, gallery1.id, gallery2.id, image.id], f.images.map(&:id)
  end

  should 'images support pagination' do
    p = create_user('test_user').person
    f = fast_create(Gallery, :profile_id => p.id)
    gallery = fast_create(Gallery, :profile_id => p.id, :parent_id => f.id)
    image = create(UploadedFile, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :parent => f, :profile => p)

    assert_equal [image], f.images.paginate(:page => 2, :per_page => 1)
  end

  should 'return newest text articles as news' do
    c = fast_create(Community)
    gallery = fast_create(Gallery, :profile_id => c.id)
    f = fast_create(Gallery, :name => 'gallery', :profile_id => c.id, :parent_id => gallery.id)
    u = create(UploadedFile, :profile => c, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :parent => gallery)
    older_t = fast_create(TextArticle, :name => 'old news', :profile_id => c.id, :parent_id => gallery.id)
    t = fast_create(TextArticle, :name => 'news', :profile_id => c.id, :parent_id => gallery.id)
    t_in_f = fast_create(TextArticle, :name => 'news', :profile_id => c.id, :parent_id => f.id)

    assert_equal [t], gallery.news(1)
  end

  should 'not return highlighted news when not asked' do
    c = fast_create(Community)
    gallery = fast_create(Gallery, :profile_id => c.id)
    highlighted_t = fast_create(TextArticle, :name => 'high news', :profile_id => c.id, :highlighted => true, :parent_id => gallery.id)
    t = fast_create(TextArticle, :name => 'news', :profile_id => c.id, :parent_id => gallery.id)

    assert_equal [t].map(&:slug), gallery.news(2).map(&:slug)
  end

  should 'return highlighted news when asked' do
    c = fast_create(Community)
    gallery = fast_create(Gallery, :profile_id => c.id)
    highlighted_t = fast_create(TextArticle, :name => 'high news', :profile_id => c.id, :highlighted => true, :parent_id => gallery.id)
    t = fast_create(TextArticle, :name => 'news', :profile_id => c.id, :parent_id => gallery.id)

    assert_equal [highlighted_t].map(&:slug), gallery.news(2, true).map(&:slug)
  end

  should 'return published images as images' do
    p = create_user('test_user').person
    i = UploadedFile.create!(:profile => p, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'))

    c = fast_create(Community)
    c.add_member(p)
    gallery = fast_create(Gallery, :profile_id => c.id)

    a = create(ApproveArticle, :article => i, :target => c, :requestor => p, :article_parent => gallery)
    a.finish

    assert_includes gallery.images(true), c.articles.find_by(name: 'rails.png')
  end

  should 'not let pass javascript in the body' do
    gallery = Gallery.new
    gallery.body = "<script> alert(Xss!); </script>"
    gallery.valid?

    assert_no_match /(<script>)/, gallery.body
  end

  should 'filter fields with white_list filter' do
    gallery = Gallery.new
    gallery.body = "<h1> Body </h1>"
    gallery.valid?

    assert_equal "<h1> Body </h1>", gallery.body
  end

  should 'not sanitize html comments' do
    gallery = Gallery.new
    gallery.body = '<p><!-- <asdf> << aasdfa >>> --> <h1> Wellformed html code </h1>'
    gallery.valid?

    assert_match  /<p><!-- .* --> <\/p><h1> Wellformed html code <\/h1>/, gallery.body
  end

  should 'accept uploads' do
    folder = fast_create(Gallery)
    assert folder.accept_uploads?
  end

end