How can I get a valid Authenticity Token with my Rails Console? - ruby-on-rails

I am trying to use my rails console to call a public post method in my controller.
rails c
app.post '/servers/important_method'
This obviously gives me:
ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):
Is there any way to create a session and call that method?
EDIT: I could create a view and restart the production environment, but I'd like to avoid that.

The key here is recognising app is an instance of ActionDispatch::Integration::Session, which includes ActionDispatch::TestProcess and therefore has a #session method that'll supply the authenticity token, once you've woken it up with a request.
We can then use this via the app::post helper, itself a simple wrapper for app's own #process method, which documents the calling parameters.
Putting all of those pieces together, in Rails 5 and later, we might write:
app.get ''
token = app.session[:_csrf_token]
app.post '/servers/important_method', params: { authenticity_token: token }
Note for older versions of Rails:
Since we're talking to the integration test API here, you may use similar forms in integration tests. When upgrading older Rails apps (v4 and earlier), you may find the following test code failing:
post '/servers/important_method', authenticity_token: token
and this should be revised to the new syntax, wrapping the parameters in params: { ... }.

Related

Integration Test Session not loading Session Cookie

Rails version: 5.1.1, upgrading from 4.2.7
I have an integration test (inherting from ActionDispatch::IntegrationTest) which hits a URL. The controller at that URL puts some stuff into the session. The test then tries to assert that certain values are now present in the session.
My session_store.rb:
Rails.application.config.session_store :cookie_store, key: '_companySite_session', expire_after: 1.year
What I'm finding is that the session variable, called from the integration test as 'session', is always an empty hash ({}).
If I look at request.host in the test, I see "www.example.com". If I look at response.cookies or cookies, I see:
#<Rack::Test::Cookie:0x00000006037108
#default_host="www.example.com",
#name="_companySite_session",
#name_value_raw=
"_companySite_session=a3RnYjRvWDlUYzRZaVlaTnlsK2RkeXBWcWFNYnQ1RVo4azNqdXNOanVWWWZVT2o5Vk0wTkVhY1dFOTBxdWNzMU5sY3c1cXhXRm0xT3JZWWZGUnluSnRuYTVoaWQ1S3ZZRXVMa1YyQ2pUMjEvOExaWkcybTdGT3lUZnZ0STRSSGVpc0dPU1VONWNwTEZxQlRvODN3eXdKZTYreVIvNDR4Y0IxMms2djlPWUZCcktYWmo1SElSZ0ttaHA5UWk5MDA5aVJzaGt3cUJ4V2tFY295ZGo4ak8ydUI3OWRnTUdkcmRpanVML3phalJtZ2c1OXByeExiRDhlcy9rR1RYUGh6NlkxV2xqNWl2VjlCMm45WXo0ajBKN0VTQ29CVGRQRVErOVIrNFZwdEZBc01oL2RaSGltUjdleGlWN3YySTdJUzcvbVlJbWdwWVNUVkw4K1pPc3JsUWxOR1BZNFVrUXFtYVVObmNlYlZrSklEbk5aU3VTck5MNm12a0NBMVF2aG5xLS1pSEJTVTVLZzJ2ZVA2M1IzTmJ4Njh3PT0%3D--f253c4e1678e53896a247b087e741cf006c5874f",
#options={"path"=>"/", "expires"=>"Fri, 08 Jun 2018 19:46:22 -0000", "HttpOnly"=>nil, "domain"=>"www.example.com"},
#value=
"a3RnYjRvWDlUYzRZaVlaTnlsK2RkeXBWcWFNYnQ1RVo4azNqdXNOanVWWWZVT2o5Vk0wTkVhY1dFOTBxdWNzMU5sY3c1cXhXRm0xT3JZWWZGUnluSnRuYTVoaWQ1S3ZZRXVMa1YyQ2pUMjEvOExaWkcybTdGT3lUZnZ0STRSSGVpc0dPU1VONWNwTEZxQlRvODN3eXdKZTYreVIvNDR4Y0IxMms2djlPWUZCcktYWmo1SElSZ0ttaHA5UWk5MDA5aVJzaGt3cUJ4V2tFY295ZGo4ak8ydUI3OWRnTUdkcmRpanVML3phalJtZ2c1OXByeExiRDhlcy9rR1RYUGh6NlkxV2xqNWl2VjlCMm45WXo0ajBKN0VTQ29CVGRQRVErOVIrNFZwdEZBc01oL2RaSGltUjdleGlWN3YySTdJUzcvbVlJbWdwWVNUVkw4K1pPc3JsUWxOR1BZNFVrUXFtYVVObmNlYlZrSklEbk5aU3VTck5MNm12a0NBMVF2aG5xLS1pSEJTVTVLZzJ2ZVA2M1IzTmJ4Njh3PT0=--f253c4e1678e53896a247b087e741cf006c5874f">
In other words, the session cookie seems to be present, and its domain seems to match the request's host. I've also used the code from How to decrypt a Rails 5 session cookie manually? to print out the value of this cookie, and the value is correct.
My test environment's config specifies config.action_controller.allow_forgery_protection = false
At this point, I'm out of ideas. The cookie is there, it seems fine, its set to the domain of the request, its not expired, but session remains an empty hash. Is it not getting updated somehow? Is it trying to update but failing silently? I've run out of things to try.
This test works fine under Rails 4.2.7.
While I haven't dived into it as fully, I've also been finding that tests' flash variable always seems to be empty, too. Again, tests that worked fine in Rails 4 are breaking because the flash always seems to be empty. I assume the flash and the session are related, so maybe the same problem affects them both.
The solution was that we had this setting in our test.rb environment file:
config.allow_concurrency = false
For some reason, after making a request in the test, this setting causes the session to be replaced with an empty hash. Rails' docs suggest that you not set this flag directly: http://guides.rubyonrails.org/v3.2/configuring.html.
I think this is something we had set in Rails 2, then got carried into Rails 4 when we upgraded, and now breaks in Rails 5.
I've logged it as an issue in the Rails repo: https://github.com/rails/rails/issues/29608

