Sha256: ab1827f2e4e9080c9af7ba8997d9c7851b5770a0513cadfdf0e0a2efcc27a7dc

Contents?: true

Size: 1.91 KB

Versions: 2

Compression:

Stored size: 1.91 KB

Contents

# frozen_string_literal: true

require 'spec_helper'
require 'spree/testing_support/factories/user_factory'

RSpec.describe 'SolidusJwt Authentication', type: :request do
  let(:params) do
    { token: token }
  end

  let(:token) { nil }
  let(:user) { FactoryBot.create(:user, spree_api_key: 'secret') }

  it 'fails if user visits api without authenticating' do
    get spree.api_user_path(user.id)
    expect(response.status).to be(401)
  end

  context 'when jwt fails to decode' do
    let(:token) { 'abc.123.efg' }

    it 'renders invalid_api_key' do
      get spree.api_user_path(user.id), params: params

      expect(response.status).to be(401)
      expect(response.body).to include('Invalid API key')
    end
  end

  context 'when spree api key is allowed' do
    context 'when spree api key is present' do
      let(:token) { user.spree_api_key }

      it 'passes authentication' do
        get spree.api_user_path(user.id), params: params
        expect(response.status).to be(200)
      end
    end

    context 'when json web token is used' do
      let(:token) { user.generate_jwt }

      it 'passes authentication' do
        get spree.api_user_path(user.id), params: params
        expect(response.status).to be(200)
      end
    end
  end

  context 'when spree api key is not allowed' do
    before do
      SolidusJwt::Config.allow_spree_api_key = false
    end

    after do
      SolidusJwt::Config.allow_spree_api_key = true
    end

    context 'when spree api key is present' do
      let(:token) { user.spree_api_key }

      it 'fails authentication' do
        get spree.api_user_path(user.id), params: params
        expect(response.status).to be(401)
      end
    end

    context 'when json web token is used' do
      let(:token) { user.generate_jwt }

      it 'passes authentication' do
        get spree.api_user_path(user.id), params: params
        expect(response.status).to be(200)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
solidus_jwt-1.2.1 spec/requests/spree/api/json_web_tokens_spec.rb
solidus_jwt-1.2.0 spec/requests/spree/api/json_web_tokens_spec.rb