Problem with Rspec testing sign_in (Devise) with Request and rswag

2.3k Views Asked by At

I was tempted to use rswag with rspec to document REST API and write test at the same time. I am trying to fallow tutorials and documentations but I cannot get sign_in endpoint working ( devise - session ).

When I do run rspec than I receive status code error.

require 'swagger_helper'
require 'rails_helper'
require 'shared_context'

describe 'Sonaaar REST API', type: :request do

...

  path '/users/sign_in' do
    post 'Sign In' do
      tags 'Session'
      consumes 'application/json'
      produces 'application/json'
      parameter name: :user, in: :body, schema: {
        type: :object,
        properties: {
          email: { type: :string },
          password: { type: :string },
        },
        required: ['email', 'password']
      }

     response '201', 'sign in', { 'HTTP_ACCEPT' => "application/json" } do
         response '201', 'sign in', { 'HTTP_ACCEPT' => "application/json" } do
        #
        # let(:user) do
        #   create(:user,  email: '[email protected]', password: 'Password1')
        # end
        #Error
        # Failure/Error:
        #        raise UnexpectedResponse,
        #          "Expected response code '#{response.code}' to match '#{expected}'\n" \
        #            "Response body: #{response.body}"
        #
        #      Rswag::Specs::UnexpectedResponse:
        #        Expected response code '401' to match '201'
        #        Response body: {"error":"You need to sign in or sign up before continuing."}


        let(:user) { { user: { login: '[email protected]', password: 'Password1' } } }
        # Rswag::Specs::UnexpectedResponse:
        #   Expected response code '401' to match '201'
        #   Response body: {"error":"You need to sign in or sign up before continuing."}
        #   /Users/filip/.rvm/gems/ruby-2.7.1/gems/rswag-specs-2.4.0/lib/rswag/specs/r
        run_test!
      end

Than i do have RSpec error:

1) Sonaaar REST API /users/sign_in post sign in returns a 201 response
     Failure/Error:
       raise UnexpectedResponse,
         "Expected response code '#{response.code}' to match '#{expected}'\n" \
           "Response body: #{response.body}"

     Rswag::Specs::UnexpectedResponse:
       Expected response code '401' to match '201'
       Response body: {"error":"You need to sign in or sign up before continuing."}

Authentication: JWT-token Content Type: application/json Stack/Gems:

2

There are 2 best solutions below

0
On

Request specs need a little bit more help than just the normal warden helpers that work for feature specs:

RSpec.configure do |config|
  config.include Warden::Test::Helpers
end

Add to rails_helper or another file and require in rails_helper:

module DeviseRequestSpecHelpers

  include Warden::Test::Helpers

  def sign_in(user)
    login_as(user, scope: :user)
  end

  def sign_out
    logout(:user)
  end

end

Then in rails_helper add

RSpec.configure do |config|
  config.include DeviseRequestSpecHelpers, type: :request
end

You should be able to login now in request specs with:

context 'some context' do
  scenario 'some scenario' do
    login_as(user)
  end
end
0
On

as @sam said, but actually you can just include the warden helpers

RSpec.configure do |config|
  config.include Warden::Test::Helpers
end

And then

let(:admin) { create(:admin) } # for this case it's devise :admins

before do
  login_as(admin, scope: :admin)
end

# If you want to logout admin
after do
  logout(:admin)
end

it 'should return 200 'do
   get 'some_path_that_needs_session'
   expect(response).to have_http_status(200)
end


You can refer to [Warden::Test::Helpers] (https://www.rubydoc.info/github/hassox/warden/Warden/Test/Helpers) docs