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