markers_spec.rb 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'API Markers' do
  4. let(:user) { Fabricate(:user) }
  5. let(:scopes) { 'read:statuses write:statuses' }
  6. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  7. let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
  8. describe 'GET /api/v1/markers' do
  9. before do
  10. Fabricate(:marker, timeline: 'home', last_read_id: 123, user: user)
  11. Fabricate(:marker, timeline: 'notifications', last_read_id: 456, user: user)
  12. get '/api/v1/markers', headers: headers, params: { timeline: %w(home notifications) }
  13. end
  14. it 'returns markers', :aggregate_failures do
  15. json = body_as_json
  16. expect(response).to have_http_status(200)
  17. expect(json.key?(:home)).to be true
  18. expect(json[:home][:last_read_id]).to eq '123'
  19. expect(json.key?(:notifications)).to be true
  20. expect(json[:notifications][:last_read_id]).to eq '456'
  21. end
  22. end
  23. describe 'POST /api/v1/markers' do
  24. context 'when no marker exists' do
  25. before do
  26. post '/api/v1/markers', headers: headers, params: { home: { last_read_id: '69420' } }
  27. end
  28. it 'creates a marker', :aggregate_failures do
  29. expect(response).to have_http_status(200)
  30. expect(user.markers.first.timeline).to eq 'home'
  31. expect(user.markers.first.last_read_id).to eq 69_420
  32. end
  33. end
  34. context 'when a marker exists' do
  35. before do
  36. post '/api/v1/markers', headers: headers, params: { home: { last_read_id: '69420' } }
  37. post '/api/v1/markers', headers: headers, params: { home: { last_read_id: '70120' } }
  38. end
  39. it 'updates a marker', :aggregate_failures do
  40. expect(response).to have_http_status(200)
  41. expect(user.markers.first.timeline).to eq 'home'
  42. expect(user.markers.first.last_read_id).to eq 70_120
  43. end
  44. end
  45. context 'when database object becomes stale' do
  46. before do
  47. allow(Marker).to receive(:transaction).and_raise(ActiveRecord::StaleObjectError)
  48. post '/api/v1/markers', headers: headers, params: { home: { last_read_id: '69420' } }
  49. end
  50. it 'returns error json' do
  51. expect(response)
  52. .to have_http_status(409)
  53. expect(body_as_json)
  54. .to include(error: /Conflict during update/)
  55. end
  56. end
  57. end
  58. end