lists_spec.rb 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'Lists' do
  4. let(:user) { Fabricate(:user) }
  5. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  6. let(:scopes) { 'read:lists write:lists' }
  7. let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
  8. describe 'GET /api/v1/lists' do
  9. subject do
  10. get '/api/v1/lists', headers: headers
  11. end
  12. let!(:lists) do
  13. [
  14. Fabricate(:list, account: user.account, title: 'first list', replies_policy: :followed),
  15. Fabricate(:list, account: user.account, title: 'second list', replies_policy: :list),
  16. Fabricate(:list, account: user.account, title: 'third list', replies_policy: :none),
  17. Fabricate(:list, account: user.account, title: 'fourth list', exclusive: true),
  18. ]
  19. end
  20. let(:expected_response) do
  21. lists.map do |list|
  22. {
  23. id: list.id.to_s,
  24. title: list.title,
  25. replies_policy: list.replies_policy,
  26. exclusive: list.exclusive,
  27. }
  28. end
  29. end
  30. before do
  31. Fabricate(:list)
  32. end
  33. it_behaves_like 'forbidden for wrong scope', 'write write:lists'
  34. it 'returns the expected lists', :aggregate_failures do
  35. subject
  36. expect(response).to have_http_status(200)
  37. expect(body_as_json).to match_array(expected_response)
  38. end
  39. end
  40. describe 'GET /api/v1/lists/:id' do
  41. subject do
  42. get "/api/v1/lists/#{list.id}", headers: headers
  43. end
  44. let(:list) { Fabricate(:list, account: user.account) }
  45. it_behaves_like 'forbidden for wrong scope', 'write write:lists'
  46. it 'returns the requested list correctly', :aggregate_failures do
  47. subject
  48. expect(response).to have_http_status(200)
  49. expect(body_as_json).to eq({
  50. id: list.id.to_s,
  51. title: list.title,
  52. replies_policy: list.replies_policy,
  53. exclusive: list.exclusive,
  54. })
  55. end
  56. context 'when the list belongs to a different user' do
  57. let(:list) { Fabricate(:list) }
  58. it 'returns http not found' do
  59. subject
  60. expect(response).to have_http_status(404)
  61. end
  62. end
  63. context 'when the list does not exist' do
  64. it 'returns http not found' do
  65. get '/api/v1/lists/-1', headers: headers
  66. expect(response).to have_http_status(404)
  67. end
  68. end
  69. end
  70. describe 'POST /api/v1/lists' do
  71. subject do
  72. post '/api/v1/lists', headers: headers, params: params
  73. end
  74. let(:params) { { title: 'my list', replies_policy: 'none', exclusive: 'true' } }
  75. it_behaves_like 'forbidden for wrong scope', 'read read:lists'
  76. it 'returns the new list', :aggregate_failures do
  77. subject
  78. expect(response).to have_http_status(200)
  79. expect(body_as_json).to match(a_hash_including(title: 'my list', replies_policy: 'none', exclusive: true))
  80. expect(List.where(account: user.account).count).to eq(1)
  81. end
  82. context 'when a title is not given' do
  83. let(:params) { { title: '' } }
  84. it 'returns http unprocessable entity' do
  85. subject
  86. expect(response).to have_http_status(422)
  87. end
  88. end
  89. context 'when the given replies_policy is invalid' do
  90. let(:params) { { title: 'a list', replies_policy: 'whatever' } }
  91. it 'returns http unprocessable entity' do
  92. subject
  93. expect(response).to have_http_status(422)
  94. end
  95. end
  96. end
  97. describe 'PUT /api/v1/lists/:id' do
  98. subject do
  99. put "/api/v1/lists/#{list.id}", headers: headers, params: params
  100. end
  101. let(:list) { Fabricate(:list, account: user.account, title: 'my list') }
  102. let(:params) { { title: 'list', replies_policy: 'followed', exclusive: 'true' } }
  103. it_behaves_like 'forbidden for wrong scope', 'read read:lists'
  104. it 'returns the updated list and updates values', :aggregate_failures do
  105. expect { subject }
  106. .to change_list_title
  107. .and change_list_replies_policy
  108. .and change_list_exclusive
  109. expect(response).to have_http_status(200)
  110. list.reload
  111. expect(body_as_json).to eq({
  112. id: list.id.to_s,
  113. title: list.title,
  114. replies_policy: list.replies_policy,
  115. exclusive: list.exclusive,
  116. })
  117. end
  118. def change_list_title
  119. change { list.reload.title }.from('my list').to('list')
  120. end
  121. def change_list_replies_policy
  122. change { list.reload.replies_policy }.from('list').to('followed')
  123. end
  124. def change_list_exclusive
  125. change { list.reload.exclusive }.from(false).to(true)
  126. end
  127. context 'when the list does not exist' do
  128. it 'returns http not found' do
  129. put '/api/v1/lists/-1', headers: headers, params: params
  130. expect(response).to have_http_status(404)
  131. end
  132. end
  133. context 'when the list belongs to another user' do
  134. let(:list) { Fabricate(:list) }
  135. it 'returns http not found' do
  136. subject
  137. expect(response).to have_http_status(404)
  138. end
  139. end
  140. end
  141. describe 'DELETE /api/v1/lists/:id' do
  142. subject do
  143. delete "/api/v1/lists/#{list.id}", headers: headers
  144. end
  145. let(:list) { Fabricate(:list, account: user.account) }
  146. it_behaves_like 'forbidden for wrong scope', 'read read:lists'
  147. it 'deletes the list', :aggregate_failures do
  148. subject
  149. expect(response).to have_http_status(200)
  150. expect(List.where(id: list.id)).to_not exist
  151. end
  152. context 'when the list does not exist' do
  153. it 'returns http not found' do
  154. delete '/api/v1/lists/-1', headers: headers
  155. expect(response).to have_http_status(404)
  156. end
  157. end
  158. context 'when the list belongs to another user' do
  159. let(:list) { Fabricate(:list) }
  160. it 'returns http not found' do
  161. subject
  162. expect(response).to have_http_status(404)
  163. end
  164. end
  165. end
  166. end