Since I implemented a copy application of a certain flea market site, I was writing a unit test code of the controller as a practice, but the following error occurred on the terminal at the time of execution.
Terminal
bundle exec rspec spec/requests/items_spec.rb
~Omission~
1) ItemsController GET #When you request the index index action, the response is returned normally.
Failure/Error: expect(response.status).to eq 200
expected: 200
got: 401
(compared using ==)
# ./spec/requests/items_spec.rb:20:in `block (3 levels) in <top (required)>'
~Omission~
Ruby 2.6.5 Rails 6.0.3.3 MySQL Visual Studio Code (Caprybara,Rspec,GoogleChrome)
An error pattern that the HTTP status is 401 (not accessible) when accessing root_path. This application has introduced ** basic authentication **, and I expected it to be related.
It is a user authentication mechanism provided in the HTTP communication standard, and is one of the simple methods for restricting access to websites. Set the user and password that can communicate with the server in advance, and make the Web application available only to the users who match it.
【sample image】 When I checked with the above error statement and basic authentication, there were cases where basic authentication did not pass during the test. Based on the reference article (posted at the end of the article), I tried to manage to pass the basic authentication user name and password to the test request.
Fighting with a mentor for about 2 hours ... I tried various methods based on the reference article, but the error was not resolved. ~~ And when the mentor investigated ... ** "As of 2020, basic authentication during testing is not recommended" **. ~~
What a mess ...!
The error resolution policy is from "passing basic authentication at the time of testing" Changed to "Do not perform basic authentication except in production environment (during test)".
application_controller.rb
class ApplicationController < ActionController::Base
before_action :basic_auth if Rails.env.production? #Branch to not perform basic authentication during testing
private
def basic_auth
authenticate_or_request_with_http_basic do |username, password|
username == 'username' && password == 'password'
end
end
end
I added ** if Rails.env.production? ** to before_action so that it applies only in the production environment. Thanks to that, you can now access the top page and execute the test without basic authentication during the test!
Since I am still a beginner, when I face an error, I tend to try to solve the problem head-on. Like this time, I realized that it is important to think from various perspectives whether there is a way to avoid errors and "do not have to do it" **.
This article was the first post. I hope you find it helpful. Thank you for browsing to the end.
What is Basic Authentication? Setting method and precautions-Explanation of the cause of the error [Rails x Basic authentication x Rspec stumbled](https://doruby.jp/users/hello_rails/entries/Rails%C3%97Basic%E8%AA%8D%E8%A8%BC%C3%97Rspec%E3 % 81% A7% E3% 81% A4% E3% 81% BE% E3% 81% A5% E3% 81% 84% E3% 81% 9F) Testing BASIC-authenticated actions with RSpec3 Pass Rails / Rspec test with http basic authentication How to write test code with Basic authentication
2020/10/25 Please point out and make the following corrections.
――As for the deprecation, I heard it only verbally and the source is uncertain, so I added a strikethrough. (I hope you can catch it to the extent that there is such a rumor.)
――In short, I changed the title because it was a story that ** could not handle the error **.
Thank you, @jnchito.
Recommended Posts