闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

295 lines
7.8 KiB

  1. require 'rails_helper'
  2. RSpec.describe Api::V1::StatusesController, type: :controller do
  3. render_views
  4. let(:user) { Fabricate(:user, account: Fabricate(:account, username: 'alice')) }
  5. let(:app) { Fabricate(:application, name: 'Test app', website: 'http://testapp.com') }
  6. let(:token) { double acceptable?: true, resource_owner_id: user.id, application: app }
  7. context 'with an oauth token' do
  8. before do
  9. allow(controller).to receive(:doorkeeper_token) { token }
  10. end
  11. describe 'GET #show' do
  12. let(:status) { Fabricate(:status, account: user.account) }
  13. it 'returns http success' do
  14. get :show, params: { id: status.id }
  15. expect(response).to have_http_status(:success)
  16. end
  17. end
  18. describe 'GET #context' do
  19. let(:status) { Fabricate(:status, account: user.account) }
  20. before do
  21. Fabricate(:status, account: user.account, thread: status)
  22. end
  23. it 'returns http success' do
  24. get :context, params: { id: status.id }
  25. expect(response).to have_http_status(:success)
  26. end
  27. end
  28. describe 'GET #reblogged_by' do
  29. let(:status) { Fabricate(:status, account: user.account) }
  30. before do
  31. post :reblog, params: { id: status.id }
  32. end
  33. it 'returns http success' do
  34. get :reblogged_by, params: { id: status.id }
  35. expect(response).to have_http_status(:success)
  36. end
  37. end
  38. describe 'GET #favourited_by' do
  39. let(:status) { Fabricate(:status, account: user.account) }
  40. before do
  41. post :favourite, params: { id: status.id }
  42. end
  43. it 'returns http success' do
  44. get :favourited_by, params: { id: status.id }
  45. expect(response).to have_http_status(:success)
  46. end
  47. end
  48. describe 'POST #create' do
  49. before do
  50. post :create, params: { status: 'Hello world' }
  51. end
  52. it 'returns http success' do
  53. expect(response).to have_http_status(:success)
  54. end
  55. end
  56. describe 'DELETE #destroy' do
  57. let(:status) { Fabricate(:status, account: user.account) }
  58. before do
  59. post :destroy, params: { id: status.id }
  60. end
  61. it 'returns http success' do
  62. expect(response).to have_http_status(:success)
  63. end
  64. it 'removes the status' do
  65. expect(Status.find_by(id: status.id)).to be nil
  66. end
  67. end
  68. describe 'POST #reblog' do
  69. let(:status) { Fabricate(:status, account: user.account) }
  70. before do
  71. post :reblog, params: { id: status.id }
  72. end
  73. it 'returns http success' do
  74. expect(response).to have_http_status(:success)
  75. end
  76. it 'updates the reblogs count' do
  77. expect(status.reblogs.count).to eq 1
  78. end
  79. it 'updates the reblogged attribute' do
  80. expect(user.account.reblogged?(status)).to be true
  81. end
  82. it 'return json with updated attributes' do
  83. hash_body = body_as_json
  84. expect(hash_body[:reblog][:id]).to eq status.id
  85. expect(hash_body[:reblog][:reblogs_count]).to eq 1
  86. expect(hash_body[:reblog][:reblogged]).to be true
  87. end
  88. end
  89. describe 'POST #unreblog' do
  90. let(:status) { Fabricate(:status, account: user.account) }
  91. before do
  92. post :reblog, params: { id: status.id }
  93. post :unreblog, params: { id: status.id }
  94. end
  95. it 'returns http success' do
  96. expect(response).to have_http_status(:success)
  97. end
  98. it 'updates the reblogs count' do
  99. expect(status.reblogs.count).to eq 0
  100. end
  101. it 'updates the reblogged attribute' do
  102. expect(user.account.reblogged?(status)).to be false
  103. end
  104. end
  105. describe 'POST #favourite' do
  106. let(:status) { Fabricate(:status, account: user.account) }
  107. before do
  108. post :favourite, params: { id: status.id }
  109. end
  110. it 'returns http success' do
  111. expect(response).to have_http_status(:success)
  112. end
  113. it 'updates the favourites count' do
  114. expect(status.favourites.count).to eq 1
  115. end
  116. it 'updates the favourited attribute' do
  117. expect(user.account.favourited?(status)).to be true
  118. end
  119. it 'return json with updated attributes' do
  120. hash_body = body_as_json
  121. expect(hash_body[:id]).to eq status.id
  122. expect(hash_body[:favourites_count]).to eq 1
  123. expect(hash_body[:favourited]).to be true
  124. end
  125. end
  126. describe 'POST #unfavourite' do
  127. let(:status) { Fabricate(:status, account: user.account) }
  128. before do
  129. post :favourite, params: { id: status.id }
  130. post :unfavourite, params: { id: status.id }
  131. end
  132. it 'returns http success' do
  133. expect(response).to have_http_status(:success)
  134. end
  135. it 'updates the favourites count' do
  136. expect(status.favourites.count).to eq 0
  137. end
  138. it 'updates the favourited attribute' do
  139. expect(user.account.favourited?(status)).to be false
  140. end
  141. end
  142. end
  143. context 'without an oauth token' do
  144. before do
  145. allow(controller).to receive(:doorkeeper_token) { nil }
  146. end
  147. context 'with a private status' do
  148. let(:status) { Fabricate(:status, account: user.account, visibility: :private) }
  149. describe 'GET #show' do
  150. it 'returns http unautharized' do
  151. get :show, params: { id: status.id }
  152. expect(response).to have_http_status(:missing)
  153. end
  154. end
  155. describe 'GET #context' do
  156. before do
  157. Fabricate(:status, account: user.account, thread: status)
  158. end
  159. it 'returns http unautharized' do
  160. get :context, params: { id: status.id }
  161. expect(response).to have_http_status(:missing)
  162. end
  163. end
  164. describe 'GET #card' do
  165. it 'returns http unautharized' do
  166. get :card, params: { id: status.id }
  167. expect(response).to have_http_status(:missing)
  168. end
  169. end
  170. describe 'GET #reblogged_by' do
  171. before do
  172. post :reblog, params: { id: status.id }
  173. end
  174. it 'returns http unautharized' do
  175. get :reblogged_by, params: { id: status.id }
  176. expect(response).to have_http_status(:missing)
  177. end
  178. end
  179. describe 'GET #favourited_by' do
  180. before do
  181. post :favourite, params: { id: status.id }
  182. end
  183. it 'returns http unautharized' do
  184. get :favourited_by, params: { id: status.id }
  185. expect(response).to have_http_status(:missing)
  186. end
  187. end
  188. end
  189. context 'with a public status' do
  190. let(:status) { Fabricate(:status, account: user.account, visibility: :public) }
  191. describe 'GET #show' do
  192. it 'returns http success' do
  193. get :show, params: { id: status.id }
  194. expect(response).to have_http_status(:success)
  195. end
  196. end
  197. describe 'GET #context' do
  198. before do
  199. Fabricate(:status, account: user.account, thread: status)
  200. end
  201. it 'returns http success' do
  202. get :context, params: { id: status.id }
  203. expect(response).to have_http_status(:success)
  204. end
  205. end
  206. describe 'GET #card' do
  207. it 'returns http success' do
  208. get :card, params: { id: status.id }
  209. expect(response).to have_http_status(:success)
  210. end
  211. end
  212. describe 'GET #reblogged_by' do
  213. before do
  214. post :reblog, params: { id: status.id }
  215. end
  216. it 'returns http success' do
  217. get :reblogged_by, params: { id: status.id }
  218. expect(response).to have_http_status(:success)
  219. end
  220. end
  221. describe 'GET #favourited_by' do
  222. before do
  223. post :favourite, params: { id: status.id }
  224. end
  225. it 'returns http success' do
  226. get :favourited_by, params: { id: status.id }
  227. expect(response).to have_http_status(:success)
  228. end
  229. end
  230. end
  231. end
  232. end