Correspond to "error that basic authentication does not pass" in the test code "The story that could not be done"

Situation at the time of error

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~
Development environment

Ruby 2.6.5 Rails 6.0.3.3 MySQL Visual Studio Code (Caprybara,Rspec,GoogleChrome)

Consideration / verification

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.

What is basic authentication?

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】 スクリーンショット 2020-10-24 19.44.28.png 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.

result

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 ...!

* ~ If you can't, don't do it ~ *

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!

Impressions

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.

Reference article

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

Correction history

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

Correspond to "error that basic authentication does not pass" in the test code "The story that could not be done"
The story of stopping because the rails test could not be done
The story that the Servlet could not be loaded in the Java Web application
The story that the forced update could not be implemented
The case that @Autowired could not be used in JUnit5
What to do if you get an error in Basic authentication during Rails test code
Story when migration could not be done
The case that "apt-get update" defined in "Dockerfile" could not be done at the time of "docker-compose build"
About the case that ("b" .. "aa") could not be used in Ruby Range
[Error] How to resolve the event that the screen does not transition after editing
Code written in Java-TeraPad starting from beginner does not work due to error
How to write test code with Basic authentication
Solved the problem that the test could not be executed from the command line with spring-boot-starter-test
The story that the variable initialization method called in the Java constructor should not be overridden
PATH does not pass ... The cause is the character code
The story that led to solving the error because postgres did not start with docker-compose up
What to do if you don't see the test code error message in the terminal console
The story that the request parameter from the iPhone application could not be obtained successfully with the Servlet
The story when the test folder was not created in Rails
Introduction to Rakefile that can be done in about 10 minutes
Java 14 new features that could be used to write code
[Docker] The story that an error occurred in docker-compose up
A collection of patterns that you want to be aware of so as not to complicate the code
[Rails] A story that continued to incorrectly verify the reason why the update action did not pass (update)
How to fix the problem that Aptana Studio does not start
[Gradle] The story that the class file did not exist in the jar file
Summary of problems that I could not log in to firebase
Note that Insert could not be done with Spring Data JDBC
The story that Tomcat suffered from a timeout error in Eclipse
The story that the build error did not stop when using Eclipse 2020
Pay.JP Solution when it can be done locally but it does not work well in the production environment
A story that even a man who does not understand C language could add new functions to Ruby 2.6
Pass the i18n locale to JavaScript
How to interact with a server that does not crash the app
[Rails] About the error that the image is not displayed in the production environment
JSESSIONID could not be assigned to the URL when using Spring Security