public
Fork of halorgium/mephisto
Description: A mirror of the mephisto code-base
Homepage: http://mephistoblog.com/
Clone URL: git://github.com/technoweenie/mephisto.git
Click here to lend your support to: mephisto and make a donation at www.pledgie.com !
mephisto / test / functional / feed_controller_test.rb
100644 151 lines (132 sloc) 5.844 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
require File.dirname(__FILE__) + '/../test_helper'
require_dependency 'feed_controller'
 
# Re-raise errors caught by the controller.
class FeedController; def rescue_action(e) raise e end; end
 
class FeedControllerTest < Test::Unit::TestCase
  fixtures :contents, :sections, :assigned_sections, :sites
  
  def setup
    @controller = FeedController.new
    @request = ActionController::TestRequest.new
    @response = ActionController::TestResponse.new
  end
  
  def test_feed_comes_from_site
    host! 'cupcake.com'
    get :feed, :sections => ['about']
    assert_equal sections(:cupcake_about), assigns(:section)
    assert_equal [contents(:cupcake_welcome)], assigns(:articles)
    assert_atom_entries_size 1
  end
  
  def test_site_in_feed_links
    host! 'cupcake.com'
    get :feed, :sections => []
    assert_models_equal [sections(:cupcake_home)], [assigns(:section)]
    assert_models_equal [contents(:cupcake_welcome)], assigns(:articles)
    assert_atom_entries_size 1
    assert_select 'link[href=?]', 'http://cupcake.com/'
  end
  
  def test_should_return_record_not_found_for_bad_feed_urls
    get :feed, :sections => %w(beastie boys)
    assert_equal '404', @response.code
  end
  
  def test_should_find_comments_by_site
    get :feed, :sections => %w(all_comments.xml)
    assert_select 'feed>title', 'Mephisto - All Comments'
    assert_nil assigns(:section)
    assert_models_equal [contents(:welcome_comment)], assigns(:comments)
    assert_atom_entries_size 1
  end
  
  def test_should_find_comments_by_section
    get :feed, :sections => %w(comments.xml)
    assert_select 'feed>title', 'Mephisto - Home Comments'
    assert_models_equal [sections(:home)], [assigns(:section)]
    assert_models_equal [contents(:welcome_comment)], assigns(:comments)
    assert_atom_entries_size 1
  end
end
 
context "About Section Feed" do
  fixtures :contents, :sections, :assigned_sections, :sites, :users, :assets, :assigned_assets
  def setup
    @controller = FeedController.new
    @request = ActionController::TestRequest.new
    @response = ActionController::TestResponse.new
    get :feed, :sections => ['about']
  end
 
  specify "should select correct records" do
    assert_equal sections(:about), assigns(:section)
    assert_equal [contents(:welcome), contents(:about), contents(:site_map)], assigns(:articles)
  end
  
  specify "should show correct titles" do
    assert_select 'feed>title', 'Mephisto - About'
    assert_select 'feed>entry>title', 3 do |titles|
      assert_equal 'Welcome to Mephisto', titles[0].children.first.content
      assert_equal 'About this page', titles[1].children.first.content
      assert_equal 'The Site Map', titles[2].children.first.content
    end
  end
  
  # TODO: Fails due to asset test deleting asset fixtures
  specify "should show correct links" do
    return if Asset.count == 1
    assert_select 'feed>link[href=?][type=?]', 'http://test.host/about', 'text/html'
    assert_select 'feed>entry>link[href]', 4 do |hrefs|
      assert_equal "http://test.host/about", hrefs[0]['href']
      assert_match /asset\.mp3$/, hrefs[1]['href']
      assert_equal "http://test.host/about/about-this-page", hrefs[2]['href']
      assert_equal "http://test.host/about/the-site-map", hrefs[3]['href']
    end
  end
end
 
context "Home Section Feed" do
  fixtures :contents, :sections, :assigned_sections, :sites, :assets, :assigned_assets
  def setup
    @controller = FeedController.new
    @request = ActionController::TestRequest.new
    @response = ActionController::TestResponse.new
    get :feed, :sections => []
    @contents = get_xpath '//entry/content'
  end
  
  specify "should show titles" do
    assert_select 'feed>title', 'Mephisto - Home'
    assert_select 'feed>entry>title', 2 do |elements|
      assert_equal 'Welcome to Mephisto', elements[0].children.first.content
      assert_equal 'Another Welcome to Mephisto', elements[1].children.first.content
    end
  end
  
  # TODO: Fails due to asset test deleting asset fixtures
  specify "should show correct links" do
    return if Asset.count == 1
    assert_select 'feed>link[href=?][type=?]', 'http://test.host/', 'text/html'
    assert_select 'feed>entry>link[href]', 3 do |hrefs|
      assert_match /\/welcome-to-mephisto$/, hrefs[0]['href']
      assert_match /asset\.mp3$/, hrefs[1]['href']
      assert_match /\/another-welcome-to-mephisto$/, hrefs[2]['href']
    end
  end
 
  # TODO: Fails due to asset test deleting asset fixtures
  specify "should show podcast" do
    return if Asset.count == 1
    assert_select 'feed>entry>link[rel=?][length=?][type=?]', 'enclosure', '252366', 'audio/mpeg'
  end
 
  specify "show absolute urls with custom relative url root" do
    begin
      old_root = ActionController::AbstractRequest.relative_url_root
      ActionController::AbstractRequest.relative_url_root = '/weblog'
      get :feed, :sections => []
      assert_select 'feed entry link' do
        assert_select '[href=?]', /^http\:\/\/test\.host\/weblog\/\d{4}\/.*$/
      end
    ensure
      ActionController::AbstractRequest.relative_url_root = old_root
    end
  end
 
  specify "should not double escape html" do
    text = @contents.first.get_text.to_s.strip
    assert text.starts_with("welcome summary\n&lt;p&gt;quentin&#8217;s &#8220;welcome&#8221;"), "'#{text.inspect}' was double escaped"
  end
  
  specify "should sanitize content" do
    text = @contents.first.get_text.to_s.strip
    evil = "<script>hi</script><a onclick=\"foo\" href=\"#\">linkage</a></p>"
    good = %(<a href="#">linkage</a></p>)
    assert !text.ends_with(CGI::escapeHTML(evil)), "'#{text.inspect}' was not sanitized"
    assert text.ends_with(CGI::escapeHTML(good)), "'#{text.inspect}' was not sanitized"
  end
end