lists_spec.rb 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 http success' do
  35. subject
  36. expect(response).to have_http_status(200)
  37. end
  38. it 'returns the expected lists' do
  39. subject
  40. expect(body_as_json).to match_array(expected_response)
  41. end
  42. end
  43. describe 'GET /api/v1/lists/:id' do
  44. subject do
  45. get "/api/v1/lists/#{list.id}", headers: headers
  46. end
  47. let(:list) { Fabricate(:list, account: user.account) }
  48. it_behaves_like 'forbidden for wrong scope', 'write write:lists'
  49. it 'returns http success' do
  50. subject
  51. expect(response).to have_http_status(200)
  52. end
  53. it 'returns the requested list correctly' do
  54. subject
  55. expect(body_as_json).to eq({
  56. id: list.id.to_s,
  57. title: list.title,
  58. replies_policy: list.replies_policy,
  59. exclusive: list.exclusive,
  60. })
  61. end
  62. context 'when the list belongs to a different user' do
  63. let(:list) { Fabricate(:list) }
  64. it 'returns http not found' do
  65. subject
  66. expect(response).to have_http_status(404)
  67. end
  68. end
  69. context 'when the list does not exist' do
  70. it 'returns http not found' do
  71. get '/api/v1/lists/-1', headers: headers
  72. expect(response).to have_http_status(404)
  73. end
  74. end
  75. end
  76. describe 'POST /api/v1/lists' do
  77. subject do
  78. post '/api/v1/lists', headers: headers, params: params
  79. end
  80. let(:params) { { title: 'my list', replies_policy: 'none', exclusive: 'true' } }
  81. it_behaves_like 'forbidden for wrong scope', 'read read:lists'
  82. it 'returns http success' do
  83. subject
  84. expect(response).to have_http_status(200)
  85. end
  86. it 'returns the new list' do
  87. subject
  88. expect(body_as_json).to match(a_hash_including(title: 'my list', replies_policy: 'none', exclusive: true))
  89. end
  90. it 'creates a list' do
  91. subject
  92. expect(List.where(account: user.account).count).to eq(1)
  93. end
  94. context 'when a title is not given' do
  95. let(:params) { { title: '' } }
  96. it 'returns http unprocessable entity' do
  97. subject
  98. expect(response).to have_http_status(422)
  99. end
  100. end
  101. context 'when the given replies_policy is invalid' do
  102. let(:params) { { title: 'a list', replies_policy: 'whatever' } }
  103. it 'returns http unprocessable entity' do
  104. subject
  105. expect(response).to have_http_status(422)
  106. end
  107. end
  108. end
  109. describe 'PUT /api/v1/lists/:id' do
  110. subject do
  111. put "/api/v1/lists/#{list.id}", headers: headers, params: params
  112. end
  113. let(:list) { Fabricate(:list, account: user.account, title: 'my list') }
  114. let(:params) { { title: 'list', replies_policy: 'followed', exclusive: 'true' } }
  115. it_behaves_like 'forbidden for wrong scope', 'read read:lists'
  116. it 'returns http success' do
  117. subject
  118. expect(response).to have_http_status(200)
  119. end
  120. it 'returns the updated list' do
  121. subject
  122. list.reload
  123. expect(body_as_json).to eq({
  124. id: list.id.to_s,
  125. title: list.title,
  126. replies_policy: list.replies_policy,
  127. exclusive: list.exclusive,
  128. })
  129. end
  130. it 'updates the list title' do
  131. expect { subject }.to change { list.reload.title }.from('my list').to('list')
  132. end
  133. it 'updates the list replies_policy' do
  134. expect { subject }.to change { list.reload.replies_policy }.from('list').to('followed')
  135. end
  136. it 'updates the list exclusive' do
  137. expect { subject }.to change { list.reload.exclusive }.from(false).to(true)
  138. end
  139. context 'when the list does not exist' do
  140. it 'returns http not found' do
  141. put '/api/v1/lists/-1', headers: headers, params: params
  142. expect(response).to have_http_status(404)
  143. end
  144. end
  145. context 'when the list belongs to another user' do
  146. let(:list) { Fabricate(:list) }
  147. it 'returns http not found' do
  148. subject
  149. expect(response).to have_http_status(404)
  150. end
  151. end
  152. end
  153. describe 'DELETE /api/v1/lists/:id' do
  154. subject do
  155. delete "/api/v1/lists/#{list.id}", headers: headers
  156. end
  157. let(:list) { Fabricate(:list, account: user.account) }
  158. it_behaves_like 'forbidden for wrong scope', 'read read:lists'
  159. it 'returns http success' do
  160. subject
  161. expect(response).to have_http_status(200)
  162. end
  163. it 'deletes the list' do
  164. subject
  165. expect(List.where(id: list.id)).to_not exist
  166. end
  167. context 'when the list does not exist' do
  168. it 'returns http not found' do
  169. delete '/api/v1/lists/-1', headers: headers
  170. expect(response).to have_http_status(404)
  171. end
  172. end
  173. context 'when the list belongs to another user' do
  174. let(:list) { Fabricate(:list) }
  175. it 'returns http not found' do
  176. subject
  177. expect(response).to have_http_status(404)
  178. end
  179. end
  180. end
  181. end