Outputting HTTP request/response data with RSpec

I have a rails project that serves a JSON API with tests written in RSpec. Often when running specs (request specs, specifically), I’m interested in seeing some details about the HTTP request/response...i.e. the request URL, request body, and response body, ideally JSON pretty-formatted for readability. This isn't for the purposes of documentation but rather as part of the development / debugging process.
I have a helper method I wrote which does this...you just drop a method call into your spec and it prints this stuff out.
But, seems like it would be better if there was a switch that’s part of the running specs. RSpec has custom formatters which I thought might be the right direction, but in trying to build one, I can't figure out how to get access to the request/response objects like you can from inside of your spec.
How can I access the request/response objects in my custom RSpec formatter? Or, perhaps another way to approach the problem?
Here's an approach:
Assuming a rails project, in spec_helper.rb, define a global "after" hook like so:
config.after(:each) do #runs after each example
if ENV['PRINTHTTP']
#use request/response objects here, e.g. puts response.status
end
end
Then, you can conditionally enable by adding the environmental variable on the command-line:
$ PRINTHTTP=1 rspec

How to test AJAX with Ruby on Rails?

Can somebody give advice, how to test AJAX in Ruby on Rails?
For example: i click to button, query going to TestController#new, and in that method we have respond_with(#test) and new.js.erb, where is html form. Ok, i can click_to this link/button, xhr query will work, but i don't get any responses, which i need to test.
Can some body help with this, please?
I'm using Rspec.
In Rails 4.2 , for send ajax request, set optional hash argument with key "xhr: true" to call of get, post, patch, put, or delete method.
For example:
get users_path + '?page=2', xhr: true
and then use assert_select for verify some HTML elements on updated page.
You should use Capybara and Selenium. Look at this great Railscast.
Depending on what your Ajax request has .. it may actually be best to test it on multiple levels.
Test your controller's response with a controller test.
Test your JS correctly dealing with this response with a pure JS testing framework like jasmine.
Test your flow through the application with something like cucumber, or an integration rspec (with the help of capybara/selenium).

Sending emails in test mode with ActionMailer in rails 3

