aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/dispatch/routing_test.rb
blob: 972bf7360284b4316675a064902676e5258b9503 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
require 'abstract_unit'
require 'controller/fake_controllers'

class TestRoutingMapper < ActionDispatch::IntegrationTest
  SprocketsApp = lambda { |env|
    [200, {"Content-Type" => "text/html"}, ["javascripts"]]
  }

  class IpRestrictor
    def self.matches?(request)
      request.ip =~ /192\.168\.1\.1\d\d/
    end
  end

  class Dispatcher
    def self.new(*args)
      lambda { |env|
        params = env['action_dispatch.request.path_parameters']
        controller, action = params[:controller], params[:action]
        [200, {'Content-Type' => 'text/html'}, ["#{controller}##{action}"]]
      }
    end
  end

  old_dispatcher = ActionDispatch::Routing::RouteSet::Dispatcher
  ActionDispatch::Routing::RouteSet.module_eval { remove_const :Dispatcher }
  ActionDispatch::Routing::RouteSet.module_eval { const_set :Dispatcher, Dispatcher }
  Routes = ActionDispatch::Routing::RouteSet.new
  Routes.draw do |map|
    controller :sessions do
      get  'login', :to => :new, :as => :login
      post 'login', :to => :create

      delete 'logout', :to => :destroy, :as => :logout
    end

    match 'account/login', :to => redirect("/login")

    match 'openid/login', :via => [:get, :post], :to => "openid#login"

    controller(:global) do
      match 'global/:action'
      match 'global/export',      :to => :export, :as => :export_request
      match 'global/hide_notice', :to => :hide_notice, :as => :hide_notice
      match '/export/:id/:file',  :to => :export, :as => :export_download, :constraints => { :file => /.*/ }
    end

    constraints(:ip => /192\.168\.1\.\d\d\d/) do
      get 'admin', :to => "queenbee#index"
    end

    constraints IpRestrictor do
      get 'admin/accounts', :to => "queenbee#accounts"
    end

    resources :projects, :controller => :project do
      resources :involvements, :attachments

      resources :participants do
        put :update_all, :on => :collection
      end

      resources :companies do
        resources :people
        resource  :avatar
      end

      resources :images do
        post :revise, :on => :member
      end

      resources :people do
        namespace ":access_token" do
          resource :avatar
        end

        member do
          put  :accessible_projects
          post :resend, :generate_new_password
        end
      end

      resources :posts do
        get  :archive, :toggle_view, :on => :collection
        post :preview, :on => :member

        resource :subscription

        resources :comments do
          post :preview, :on => :collection
        end
      end
    end

    match 'sprockets.js', :to => SprocketsApp

    match 'people/:id/update', :to => 'people#update', :as => :update_person
    match '/projects/:project_id/people/:id/update', :to => 'people#update', :as => :update_project_person

    # misc
    match 'articles/:year/:month/:day/:title', :to => "articles#show", :as => :article

    namespace :account do
      resource :subscription, :credit, :credit_card
    end

    controller :articles do
      scope 'articles' do
        scope ':title', :title => /[a-z]+/, :as => :with_title do
          match ':id', :to => :with_id
        end
      end
    end

    scope ':access_token', :constraints => { :access_token => /\w{5,5}/ } do
      resources :rooms
    end
  end
  ActionDispatch::Routing::RouteSet.module_eval { remove_const :Dispatcher }
  ActionDispatch::Routing::RouteSet.module_eval { const_set :Dispatcher, old_dispatcher }

  def app
    Routes
  end

  def test_logout
    with_test_routes do
      delete '/logout'
      assert_equal 'sessions#destroy', @response.body

      assert_equal '/logout', logout_path
    end
  end

  def test_login
    with_test_routes do
      get '/login'
      assert_equal 'sessions#new', @response.body

      post '/login'
      assert_equal 'sessions#create', @response.body

      assert_equal '/login', login_path
    end
  end

  def test_login_redirect
    with_test_routes do
      get '/account/login'
      assert_equal 301, @response.status
      assert_equal 'http://www.example.com/login', @response.headers['Location']
      assert_equal 'Moved Permanently', @response.body
    end
  end

  def test_openid
    with_test_routes do
      get '/openid/login'
      assert_equal 'openid#login', @response.body

      post '/openid/login'
      assert_equal 'openid#login', @response.body
    end
  end

  # TODO: rackmount is broken
  # def test_admin
  #   with_test_routes do
  #     get '/admin', {}, {'REMOTE_ADDR' => '192.168.1.100'}
  #     assert_equal 'queenbee#index', @response.body
  #
  #     assert_raise(ActionController::RoutingError) { get '/admin', {}, {'REMOTE_ADDR' => '10.0.0.100'} }
  #
  #     get '/admin/accounts', {}, {'REMOTE_ADDR' => '192.168.1.100'}
  #     assert_equal 'queenbee#accounts', @response.body
  #
  #     assert_raise(ActionController::RoutingError) { get '/admin/accounts', {}, {'REMOTE_ADDR' => '10.0.0.100'} }
  #   end
  # end

  def test_global
    with_test_routes do
      get '/global/dashboard'
      assert_equal 'global#dashboard', @response.body

      get '/global/export'
      assert_equal 'global#export', @response.body

      get '/global/hide_notice'
      assert_equal 'global#hide_notice', @response.body

      get '/export/123/foo.txt'
      assert_equal 'global#export', @response.body

      assert_equal '/global/export', export_request_path
      assert_equal '/global/hide_notice', hide_notice_path
      assert_equal '/export/123/foo.txt', export_download_path(:id => 123, :file => 'foo.txt')
    end
  end

  def test_projects
    with_test_routes do
      get '/projects/1'
      assert_equal 'projects#show', @response.body
    end
  end

  def test_projects_involvements
    with_test_routes do
      get '/projects/1/involvements'
      assert_equal 'involvements#index', @response.body

      get '/projects/1/involvements/1'
      assert_equal 'involvements#show', @response.body
    end
  end

  def test_projects_attachments
    with_test_routes do
      get '/projects/1/attachments'
      assert_equal 'attachments#index', @response.body
    end
  end

  def test_projects_participants
    with_test_routes do
      get '/projects/1/participants'
      assert_equal 'participants#index', @response.body

      put '/projects/1/participants/update_all'
      assert_equal 'participants#update_all', @response.body
    end
  end

  def test_projects_companies
    with_test_routes do
      get '/projects/1/companies'
      assert_equal 'companies#index', @response.body

      get '/projects/1/companies/1/people'
      assert_equal 'people#index', @response.body

      get '/projects/1/companies/1/avatar'
      assert_equal 'avatar#show', @response.body
    end
  end

  def test_project_images
    with_test_routes do
      get '/projects/1/images'
      assert_equal 'images#index', @response.body

      post '/projects/1/images/1/revise'
      assert_equal 'images#revise', @response.body
    end
  end

  def test_projects_people
    with_test_routes do
      get '/projects/1/people'
      assert_equal 'people#index', @response.body

      get '/projects/1/people/1'
      assert_equal 'people#show', @response.body

      get '/projects/1/people/1/7a2dec8/avatar'
      assert_equal 'avatar#show', @response.body

      put '/projects/1/people/1/accessible_projects'
      assert_equal 'people#accessible_projects', @response.body

      post '/projects/1/people/1/resend'
      assert_equal 'people#resend', @response.body

      post '/projects/1/people/1/generate_new_password'
      assert_equal 'people#generate_new_password', @response.body
    end
  end

  def test_projects_posts
    with_test_routes do
      get '/projects/1/posts'
      assert_equal 'posts#index', @response.body

      get '/projects/1/posts/archive'
      assert_equal 'posts#archive', @response.body

      get '/projects/1/posts/toggle_view'
      assert_equal 'posts#toggle_view', @response.body

      post '/projects/1/posts/1/preview'
      assert_equal 'posts#preview', @response.body

      get '/projects/1/posts/1/subscription'
      assert_equal 'subscription#show', @response.body

      get '/projects/1/posts/1/comments'
      assert_equal 'comments#index', @response.body

      post '/projects/1/posts/1/comments/preview'
      assert_equal 'comments#preview', @response.body
    end
  end

  def test_sprockets
    with_test_routes do
      get '/sprockets.js'
      assert_equal 'javascripts', @response.body
    end
  end

  def test_update_person_route
    with_test_routes do
      get '/people/1/update'
      assert_equal 'people#update', @response.body

      assert_equal '/people/1/update', update_person_path(:id => 1)
    end
  end

  def test_update_project_person
    with_test_routes do
      get '/projects/1/people/2/update'
      assert_equal 'people#update', @response.body

      assert_equal '/projects/1/people/2/update', update_project_person_path(:project_id => 1, :id => 2)
    end
  end

  def test_articles_perma
    with_test_routes do
      get '/articles/2009/08/18/rails-3'
      assert_equal 'articles#show', @response.body

      assert_equal '/articles/2009/8/18/rails-3', article_path(:year => 2009, :month => 8, :day => 18, :title => 'rails-3')
    end
  end

  def test_account_namespace
    with_test_routes do
      get '/account/subscription'
      assert_equal 'subscription#show', @response.body

      get '/account/credit'
      assert_equal 'credit#show', @response.body

      get '/account/credit_card'
      assert_equal 'credit_card#show', @response.body
    end
  end

  def test_articles_with_id
    with_test_routes do
      get '/articles/rails/1'
      assert_equal 'articles#with_id', @response.body

      assert_raise(ActionController::RoutingError) { get '/articles/123/1' }

      assert_equal '/articles/rails/1', with_title_path(:title => 'rails', :id => 1)
    end
  end

  def test_access_token_rooms
    with_test_routes do
      get '/12345/rooms'
      assert_equal 'rooms#index', @response.body

      get '/12345/rooms/1'
      assert_equal 'rooms#show', @response.body

      get '/12345/rooms/1/edit'
      assert_equal 'rooms#edit', @response.body
    end
  end

  private
    def with_test_routes
      real_routes, temp_routes = ActionController::Routing::Routes, Routes

      ActionController::Routing.module_eval { remove_const :Routes }
      ActionController::Routing.module_eval { const_set :Routes, temp_routes }

      yield
    ensure
      ActionController::Routing.module_eval { remove_const :Routes }
      ActionController::Routing.const_set(:Routes, real_routes)
    end
end