What to do if you get an error in Basic authentication during Rails test code

Introduction

This time, I would like to describe the error in the title that appeared when implementing the unit test code of the controller. But first, I would like to briefly explain Basic authentication.

What is Basic authentication?

One of the ways to restrict access to websites. Basic authentication is the easiest way to apply restrictions. Basic authentication is one of the functions that comes with the Web server, and you can easily restrict access by just writing a few lines in the file.

** "Reference image for Basic authentication" ** Image from Gyazo

** "Site I quoted" ** What is Basic Authentication? Setting method and precautions-Explanation of the cause of the error

error contents

Now, let me explain the main subject of this time again. First of all, I got the following error </ font> when executing the unit test code of the controller.

Terminal


bundle exec rspec spec/requests/items_spec.rb

~Omission~

Failures:

  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:28:in `block (3 levels) in <top (required)>'

  2) ItemsController GET /When you request the index index action, the listed product name exists in the response.
     Failure/Error: expect(response.body).to include @item.goods
     
       expected "HTTP Basic: Access denied.\n" to include "Custard Apples Daikon"
       Diff:
       @@ -1 +1 @@
       -Custard Apples Daikon
       +HTTP Basic: Access denied.
       
~Omission~

Verification of error content

From the above contents, the following two points are considered to be the cause of this error </ font>.

Terminal


1)
       expected: 200
            got: 401

2)
expected "HTTP Basic: Access denied.\n" to include 

The first is expected, which is expected to be ** "200" (success) **, but the actual response was ** "401" (unauthenticated) **.

And since the word ** Basic ** appears in the second one, I assumed that it was rejected by the ** Basic authentication ** that I had set. (Well, the English content ** (HTTP Basic: Access denied) ** also simply means that access was denied by HTTP Basic.)

From the above two error contents, it was judged that the test code failed because the basic authentication part set in ** "application_controller.rb" ** was not passed.

Therefore, I decided to comment out the relevant Basic authentication part only when executing the test code.

application_controller.rb


class ApplicationController < ActionController::Base
  before_action :basic_auth (Comment out this line)

Then run the unit test code again

Terminal


 bundle exec rspec spec/requests/items_spec.rb

ItemsController
  GET /index
When I request the index action, the response is returned normally.
When you request the index action, the listed product name exists in the response.
When you request the index action, there is an image of the listed item in the response
When you request the index action, the selling price of the listed item exists in the response
When you request the index action, there is a list of newly posted products in the response
  
Finished in 3.21 seconds (files took 4.76 seconds to load)
5 examples, 0 failures

As mentioned above, the test code was successfully executed. </ font>

in conclusion

When I was investigating, it seemed that there was a way to do the test code while leaving the basic authentication part, but it seemed a bit complicated, so this time I tried this simple method.

The site that I referred to in this article

--Reference site for HTTP response status code -What is Basic Authentication? Explanation of setting method and precautions / causes of error

Recommended Posts