1 require File.dirname(__FILE__) + '/../test_helper'
3 class TraceControllerTest < ActionController::TestCase
4 fixtures :users, :gpx_files
5 set_fixture_class :gpx_files => 'Trace'
7 # Check that the list of changesets is displayed
10 assert_response :success
11 assert_template 'list'
14 # Check that I can get mine
16 # First try to get it when not logged in
18 assert_redirected_to :controller => 'user', :action => 'login', :referer => '/traces/mine'
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
25 # Check that the rss loads
30 get :georss, :display_name => users(:normal_user).display_name
34 def assert_rss_success
35 assert_response :success
37 assert_equal "application/rss+xml", @response.content_type
40 # Check getting a specific trace through the api
43 get :api_read, :id => gpx_files(:public_trace_file).id
44 assert_response :unauthorized
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
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
57 # Check an anoymous trace can't be specifically fetched by another user
58 def test_api_read_anon
60 get :api_read, :id => gpx_files(:anon_trace_file).id
61 assert_response :unauthorized
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
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
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
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
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
91 # Check updating a trace through the api
94 content gpx_files(:public_trace_file).to_xml
95 put :api_update, :id => gpx_files(:public_trace_file).id
96 assert_response :unauthorized
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
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
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
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"
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"
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
136 # Check deleting a trace through the api
139 delete :api_delete, :id => gpx_files(:public_trace_file).id
140 assert_response :unauthorized
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
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
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
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