Sha256: 1db412d5f07632c1b2f1db283784adc2baa060b29f45de0702b1e6cac28c8b72

Contents?: true

Size: 1.24 KB

Versions: 6

Compression:

Stored size: 1.24 KB

Contents

RSpec.describe Commons::Authentication::JSONWebToken do
  let(:user) { create(:user) }
  let(:exp) { 24.hours.from_now }
  let(:jwt) { Commons::Authentication::JSONWebToken.encode({ user_id: user.id }, exp) }

  describe 'decode' do
    subject { Commons::Authentication::JSONWebToken.decode(jwt) }

    context 'works ok' do
      it do
        expect { subject }.not_to raise_error
        expect(subject[:user_id]).not_to be_nil
        expect(subject[:expires_at]).not_to be_nil
        expect(subject[:user_id]).to eq user.id
        expect(subject[:expires_at]).to eq exp.to_i
      end
    end

    context 'raises error when invalid' do
      let(:jwt) { Faker::Name.last_name }
      it do
        expect { subject }.to raise_error JWT::DecodeError
      end
    end
  end

  describe 'encode' do
    subject { Commons::Authentication::JSONWebToken.encode({ user_id: user.id }, exp) }

    context 'works ok' do
      it { expect { subject }.not_to raise_error }
      it do
        token = Commons::Authentication::JSONWebToken.decode(subject)
        expect(token[:user_id]).not_to be_nil
        expect(token[:user_id]).to eq user.id
        expect(token[:expires_at]).not_to be_nil
        expect(token[:expires_at]).to eq exp.to_i
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
commons_yellowme-0.16.0 spec/commons/authentication/json_web_token_spec.rb
commons_yellowme-0.15.0 spec/commons/authentication/json_web_token_spec.rb
commons_yellowme-0.12.0 spec/commons/authentication/json_web_token_spec.rb
commons_yellowme-0.11.3 spec/commons/authentication/json_web_token_spec.rb
commons_yellowme-0.11.2 spec/commons/authentication/json_web_token_spec.rb
commons_yellowme-0.11.1 spec/commons/authentication/json_web_token_spec.rb