Troubleshooting RSpec API Request Tests: Investigating a 401 Error
Asked by fero46:

Hello everyone,

I attempted to use the pipeline feature and it appears to be functioning properly. However, I encountered an issue with my RSpec tests that I was unable to resolve. Specifically, all of my request tests failed.

It's particularly strange because it works perfectly fine on my local environment. The following aspects are functioning correctly: DB, Redis, migration, test running, and model tests.

Unfortunately, the API request tests are not working as expected. Returns a 401 error.  

```
Gemfile

# frozen_string_literal: true

source 'https://rubygems.org'

git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '3.0.5'

# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"

gem 'rails', '~> 7.0.4', '>= 7.0.4.2'

# Use mysql as the database for Active Record

gem 'mysql2', '~> 0.5'

# Use the Puma web server [https://github.com/puma/puma]

gem 'puma', '~> 5.0'

# Build JSON APIs with ease [https://github.com/rails/jbuilder]

gem 'jbuilder'

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem

gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby]

# Reduces boot times through caching; required in config/boot.rb

gem 'bootsnap', require: false

# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]

gem 'image_processing', '~> 1.2'

gem 'devise'

gem 'devise-jwt'

# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible

gem 'rack-cors'

group :development, :test do

# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem

gem 'debug', platforms: %i[mri mingw x64_mingw]

gem 'dotenv-rails'

gem 'rspec-rails', '~> 6.0.0'

end

gem 'rubocop-rails', require: false

group :test do

gem 'shoulda', '~> 4.0'

end

group :development do

# Speed up commands on slow machines / big apps [https://github.com/rails/spring]

# gem "spring"

end

gem 'factory_bot', '~> 6.2'

gem 'pundit', '~> 2.3'

gem 'friendly_id', '~> 5.5'

gem 'aws-sdk-s3', require: false

gem 'acts_as_paranoid', '~> 0.8.1'

gem 'acts_as_list', '~> 1.1'

gem 'activestorage-validator', '~> 0.2.2'

gem 'state_machines', '~> 0.5.0'

gem 'sidekiq', '~> 7.0'

gem 'kaminari', '~> 1.2'

gem 'paranoia', '~> 2.6'


Test Example Results:


```

/core/ecommerces POST /create with valid parameters renders a JSON response with the new core_ecommerce

```
?
```

`Failure/Error: expect(response).to have_http_status(:created)`


    expected the response to have status code :created (201) but it was :unauthorized (401)

    ./spec/requests/core/ecommerces_spec.rb:84:in `block (4 levels) in <main>'

```
 
```


Answers:
author: Erez Maadani
body: Hi [@fero46](/t5/user/viewprofilepage/user-id/5203203)

Based on error code and the fact that it works on your local, my first hunch is that your authentication secrets are not passing correctly to your code.

If you are passing the secrets as env parameters, try running \`printenv\` at the beginning of the step to make sure you get the secrets as expected.

If not, try pulling the step image locally and then run the same commands on your local machine. This should enable you to reproduce and debug the problem locally.

comments: author: fero46
body: Yes, this was the problem. I had to add the JWT environment secret. Thank you for helping.


Tags: bitbucket-cloud

cloud