Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Troubleshooting RSpec API Request Tests: Investigating a 401 Error

fero46 April 24, 2023

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>'
 

1 answer

1 accepted

1 vote
Answer accepted
Erez Maadani
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 24, 2023

Hi @fero46 

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.  

fero46 April 24, 2023

Yes, this was the problem. I had to add the JWT environment secret. Thank you for helping.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events