]> git.openstreetmap.org Git - rails.git/blob - test/controllers/traces/feeds_controller_test.rb
Add frozen_string_literal comments to ruby files
[rails.git] / test / controllers / traces / feeds_controller_test.rb
1 # frozen_string_literal: true
2
3 require "test_helper"
4
5 module Traces
6   class FeedsControllerTest < ActionDispatch::IntegrationTest
7     ##
8     # test all routes which lead to this controller
9     def test_routes
10       assert_routing(
11         { :path => "/traces/rss", :method => :get },
12         { :controller => "traces/feeds", :action => "show", :format => :rss }
13       )
14       assert_routing(
15         { :path => "/traces/tag/tagname/rss", :method => :get },
16         { :controller => "traces/feeds", :action => "show", :tag => "tagname", :format => :rss }
17       )
18       assert_routing(
19         { :path => "/user/username/traces/rss", :method => :get },
20         { :controller => "traces/feeds", :action => "show", :display_name => "username", :format => :rss }
21       )
22       assert_routing(
23         { :path => "/user/username/traces/tag/tagname/rss", :method => :get },
24         { :controller => "traces/feeds", :action => "show", :display_name => "username", :tag => "tagname", :format => :rss }
25       )
26     end
27
28     def test_show
29       user = create(:user)
30       # The fourth test below is surprisingly sensitive to timestamp ordering when the timestamps are equal.
31       trace_a = create(:trace, :visibility => "public", :timestamp => 4.seconds.ago) do |trace|
32         create(:tracetag, :trace => trace, :tag => "London")
33       end
34       trace_b = create(:trace, :visibility => "public", :timestamp => 3.seconds.ago) do |trace|
35         create(:tracetag, :trace => trace, :tag => "Birmingham")
36       end
37       create(:trace, :visibility => "private", :user => user, :timestamp => 2.seconds.ago) do |trace|
38         create(:tracetag, :trace => trace, :tag => "London")
39       end
40       create(:trace, :visibility => "private", :user => user, :timestamp => 1.second.ago) do |trace|
41         create(:tracetag, :trace => trace, :tag => "Birmingham")
42       end
43
44       # First with the public feed
45       get traces_feed_path
46       check_trace_feed [trace_b, trace_a]
47
48       # Restrict traces to those with a given tag
49       get traces_feed_path(:tag => "London")
50       check_trace_feed [trace_a]
51     end
52
53     def test_show_user
54       user = create(:user)
55       second_user = create(:user)
56       create(:user)
57       create(:trace)
58       trace_b = create(:trace, :visibility => "public", :timestamp => 4.seconds.ago, :user => user)
59       trace_c = create(:trace, :visibility => "public", :timestamp => 3.seconds.ago, :user => user) do |trace|
60         create(:tracetag, :trace => trace, :tag => "London")
61       end
62       create(:trace, :visibility => "private")
63
64       # Test a user with no traces
65       get traces_feed_path(:display_name => second_user)
66       check_trace_feed []
67
68       # Test the user with the traces - should see only public ones
69       get traces_feed_path(:display_name => user)
70       check_trace_feed [trace_c, trace_b]
71
72       # Should only see traces with the correct tag when a tag is specified
73       get traces_feed_path(:display_name => user, :tag => "London")
74       check_trace_feed [trace_c]
75
76       # Should no traces if the user does not exist
77       get traces_feed_path(:display_name => "UnknownUser")
78       check_trace_feed []
79     end
80
81     private
82
83     def check_trace_feed(traces)
84       assert_response :success
85       assert_template "traces/feeds/show"
86       assert_equal "application/rss+xml", @response.media_type
87       assert_select "rss", :count => 1 do
88         assert_select "channel", :count => 1 do |channel|
89           assert_select "title"
90           assert_select "description"
91           assert_select "link"
92           assert_select "image"
93           assert_select "item", :count => traces.length
94
95           traces.zip(channel.css("item")).each do |trace, item|
96             assert_select item, "title", trace.name
97             assert_select item, "link", "http://www.example.com/user/#{ERB::Util.u(trace.user.display_name)}/traces/#{trace.id}"
98             assert_select item, "guid", "http://www.example.com/user/#{ERB::Util.u(trace.user.display_name)}/traces/#{trace.id}"
99             assert_select item, "description" do
100               assert_dom_encoded do
101                 assert_select "img[src='#{trace_icon_url trace.user, trace}']"
102               end
103             end
104             # assert_select item, "dc:creator", trace.user.display_name
105             assert_select item, "pubDate", trace.timestamp.rfc822
106           end
107         end
108       end
109     end
110   end
111 end