annual_reports_spec.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'API V1 Annual Reports' do
  4. let(:user) { Fabricate(:user) }
  5. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  6. let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
  7. describe 'GET /api/v1/annual_reports' do
  8. context 'when not authorized' do
  9. it 'returns http unauthorized' do
  10. get api_v1_annual_reports_path
  11. expect(response)
  12. .to have_http_status(401)
  13. expect(response.content_type)
  14. .to start_with('application/json')
  15. end
  16. end
  17. context 'with wrong scope' do
  18. before do
  19. get api_v1_annual_reports_path, headers: headers
  20. end
  21. it_behaves_like 'forbidden for wrong scope', 'write write:accounts'
  22. end
  23. context 'with correct scope' do
  24. let(:scopes) { 'read:accounts' }
  25. it 'returns http success' do
  26. get api_v1_annual_reports_path, headers: headers
  27. expect(response)
  28. .to have_http_status(200)
  29. expect(response.content_type)
  30. .to start_with('application/json')
  31. expect(response.parsed_body)
  32. .to be_present
  33. end
  34. end
  35. end
  36. describe 'POST /api/v1/annual_reports/:id/read' do
  37. context 'with correct scope' do
  38. let(:scopes) { 'write:accounts' }
  39. it 'returns success and marks the report as read' do
  40. annual_report = Fabricate :generated_annual_report, account: user.account
  41. expect { post read_api_v1_annual_report_path(id: annual_report.year), headers: headers }
  42. .to change { annual_report.reload.viewed? }.to(true)
  43. expect(response)
  44. .to have_http_status(200)
  45. expect(response.content_type)
  46. .to start_with('application/json')
  47. end
  48. end
  49. end
  50. end