Commit Graph

1499 Commits

Author SHA1 Message Date
yuuji.yaginuma
70b5a7594e Add test for config.active_storage.routes_prefix
Follow up #33883.
2018-09-15 10:22:04 +09:00
Rafael Mendonça França
a8359d837c
Add a test that exercice better the behavior we expect in the query cache
In production the query cache was already being loaded before the first
request even without #33856, so added a test to make sure of it.

This new test is passing even if #33856 is reverted.
2018-09-12 20:57:52 -04:00
bogdanvlviv
d1a14865e0
Build string set when filter_attributes is assigned
It would allow `filter_attributes` to be reused across multiple
calls to `#inspect` or `#pretty_print`.

- Add `require "set"`
- Remove `filter_attributes` instance reader. I think there is no need
to keep it.
2018-09-12 18:51:55 +03:00
Eileen Uchitelle
349db176d8 Fix query cache to load before first request
In a test app we observed that the query cache was not enabled on the
first request. This was because the query cache hooks are installed on
load and active record is loaded in the middle of the first request.

If we remove the `on_load` from the railtie the query cache hooks will
be installed before the first request, allowing the cache to be enabled
on that first request.

This is ok because query cache doesn't load anything else, only itself
so we're not eager loading all of active record before the first
request, just the query cache hooks.

[Eileen M. Uchitelle & Matthew Draper]
2018-09-12 10:09:59 -04:00
Rafael França
21b32bb2a8
Merge pull request #33815 from mberlanda/mberlanda/enhance-config-for
Use ActiveSupport::InheritableOptions and deep_symbolize_keys in config_for
2018-09-11 18:26:59 -04:00
Mauro Berlanda
b167d521ab
refacto: config_for with ActiveSupport::InheritableOptions and symbolized keys 2018-09-11 23:47:41 +02:00
yuuji.yaginuma
5b0b1ee8fd Use correct variable
Follow up of 3e81490717a314437f9123d86fa3e9dc55558e95.
2018-09-11 07:06:30 +09:00
Rafael Mendonça França
3e81490717
Remove all references to slave in the codebase 2018-09-10 16:31:32 -04:00
Zhang Kang
180dcd1bfa Configuration item config.filter_parameters could also filter out sensitive value of database column when call #inspect
* Why
Some sensitive data will be exposed in log accidentally by calling `#inspect`, e.g.

```ruby
@account = Account.find params[:id]
payload = { account: @account }
logger.info "payload will be #{ payload }"
```

All the information of `@account` will be exposed in log.

* Solution
Add a class attribute filter_attributes to specify which values of columns shouldn't be exposed.
This attribute equals to `Rails.application.config.filter_parameters` by default.

```ruby
Rails.application.config.filter_parameters += [:credit_card_number]
Account.last.insepct # => #<Account id: 123, credit_card_number: [FILTERED] ...>
```
2018-09-07 09:52:13 +08:00
yuuji.yaginuma
fb3642b0ca Respect config setting when output deprecation notice in rake tasks
The rake tasks which became deprecate now does not load the environment.
Therefore, even if the application specifies the behavior of deprecating,
the message is output to stderr ignoring the specification.

It seems that this is not the expected behavior.
We should respect the setting even in the rake tasks.
2018-09-04 20:06:54 +09:00
Eileen Uchitelle
a572d2283b Convert configs_for to kwargs, add include_replicas
Changes the `configs_for` method from using traditional arguments to
using kwargs. This is so I can add the `include_replicas` kwarg without
having to always include `env_name` and `spec_name` in the method call.

`include_replicas` defaults to false because everywhere internally in
Rails we don't want replicas. `configs_for` is for iterating over
configurations to create / run rake tasks, so we really don't ever need
replicas in that case.
2018-08-31 16:07:09 -04:00
Eileen Uchitelle
fdf3f0b930 Refactors Active Record connection management
While the three-tier config makes it easier to define databases for
multiple database applications, it quickly became clear to offer full
support for multiple databases we need to change the way the connections
hash was handled.

