feed_reader_block_test.rb
6.09 KB
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
require File.dirname(__FILE__) + '/../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 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 = feed.content
    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 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 'notice when content not fetched yet' do
    assert_equal'Feed content was not loaded yet', feed.footer
  end
  should 'display last fetched date' do
    now = Time.now
    feed.feed_items = ['one', 'two']
    feed.fetched_at = now
    assert_equal "Updated: #{show_date(now)}", feed.footer
  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 '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
    assert_tag_in_string feed.formatted_feed_content, :tag => 'a', :attributes => { :href => 'http://localhost/second-post' }, :content => 'second-post'
    assert_no_tag_in_string feed.formatted_feed_content, :tag => 'a', :attributes => { :href => 'http://localhost/first-post' }, :content => 'first-post'
  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 "display error message as content when it's the case" do
    msg = "there was a problem"
    feed.error_message = msg
    assert_match(msg, feed.content)
  end
  should 'expire after a period' do
    # save current time
    now = Time.now
    expired = FeedReaderBlock.create!
    not_expired = FeedReaderBlock.create!
    # 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, :enabled => true, :address => 'http://www.example.com/feed')
    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