cirandas.net

ref: master

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

class FeedReaderBlockTest < ActiveSupport::TestCase

  include DatesHelper

  def setup
    @feed = create(:feed_reader_block)
  end
  attr_reader :feed

  should 'default describe' do
    assert_not_equal Block.description, FeedReaderBlock.description
  end

  should 'have address and limit' do
    assert_respond_to feed, :address
    assert_respond_to feed, :limit
  end

  should 'default value of limit' do
    assert_equal 5, feed.limit
  end

  should 'is editable' do
    assert feed.editable?
  end

  should 'display channel title as title by default' do
    feed.feed_title = 'Feed for unit tests'
    assert_equal 'Feed for unit tests', feed.title
  end

  should 'display default title when hasnt feed_content' do
    assert_equal 'Feed Reader', feed.title
  end

  should 'clear feed title and items' do
    feed.feed_items = %w[ last-post second-post first-post ]
    feed.feed_title = 'Feed Test'
    feed.clear
    assert_nil feed.feed_title
    assert_equal [], feed.feed_items
  end

  should 'save! when commit' do
    feed.expects(:save!)
    feed.finish_fetch
  end

  should 'set fetched_at when finishing a fetch' do
    feed.stubs(:save!)
    feed.finish_fetch
    assert_not_nil feed.fetched_at
  end

  should 'have empty fetched_at by default' do
    assert_nil feed.fetched_at
  end

  should 'display the latest post first' do
    %w[ first-post second-post last-post ].each do |i|
      feed.add_item(i, "http://localhost/#{i}", Date.today, "some contet for #{i}")
    end

    assert_equal %w[ last-post second-post first-post ], feed.feed_items.map{|i|i[:title]}
  end

  should 'have empty error message by default' do
    assert FeedReaderBlock.new.error_message.blank?, 'new feed reader block expected to have empty error message'
  end

  should 'expire after a period' do
    # save current time
    now = Time.now
    expired =  FeedReaderBlock.new
    expired.save
    not_expired = FeedReaderBlock.new
    not_expired.save

    # Noosfero is configured to update feeds every 4 hours
    FeedUpdater.stubs(:update_interval).returns(4.hours)

    # 5 hours ago
    Time.stubs(:now).returns(now  - 5.hours)
    expired.finish_fetch

    # 3 hours ago
    Time.stubs(:now).returns(now - 3.hours)
    not_expired.finish_fetch

    # now one block should be expired and not the other
    Time.stubs(:now).returns(now)
    expired_list = FeedReaderBlock.expired
    assert_includes expired_list, expired
    assert_not_includes expired_list, not_expired
  end

  should 'consider recently-created as expired' do
    # feed is created in setup
    assert_includes FeedReaderBlock.expired, feed
  end

  should 'have an update errors counter' do
    assert_equal 5, FeedReaderBlock.new(:update_errors => 5).update_errors
  end

  should 'have 0 errors by default' do
    assert_equal 0, FeedReaderBlock.new.update_errors
  end

  should 'be disabled by default' do
    assert_equal false, FeedReaderBlock.new.enabled
  end

  should 'be enabled when address is filled' do
    reader = build(:feed_reader_block, :address => 'http://www.example.com/feed')
    assert_equal true, reader.enabled
  end

  should 'be expired when address is updated' do
    reader = build(:feed_reader_block, :address => 'http://www.example.com/feed')
    reader.finish_fetch
    expired_list = FeedReaderBlock.expired
    assert_not_includes expired_list, reader
    reader.address = "http://www.example.com/new-feed"
    reader.save!
    expired_list = FeedReaderBlock.expired
    assert_includes expired_list, reader
  end

  should 'be disabled when address is empty' do
    reader = build(:feed_reader_block, :address => 'http://www.example.com/feed').tap do |f|
      f.enabled = true
    end
    reader.address = nil
    assert_equal false, reader.enabled
  end

  should 're-enable when address is changed' do
    reader = build(:feed_reader_block, :address => 'http://www.example.com/feed')
    reader.enabled = false

    reader.address = 'http://www.example.com/feed'
    assert_equal false, reader.enabled, 'must not enable when setting to the same address'

    reader.address = 'http://www.acme.com/feed'
    assert_equal true, reader.enabled, 'must enable when setting to new address'
  end

  should 'keep enable when address is not changed' do
    reader = build(:feed_reader_block, :address => 'http://www.example.com/feed')
    reader.address = 'http://www.example.com/feed'
    assert_equal true, reader.enabled
  end

end

require 'boxes_helper'
require 'block_helper'
require 'dates_helper'

class FeedReaderBlockViewTest < ActionView::TestCase
  include BoxesHelper
  include DatesHelper

  ActionView::Base.send :include, BlockHelper
  ActionView::Base.send :include, DatesHelper

  def setup
    @feed = create(:feed_reader_block)
  end
  attr_reader :feed

  should "display error message as content when it's the case" do
    msg = "there was a problem"
    feed.error_message = msg

    ActionView::Base.any_instance.stubs(:block_title).returns("")

    assert_match(msg, render_block_content(feed))
  end

  should 'display feed posts from content' do
    feed.feed_items = []
    %w[ last-post second-post first-post ].each do |i|
      feed.feed_items << {:title => i, :link => "http://localhost/#{i}"}
    end
    feed.feed_title = 'Feed for unit tests'

    feed_content = render_block_content(feed)

    assert_tag_in_string feed_content, :tag => 'h3', :content => 'Feed for unit tests'
    assert_tag_in_string feed_content, :tag => 'a', :attributes => { :href => 'http://localhost/last-post' }, :content => 'last-post'
    assert_tag_in_string feed_content, :tag => 'a', :attributes => { :href => 'http://localhost/second-post' }, :content => 'second-post'
    assert_tag_in_string feed_content, :tag => 'a', :attributes => { :href => 'http://localhost/first-post' }, :content => 'first-post'
  end

  should 'display only limit posts' do
    feed.limit = 1; feed.save!
    %w[ first-post second-post ].each do |i|
      feed.add_item(i, "http://localhost/#{i}", Date.today, "some contet for #{i}")
    end

    ActionView::Base.any_instance.stubs(:block_title).returns("")

    assert_tag_in_string render_block_content(feed), :tag => 'a', :attributes => { :href => 'http://localhost/second-post' }, :content => 'second-post'
    assert_no_tag_in_string render_block_content(feed), :tag => 'a', :attributes => { :href => 'http://localhost/first-post' }, :content => 'first-post'
  end

  should 'notice when content not fetched yet' do
    assert_equal "  Feed content was not loaded yet\n", render_block_footer(feed)
  end

  should 'display last fetched date' do
    now = Time.new(2014,1,1)
    feed.feed_items = ['one', 'two']
    feed.fetched_at = now
    assert_equal "  Updated: #{show_date(now)}\n", render_block_footer(feed)
  end
end