action_args 2.3.0 → 2.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,44 +0,0 @@
1
- # frozen_string_literal: true
2
- require 'test_helper'
3
-
4
- class BooksControllerTest < ActionController::TestCase
5
- sub_test_case 'GET index (having an optional parameter)' do
6
- setup do
7
- @books = []
8
- Book.delete_all
9
- 100.times {|i| @books << Book.create!(title: 'book'+i.to_s) }
10
- end
11
-
12
- test 'without page parameter' do
13
- get :index
14
- assert 200, response.code
15
- assert_equal @books[0..9], assigns(:books)
16
- end
17
-
18
- test 'with page parameter' do
19
- get :index, params: {page: 3}
20
- assert 200, response.code
21
- assert_equal @books[20..29], assigns(:books)
22
- end
23
-
24
- test 'first param is nil and second is not nil' do
25
- rhg = Book.create! title: 'RHG'
26
- Book.create! title: 'AWDwR'
27
- get :index, params: {q: 'RH'}
28
- assert_equal [rhg], assigns(:books)
29
- end
30
- end
31
-
32
- test 'GET show' do
33
- rhg = Book.create! title: 'RHG'
34
- get :show, params: {id: rhg.id}
35
- assert_equal rhg, assigns(:book)
36
- end
37
-
38
- test 'POST create' do
39
- Book.create! title: 'RHG'
40
- books_count_was = Book.count
41
- post :create, params: {book: {title: 'AWDwR', price: 24}}
42
- assert_equal 1, Book.count - books_count_was
43
- end
44
- end
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
- require 'test_helper'
3
-
4
- class BooksControllerTest < ActionController::TestCase
5
- setup do
6
- Book.delete_all
7
- @book = Book.create! title: 'Head First ActionArgs'
8
- get :show, params: {id: @book.id}
9
- end
10
-
11
- sub_test_case 'before_action' do
12
- test 'via Symbol' do
13
- assert_equal @book, assigns(:book)
14
- end
15
-
16
- if Rails.version < '5.1'
17
- test 'via String' do
18
- assert assigns(:string_filter_executed)
19
- end
20
- end
21
-
22
- test 'via Proc' do
23
- assert assigns(:proc_filter_executed)
24
- end
25
- end
26
-
27
- test 'around_action' do
28
- assert_not_nil assigns(:elapsed_time)
29
- end
30
- end
@@ -1,31 +0,0 @@
1
- # frozen_string_literal: true
2
- require 'test_helper'
3
-
4
- class KwBooksControllerTest < ActionController::TestCase
5
- sub_test_case 'GET index (having an optional parameter)' do
6
- test 'without giving any kw parameter (not even giving :required one)' do
7
- assert_raises(ActionController::BadRequest) { get :index }
8
- end
9
-
10
- test 'without giving any optional kw parameter' do
11
- get :index, params: {author_name: 'nari'}
12
- assert 200, response.code
13
- end
14
-
15
- test 'with kw parameter defaults to non-nil value' do
16
- get :index, params: {author_name: 'nari', page: 3}
17
- body = eval response.body
18
- assert_equal 'nari', body[:author_name]
19
- assert_equal '3', body[:page]
20
- assert_nil body[:q]
21
- end
22
-
23
- test 'with kw parameter defaults to nil' do
24
- get :index, params: {author_name: 'nari', q: 'Rails'}
25
- body = eval response.body
26
- assert_equal 'nari', body[:author_name]
27
- assert_equal '1', body[:page]
28
- assert_equal 'Rails', body[:q]
29
- end
30
- end
31
- end
@@ -1,31 +0,0 @@
1
- # frozen_string_literal: true
2
- require 'test_helper'
3
-
4
- class KwKeyreqBooksControllerTest < ActionController::TestCase
5
- sub_test_case 'GET index (having an optional parameter)' do
6
- test 'without giving any kw parameter (not even giving :required one)' do
7
- assert_raises(ActionController::BadRequest) { get :index }
8
- end
9
-
10
- test 'without giving any kw parameter' do
11
- get :index, params: {author_name: 'nari'}
12
- assert 200, response.code
13
- end
14
-
15
- test 'with kw parameter defaults to non-nil value' do
16
- get :index, params: {author_name: 'nari', page: 3}
17
- body = eval response.body
18
- assert_equal 'nari', body[:author_name]
19
- assert_equal '3', body[:page]
20
- assert_nil body[:q]
21
- end
22
-
23
- test 'with kw parameter defaults to nil' do
24
- get :index, params: {author_name: 'nari', q: 'Rails'}
25
- body = eval response.body
26
- assert_equal 'nari', body[:author_name]
27
- assert_equal '1', body[:page]
28
- assert_equal 'Rails', body[:q]
29
- end
30
- end
31
- end if RUBY_VERSION >= '2.1'
@@ -1,10 +0,0 @@
1
- # frozen_string_literal: true
2
- require 'test_helper'
3
-
4
- class AuthorsControllerTest < ActionController::TestCase
5
- test 'GET show' do
6
- matz = Author.create! name: 'Matz'
7
- get :show, params: {id: matz.id}
8
- assert_equal matz, assigns(:author)
9
- end
10
- end
@@ -1,58 +0,0 @@
1
- # frozen_string_literal: true
2
- require 'test_helper'
3
-
4
- class StoresControllerTest < ActionController::TestCase
5
- test 'GET show' do
6
- tatsu_zine = Store.create! name: 'Tatsu-zine'
7
- get :show, params: {id: tatsu_zine.id}
8
-
9
- assert_equal tatsu_zine, assigns(:store)
10
- end
11
-
12
- sub_test_case 'GET new' do
13
- test 'without store parameter' do
14
- get :new
15
- assert 200, response.code
16
- assert_equal 'PragProg', assigns(:store).name
17
- end
18
- test 'with store parameter' do
19
- get :new, params: {store: {name: 'Tatsu-zine'}}
20
- assert 200, response.code
21
- assert_equal 'Tatsu-zine', assigns(:store).name
22
- end
23
- end
24
-
25
- test 'POST create' do
26
- store_count_was = Store.count
27
- post :create, params: {store: {name: 'Tatsu-zine', url: 'http://tatsu-zine.com'}}
28
-
29
- assert_equal 1, Store.count - store_count_was
30
- end
31
- end
32
-
33
- class MoviesControllerTest < ActionController::TestCase
34
- test 'POST create' do
35
- movie_count_was = Movie.count
36
- post :create, params: {movie: {title: 'Dr. No', actors_attributes: [{name: 'Bernard Lee'}]}}
37
-
38
- assert_equal 1, Movie.count - movie_count_was
39
- end
40
- end
41
-
42
- # this controller doesn't permit price of new book do
43
- class Admin::BooksControllerTest < ActionController::TestCase
44
- test 'POST create' do
45
- post :create, params: {book: {title: 'naruhoUnix', price: 30}}
46
-
47
- assert_nil Book.last.price
48
- end
49
- end
50
-
51
- class Admin::AccountsControllerTest < ActionController::TestCase
52
- test 'POST create' do
53
- admin_account_count_was = Admin::Account.count
54
- post :create, params: {admin_account: {name: 'amatsuda'}}
55
-
56
- assert_equal 1, Admin::Account.count - admin_account_count_was
57
- end
58
- end
data/test/fake_app.rb DELETED
@@ -1,184 +0,0 @@
1
- # coding: utf-8
2
- # frozen_string_literal: true
3
-
4
- # config
5
- ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
6
-
7
- module ActionArgsTestApp
8
- class Application < Rails::Application
9
- config.secret_key_base = config.secret_token = [*'A'..'z'].join
10
- config.session_store :cookie_store, :key => '_myapp_session'
11
- config.active_support.deprecation = :log
12
- config.eager_load = false
13
- end
14
- end
15
- ActionArgsTestApp::Application.initialize!
16
-
17
- # routes
18
- ActionArgsTestApp::Application.routes.draw do
19
- resources :authors
20
- resources :books
21
- resources :kw_books # 2.0+ only
22
- resources :kw_keyreq_books # 2.1+ only
23
- resources :stores
24
- resources :movies
25
-
26
- namespace :admin do
27
- resources :accounts
28
- resources :books
29
- end
30
- end
31
-
32
- # models
33
- class Author < ActiveRecord::Base
34
- end
35
- class Book < ActiveRecord::Base
36
- end
37
- class Store < ActiveRecord::Base
38
- end
39
- class Movie < ActiveRecord::Base
40
- has_many :actors
41
- accepts_nested_attributes_for :actors
42
- end
43
- class Actor < ActiveRecord::Base
44
- belongs_to :movie
45
- end
46
- module Admin
47
- def self.table_name_prefix() 'admin_' end
48
- class Account < ActiveRecord::Base
49
- end
50
- end
51
-
52
- # mailers
53
- require "action_mailer/railtie"
54
- class UserMailer < ActionMailer::Base
55
- def send_email_without_args
56
- mail(
57
- to: 'to@example.com',
58
- from: 'from@example.com',
59
- subject: 'Action Args!!!',
60
- body: 'test'
61
- )
62
- end
63
- end
64
-
65
- # helpers
66
- module ApplicationHelper; end
67
-
68
- # controllers
69
- class ApplicationController < ActionController::Base
70
- end
71
- class AuthorsController < ApplicationController
72
- def show
73
- @author = Author.find params[:id]
74
- render plain: @author.name
75
- end
76
- end
77
- class BooksController < ApplicationController
78
- before_action :set_book, only: :show
79
- before_action -> { @proc_filter_executed = true }, only: :show
80
- if Rails.version < '5.1'
81
- before_action '@string_filter_executed = true', only: :show
82
- end
83
- around_action :benchmark_action
84
- before_action :omg
85
- skip_before_action :omg
86
-
87
- # optional parameter
88
- def index(page = 1, q = nil, limit = 10)
89
- @books = Book.limit(limit.to_i).offset(([page.to_i - 1, 0].max) * 10)
90
- @books = @books.where('title like ?', "%#{q}%") unless q.blank?
91
- render plain: 'index', books: @books
92
- end
93
-
94
- def show(id)
95
- render plain: @book.title
96
- end
97
-
98
- def create(book)
99
- book = book.permit :title, :price
100
- @book = Book.create! book
101
- render plain: @book.title
102
- end
103
-
104
- private
105
- def set_book(id)
106
- @book = Book.find(id)
107
- end
108
-
109
- def benchmark_action
110
- start = Time.now
111
- yield
112
- @elapsed_time = Time.now - start
113
- end
114
-
115
- def omg
116
- raise '💣'
117
- end
118
- end
119
- class MoviesController < ApplicationController
120
- permits :title, actors_attributes: [:name]
121
-
122
- def create(movie)
123
- @movie = Movie.create! movie
124
- render plain: @movie.title
125
- end
126
- end
127
- class StoresController < ApplicationController
128
- permits :name, :url
129
-
130
- def show(id)
131
- @store = Store.find(id)
132
- render plain: @store.name
133
- end
134
-
135
- def new(store = {name: 'PragProg'})
136
- @store = Store.new store
137
- render plain: @store.name
138
- end
139
-
140
- def create(store)
141
- @store = Store.create! store
142
- render plain: @store.name
143
- end
144
- end
145
- module Admin
146
- class AccountsController < ::ApplicationController
147
- permits :name, model_name: 'Admin::Account'
148
-
149
- def create(admin_account)
150
- @admin_account = Admin::Account.create! admin_account
151
- render plain: @admin_account.name
152
- end
153
- end
154
-
155
- class BooksController < ::ApplicationController
156
- permits :title
157
-
158
- def create(book)
159
- @book = Book.create! book
160
- render plain: @book.title
161
- end
162
- end
163
- end
164
-
165
- require_relative 'kwargs_controllers'
166
- require_relative 'kwargs_keyreq_controllers' if RUBY_VERSION >= '2.1'
167
-
168
- # migrations
169
- class CreateAllTables < ActiveRecord::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[5.0] : ActiveRecord::Migration
170
- def self.up
171
- create_table(:authors) {|t| t.string :name}
172
- create_table(:books) {|t| t.string :title; t.integer :price}
173
- create_table(:stores) {|t| t.string :name; t.string :url}
174
- create_table(:admin_accounts) {|t| t.string :name}
175
- create_table(:movies) {|t| t.string :title}
176
- create_table(:actors) {|t| t.string :name; t.references :movie}
177
- end
178
- end
179
-
180
- if ActiveRecord::Base.connection.respond_to? :data_source_exists?
181
- CreateAllTables.up unless ActiveRecord::Base.connection.data_source_exists? 'authors'
182
- else
183
- CreateAllTables.up unless ActiveRecord::Base.connection.table_exists? 'authors'
184
- end
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
- # this file should not be loaded from Ruby <2.0
3
-
4
- class KwBooksController < ApplicationController
5
- # keyword arguments
6
- def index(author_name, page: '1', q: nil)
7
- render plain: {author_name: author_name, page: page, q: q}.inspect
8
- end
9
- end
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
- # this file should not be loaded from Ruby <2.1
3
-
4
- class KwKeyreqBooksController < ApplicationController
5
- # keyword arguments
6
- def index(author_name:, page: '1', q: nil)
7
- render plain: {author_name: author_name, page: page, q: q}.inspect
8
- end
9
- end
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
- require 'test_helper'
3
-
4
- class UserMailerTest < ActionMailer::TestCase
5
- test '#send_email_without_args' do
6
- #it should not raise NameError: undefined local variable or method `params' for ...
7
- assert UserMailer.send_email_without_args
8
- end
9
- end
@@ -1,193 +0,0 @@
1
- # frozen_string_literal: true
2
- require 'test_helper'
3
- using ActionArgs::ParamsHandler
4
-
5
- class ActionArgs::ParamsHandlerTest < ActiveSupport::TestCase
6
- sub_test_case 'extract_method_arguments_from_params' do
7
- setup do
8
- params = {a: '1', b: '2'}
9
- @controller = Class.new(ApplicationController).new.tap {|c| c.params = params }
10
- end
11
- test 'no parameters' do
12
- def @controller.m() end
13
-
14
- assert_equal [], @controller.extract_method_arguments_from_params(:m)
15
- end
16
-
17
- test '1 req' do
18
- def @controller.m(a) end
19
-
20
- assert_equal ['1'], @controller.extract_method_arguments_from_params(:m)
21
- end
22
-
23
- test '2 reqs' do
24
- def @controller.m(a, b) end
25
-
26
- assert_equal ['1', '2'], @controller.extract_method_arguments_from_params(:m)
27
- end
28
-
29
- test '1 opt with value' do
30
- def @controller.m(a = 'a') end
31
-
32
- assert_equal ['1'], @controller.extract_method_arguments_from_params(:m)
33
- end
34
-
35
- test '1 opt without value' do
36
- def @controller.m(x = 'x') end
37
-
38
- assert_equal [], @controller.extract_method_arguments_from_params(:m)
39
- end
40
-
41
- test 'req, opt with value' do
42
- def @controller.m(a, b = 'b') end
43
-
44
- assert_equal ['1', '2'], @controller.extract_method_arguments_from_params(:m)
45
- end
46
-
47
- test 'req, opt without value' do
48
- def @controller.m(a, x = 'x') end
49
-
50
- assert_equal ['1'], @controller.extract_method_arguments_from_params(:m)
51
- end
52
-
53
- test 'opt with value, opt with value' do
54
- def @controller.m(a = 'a', b = 'b') end
55
-
56
- assert_equal ['1', '2'], @controller.extract_method_arguments_from_params(:m)
57
- end
58
-
59
- test 'opt with value, opt without value' do
60
- def @controller.m(a = 'a', x = 'x') end
61
-
62
- assert_equal ['1'], @controller.extract_method_arguments_from_params(:m)
63
- end
64
-
65
- test 'opt without value, opt with value' do
66
- def @controller.m(x = 'x', a = 'a') end
67
-
68
- assert_equal [nil, '1'], @controller.extract_method_arguments_from_params(:m)
69
- end
70
-
71
- test 'opt without value, opt without value' do
72
- def @controller.m(x = 'x', y = 'y') end
73
-
74
- assert_equal [], @controller.extract_method_arguments_from_params(:m)
75
- end
76
-
77
- test 'opt with value, req' do
78
- def @controller.m(a = 'a', b) end
79
-
80
- assert_equal ['1', '2'], @controller.extract_method_arguments_from_params(:m)
81
- end
82
-
83
- test 'opt without value, req' do
84
- def @controller.m(x = 'x', a) end
85
-
86
- assert_equal ['1'], @controller.extract_method_arguments_from_params(:m)
87
- end
88
-
89
- test 'opt without value, opt with value, req' do
90
- def @controller.m(x = 'x', b = 'b', a) end
91
-
92
- assert_equal [nil, '2', '1'], @controller.extract_method_arguments_from_params(:m)
93
- end
94
-
95
- test 'opt with value, opt without value, req' do
96
- def @controller.m(b = 'b', x = 'x', a) end
97
-
98
- assert_equal ['2', '1'], @controller.extract_method_arguments_from_params(:m)
99
- end
100
-
101
- test 'req without a value' do
102
- def @controller.m(x) end
103
-
104
- assert_raises(ActionController::BadRequest) { @controller.extract_method_arguments_from_params(:m) }
105
- end
106
-
107
- test 'key' do
108
- def @controller.m(a: nil) end
109
-
110
- assert_equal [a: '1'], @controller.extract_method_arguments_from_params(:m)
111
- end
112
-
113
- test 'key, key without value' do
114
- def @controller.m(a: nil, x: 'x') end
115
-
116
- assert_equal [a: '1'], @controller.extract_method_arguments_from_params(:m)
117
- end
118
-
119
- if RUBY_VERSION >= '2.1'
120
- eval <<-KWARGS_KEYREQ_TEST
121
- test 'keyreq' do
122
- def @controller.m(a:) end
123
-
124
- assert_equal [a: '1'], @controller.extract_method_arguments_from_params(:m)
125
- end
126
-
127
- test 'keyreq, keyreq without value' do
128
- def @controller.m(a:, x:) end
129
-
130
- assert_raises(ActionController::BadRequest) { @controller.extract_method_arguments_from_params(:m) }
131
- end
132
- KWARGS_KEYREQ_TEST
133
- end
134
- end
135
-
136
- sub_test_case 'strengthen_params!' do
137
- setup do
138
- @params = ActionController::Parameters.new(x: '1', y: '2', foo: {a: 'a', b: 'b'}, bar: {a: 'a', b: 'b'}, baz: {a: 'a', b: 'b'}, hoge: {a: 'a', b: 'b'}, fuga: {a: 'a', b: 'b'})
139
- end
140
-
141
- def execute_strengthen_params!(controller)
142
- c = controller.new
143
- c.instance_variable_set :@_params, @params
144
- c.strengthen_params! :a
145
- end
146
-
147
- test 'requiring via :req, permitting all scalars' do
148
- execute_strengthen_params! FooController ||= Class.new(ApplicationController) { permits :a, :b; def a(foo) end }
149
-
150
- assert @params[:foo].permitted?
151
- assert_not_nil @params[:foo][:a]
152
- assert_not_nil @params[:foo][:b]
153
- end
154
-
155
- test 'requiring via :req, not permitting all scalars' do
156
- execute_strengthen_params! BarController ||= Class.new(ApplicationController) { permits :a; def a(bar, x = 'x') end }
157
-
158
- assert @params[:bar].permitted?
159
- assert_not_nil @params[:bar][:a]
160
- assert_nil @params[:bar][:b]
161
- end
162
-
163
- test 'requiring via :req, not permitting any scalars' do
164
- execute_strengthen_params! BazController ||= Class.new(ApplicationController) { def a(baz, aho = 'omg') end }
165
-
166
- refute @params[:baz].permitted?
167
- end
168
-
169
- test 'requiring via :opt, permitting all scalars' do
170
- execute_strengthen_params! HogeController ||= Class.new(ApplicationController) { permits :a, :b; def a(hoge = {}) end }
171
-
172
- assert @params[:hoge].permitted?
173
- assert_not_nil @params[:hoge][:a]
174
- assert_not_nil @params[:hoge][:b]
175
- end
176
-
177
- test 'requiring via :key, permitting all scalars' do
178
- execute_strengthen_params! FugaController ||= Class.new(ApplicationController) { permits :a, :b; def a(fuga: {}) end }
179
-
180
- assert @params[:fuga].permitted?
181
- assert_not_nil @params[:fuga][:a]
182
- assert_not_nil @params[:fuga][:b]
183
- end
184
-
185
- test '"model_name" option' do
186
- execute_strengthen_params! PiyoController ||= Class.new(ApplicationController) { permits :a, :b, model_name: 'Foo'; def a(foo) end }
187
-
188
- assert @params[:foo].permitted?
189
- assert_not_nil @params[:foo][:a]
190
- assert_not_nil @params[:foo][:b]
191
- end
192
- end
193
- end