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 @request.cookies["_osm_username"] = users(:public_user).display_name
18 # First try to get it when not logged in
20 assert_redirected_to :controller => 'user', :action => 'login', :referer => '/traces/mine'
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
27 # Check that the rss loads
32 get :georss, :display_name => users(:normal_user).display_name
36 def assert_rss_success
37 assert_response :success
39 assert_equal "application/rss+xml", @response.content_type
42 # Check getting a specific trace through the api
45 get :api_read, :id => gpx_files(:public_trace_file).id
46 assert_response :unauthorized
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
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
59 # Check an anoymous trace can't be specifically fetched by another user
60 def test_api_read_anon
62 get :api_read, :id => gpx_files(:anon_trace_file).id
63 assert_response :unauthorized
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
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
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
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
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
93 # Check updating a trace through the api
96 content gpx_files(:public_trace_file).to_xml
97 put :api_update, :id => gpx_files(:public_trace_file).id
98 assert_response :unauthorized
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
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
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
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"
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"
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
138 # Check deleting a trace through the api
141 delete :api_delete, :id => gpx_files(:public_trace_file).id
142 assert_response :unauthorized
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
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
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
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