-
Notifications
You must be signed in to change notification settings - Fork 57
/
token_service_spec.rb
58 lines (45 loc) · 1.47 KB
/
token_service_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# frozen_string_literal: true
require 'rails_helper'
describe CheckIn::Map::TokenService do
subject { described_class.build(opts) }
let(:patient_icn) { '123' }
let(:opts) { { patient_icn: } }
let(:memory_store) { ActiveSupport::Cache.lookup_store(:memory_store) }
before do
allow(Rails).to receive(:cache).and_return(memory_store)
Rails.cache.clear
end
describe '.build' do
it 'returns an instance of Service' do
expect(subject).to be_an_instance_of(described_class)
end
end
describe '#initialize' do
it 'has a redis client' do
expect(subject.redis_client).to be_a(CheckIn::Map::RedisClient)
end
end
describe '#token' do
let(:access_token) { 'test-token-123' }
let(:expiration) { Time.zone.now + 20 }
context 'when it exists in redis' do
before do
allow_any_instance_of(CheckIn::Map::RedisClient).to receive(:token).and_return(access_token)
end
it 'returns token from redis' do
expect(subject.token).to eq(access_token)
end
end
context 'when it does not exist in redis' do
before do
expect_any_instance_of(MAP::SecurityToken::Service).to receive(:token)
.and_return({ access_token:, expiration: })
end
it 'returns token by calling client and saves it in redis' do
redis_client = subject.redis_client
expect(redis_client).to receive(:save_token)
expect(subject.token).to eq(access_token)
end
end
end
end