]> git.openstreetmap.org Git - rails.git/blob - test/functional/trace_controller_test.rb
28c09c8995c96d42acf8cd839bd40e0bdb0e4e78
[rails.git] / test / functional / trace_controller_test.rb
1 require File.dirname(__FILE__) + '/../test_helper'
2
3 class TraceControllerTest < ActionController::TestCase
4   fixtures :users, :gpx_files
5   set_fixture_class :gpx_files => 'Trace'
6
7   # Check that the list of changesets is displayed
8   def test_list
9     get :list
10     assert_response :success
11     assert_template 'list'
12   end
13
14   # Check that I can get mine
15   def test_list_mine
16     @request.cookies["_osm_username"] = users(:public_user).display_name
17
18     # First try to get it when not logged in
19     get :mine
20     assert_redirected_to :controller => 'user', :action => 'login', :referer => '/traces/mine'
21
22     # Now try when logged in
23     get :mine, {}, {:user => users(:public_user).id}
24     assert_redirected_to :controller => 'trace', :action => 'list', :display_name => users(:public_user).display_name
25   end
26
27   # Check that the rss loads
28   def test_rss
29     get :georss
30     assert_rss_success
31
32     get :georss, :display_name => users(:normal_user).display_name
33     assert_rss_success
34   end
35
36   def assert_rss_success
37     assert_response :success
38     assert_template nil
39     assert_equal "application/rss+xml", @response.content_type
40   end
41
42   # Check getting a specific trace through the api
43   def test_api_read
44     # First with no auth
45     get :api_read, :id => gpx_files(:public_trace_file).id
46     assert_response :unauthorized
47
48     # Now with some other user, which should work since the trace is public
49     basic_authorization(users(:public_user).display_name, "test")
50     get :api_read, :id => gpx_files(:public_trace_file).id
51     assert_response :success
52
53     # And finally we should be able to do it with the owner of the trace
54     basic_authorization(users(:normal_user).display_name, "test")
55     get :api_read, :id => gpx_files(:public_trace_file).id
56     assert_response :success
57   end
58
59   # Check an anoymous trace can't be specifically fetched by another user
60   def test_api_read_anon
61     # Furst with no auth
62     get :api_read, :id => gpx_files(:anon_trace_file).id
63     assert_response :unauthorized
64
65     # Now try with another user, which shouldn't work since the trace is anon
66     basic_authorization(users(:normal_user).display_name, "test")
67     get :api_read, :id => gpx_files(:anon_trace_file).id
68     assert_response :forbidden
69
70     # And finally we should be able to get the trace details with the trace owner
71     basic_authorization(users(:public_user).display_name, "test")
72     get :api_read, :id => gpx_files(:anon_trace_file).id
73     assert_response :success
74   end
75
76   # Check the api details for a trace that doesn't exist
77   def test_api_read_not_found
78     # Try first with no auth, as it should requure it
79     get :api_read, :id => 0
80     assert_response :unauthorized
81
82     # Login, and try again
83     basic_authorization(users(:public_user).display_name, "test")
84     get :api_read, :id => 0
85     assert_response :not_found
86
87     # Now try a trace which did exist but has been deleted
88     basic_authorization(users(:public_user).display_name, "test")
89     get :api_read, :id => 5
90     assert_response :not_found
91   end
92
93   # Check updating a trace through the api
94   def test_api_update
95     # First with no auth
96     content gpx_files(:public_trace_file).to_xml
97     put :api_update, :id => gpx_files(:public_trace_file).id
98     assert_response :unauthorized
99
100     # Now with some other user, which should fail
101     basic_authorization(users(:public_user).display_name, "test")
102     content gpx_files(:public_trace_file).to_xml
103     put :api_update, :id => gpx_files(:public_trace_file).id
104     assert_response :forbidden
105
106     # Now with a trace which doesn't exist
107     basic_authorization(users(:public_user).display_name, "test")
108     content gpx_files(:public_trace_file).to_xml
109     put :api_update, :id => 0
110     assert_response :not_found
111
112     # Now with a trace which did exist but has been deleted
113     basic_authorization(users(:public_user).display_name, "test")
114     content gpx_files(:deleted_trace_file).to_xml
115     put :api_update, :id => gpx_files(:deleted_trace_file).id
116     assert_response :not_found
117
118     # Now try an update with the wrong ID
119     basic_authorization(users(:normal_user).display_name, "test")
120     content gpx_files(:anon_trace_file).to_xml
121     put :api_update, :id => gpx_files(:public_trace_file).id
122     assert_response :bad_request, 
123        "should not be able to update a trace with a different ID from the XML"
124
125     # And finally try an update that should work
126     basic_authorization(users(:normal_user).display_name, "test")
127     t = gpx_files(:public_trace_file)
128     t.description = "Changed description"
129     t.visibility = "private"
130     content t.to_xml
131     put :api_update, :id => t.id
132     assert_response :success
133     nt = Trace.find(t.id)
134     assert_equal nt.description, t.description
135     assert_equal nt.visibility, t.visibility
136   end
137
138   # Check deleting a trace through the api
139   def test_api_delete
140     # First with no auth
141     delete :api_delete, :id => gpx_files(:public_trace_file).id
142     assert_response :unauthorized
143
144     # Now with some other user, which should fail
145     basic_authorization(users(:public_user).display_name, "test")
146     delete :api_delete, :id => gpx_files(:public_trace_file).id
147     assert_response :forbidden
148
149     # Now with a trace which doesn't exist
150     basic_authorization(users(:public_user).display_name, "test")
151     delete :api_delete, :id => 0
152     assert_response :not_found
153
154     # And finally we should be able to do it with the owner of the trace
155     basic_authorization(users(:normal_user).display_name, "test")
156     delete :api_delete, :id => gpx_files(:public_trace_file).id
157     assert_response :success
158
159     # Try it a second time, which should fail
160     basic_authorization(users(:normal_user).display_name, "test")
161     delete :api_delete, :id => gpx_files(:public_trace_file).id
162     assert_response :not_found
163   end
164 end