I am having a slightly odd problem with sending mail in test mode with Rails 3
It seems that my mailers are not returning anything. For example I have a mailer called UserMailer. Users can make changes that require approval in the app so this has a method called changes_approved that should send the user an email notifying them that their changes have been approved.class
UserMailer < ActionMailer::Base
default :from => "from#example.com"
def changes_approved(user, page)
#user = user
#page = page
mail(:to => user.email, :subject => "Your changes have been approved")
end
end
In my controller I have the following line
UserMailer.changes_approved(#page_revision.created_by, #page_revision.page).deliver
However my tests fail at this point with the error:
undefined method `deliver' for nil:NilClass
When I trigger the same actions on the development site tho (http://localhost:3000 through a browser), the emails are sent out correctly and everything works quite happily
And to add further confusion, I am using devise for authentication and the emails for that seem to be working correctly both in test and development modes. Certainly I am not getting this same error and according to my email-spec tests, everythings working
So this leads me to believe that I have a problem with my mailers rather than my test mail config per se but I have no idea what. Any suggestions would be much appreciated
Thanks
I used https://gist.github.com/1031144
to convert
# Rails 2 method:
UserMailer.should_receive(:deliver_signup)
to
# Cumbersome Rails 3 method:
mailer = mock
mailer.should_receive(:deliver)
UserMailer.should_receive(:signup).and_return(mailer)
I had a similar problem - probably the UserMailer.changes_approved method is being replaced with a mock method, which returns nil (I wasn't using shoulda for that test, but that's my best guess).
My code looked like this (modified to use your example):
UserMailer.expects(:changes_approved).once
I fixed it with an additional stub:
#mailer = stub(:deliver)
UserMailer.expects(:changes_approved).once.returns(#mailer)
The nil is now replaced with #mailer.
To test the delayed action mailer we need to first change the configuration of delayed_job (in config/initializers/delayed_job_config.rb) to
Delayed::Worker.delay_jobs = !Rails.env.test?
and in your tests the expectation should be set to
mock_mail = mock(:mail)
mock_mail.should_receive(:deliver)
UserMailer.should_receive(:changes_approved).with(user, page).and_return(mock_mail)
Well I have found the answer,
it looks like the problem was in the way I was testing these mailers. In each of the controller tests I had a line similar to
UserMailer.should_receive(:changes_approved).with(user, page)
Whilst this test was passing fine, it appeared to break the mailer itself. I have removed this line from the tests and now they pass ok. Subsequent tests against ActionMailer::Base.deliveries.last to check the details of the sent email are correct appear to be ok so I am happy that this line is not neccessary.
If anyone has an explanation as to why this breaks tho, I would be interested to find out
Thanks anyways

Tests pass using "autotest" but not "rake test" using Authlogic

My tests fail when doing "rake test:functionals" but they pass consistently using autotest.
The failing tests in question seems to be related to Authlogic not logging in the user properly when using rake.
For facilitating signing in a user in tests, I have a test helper method as follows:
class ActionController::TestCase
def signin(user, role = nil)
activate_authlogic
UserSession.create(user)
user.has_role!(role) if role
end
end
The above method is used to signin a user
My stack is shoulda/authlogic/acl9/factory_girl/mocha
The reason why I suspect Authlogic being the issue is the failing tests look like this:
54) Failure:
test: A logged in user PUT :update with valid data should redirect to user profile. (UsersControllerTest)
[/var/lib/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/action_controller/macros.rb:202:in `__bind_1251895098_871629'
/var/lib/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/context.rb:351:in `call'
/var/lib/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/context.rb:351:in `test: A logged in user PUT :update with valid data should redirect to user profile. ']:
Expected response to be a redirect to <http://test.host/users/92> but was a redirect to <http://test.host/signin>.
55) Failure:
test: A logged in user PUT :update with valid data should set the flash to /updated successfully/i. (UsersControllerTest)
[/var/lib/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/assertions.rb:55:in `assert_accepts'
/var/lib/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/action_controller/macros.rb:41:in `__bind_1251895098_935749'
/var/lib/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/context.rb:351:in `call'
/var/lib/gems/1.8/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/context.rb:351:in `test: A logged in user PUT :update with valid data should set the flash to /updated successfully/i. ']:
Expected the flash to be set to /updated successfully/i, but was {:error=>"You must be signed in to access this page"}
Autotest reads all test files upfront AFAIR (it does so with RSpec, I haven't been using plain tests for a long time now so I may be wrong).
To properly test controllers you need to call activate_authlogic in your setUp method. This is probably done automatically (globally) for integration tests.
Since autotest reads all tests it runs this global setUp and functional tests pass. When you run only functional tests authlogic is not enabled and your tests fail.
I'm not sure about where your problem lies, but I suggest you use Cucumber for testing controllers and user interaction instead of unit tests/rspec. The reason for that is that you exercise your entire app, including the authentication and authorization code you have.
Clearly the user is not getting logged in. Seems like Bragi Ragnarson might be on to something.
Here are some other things to isolate the problem:
Understand if the test is incomplete or relying on some side-effect of autotest. Run the test by itself:
ruby test/functionals/users_controllers_test.rb
Presumably that won't work. If it doesn't, there's some global code that is getting invoked for non functional tests by autotest. It's probably code setting in the test/integration or test/units directories, or one of their requires there.

Resources