|
1 | 1 | require 'rails_helper' |
2 | 2 |
|
3 | 3 | describe EfeesInvoice do |
| 4 | + include ActiveJob::TestHelper |
| 5 | + |
4 | 6 | let(:alma_api_key) { 'fake-api-key' } |
5 | | - let(:request_headers) { { 'Accept' => 'application/json', 'Authorization' => "apikey #{alma_api_key}" } } |
| 7 | + let(:alma_id) { '10335026' } |
| 8 | + let(:request_headers) do |
| 9 | + { |
| 10 | + 'Accept' => 'application/json', |
| 11 | + 'Authorization' => "apikey #{alma_api_key}" |
| 12 | + } |
| 13 | + end |
6 | 14 |
|
7 | | - before do |
8 | | - alma_id = '10335026' |
| 15 | + context 'JWT generation' do |
| 16 | + before do |
| 17 | + allow(Rails.application.config) |
| 18 | + .to receive(:alma_api_key) |
| 19 | + .and_return(alma_api_key) |
9 | 20 |
|
10 | | - allow(Rails.application.config).to receive(:alma_api_key).and_return(alma_api_key) |
| 21 | + stub_request( |
| 22 | + :get, |
| 23 | + "https://api-na.hosted.exlibrisgroup.com/almaws/v1/users/#{alma_id}?expand=fees&view=full" |
| 24 | + ) |
| 25 | + .with(headers: request_headers) |
| 26 | + .to_return( |
| 27 | + status: 200, |
| 28 | + body: File.new('spec/data/fees/efee-lookup-data.json') |
| 29 | + ) |
11 | 30 |
|
12 | | - stub_request(:get, "https://api-na.hosted.exlibrisgroup.com/almaws/v1/users/#{alma_id}?expand=fees&view=full") |
13 | | - .with(headers: request_headers) |
14 | | - .to_return(status: 200, body: File.new('spec/data/fees/efee-lookup-data.json')) |
| 31 | + stub_request( |
| 32 | + :get, |
| 33 | + "https://api-na.hosted.exlibrisgroup.com/almaws/v1/users/#{alma_id}/fees" |
| 34 | + ) |
| 35 | + .with(headers: request_headers) |
| 36 | + .to_return( |
| 37 | + status: 200, |
| 38 | + body: File.new('spec/data/fees/efee-lookup-fees.json') |
| 39 | + ) |
| 40 | + end |
15 | 41 |
|
16 | | - stub_request(:get, "https://api-na.hosted.exlibrisgroup.com/almaws/v1/users/#{alma_id}/fees") |
17 | | - .with(headers: request_headers) |
18 | | - .to_return(status: 200, body: File.new('spec/data/fees/efee-lookup-fees.json')) |
| 42 | + it 'encodes the user info into a jwt' do |
| 43 | + invoice = EfeesInvoice.new(alma_id) |
| 44 | + decoded_token = EfeesInvoice.decode(invoice.jwt) |
19 | 45 |
|
20 | | - @invoice = EfeesInvoice.new(alma_id) |
| 46 | + expect(decoded_token[0]).to have_key('userName') |
| 47 | + expect(decoded_token[0]['userName']).to eq(alma_id) |
| 48 | + end |
21 | 49 | end |
22 | 50 |
|
23 | | - it 'encodes the user info into a jwt' do |
24 | | - decoded_token = EfeesInvoice.decode(@invoice.jwt) |
25 | | - expect(decoded_token[0] { userName }).to have_key('userName') |
26 | | - expect(decoded_token[0] { userName }).to have_value('10335026') |
27 | | - end |
| 51 | + describe '#submit!' do |
| 52 | + let(:alma_id) { '123456' } |
| 53 | + |
| 54 | + let(:user_double) do |
| 55 | + double( |
| 56 | + id: alma_id, |
| 57 | + email: 'test@example.com', |
| 58 | + name: 'Test User', |
| 59 | + fees: 100.0 |
| 60 | + ) |
| 61 | + end |
| 62 | + |
| 63 | + before do |
| 64 | + allow(Alma::User) |
| 65 | + .to receive(:find_if_exists) |
| 66 | + .with(alma_id) |
| 67 | + .and_return(user_double) |
| 68 | + end |
| 69 | + |
| 70 | + it 'enqueues the efee invoice email' do |
| 71 | + invoice = EfeesInvoice.new(alma_id) |
28 | 72 |
|
29 | | - it 'submits the request mailer' do # rubocop:disable RSpec/NoExpectationExample |
30 | | - @invoice.submit! |
| 73 | + expect do |
| 74 | + invoice.submit! |
| 75 | + end.to have_enqueued_job(ActionMailer::MailDeliveryJob) |
| 76 | + .with( |
| 77 | + 'RequestMailer', |
| 78 | + 'efee_invoice_email', |
| 79 | + 'deliver_now', |
| 80 | + { args: [alma_id] } |
| 81 | + ) |
| 82 | + end |
31 | 83 | end |
32 | 84 | end |
0 commit comments