File: scaffold_controller_generator_test.rb

package info (click to toggle)
rails 2%3A7.2.2.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,348 kB
  • sloc: ruby: 349,797; javascript: 30,703; yacc: 46; sql: 43; sh: 29; makefile: 27
file content (364 lines) | stat: -rw-r--r-- 13,057 bytes parent folder | download | duplicates (2)
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
# frozen_string_literal: true

require "plugin_helpers"
require "generators/generators_test_helper"
require "rails/generators/rails/scaffold_controller/scaffold_controller_generator"

module Unknown
  module Generators
  end
end

class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
  include PluginHelpers
  include GeneratorsTestHelper
  arguments %w(User name:string age:integer)

  setup :copy_routes

  def test_controller_skeleton_is_created
    run_generator

    assert_file "app/controllers/users_controller.rb" do |content|
      assert_match(/class UsersController < ApplicationController/, content)

      assert_instance_method :index, content do |m|
        assert_match(/@users = User\.all/, m)
      end

      assert_instance_method :show, content

      assert_instance_method :new, content do |m|
        assert_match(/@user = User\.new/, m)
      end

      assert_instance_method :edit, content

      assert_instance_method :create, content do |m|
        assert_match(/@user = User\.new\(user_params\)/, m)
        assert_match(/@user\.save/, m)
      end

      assert_instance_method :update, content do |m|
        assert_match(/@user\.update\(user_params\)/, m)
        assert_match(/status: :see_other/, m)
      end

      assert_instance_method :destroy, content do |m|
        assert_match(/@user\.destroy/, m)
        assert_match(/User was successfully destroyed/, m)
        assert_match(/status: :see_other/, m)
      end

      assert_instance_method :set_user, content do |m|
        assert_match(/@user = User\.find\(params\[:id\]\)/, m)
      end

      assert_match(/def user_params/, content)
      assert_match(/params\.require\(:user\)\.permit\(:name, :age\)/, content)
    end
  end

  def test_dont_use_require_or_permit_if_there_are_no_attributes
    run_generator ["User"]

    assert_file "app/controllers/users_controller.rb" do |content|
      assert_match(/def user_params/, content)
      assert_match(/params\.fetch\(:user, \{\}\)/, content)
    end
  end

  def test_controller_permit_references_attributes
    run_generator ["LineItem", "product:references", "cart:belongs_to"]

    assert_file "app/controllers/line_items_controller.rb" do |content|
      assert_match(/def line_item_params/, content)
      assert_match(/params\.require\(:line_item\)\.permit\(:product_id, :cart_id\)/, content)
    end
  end

  def test_controller_permit_polymorphic_references_attributes
    run_generator ["LineItem", "product:references{polymorphic}"]

    assert_file "app/controllers/line_items_controller.rb" do |content|
      assert_match(/def line_item_params/, content)
      assert_match(/params\.require\(:line_item\)\.permit\(:product_id, :product_type\)/, content)
    end
  end

  def test_controller_permit_attachment_attributes
    run_generator ["Message", "video:attachment", "photos:attachments"]

    assert_file "app/controllers/messages_controller.rb" do |content|
      assert_match(/def message_params/, content)
      assert_match(/params\.require\(:message\)\.permit\(:video, photos: \[\]\)/, content)
    end
  end

  def test_controller_permit_attachments_attributes_only
    run_generator ["Message", "photos:attachments"]

    assert_file "app/controllers/messages_controller.rb" do |content|
      assert_match(/def message_params/, content)
      assert_match(/params\.require\(:message\)\.permit\(photos: \[\]\)/, content)
    end
  end

  def test_controller_route_are_added
    run_generator ["Message", "photos:attachments"]

    assert_file "config/routes.rb" do |route|
      assert_match(/resources :messages$/, route)
    end
  end

  def test_controller_route_are_skipped
    run_generator ["Message", "photos:attachments", "--skip-routes"]

    assert_file "config/routes.rb" do |route|
      assert_no_match(/resources :messages$/, route)
    end
  end

  def test_helper_are_invoked_with_a_pluralized_name
    run_generator
    assert_file "app/helpers/users_helper.rb", /module UsersHelper/
  end

  def test_views_are_generated
    run_generator

    %w(index edit new show).each do |view|
      assert_file "app/views/users/#{view}.html.erb"
    end
    assert_no_file "app/views/layouts/users.html.erb"
  end

  def test_index_page_have_notice
    run_generator

    %w(index show).each do |view|
      assert_file "app/views/users/#{view}.html.erb", /notice/
    end
  end

  def test_functional_tests
    run_generator ["User", "name:string", "age:integer", "organization:references{polymorphic}"]

    assert_file "test/controllers/users_controller_test.rb" do |content|
      assert_match(/class UsersControllerTest < ActionDispatch::IntegrationTest/, content)
      assert_match(/test "should get index"/, content)
      assert_match(/post users_url, params: \{ user: \{ age: @user\.age, name: @user\.name, organization_id: @user\.organization_id, organization_type: @user\.organization_type \} \}/, content)
      assert_match(/patch user_url\(@user\), params: \{ user: \{ age: @user\.age, name: @user\.name, organization_id: @user\.organization_id, organization_type: @user\.organization_type \} \}/, content)
    end
  end

  def test_functional_tests_without_attributes
    run_generator ["User"]

    assert_file "test/controllers/users_controller_test.rb" do |content|
      assert_match(/class UsersControllerTest < ActionDispatch::IntegrationTest/, content)
      assert_match(/test "should get index"/, content)
      assert_match(/post users_url, params: \{ user: \{\} \}/, content)
      assert_match(/patch user_url\(@user\), params: \{ user: \{\} \}/, content)
    end
  end

  def test_skip_helper_if_required
    run_generator ["User", "name:string", "age:integer", "--no-helper"]
    assert_no_file "app/helpers/users_helper.rb"
  end

  def test_skip_layout_if_required
    run_generator ["User", "name:string", "age:integer", "--no-layout"]
    assert_no_file "app/views/layouts/users.html.erb"
  end

  def test_default_orm_is_used
    run_generator ["User", "--orm=unknown"]

    assert_file "app/controllers/users_controller.rb" do |content|
      assert_match(/class UsersController < ApplicationController/, content)

      assert_instance_method :index, content do |m|
        assert_match(/@users = User\.all/, m)
      end
    end
  end

  def test_customized_orm_is_used
    klass = Class.new(Rails::Generators::ActiveModel) do
      def self.all(klass)
        "#{klass}.find(:all)"
      end
    end

    Unknown::Generators.const_set(:ActiveModel, klass)
    run_generator ["User", "--orm=unknown"]

    assert_file "app/controllers/users_controller.rb" do |content|
      assert_match(/class UsersController < ApplicationController/, content)

      assert_instance_method :index, content do |m|
        assert_match(/@users = User\.find\(:all\)/, m)
        assert_no_match(/@users = User\.all/, m)
      end
    end
  ensure
    Unknown::Generators.send :remove_const, :ActiveModel
  end

  def test_model_name_option
    run_generator ["Admin::User", "--model-name=User"]
    assert_file "app/controllers/admin/users_controller.rb" do |content|
      assert_match "# GET /admin/users", content
      assert_instance_method :index, content do |m|
        assert_match("@users = User.all", m)
      end

      assert_match "# POST /admin/users", content
      assert_instance_method :create, content do |m|
        assert_match("redirect_to [:admin, @user]", m)
      end

      assert_match "# PATCH/PUT /admin/users/1", content
      assert_instance_method :update, content do |m|
        assert_match("redirect_to [:admin, @user]", m)
      end
    end

    assert_file "app/views/admin/users/index.html.erb" do |content|
      assert_match %{@users.each do |user|}, content
      assert_match %{render user}, content
      assert_match %{"Show this user", [:admin, user]}, content
      assert_match %{"New user", new_admin_user_path}, content
    end

    assert_file "app/views/admin/users/show.html.erb" do |content|
      assert_match %{render @user}, content
      assert_match %{"Edit this user", edit_admin_user_path(@user)}, content
      assert_match %{"Back to users", admin_users_path}, content
      assert_match %{"Destroy this user", [:admin, @user]}, content
    end

    assert_file "app/views/admin/users/_user.html.erb" do |content|
      assert_match "user", content
      assert_no_match "admin_user", content
    end

    assert_file "app/views/admin/users/new.html.erb" do |content|
      assert_match %{render "form", user: @user}, content
      assert_match %{"Back to users", admin_users_path}, content
    end

    assert_file "app/views/admin/users/edit.html.erb" do |content|
      assert_match %{render "form", user: @user}, content
      assert_match %{"Show this user", [:admin, @user]}, content
      assert_match %{"Back to users", admin_users_path}, content
    end

    assert_file "app/views/admin/users/_form.html.erb" do |content|
      assert_match %{model: [:admin, user]}, content
    end

    assert_file "test/controllers/admin/users_controller_test.rb" do |content|
      assert_match " admin_users_url", content
      assert_match " new_admin_user_url", content
      assert_match " edit_admin_user_url", content
      assert_match " admin_user_url(@user)", content
      assert_no_match %r/\b(new_|edit_)?users?_(path|url)/, content
    end

    assert_file "test/system/users_test.rb"
  end

  def test_controller_tests_pass_by_default_inside_mountable_engine
    engine_path = File.join(destination_root, "bukkits")

    with_new_plugin(engine_path, "--mountable") do
      quietly { `bin/rails g controller dashboard foo` }
      quietly { `bin/rails db:migrate RAILS_ENV=test` }
      assert_match(/2 runs, 2 assertions, 0 failures, 0 errors/, `bin/rails test 2>&1`)
    end
  end

  def test_controller_tests_pass_by_default_inside_full_engine
    engine_path = File.join(destination_root, "bukkits")

    with_new_plugin(engine_path, "--full") do
      quietly { `bin/rails g controller dashboard foo` }
      quietly { `bin/rails db:migrate RAILS_ENV=test` }
      assert_match(/2 runs, 2 assertions, 0 failures, 0 errors/, `bin/rails test 2>&1`)
    end
  end

  def test_api_only_generates_a_proper_api_controller
    run_generator ["User", "--api"]

    assert_file "app/controllers/users_controller.rb" do |content|
      assert_match(/class UsersController < ApplicationController/, content)
      assert_no_match(/respond_to/, content)

      assert_match(/before_action :set_user, only: %i\[ show update destroy \]/, content)

      assert_instance_method :index, content do |m|
        assert_match(/@users = User\.all/, m)
        assert_match(/render json: @users/, m)
      end

      assert_instance_method :show, content do |m|
        assert_match(/render json: @user/, m)
      end

      assert_instance_method :create, content do |m|
        assert_match(/@user = User\.new\(user_params\)/, m)
        assert_match(/@user\.save/, m)
        assert_match(/@user\.errors/, m)
      end

      assert_instance_method :update, content do |m|
        assert_match(/@user\.update\(user_params\)/, m)
        assert_match(/@user\.errors/, m)
      end

      assert_instance_method :destroy, content do |m|
        assert_match(/@user\.destroy/, m)
      end
    end

    assert_no_file "app/views/users/index.html.erb"
    assert_no_file "app/views/users/edit.html.erb"
    assert_no_file "app/views/users/show.html.erb"
    assert_no_file "app/views/users/new.html.erb"
    assert_no_file "app/views/users/_form.html.erb"
  end

  def test_api_controller_tests
    run_generator ["User", "name:string", "age:integer", "organization:references{polymorphic}", "--api"]

    assert_file "test/controllers/users_controller_test.rb" do |content|
      assert_match(/class UsersControllerTest < ActionDispatch::IntegrationTest/, content)
      assert_match(/test "should get index"/, content)
      assert_match(/post users_url, params: \{ user: \{ age: @user\.age, name: @user\.name, organization_id: @user\.organization_id, organization_type: @user\.organization_type \} \}, as: :json/, content)
      assert_match(/patch user_url\(@user\), params: \{ user: \{ age: @user\.age, name: @user\.name, organization_id: @user\.organization_id, organization_type: @user\.organization_type \} \}, as: :json/, content)
      assert_no_match(/assert_redirected_to/, content)
    end
  end

  def test_api_only_generates_params_for_attachments
    run_generator ["Message", "video:attachment", "photos:attachments", "--api"]

    assert_file "app/controllers/messages_controller.rb" do |content|
      assert_match(/def message_params/, content)
      assert_match(/params\.require\(:message\)\.permit\(:video, photos: \[\]\)/, content)
    end
  end

  def test_check_class_collision
    Object.const_set :UsersController, Class.new
    content = capture(:stderr) { run_generator }
    assert_match(/The name 'UsersController' is either already used in your application or reserved/, content)
  ensure
    Object.send :remove_const, :UsersController
  end
end