A three-tier config means that when Rails needed to choose a default
configuration (in the case a user doesn't ask for a specific
configuration) it wasn't clear to Rails which the default was. I
[bandaid fixed this so the rake tasks could work](#32271) but that fix
wasn't correct because it actually doubled up the configuration hashes.

Instead of attemping to manipulate the hashes @tenderlove and I decided
that it made more sense if we converted the hashes to objects so we can
easily ask those object questions. In a three tier config like this:

```
development:
  primary:
    database: "my_primary_db"
  animals:
    database; "my_animals_db"
```

We end up with an object like this:

```
  @configurations=[
    #<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007fd1acbded10
      @env_name="development",@spec_name="primary",
      @config={"adapter"=>"sqlite3", "database"=>"db/development.sqlite3"}>,
    #<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007fd1acbdea90
      @env_name="development",@spec_name="animals",
      @config={"adapter"=>"sqlite3", "database"=>"db/development.sqlite3"}>
]>
```

The configurations setter takes the database configuration set by your
application and turns them into an
`ActiveRecord::DatabaseConfigurations` object that has one getter -
`@configurations` which is an array of all the database objects.

The configurations getter returns this object by default since it acts
like a hash in most of the cases we need. For example if you need to
access the default `development` database we can simply request it as we
did before:

```
ActiveRecord::Base.configurations["development"]
```

This will return primary development database configuration hash:

```
{ "database" => "my_primary_db" }
```

Internally all of Active Record has been converted to use the new
objects. I've built this to be backwards compatible but allow for
accessing the hash if needed for a deprecation period. To get the
original hash instead of the object you can either add `to_h` on the
configurations call or pass `legacy: true` to `configurations.

```
ActiveRecord::Base.configurations.to_h
=> { "development => { "database" => "my_primary_db" } }

ActiveRecord::Base.configurations(legacy: true)
=> { "development => { "database" => "my_primary_db" } }
```

The new configurations object allows us to iterate over the Active
Record configurations without losing the known environment or
specification name for that configuration. You can also select all the
configs for an env or env and spec. With this we can always ask
any object what environment it belongs to:

```
db_configs = ActiveRecord::Base.configurations.configurations_for("development")
=> #<ActiveRecord::DatabaseConfigurations:0x00007fd1acbdf800
  @configurations=[
    #<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007fd1acbded10
      @env_name="development",@spec_name="primary",
      @config={"adapter"=>"sqlite3", "database"=>"db/development.sqlite3"}>,
    #<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007fd1acbdea90
      @env_name="development",@spec_name="animals",
      @config={"adapter"=>"sqlite3", "database"=>"db/development.sqlite3"}>
]>

db_config.env_name
=> "development"

db_config.spec_name
=> "primary"

db_config.config
=> { "adapter"=>"sqlite3", "database"=>"db/development.sqlite3" }
```

The configurations object is more flexible than the configurations hash
and will allow us to build on top of the connection management in order
to add support for primary/replica connections, sharding, and
constructing queries for associations that live in multiple databases.
2018-08-30 10:06:45 -04:00
Ryuta Kamizono
cc81cd359c
Merge pull request #33744 from bogdanvlviv/fixes-27852
Prevent leaking of user's DB credentials on `rails db:create` failure
2018-08-30 02:44:58 +09:00
Eileen Uchitelle
a64dba1dc5 Remove this conditional
I removed the argument so I should remove the conditional too.
2018-08-29 12:08:23 -04:00
Eileen Uchitelle
c91f1482a1 Remove unused argument
The test that used this was updated and it's no longer needed.
2018-08-29 11:08:10 -04:00
Eileen Uchitelle
6b5df90fb5 Drop load_database_yaml and fix test
We originally did the whole `load_database_yaml` thing because this test
wasn't cooperating and we needed to finish the namespaced rake tasks for
multiple databases.

However, it turns out that YAML can't eval ERB if you don't tell it it's
ERB so you get Pysch parse errors if you're using multi-line ERB or
ERB with conditionals. It's a hot mess.

After trying a few things and thinking it over we decided that it wasn't
worth bandaiding over, the test needed to be improved. The test was
added in #31135 to test that the env is loaded in these tasks. But it
was blowing up because we were trying to read a database name out of the
configuration - however that's not the purpose of this change. We want
to read environment files in the rake tasks, but not in the config
file.

In this PR we changed the test to test what the PR was actually fixing.
We've also deleted the `load_database_yaml` because it caused more
problems than it was worth. This should fix the issues described in
https://github.com/rails/rails/pull/32274#issuecomment-384161057. We
also had these problems at GitHub.

Co-authored-by: alimi <aibrahim2k2@gmail.com>
2018-08-29 10:26:44 -04:00
bogdanvlviv
9b455fe6f0
Prevent leaking of user's DB credentials on rails db:create failure
Issue #27852 reports that when `rails db:create` fails, it causes
leaking of user's DB credentials to $stderr.
We print a DB's configuration hash in order to help users more quickly
to figure out what could be wrong with his configuration.

This commit changes message from
"Couldn't create database for #{configuration.inspect}" to
"Couldn't create '#{configuration['database']}' database. Please check your configuration.".

There are two PRs that fixing it #27878, #27879, but they need a bit more work.
I decided help to finish this and added Author of those PRs credit in this commit.

Since it is a security issue, I think we should backport it to
`5-2-stable`, and `5-1-stable`.
Guided by https://edgeguides.rubyonrails.org/maintenance_policy.html#security-issues

Fixes #27852
Closes #27879
Related to #27878

[Alexander Marrs & bogdanvlviv]
2018-08-29 12:40:30 +03:00
yuuji.yaginuma
41ad613e4c Make rake routes deprecate before deleting
`rake routes` was a public task. Therefore, I think that we should deprecate
it before deleting it.

Related to #32121.
2018-08-20 08:47:29 +09:00
Annie-Claude Côté
0d3b5fc0f5 Update 'rake initializers' to use Rails::Command under the hood
* Invoke Rails::Command within the rake task
* Adds test for calling initializers with 'bin/rake'
* Adds deprecation warning to the rake task
2018-08-16 10:50:37 -04:00
Annie-Claude Côté
35a70f8422 Have bin:rake dev:cache use the Dev Rails Command under the hood
* Call the Rails::Command::DevCommand in the rake task for dev:cache
* Add deprecation for using `bin/rake` in favor of `bin/rails`
2018-08-13 11:27:01 -04:00
Assain
1cda4fb5df Purpose Metadata For Signed And Encrypted Cookies
Purpose metadata prevents cookie values from being
copy-pasted and ensures that the cookie is used only
for its originally intended purpose.

The Purpose and Expiry metadata are embedded inside signed/encrypted
cookies and will not be readable on previous versions of Rails.

We can switch off purpose and expiry metadata embedded in
signed and encrypted cookies using

	config.action_dispatch.use_cookies_with_metadata = false

if you want your cookies to be readable on older versions of Rails.
2018-08-12 21:50:35 +05:30
bogdanvlviv
d0edc9c7ae
Ensure that running tests in parallel doesn't display schema load output
https://github.com/rails/rails/pull/33479 changed `#load_schema` to
prevent displaying schema load on running tests in parallel.
We should test this in order to prevent any regression in the future.

Context https://github.com/rails/rails/pull/33479#discussion_r206870727
2018-08-08 16:20:38 +03:00
Atul Bhosale
7e40e9585a Log the remote IP addr of clients behind a proxy
[Atul Bhosale, Victor Nawothnig]
2018-07-31 20:45:37 +05:30
Matthew Draper
ec387c6dd9
Merge pull request #33229 from albertoalmagro/albertoalmagro/prefer-rails-command-over-bin-rails
Prefer rails command over bin/rails
2018-07-25 04:10:29 +09:30
bogdanvlviv
a6ebafc5ac
Clarify railties/test/application/rake/notes_test.rb
After 1996fbe2a3e46ff5698bfa3812afb7f42cdfa899 `rails notes`
isn't the same as `rake notes`.
Since that, we should test `rake routes` instead of `rails notes` in
`railties/test/application/rake/notes_test.rb` file.
So I changed all occurrences of `rails routes` to `rake routes` in that file,
and added assertions about deprecation warning of using `rake notes`.
It will help to figure out that we should remove
`railties/test/application/rake/notes_test.rb` entirely in favour of
`railties/test/commands/notes_test.rb` that was added
in 1996fbe2a3e46ff5698bfa3812afb7f42cdfa899.
2018-07-21 15:03:10 +03:00
yuuji.yaginuma
1a0bcc3b49 Fix deprecation message for SOURCE_ANNOTATION_DIRECTORIES
Also, added a test that a deprecated message will be output.
2018-07-20 13:19:10 +09:00
Alberto Almagro
a7986aeda0 Show rails instead of bin/rails on USAGE instructions
With this commit, rails commands usage instructions display now +rails+
instead of +bin/rails+ within their recommendations.
2018-07-06 22:46:54 +02:00
Alberto Almagro
40b209db53 Recommend use of rails over bin/rails
As discussed in #33203 rails command already looks for, and runs,
bin/rails if it is present.

We were mixing recommendations within guides and USAGE guidelines,
in some files we recommended using rails, in others bin/rails and
in some cases we even had both options mixed together.
2018-07-06 22:46:35 +02:00
bogdanvlviv
f1de019993
Include ActiveSupport::Testing::MethodCallAssertions in railties/test/isolation/abstract_unit.rb
Related to #33102
2018-06-09 00:00:28 +03:00
Ryuta Kamizono
a77447f4da Enable Lint/StringConversionInInterpolation rubocop rule
To prevent redundant `to_s` like https://github.com/rails/rails/pull/32923#discussion_r189460008
automatically in the future.
2018-05-21 21:10:14 +09:00
yuuji.yaginuma
ce4d467f7c Add test case that configure config.action_view.finalize_compiled_template_methods
Follow up of #32418.
2018-05-20 10:19:12 +09:00
Ryuta Kamizono
1dc17e7b2e Fix CustomCops/AssertNot to allow it to have failure message
Follow up of #32605.
2018-05-13 11:32:47 +09:00
Alberto Almagro
71a006df0e Fix typo on method name 2018-04-20 13:56:55 +02:00
Daniel Colson
a1ac18671a Replace assert ! with assert_not
This autocorrects the violations after adding a custom cop in
3305c78dcd.
2018-04-19 08:11:33 -04:00
utilum
74982a1be5 Fix test to allow IF NOT EXISTS in structure:dump
Before:

```
$ ruby -v
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]

$ ruby -w  -Itest -Ilib -I../activesupport/lib -I../actionpack/lib -I../actionview/lib -I../activemodel/lib test/application/rake/multi_dbs_test.rb
Run options: --seed 28744

F

Failure:
ApplicationTests::RakeTests::RakeMultiDbsTest#test_db:migrate_and_db:structure:dump_and_db:structure:load_works_on_all_databases [test/application/rake/multi_dbs_test.rb:70]:
Expected /CREATE TABLE \"books\"/ to match "CREATE TABLE IF NOT EXISTS \"schema_migrations\" (\"version\" varchar NOT NULL PRIMARY KEY);\nCREATE TABLE IF NOT EXISTS \"ar_internal_metadata\" (\"key\" varchar NOT NULL PRIMARY KEY, \"value\" varchar, \"created_at\" datetime NOT NULL, \"updated_at\" datetime NOT NULL);\nCREATE TABLE IF NOT EXISTS \"books\" (\"id\" integer PRIMARY KEY AUTOINCREMENT NOT NULL, \"title\" varchar, \"created_at\" datetime NOT NULL, \"updated_at\" datetime NOT NULL);\nCREATE TABLE sqlite_sequence(name,seq);\nINSERT INTO \"schema_migrations\" (version) VALUES\n('20180416201805');\n\n\n".
```
2018-04-16 22:19:07 +02:00
eileencodes
fa5a028ed9 Add multidb application test
I realized I wasn't really testing some of the new rake tasks added so I
built out this new test that uses a multi-db database.yml and allows us
to run create/drop/migrate/schema:dump/schema:load and those that are
namespaced like create:animals. This will make our testing more robust
so we can catch problems quicker and set a good place to add future
tests as these features evolve.
2018-04-09 13:08:05 -04:00
Rafael França
fe4e9d4c5d
Merge pull request #32441 from composerinteralia/refute-not
Add custom RuboCop for `assert_not` over `refute`
2018-04-04 18:19:35 -04:00
Daniel Colson
c1ceafc9d1 Autocorrect refute RuboCop violations
73e7aab behaved as expected on codeship, failing the build with
exactly these RuboCop violations. Hopefully `rubocop -a` will
have been enough to get a passing build!
2018-04-03 22:35:49 -04:00
Yoshiyuki Hirano
53f2241a95 Fix RenderingTest in railtie
Test class name is not `RoutingTest` but `RenderingTest`
2018-04-04 06:12:24 +09:00
Kasper Timm Hansen
0ec23effa7
Merge pull request #32065 from sikachu/move-SourceAnnotationExtractor-under-rails-namespec
Move SourceAnnotationExtractor under Rails module
2018-04-02 16:15:52 +02:00
Yoshiyuki Kinjo
7419a4f911 Deriving secret_key_base breaks key_generator defined in 5.1.
If one created Rails 5.1 app and then updated to 5.2,
`secret_key_base` defined in `config/secrets.yml` is ignored for
`development` and `test` environment.
A change in `secret_key_base` in turn breaks
`Rails.application.key_generator`.

If one encrypt data in Rails 5.1, she cannot decrypt it in Rails 5.2
for `development` and `test` environment.
2018-03-31 23:12:16 +09:00
Prem Sichanugrist
67cc450086 Move SourceAnnotationExtractor under Rails module
This class should be under Rails module as it belongs to Rails.
2018-03-22 17:06:14 +00:00
Andrew White
ae7a57209d Pass the skip_pipeline option in image_submit_tag
Fixes #32248.
2018-03-14 11:18:06 +00:00
Benoit Tigeot
c6d928f3ca Add --expanded option to "rails routes"
When using rails routes with small terminal or complicated routes it can be
very difficult to understand where is the element listed in header. psql
had the same issue, that's why they created "expanded mode" you can
switch using `\x` or by starting psql with
```
-x
--expanded

    Turn on the expanded table formatting mode. This is equivalent to the \x command.
```
The output is similar to one implemented here for rails routes:

db_user-# \du
List of roles
-[ RECORD 1 ]----------------------------------------------
Role name  | super
Attributes | Superuser, Create role, Create DB
Member of  | {}
-[ RECORD 2 ]----------------------------------------------
Role name  | role
Attributes | Superuser, Create role, Create DB, Replication
Member of  | {}
2018-02-28 22:32:34 +01:00
Rafael França
6bd33d66dd
Merge pull request #32121 from benoittgt/move-rails-routes-to-rails-command
Move rake routes task to rails command
2018-02-27 14:18:00 -05:00
Andrew White
96eeea538c Don't enforce UTF-8 by default
With the disabling of TLS 1.0 by most major websites, continuing to run
IE8 or lower becomes increasingly difficult so default to not enforcing
UTF-8 encoding as it's not relevant to other browsers.
2018-02-27 15:03:50 +00:00
Benoit Tigeot
a2748eda58 Move rake routes task to rails command
After a discussion with matthewd. It was mentioned that rake tasks need
to be moved to rails command.
See: https://github.com/rails/rails/issues/32117
2018-02-27 15:22:38 +01:00
Eugene Kenny
eb834811dc Use lazy load hook to configure ActiveStorage::Blob
`to_prepare` callbacks are run during initialization; using one here
meant that `ActiveStorage::Blob` would be loaded when the app boots,
which would in turn load `ActiveRecord::Base`.

By using a lazy load hook to configure `ActiveStorage::Blob` instead,
we can avoid loading `ActiveRecord::Base` unnecessarily.
2018-02-25 00:54:27 +00:00
Andrew White
d85283cc42 Remove trailing semi-colon from CSP
Although the spec[1] is defined in such a way that a trailing semi-colon
is valid it also doesn't allow a semi-colon by itself to indicate an
empty policy. Therefore it's easier (and valid) just to omit it rather
than to detect whether the policy is empty or not.

[1]: https://www.w3.org/TR/CSP2/#policy-syntax
2018-02-19 12:20:43 +00:00
Andrew White
57f9c36387 Don't accidentally create an empty CSP
Setting up the request environment was accidentally creating a CSP
as a consequence of accessing the option - only set the instance
variable if a block is passed.
2018-02-19 12:17:51 +00:00
Andrew White
52a1f1c226 Revert "Merge pull request #32045 from eagletmt/skip-csp-header"
This reverts commit 86f7c269073a3a9e6ddec9b957deaa2716f2627d, reversing
changes made to 5ece2e4a4459065b5efd976aebd209bbf0cab89b.

If a policy is set then we should generate it even if it's empty.
However what is happening is that we're accidentally generating an
empty policy when the initializer is commented out by default.
2018-02-19 12:00:29 +00:00
Kohei Suzuki
53d863d4bb
Skip generating empty CSP header when no policy is configured
`Rails.application.config.content_security_policy` is configured with no
policies by default. In this case, Content-Security-Policy header should
not be generated instead of generating the header with no directives.
Firefox also warns "Content Security Policy: Couldn't process unknown
directive ''".
2018-02-18 23:45:57 +09:00
Guillermo Iguaran
debe9a5cbe Multipart file uploads are very rare in API only apps so don't include Rack::TemfileReaper in default middleware stack for API only apps 2018-02-17 15:52:41 -05:00
Rafael França
21cc0432e4
Merge pull request #32002 from y-yagi/fix_set_serializer
Fix custome serializer setting
2018-02-17 00:09:23 -05:00
yuuji.yaginuma
933bbb9c37 Remove needless print
It seems to debug print.
2018-02-17 10:08:51 +09:00
Rafael Mendonça França
89bcca59e9 Remove usage of strip_heredoc in the framework in favor of <<~
Some places we can't remove because Ruby still don't have a method
equivalent to strip_heredoc to be called in an already existent string.
2018-02-16 19:28:30 -05:00
Yuji Yaginuma
3cc93de6ad Fix custome serializer setting
The serializer should be set up in `after_initialize` so that it work
properly even if the user specifies serializer with initializers.

Also, since `custom_serializers` is `Array`, it needs to be flattened
before setting the value.
2018-02-16 14:56:05 +09:00
eileencodes
26821d9b57 Add test parallelization to Rails
Provides both a forked process and threaded parallelization options. To
use add `parallelize` to your test suite.

Takes a `workers` argument that controls how many times the process
is forked. For each process a new database will be created suffixed
with the worker number; test-database-0 and test-database-1
respectively.

If `ENV["PARALLEL_WORKERS"]` is set the workers argument will be ignored
and the environment variable will be used instead. This is useful for CI
environments, or other environments where you may need more workers than
you do for local testing.

If the number of workers is set to `1` or fewer, the tests will not be
parallelized.

The default parallelization method is to fork processes. If you'd like to
use threads instead you can pass `with: :threads` to the `parallelize`
method. Note the threaded parallelization does not create multiple
database and will not work with system tests at this time.

parallelize(workers: 2, with: :threads)

The threaded parallelization uses Minitest's parallel exector directly.
The processes paralleliztion uses a Ruby Drb server.

For parallelization via threads a setup hook and cleanup hook are
provided.

```
class ActiveSupport::TestCase
  parallelize_setup do |worker|
    # setup databases
  end

  parallelize_teardown do |worker|
    # cleanup database
  end

  parallelize(workers: 2)
end
```

[Eileen M. Uchitelle, Aaron Patterson]
2018-02-15 19:21:24 -05:00
George Claghorn
54bb2f74b5 Add Rack::TempfileReaper to tests and docs 2018-01-30 18:21:07 -05:00
Daniel Colson
fda1863e1a Remove extra whitespace 2018-01-25 23:32:59 -05:00
Daniel Colson
94333a4c31 Use assert_predicate and assert_not_predicate 2018-01-25 23:32:59 -05:00
Daniel Colson
211adb47e7 Change refute to assert_not 2018-01-25 23:32:58 -05:00
Daniel Colson
0d50cae996 Use respond_to test helpers 2018-01-25 23:32:58 -05:00
Hitoshi Nakashima
5fe603ac28 Add locale selector to email preview (#31596)
- Add set_locale to detect suitable locale
- Make feature compatible with Rails 5.x
2018-01-18 15:22:10 -05:00
eileencodes
e0ad907ade Add test to properly test down with a block
down is only called with a block from the rake tasks where it passes a
`SCOPE`. Technically this was tested but since we don't run all the
migrations we're not actually testing the down works with a `SCOPE`. To
ensure we're testing both we can run `db:migrate` again to migrate users
and then run `down` with a scope to test that only the bukkits migration
is reverted.

Updates test to prevent having to fix regressions like we did in
4d4db4c.
2018-01-18 13:09:15 -05:00
eileencodes
a2827ec981 Refactor migration to move migrations paths to connection
Rails has some support for multiple databases but it can be hard to
handle migrations with those. The easiest way to implement multiple
databases is to contain migrations into their own folder ("db/migrate"
for the primary db and "db/seconddb_migrate" for the second db). Without
this you would need to write code that allowed you to switch connections
in migrations. I can tell you from experience that is not a fun way to
implement multiple databases.

This refactoring is a pre-requisite for implementing other features
related to parallel testing and improved handling for multiple
databases.

The refactoring here moves the class methods from the `Migrator` class
into it's own new class `MigrationContext`. The goal was to move the
`migrations_paths` method off of the `Migrator` class and onto the
connection. This allows users to do the following in their
`database.yml`:

```
development:
  adapter: mysql2
  username: root
  password:

development_seconddb:
  adapter: mysql2
  username: root
  password:
  migrations_paths: "db/second_db_migrate"
```

Migrations for the `seconddb` can now be store in the
`db/second_db_migrate` directory. Migrations for the primary database
are stored in `db/migrate`".

The refactoring here drastically reduces the internal API for migrations
since we don't need to pass `migrations_paths` around to every single
method. Additionally this change does not require any Rails applications
to make changes unless they want to use the new public API. All of the
class methods from the `Migrator` class were `nodoc`'d except for the
`migrations_paths` and `migrations_path` getter/setters respectively.
2018-01-18 08:55:03 -05:00
Eugene Kenny
d034f488f9 Use SHA-1 for non-sensitive digests by default
Instead of providing a configuration option to set the hash function,
switch to SHA-1 for new apps and allow upgrading apps to opt in later
via `new_framework_defaults_5_2.rb`.
2018-01-08 20:45:46 +00:00
Eugene Kenny
d2113777a1 Allow use_authenticated_message_encryption to be set in new_framework_defaults_5_2.rb
Enabling this option in new_framework_defaults_5_2.rb didn't work
before, as railtie initializers run before application initializers.

Using `respond_to?` to decide whether to set the option wasn't working
either, as `ActiveSupport::OrderedOptions` responds to any message.
2018-01-07 20:13:58 +00:00
Prathamesh Sonpatki
b821f95403
Improve the deprecation message for using subclass of Rails::Application to start the Rails server 2018-01-07 20:48:53 +05:30
yuuji.yaginuma
91a4a820fe Ensure to use repo's Gemfile in application
Puma gets bundler's info from `Bundler::ORIGINAL_ENV` for restart.
f6f3892f4d/lib/puma/launcher.rb (L168)

So, specified `BUNDLE_GEMFILE` env for use same Gemfile in the restart.

Fixes #31351
2017-12-25 06:25:10 +09:00
Ryuta Kamizono
5232ddad65
Merge pull request #31520 from yahonda/introduce_frozen_error_class
Handle `FrozenError` if it is available
2017-12-20 21:29:43 +09:00
Yasuo Honda
01efbc128d Handle FrozenError if it is available
This pull request handles `FrozenError` introduced by Ruby 2.5.
Refer https://bugs.ruby-lang.org/projects/ruby-trunk/repository/revisions/61131

Since `FrozenError` is a subclass of `RuntimeError` minitest used by master
branch can handle it, though it would be better to handle `FrozenError`
explicitly if possible.

`FrozenError` does not exist in Ruby 2.4 or lower, `frozen_error_class`
handles which exception is expected to be raised.

This pull request is intended to be merged to master,
then backported to `5-1-stable` to address #31508
2017-12-20 11:52:01 +00:00
yuuji.yaginuma
3fcf72d480 Add test case that configure config.active_support.hash_digest_class
Follow up of #31289.
2017-12-20 19:28:54 +09:00
yuuji.yaginuma
35373219c9 Raise an error only when require_master_key is specified
To prevent errors from being raise in environments where credentials
is unnecessary.

Context: https://github.com/rails/rails/issues/31283#issuecomment-348801489

Fixes #31283
2017-12-18 08:04:15 +09:00
Eileen M. Uchitelle
4bd28efc18
Merge pull request #26815 from olivierlacan/log-query-source
Log the original call site for an ActiveRecord query
2017-12-14 08:10:19 -05:00
Ryuta Kamizono
245c1dafa8 Enable Layout/LeadingCommentSpace to not allow cosmetic changes in the future
Follow up of #31432.
2017-12-14 17:30:54 +09:00
Olivier Lacan
3876defd7c Log call site for all queries
This new ActiveRecord configuration option allows you to easily
pinpoint what line of application code is triggering SQL queries in the
development log by appending below each SQL statement log the line of
Ruby code that triggered it.

It’s useful with N+1 issues, and to locate stray queries.

By default this new option ignores Rails and Ruby code in order to
surface only callers from your application Ruby code or your gems.

It is enabled on newly generated Rails 5.2 applications and can be
enabled on existing Rails applications:

```ruby
Rails.application.configure do
  # ...
  config.active_record.verbose_query_logs = true
end
```

The `rails app:upgrade` task will also add it to
`config/development.rb`.

This feature purposely avoids coupling with
ActiveSupport::BacktraceCleaner since ActiveRecord can be used without
ActiveRecord. This decision can be reverted in the future to allow more
configurable backtraces (the exclusion of gem callers for example).
2017-12-13 20:13:21 -05:00
Mehmet Emin INAC
ff25c25127
Expose Active Storage routes 2017-12-13 19:28:57 +01:00
Aaron Patterson
a50b8ea350
Set the Rails environment from an environment variable
Option parsing happens too late to have any impact on the Rails
environment.  Rails accesses the environment name and memoizes it too
early in the boot process for a commandline option to have any impact on
the database connection, so we'll change this test to set the
environment from an environment variable (and ensure it still works when
running tests with `ruby`)
2017-12-08 14:50:27 -08:00
Aaron Patterson
a58543dbb1
Add failing test for wrong database connection
When tests are run with just `ruby`, the RAILS_ENV is set to
`development` too early, and we connect to the development database
rather than the test database.
2017-12-08 13:23:31 -08:00
Aaron Patterson
da225c0db6
Fix Rails environment when running tests with Ruby
I frequently run tests with `ruby`, not with a runner like `rake` or
`rails`.  When running the test with just `ruby` the `RAILS_ENV`
environment variable did not get set to "test", and this would cause the
tests to fail (and even mutate the development database!)

This commit adds integration tests for running tests with just `ruby`
and ensures the environment gets defaulted to "test".  I also added a
test to ensure that passing an environment to `-e` actually works (and
fixed that case too).

An interesting / annoying thing is that Minitest picks up it's plugins
by asking RubyGems for a list of files:

  ca6a71ca90/lib/minitest.rb (L92-L100)

This means that RubyGems needs to somehow know about the file before it
can return it to Minitest.  Since we are not packaging Rails as a Gem
before running the integration tests on it (duh, why would you do
that?), RubyGems doesn't know about the file, so it can't tell Minitest,
so Minitest doesn't automatically require it.  This means I had to
manually require and insert the plugin in our integration test.  I've
left comments about that in the test as well.

Ugh.
2017-12-06 20:40:04 -08:00
yuuji.yaginuma
dbee80bca0 Make Migrator.current_version work without a current database
This is necessary in order to make the processing dependent on
`Migrator.current_version` work even without database.

Context: https://github.com/rails/rails/pull/31135#issuecomment-348404326
2017-12-03 09:50:28 +09:00
Andrew White
456c3ffdbe Add DSL for configuring Content-Security-Policy header
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy
2017-11-27 05:59:26 +00:00
npezza93
260d6f112a
Change form_with to generates ids by default
When `form_with` was introduced we disabled the automatic
generation of ids that was enabled in `form_for`. This usually
is not an good idea since labels don't work when the input
doesn't have an id and it made harder to test with Capybara.

You can still disable the automatic generation of ids setting
`config.action_view.form_with_generates_ids` to `false.`
2017-11-25 11:55:02 -05:00
Yuji Yaginuma
b6d5e46311
Add environment as dependency of load_config (#31135)
Currently the environment is not loaded in some db tasks.
Therefore, if use encrypted secrets values in `database.yml`,
`read_encrypted_secrets` will not be true, so the value can not be
used correctly.

To fix this, added `environment` as dependency of `load_config`.
It also removes explicit `environment` dependencies that are no longer
needed.

Fixes #30717
2017-11-14 13:54:58 +09:00
yuuji.yaginuma
d1e0bc7c17 Do not show credentials in generators help
Since credentials generator is executed via the credentials command and
does not need to be executed directly, so it is not necessary to show it in
help.
2017-11-09 20:59:16 +09:00
yuuji.yaginuma
8e1dca10cd Remove unnecessary migration deletion
Since isolation application is generated with the `--skip-gemfile`
option, so `active_storage:install` is not executed.
2017-11-08 13:24:16 +09:00
bogdanvlviv
90fe2a42f0
Fix bin/rails db:migrate with specified VERSION
Ensure that `bin/rails db:migrate` with specified `VERSION` reverts
all migrations only if `VERSION` is `0`.
Raise error if target migration doesn't exist.
2017-11-06 22:40:10 +00:00
Rafael França
63f0c04850
Merge pull request #30101 from bogdanvlviv/initialization-active_storage
Provide initialization of Active Storage
2017-11-06 17:25:54 -05:00
bogdanvlviv
0835527d6b
rails new runs rails active_storage:install
Omit `rails activestorage:install` for jdbcmysql, jdbc and shebang tests

AppGeneratorTest#test_config_jdbcmysql_database

  rails aborted!
  LoadError: Could not load 'active_record/connection_adapters/mysql_adapter'.
  Make sure that the adapter in config/database.yml is valid.
  If you use an adapter other than 'mysql2', 'postgresql' or 'sqlite3' add
  the necessary adapter gem to the Gemfile.
  (compressed)
  bin/rails:4:in `<main>'
  Tasks: TOP => activestorage:install => environment
  (See full trace by running task with --trace)

AppGeneratorTest#test_config_jdbc_database

  rails aborted!
  LoadError: Could not load 'active_record/connection_adapters/jdbc_adapter'.
  Make sure that the adapter in config/database.yml is valid.
  If you use an adapter other than 'mysql2', 'postgresql' or 'sqlite3' add
  the necessary adapter gem to the Gemfile.
  (compressed)
  bin/rails:4:in `<main>'
  Tasks: TOP => activestorage:install => environment
  (See full trace by running task with --trace)

AppGeneratorTest#test_shebang_is_added_to_rails_file

  /home/ubuntu/.rbenv/versions/2.4.1/bin/ruby: no Ruby script found in input (LoadError)

Prevent PendingMigrationError in tests

 * Run `bin/rails db:migrate RAILS_ENV=test` in test_cases before start tests to prevent PendingMigrationError
 * FileUtils.rm_r("db/migrate")
 * --skip-active-storage

Fix failed tests in `railties/test/railties/engine_test.rb`

Related to #30111

Imporve `SharedGeneratorTests#test_default_frameworks_are_required_when_others_are_removed`

 - Explicitly skip active_storage
 - Ensure that skipped frameworks are commented
 - Ensure that default frameworks are not commented

Fix error `Errno::ENOSPC: No space left on device - sendfile`

Since `rails new` runs `rails active_storage:install`
that boots an app.

Since adding Bootsnap 0312a5c67e35b960e33677b5358c539f1047e4e1
during booting an app, it creates the cache:

   264K    tmp/cache/bootsnap-load-path-cache
   27M     tmp/cache/bootsnap-compile-cache

* teardown_app must remove app
2017-11-06 21:29:14 +00:00
Akira Matsuda
85cda0f6f3 s/an/a/ 2017-11-04 20:11:36 +09:00
Sean Griffin
9138f2da22 Merge pull request #30579 from bogdanvlviv/fix_ar_internal_metadata_for_a_test_database
Fix `bin/rails db:setup` and `bin/rails db:test:prepare` create  wrong ar_internal_metadata's data for a test database.
2017-10-18 15:24:06 -04:00
yuuji.yaginuma
3695bbaf5f Remove unnecessary allow_failure: true option
`routes` task always returns zero to status, so status is not to non-zeno.
Ref: b1867c480d/railties/lib/rails/tasks/routes.rake (L30)
2017-10-18 15:03:01 +09:00
bogdanvlviv
678e563da3
ActiveRecord::Tasks::DatabaseTasks.load_schema has always to establish database connection
When load schema from `structure.sql`, database connection isn't
  established. `ActiveRecord::Tasks::DatabaseTasks.load_schema` has to
  establish database connection since it executes
  ```
  ActiveRecord::InternalMetadata.create_table
  ActiveRecord::InternalMetadata[:environment] = environment
  ```
2017-10-15 22:27:54 +03:00
bogdanvlviv
99b2bf8db3
Fix bin/rails db:setup and bin/rails db:test:prepare create wrong ar_internal_metadata's data for a test database.
Before:
  ```
  $ RAILS_ENV=test rails dbconsole
  > SELECT * FROM ar_internal_metadata;
  key|value|created_at|updated_at
  environment|development|2017-09-11 23:14:10.815679|2017-09-11 23:14:10.815679
  ```

  After:
  ```
  $ RAILS_ENV=test rails dbconsole
  > SELECT * FROM ar_internal_metadata;
  key|value|created_at|updated_at
  environment|test|2017-09-11 23:14:10.815679|2017-09-11 23:14:10.815679
  ```

  Fixes #26731.
2017-10-15 22:27:54 +03:00
bogdanvlviv
2329207bec
Improve RakeTest#test_db_test_prepare_when_using_sql_format
- Remove redundant setting `RAILS_ENV` for `db:test:prepare`.
    `db:test:prepare` doesn't require it.
2017-10-08 23:29:28 +03:00
bogdanvlviv
0eb2d6079a
Invoke rails command inside the railties' test app with TestHelpers::Generation#rails
See #30520
2017-10-08 23:28:51 +03:00
bogdanvlviv
ff67743fb2
Remove redundant execution of Dir.chdir(app_path) { } in railties' tests 2017-10-08 23:04:04 +03:00
Kasper Timm Hansen
fbcc4bfe9a
Deprecate secret_token, long since usurped by secret_key_base.
See the changelog entry.

Remove `secrets.secret_token` from the bug report templates,
since we don't accept bug reports for Rails versions that
don't support a `secret_key_base`.

[ claudiob & Kasper Timm Hansen ]
2017-09-28 20:46:01 +02:00
Kasper Timm Hansen
1fa268bfa5
Fix cookies/session tests broken after merging key rotation.
Based on, yet closes https://github.com/rails/rails/pull/30708

Fix the session test by properly truncating the legacy encryption
key for cbc encryption. Borrowed straight from 👆.

Fix the cookies test a little differently than the PR. Basically
keep every config within the config block.

[ Michael Coyne & Kasper Timm Hansen ]
2017-09-25 20:28:26 +02:00
Kasper Timm Hansen
0c4dc1639c
Skip complex cookie tests for now; I'll deal with them tomorrow. 2017-09-24 23:07:09 +02:00
Michael Coyne
39f8ca64ce Add key rotation message Encryptor and Verifier
Both classes now have a rotate method where new instances are added for
each call. When decryption or verification fails the next rotation
instance is tried.
2017-09-23 17:16:21 -04:00
yuuji.yaginuma
daa592293b Use TOPLEVEL_BINDING in rails runner command
Binding to capture the local scope. This means that if a constant with same
name as constant specified by the user exists in local scope, constant
defined in local will use. This is different from what the user expects.
Therefore, fixed to use top-level binding instead of local scope.

Fixes #30644
2017-09-22 10:42:28 +09:00
yuuji.yaginuma
3bf95f9513 Don't expose Active Storage routes
These routes are only used internally in Active Storage, and it seems
that there is no need for the user to directly use them.

Therefore, I think that routes should not be exposed to users.
2017-09-16 14:54:51 +09:00
David Heinemeier Hansson
69f976b859 Add credentials using a generic EncryptedConfiguration class (#30067)
* WIP: Add credentials using a generic EncryptedConfiguration class

This is sketch code so far.

* Flesh out EncryptedConfiguration and test it

* Better name

* Add command and generator for credentials

* Use the Pathnames

* Extract EncryptedFile from EncryptedConfiguration and add serializers

* Test EncryptedFile

* Extract serializer validation

* Stress the point about losing comments

* Allow encrypted configuration to be read without parsing for display

* Use credentials by default and base them on the master key

* Derive secret_key_base in test/dev, source it from credentials in other envs

And document the usage.

* Document the new credentials setup

* Stop generating the secrets.yml file now that we have credentials

* Document what we should have instead

Still need to make it happen, tho.

* [ci skip] Keep wording to `key base`; prefer defaults.

Usually we say we change defaults, not "spec" out a release.

Can't use backticks in our sdoc generated documentation either.

* Abstract away OpenSSL; prefer MessageEncryptor.

* Spare needless new when raising.

* Encrypted file test shouldn't depend on subclass.

* [ci skip] Some woordings.

* Ditch serializer future coding.

* I said flip it. Flip it good.

* [ci skip] Move require_master_key to the real production.rb.

* Add require_master_key to abort the boot process.

In case the master key is required in a certain environment
we should inspect that the key is there and abort if it isn't.

* Print missing key message and exit immediately.

Spares us a lengthy backtrace and prevents further execution.

I've verified the behavior in a test app, but couldn't figure the
test out as loading the app just exits immediately with:

```
/Users/kasperhansen/Documents/code/rails/activesupport/lib/active_support/testing/isolation.rb:23:in `load': marshal data too short (ArgumentError)
	from /Users/kasperhansen/Documents/code/rails/activesupport/lib/active_support/testing/isolation.rb:23:in `run'
	from /Users/kasperhansen/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/minitest-5.10.2/lib/minitest.rb:830:in `run_one_method'
	from /Users/kasperhansen/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/minitest-5.10.2/lib/minitest/parallel.rb:32:in `block (2 levels) in start'
```

It's likely we need to capture and prevent the exit somehow.
Kernel.stub(:exit) didn't work. Leaving it for tomorrow.

* Fix require_master_key config test.

Loading the app would trigger the `exit 1` per require_master_key's
semantics, which then aborted the test.

Fork and wait for the child process to finish, then inspect the
exit status.

Also check we aborted because of a missing master key, so something
else didn't just abort the boot.

Much <3 to @tenderlove for the tip.

* Support reading/writing configs via methods.

* Skip needless deep symbolizing.

* Remove save; test config reader elsewhere.

* Move secret_key_base check to when we're reading it.

Otherwise we'll abort too soon since we don't assign the secret_key_base
to secrets anymore.

* Add missing string literal comments; require unneeded yaml require.

* ya ya ya, rubocop.

* Add master_key/credentials after bundle.

Then we can reuse the existing message on `rails new bc4`.

It'll look like:

```
Using web-console 3.5.1 from https://github.com/rails/web-console.git (at master@ce985eb)
Using rails 5.2.0.alpha from source at `/Users/kasperhansen/Documents/code/rails`
Using sass-rails 5.0.6
Bundle complete! 16 Gemfile dependencies, 72 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
Adding config/master.key to store the master encryption key: 97070158c44b4675b876373a6bc9d5a0

Save this in a password manager your team can access.

If you lose the key, no one, including you, can access anything encrypted with it.

      create  config/master.key
```

And that'll be executed even if `--skip-bundle` was passed.

* Ensure test app has secret_key_base.

* Assign secret_key_base to app or omit.

* Merge noise

* Split options for dynamic delegation into its own method and use deep symbols to make it work

* Update error to point to credentials instead

* Appease Rubocop

* Validate secret_key_base when reading it.

Instead of relying on the validation in key_generator move that into
secret_key_base itself.

* Fix generator and secrets test.

Manually add config.read_encrypted_secrets since it's not there by default
anymore.

Move mentions of config/secrets.yml to config/credentials.yml.enc.

* Remove files I have no idea how they got here.

* [ci skip] swap secrets for credentials.

* [ci skip] And now, changelogs are coming.
2017-09-11 20:21:20 +02:00
yuuji.yaginuma
603475b76f Remove needless silence_warnings
Since ff30db1, warning is not shown.
2017-09-09 09:48:15 +09:00
Matthew Draper
802ce8a239 Run in-app rails commands via fork+load where possible
While this avoids shell argument parsing, we still pass through
everything in our stack.
2017-09-04 20:19:39 +09:30
Matthew Draper
07bac9ef93 Don't need the layout here 2017-09-04 05:44:45 +09:30
Matthew Draper
e4b0488851 Preload Rails component gems in railties tests 2017-09-04 05:43:01 +09:30
yuuji.yaginuma
5d0a4c9aa6 Remove needless silence_warnings
Since ff30db1, warning is not show.
2017-09-01 15:41:30 +09:00
yuuji.yaginuma
2904ee23bf Make restart and dev:cache tasks work when customizing pid file path
Originally, it hard-coded pid file path. It can not be removed when customizing
pid file path.
But rake task can not get pid file path. Therefore, do not remove file in rake
task, makes it possible to judge whether it is restart from the argument of the
command and removes the file in server command.

Fixes #29306
2017-08-21 05:44:11 +09:00
Koichi ITO
7c260ae201 Fix RuboCop offenses
And enable `context_dependent` of Style/BracesAroundHashParameters cop.
2017-08-16 17:55:25 +09:00
Rafael Mendonça França
11fd246d8f Add test case to make sure Parameters configuration are executed once
Test case for #30045
2017-08-15 17:23:52 -04:00
Pat Allan
acea68de02 Adding frozen_string_literal pragma to Railties. 2017-08-14 19:08:09 +02:00
Pat Allan
d435c92721 Railties updates for frozen string literals. 2017-08-14 19:00:24 +02:00
Rafael França
b9e0b4f199 Merge pull request #29559 from kirs/eager-load-controller-actions
Eager load controller actions to reduce response time of the first request
2017-08-11 17:54:04 -04:00
yuuji.yaginuma
f217364893 Deprecate support of older config.ru
Since Rails 4.0, `config.ru` generated by default uses instances of
`Rails.application`.  Therefore, I think that it is good to deprecate
the old behavior.

Related: #9669
2017-08-08 07:47:11 +09:00
George Claghorn
1ab1f87596 Check for app.secrets.secret_key_base, not app.config.secret_key_base
By default, apps only have the former set.
2017-08-05 10:03:53 -04:00
David Heinemeier Hansson
2194c27091 Convert to strings so array can be sorted deterministically 2017-08-04 10:30:19 -05:00
David Heinemeier Hansson
31f6100835 Deterministic comparisons please 2017-08-03 22:49:36 -05:00
Rafael Mendonça França
f601a01b2c Do not eager load ActiveRecord::Base
Everything inside the app directory of a engine is autoload/eager loaded automatically so we don't need to require them.
2017-08-03 16:57:48 -04:00
David Heinemeier Hansson
5b49e22f75 Active Storage routes are now part of the default routes
It's worth considering whether we should hide these by default, but I'm kinda thinking no. It's very reasonable that someone would want to call these directly, so they should be documented.
2017-08-03 15:54:38 -05:00
David Heinemeier Hansson
d84a126d25 Same issue from AR getting loaded earlier
cc @rafaelfranca
2017-08-03 15:13:08 -05:00
David Heinemeier Hansson
a56b2a5e40 Including new default classes in loading test 2017-08-03 15:01:22 -05:00
David Heinemeier Hansson
425aaabcfe Active Storage loads AR earlier to extend it so require env later 2017-08-03 14:54:26 -05:00
David Heinemeier Hansson
6b40fed4e2 Generating the app is where the exception is now raised 2017-08-03 10:02:08 -05:00
Rafael Mendonça França
feb1ddae02 Merge remote-tracking branch 'origin/master' into unlock-minitest 2017-08-01 17:34:14 -04:00
yuuji.yaginuma
0348a9e4ae Fix test runner's output
Output changed due to specification change of `SummaryReporter#aggregated_results`
in minitest 5.10.2.
In my opinion, that should fix rails's test runner(proceeding with #29354).
However, we still need discussion and the fix itself is minor, so I think
that we can fix only the test first.
2017-07-30 09:38:12 +09:00
Kir Shatrov
0668c22a41 Eager load controller and mailer actions
On the first request, ActionController::Base#action_methods computes
and memoized the list of available actions [1]. With this PR we move
this expensive operation into eager load step to reduce response time
of the first request served in production.

This also reduces the memory footprint when running on forking server
like Unicorn.

[1] a3813dce9a/actionpack/lib/abstract_controller/base.rb (L66-L77)
2017-07-29 14:03:52 +03:00
yuuji.yaginuma
65a1733545 Fix warning: method redefined;
This fixes the following warning:

```
/tmp/d20170727-7039-kmdtb1/app/app/models/user.rb:5: warning: method redefined; discarding old model_name
rails/activemodel/lib/active_model/naming.rb:222: warning: previous definition of model_name was here
```
2017-07-28 12:18:14 +09:00
Kasper Timm Hansen
5948871e82
Merge pull request #29926 from pawandubey:fix-test-with-absolute-paths 2017-07-25 21:16:05 +02:00
Pawan Dubey
07d84b7c8d
Allow bin/rails test task to take absolute paths as arguments
Solves #29923

This regression was caused due to a wrong regex to filter out
paths, introduced in commit 796a1cf0e

The regex was /^\w+\// which did not accept paths with a leading
slash and hence all absolute paths were filtered out.

This change introduces a change in regex which allows for a leading
slash and acts on the matched term accordingly.

While cascading through the case block, the paths are checked for
line number specification, existence of a directory at that path
and if none of those match, then it is considered to be a path to the
file. The regex matchers specified are filtered out via the call
to `Array#compact` since they do not match any of these conditions.
2017-07-25 21:00:43 +02:00
Sean Griffin
a1fa2fbaf1 Merge pull request #29931 from y-yagi/extract_assert_output_and_available_pty_to_module
Extract `assert_output` and `available_pty?` into `ConsoleHelpers` module
2017-07-25 11:46:20 -04:00
Sean Griffin
b691a946ba Fix dbconsole test when tempdir is a long path
The output of `.databases` in SQLite will truncate to a certain size.
This causes the test to fail when run locally from a mac, or anything
which has a tempdir with more than a few characters. This pragma has
the same output, but presented as a normal query, meaning no truncation
will occur.
2017-07-25 08:12:11 -04:00
yuuji.yaginuma
af4cef024b Extract assert_output and available_pty? into ConsoleHelpers module
We define almost the same method with multiple tests. Therefore, it extract
into module.
2017-07-25 15:01:33 +09:00
yuuji.yaginuma
8a0f235fd3 Fix warning: ambiguous first argument
This fixes the following warning:

```
railties/test/application/rake/dbs_test.rb:265: warning: ambiguous first argument; put parentheses or a space even after `/' operator
```
2017-07-25 08:19:41 +09:00
Eugene Kenny
2b331e9098 Avoid modifying frozen string in check_schema_file
This was missed when the frozen string literal pragma was added to this
file because the string is only modified when running in the context of
a full Rails app, which wasn't covered by the test suite.
2017-07-23 20:40:00 +01:00
Cody Cutrer
ed44b145bd support - as an argument to rails runner
in Rails 4.0, you could use `/dev/stdin` on both Linux and Mac, but with
the switch to Kernel.load in Rails 4.1, this broke on Linux (you get
a LoadError). Instead, explicitly detect `-` as meaning stdin, then
read from stdin explicitly, instead of performing file gymnastics. This
should now work on any platform uniformly.

Passing a script via stdin is useful when you're sshing to a server,
and the script you want to run is stored locally. You could theoretically
pass the entire script on the command line, but in reality you'll run
into problems with the command being too long.
2017-07-17 13:46:44 -06:00
Kasper Timm Hansen
c24be36932 Rename helper to force_lazy_load_hooks.
Clarifies the intent that aren't just loading the
model but really caring about triggering the on_load
callbacks.
2017-07-17 21:37:03 +02:00
yuuji.yaginuma
8be50181d3 Set RAILS_ENV before load application file
Since #29725, load application file when `dbconsole` command is executed.
However, if do not set `RAILS_ENV` before reading the application file,
can not connect to the env specified in option, so added the setting
of `RAILS_ENV`.
2017-07-17 09:11:21 +09:00
yuuji.yaginuma
c98a641ff4 add helper method for explicit lazy load 2017-07-16 20:03:56 +09:00
yuuji.yaginuma
a18cf23a9c Set represent_boolean_as_integer via configuration 2017-07-16 20:03:56 +09:00
Kasper Timm Hansen
aad42dce10 Merge branch 'master' into unlock-minitest 2017-07-15 21:17:27 +02:00
Lisa Ugray
52e050ed00 Change sqlite3 boolean serialization to use 1 and 0
Abstract boolean serialization has been using 't' and 'f', with MySQL
overriding that to use 1 and 0.

This has the advantage that SQLite natively recognizes 1 and 0 as true
and false, but does not natively recognize 't' and 'f'.

This change in serialization requires a migration of stored boolean data
for SQLite databases, so it's implemented behind a configuration flag
whose default false value is deprecated. The flag itself can be
deprecated in a future version of Rails.  While loaded models will give
the correct result for boolean columns without migrating old data,
where() clauses will interact incorrectly with old data.

While working in this area, also change the abstract adapter to use
`"TRUE"` and `"FALSE"` as quoted values and `true` and `false` for
unquoted.  These are supported by PostreSQL, and MySQL remains
overriden.
2017-07-11 14:52:46 -04:00
Rafael França
48cb8b3e70 Merge pull request #29742 from lugray/default_protect_from_forgery
Default protect from forgery
2017-07-10 17:24:31 -04:00
Lisa Ugray
ec4a836919 Protect from forgery by default
Rather than protecting from forgery in the generated
ApplicationController, add it to ActionController::Base by config. This
configuration defaults to false to support older versions which have
removed it from their ApplicationController, but is set to true for
Rails 5.2.
2017-07-10 16:23:47 -04:00
Kasper Timm Hansen
0d72489b2a * Don't eagerly require Rails' minitest plugin.
By making the Rails minitest behave like a standard minitest plugin
we're much more likely to not break when people use other minitest
plugins. Like minitest-focus and pride.

To do this, we need to behave like minitest: require files up front
and then perform the plugin behavior via the at_exit hook.
This also saves us a fair bit of wrangling with test file loading.

Finally, since the environment and warnings options have to be applied
as early as possible, and since minitest loads plugins at_exit, they
have to be moved to the test command.

* Don't expect the root method.

It's likely this worked because we eagerly loaded the Rails minitest plugin
and that somehow defined a root method on `Rails`.

* Assign a backtrace to failed exceptions.

Otherwise Minitest pukes when attempting to filter the backtrace (which
Rails' backtrace cleaner then removes).

Means the exception message test has to be revised too.

This is likely caused by the rails minitest plugin now being loaded for
these tests and assigning a default backtrace cleaner.
2017-07-10 20:40:16 +02:00
yuuji.yaginuma
e12715bfd5 Load environment file in dbconsole command
Currently the environment file is not loaded in `dbconsole` command.
Therefore, for example, if use encrypted secrets values in database.yml,
`read_encrypted_secrets` will not be true, so the value can not be
used correctly.

Fixes #29717
2017-07-09 13:18:56 +09:00
Rafael Mendonça França
2ae84d2fa0 Merge pull request #29677 from eugeneius/parameters_configuration_tests
Fix Parameters configuration integration tests
2017-07-05 12:24:09 -04:00
Rafael Mendonça França
056ffa42af Force ActionController::Base lazy laod hooks to run
Now that the parameters configurations are only loaded when
ActionController::Base is we need to foce them to load in our tests. In
an application this is not needed since every request already load the
controllers.
2017-07-05 12:21:55 -04:00
Eugene Kenny
458a5502a1 Fix Parameters configuration integration tests
These tests relied on `ActionController::Parameters` being configured as
part of the boot process; since that now happens lazily we need to force
`ActionController::Base` to load so that we can test the behaviour.

The new tests added here ensure that `ActionController::Parameters` can
be configured from an initializer, which was broken until recently.
2017-07-04 22:13:29 +01:00
Ryuta Kamizono
6aa658e329 Remove redundant assert_respond_to
It is covered by following assertion.
2017-07-03 00:16:53 +09:00
yuuji.yaginuma
06b1e5f507 Move test related to tmp:clear task to tmp_test.rb 2017-07-02 09:13:55 +09:00
Matthew Draper
87b3e226d6 Revert "Merge pull request #29540 from kirs/rubocop-frozen-string"
This reverts commit 3420a14590c0e6915d8b6c242887f74adb4120f9, reversing
changes made to afb66a5a598ce4ac74ad84b125a5abf046dcf5aa.
2017-07-02 02:15:17 +09:30
Matthew Draper
3420a14590 Merge pull request #29540 from kirs/rubocop-frozen-string
Enforce frozen string in Rubocop
2017-07-02 01:11:50 +09:30
Kir Shatrov
cfade1ec7e Enforce frozen string in Rubocop 2017-07-01 02:11:03 +03:00
yuuji.yaginuma
6fbd405a2e Clear screenshots files in tmp:clear task
If system test fails, it creates screenshot under `tmp/screenshots`.
34fe2a4fc7/actionpack/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb (L45)

But currently, screenshot files is not cleared by `tmp:clear` task.
This patch make clears screenshot files with `tmp:clear` task as well
as other tmp files.
2017-06-27 07:13:00 +09:00
Guillermo Iguaran
3260c6e162 Make i18n test match the description of the test 2017-06-09 09:52:28 -05:00
Kasper Timm Hansen
e1758b5e8c Merge branch 'master' into unlock-minitest 2017-05-29 20:37:35 +02:00
Kasper Timm Hansen
b88200f103 Merge pull request #28132 from mikeycgto/aead-encrypted-cookies
AEAD encrypted cookies and sessions
2017-05-28 17:02:14 +02:00
Kasper Timm Hansen
96be81303e Make reset execution assertions easier to read.
The app is booted by then, so there's no need to stash the code away in
some other script.
2017-05-28 10:19:32 +02:00
Kasper Timm Hansen
9dc1871acb Use models to match the docs. 2017-05-27 14:34:13 +02:00
David Heinemeier Hansson
24a864437e ActiveSupport::CurrentAttributes provides a thread-isolated attributes singleton (#29180)
* Add ActiveSupport::CurrentAttributes to provide a thread-isolated attributes singleton

* Need to require first

* Move stubs into test namespace.

Thus they won't conflict with other Current and Person stubs.

* End of the line for you, whitespace!

* Support super in attribute methods.

Define instance level accessors in an included module such that
`super` in an overriden accessor works, akin to Active Model.

* Spare users the manual require.

Follow the example of concerns, autoload in the top level Active Support file.

* Add bidelegation support

* Rename #expose to #set. Simpler, clearer

* Automatically reset every instance.

Skips the need for users to actively embed something that resets
their CurrentAttributes instances.

* Fix test name; add tangible name value when blank.

* Try to ensure we run after a request as well.

* Delegate all missing methods to the instance

This allows regular `delegate` to serve, so we don't need bidelegate.

* Properly test resetting after execution cycle.

Also remove the stale puts debugging.

* Update documentation to match new autoreset
2017-05-26 20:00:27 +02:00
bogdanvlviv
40bdbce191
Define path with __dir__
".. with __dir__ we can restore order in the Universe." - by @fxn

Related to 5b8738c2df003a96f0e490c43559747618d10f5f
2017-05-23 00:53:51 +03:00
Michael Coyne
5a3ba63d9a AEAD encrypted cookies and sessions
This commit changes encrypted cookies from AES in CBC HMAC mode to
Authenticated Encryption using AES-GCM. It also provides a cookie jar
to transparently upgrade encrypted cookies to this new scheme. Some
other notable changes include:

- There is a new application configuration value:
  +use_authenticated_cookie_encryption+. When enabled, AEAD encrypted
  cookies will be used.

- +cookies.signed+ does not raise a +TypeError+ now if the name of an
  encrypted cookie is used. Encrypted cookies using the same key as
  signed cookies would be verified and serialization would then fail
  due the message still be encrypted.
2017-05-22 08:50:36 +00:00
David Heinemeier Hansson
75fa8dd309 Use recyclable cache keys (#29092) 2017-05-18 18:12:32 +02:00
yuuji.yaginuma
c776b64708 Allow irb options to be passed from rails console command
Fixes #28988
2017-05-08 10:07:17 +09:00
bogdanvlviv
f0b93d135e
Set proper assertion to be sure the test failed 2017-05-02 13:09:21 +00:00
Rafael Mendonça França
15a4d3c383
Also raise error when VERSION is nil
Fix #28905
2017-04-27 10:21:28 -07:00
Rafael França
28cd12c345 Merge pull request #28896 from pschambacher/load_with_shared
Added a shared section to config/database.yml that will be loaded for all envs
2017-04-26 23:13:59 -07:00
Pavel Valena
aceee198c9
New minitest 'assert false' message 2017-04-26 22:59:35 -07:00
Aaron Patterson
c5663c4282 Merge pull request #28897 from rafaelfranca/fix-name-error-error-page
Do not try to encoding the parameters when the controller is not defined
2017-04-26 20:54:12 -07:00
Pierre Schambacher
dfc361df36 Added a shared section to config/database.yml that will be loaded for all environments 2017-04-26 20:47:56 -07:00
Rafael França
f680664d4e Merge pull request #28244 from ixti/improve/action-mailer-preview-params
Pass request params to ActionMailer::Preview
2017-04-26 20:15:30 -07:00
Rafael Mendonça França
e06f68fdb2
Do not try to encoding the parameters when the controller is not defined
When you have a route that points to an nonexistent controller we raise
an exception.

This exception was being caught by the DebugExceptions middleware in
development, but when trying to render the error page, we are reading
the request format[[1][]]. To determine the request format we are reading
the format parameters[[2][]], and to be able to read the parameters we need
to encode them[[3][]]. This was raising another exception that to encode the
parameter we try to load the controller to determine if we need to
encode the parameters are binary[[4][]]. This new exception inside the
DebugExceptions middleware makes Rails to render a generic error page.

To avoid this new exception now we only encode the parameters when the
controller can be loaded.

Fixes #28892

[1]: f52cdaac63/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb (L80)
[2]: f52cdaac63/actionpack/lib/action_dispatch/http/mime_negotiation.rb (L63)
[3]: f52cdaac63/actionpack/lib/action_dispatch/http/parameters.rb (L58)
[4]: f52cdaac63/actionpack/lib/action_dispatch/http/parameters.rb (L88)
2017-04-26 19:45:55 -07:00
yuuji.yaginuma
87d6cb379d Set to form_with_generates_remote_forms only when config is explicitly specified
Without this check, even if config is not specified, `ActionView::Helpers::FormHelper.form_with_generates_remote_forms`
always be set to nil and remote form not be generated.

Follow up to 128b804c6ce40fcbde744f294f8cb98654f6efec
2017-04-22 12:37:48 +09:00
Rafael Mendonça França
128b804c6c
Configure form_with_generates_remote_forms in its own initializer
This configuration is not present in ActionView::Base so we can't let
the action_view.set_configs initializer set it.

Also add tests to make sure this config works.

Fixes #28824
2017-04-21 12:23:49 -04:00
bogdanvlviv
bb9d6eb094 Add additional raise UnknownMigrationVersionError
Raise error on the movement of migrations
when the current migration does not exist.
2017-04-19 23:37:58 +03:00
bogdanvlviv
b77d2aa0c3 Fix bin/rails db:forward first migration 2017-04-19 21:32:26 +03:00
Ryuta Kamizono
a315846923 Remove duplicated "test" prefix 2017-04-07 08:40:52 +09:00
Matthew Draper
3ecffab324 Merge pull request #28057 from eugeneius/clear_active_connections
Clear active connections after initialization
2017-04-04 04:12:56 +09:30
Rafael Mendonça França
825447130d
Fix the tests to test what they should be testing
With Rack::Test the headers needs to match the `HTTP_` format. The tests
were passing before because they are not asserting the response was a
cache hit.
2017-03-27 22:49:24 -04:00
Rafael França
8c658a0ecc Merge pull request #28485 from quantumlicht/fix_migrate_with_empty_version
fix migrate with empty version
2017-03-27 17:07:56 -04:00
Philippe Guay
14739b5e27 Fixes #28359
Add stronger assertions to rake migration tasks to make sure the user is providing a numeric VERSION
An empty string was getting converted to version = 0. This would in turn pass the presence check.

Address linting warning

Add test for rake task and refactor code to meet expectations
In particular passing VERSION=0 should not raise an error.

Addressed Comments for PR #28485. Trimmed empty lines + change of wording for error message

Adjust test for change of wording in error message

Change condition to follow rails idioms
2017-03-26 21:06:02 -04:00
yuuji.yaginuma
9a0ad3f5ef Do not show hidden namespaces in destroy commnad help 2017-03-27 08:29:20 +09:00
Robert Thau
9bc6178e00 Fixup trailing whitespace, per complaints from CodeClimate. 2017-03-22 12:36:27 -04:00
Robert Thau
a06a643e05 Correctly reset ARGV for "rails runner `CODE' arg arg arg..."
The code itself should not be in the ARGV vector.

Fixes #28515
2017-03-22 12:04:19 -04:00
Robin Dupret
4a77213eea Avoid running system tests by default
These tests may be expansive so let's only allow users to run them
through `bin/rails test:system` or by passing a path to the `test`
command.

The same applies for `bin/rake test`.

Refs #28109.
2017-03-05 13:24:43 +01:00
Fumiaki MATSUSHIMA
5edbdca5c0 Fix random failure on system test with ajax
If application has ajax, browser may begin request after rollback.
`teardown_fixtures` will be called after `super` on `after_teardown`
so we must call `Capybara.reset_sessions!` before `super`

b61a56541a/activerecord/lib/active_record/fixtures.rb (L857)
2017-03-03 19:36:56 +09:00
Alexey Zapparov
8e6c6d854c
Pass request params to ActionMailer::Preview 2017-03-01 20:54:14 +01:00
yuuji.yaginuma
87a8206dbc does not show hidden namespaces in generator's help 2017-02-24 23:45:38 +09:00
yuuji.yaginuma
e69a0e3449 Make help short-cut alias to work 2017-02-24 09:17:13 +09:00
yuuji.yaginuma
08d4aaae0c Make version short-cut alias to work 2017-02-24 09:13:44 +09:00
Kasper Timm Hansen
72fc0b4466 Add back tests for test:units and test:functionals.
Would have caught that the invoke changes broke rake delegation
behavior.

And we do ship the behavior so we should test it.
2017-02-23 18:38:43 +01:00
Andrew White
d7c1e62c2c Split direct method into two
Use a separate method called `resolve` for the custom polymorphic
mapping to clarify the API.
2017-02-21 15:30:48 +00:00
Andrew White
c116eaf221 Prefer remove_method over undef_method
Using `undef_method` means that when a route is removed any other
implementations of that method in the ancestor chain are inaccessible
so instead use `remove_method` which restores access to the ancestor.
2017-02-21 15:30:47 +00:00
Andrew White
3bf47b018b Add custom polymorphic mapping
Allow the use of `direct` to specify custom mappings for polymorphic_url, e.g:

  resource :basket
  direct(class: "Basket") { [:basket] }

This will then generate the following:

  >> link_to "Basket", @basket
  => <a href="/basket">Basket</a>

More importantly it will generate the correct url when used with `form_for`.

Fixes #1769.
2017-02-21 15:30:47 +00:00
Andrew White
47a27e8950 Rename url_helper to direct 2017-02-21 15:30:46 +00:00
Andrew White
ce7d5fb2e6 Add support for defining custom url helpers in routes.rb
Allow the definition of custom url helpers that will be available
automatically wherever standard url helpers are available. The
current solution is to create helper methods in ApplicationHelper
or some other helper module and this isn't a great solution since
the url helper module can be called directly or included in another
class which doesn't include the normal helper modules.

Reference #22512.
2017-02-21 15:30:46 +00:00
Kasper Timm Hansen
c1b64429b1 Fix run_via[]= backwards compatibility.
```
Minitest.run_via[:rails] = true
```

👆 would break because a simple alias won't catch the second
true argument there.
2017-02-21 08:28:28 +01:00