闭社主体 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.

328 lines
8.7 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. describe 'POST #mute' do
  143. let(:status) { Fabricate(:status, account: user.account) }
  144. before do
  145. post :mute, params: { id: status.id }
  146. end
  147. it 'returns http success' do
  148. expect(response).to have_http_status(:success)
  149. end
  150. it 'creates a conversation mute' do
  151. expect(ConversationMute.find_by(account: user.account, conversation_id: status.conversation_id)).to_not be_nil
  152. end
  153. end
  154. describe 'POST #unmute' do
  155. let(:status) { Fabricate(:status, account: user.account) }
  156. before do
  157. post :mute, params: { id: status.id }
  158. post :unmute, params: { id: status.id }
  159. end
  160. it 'returns http success' do
  161. expect(response).to have_http_status(:success)
  162. end
  163. it 'destroys the conversation mute' do
  164. expect(ConversationMute.find_by(account: user.account, conversation_id: status.conversation_id)).to be_nil
  165. end
  166. end
  167. end
  168. context 'without an oauth token' do
  169. before do
  170. allow(controller).to receive(:doorkeeper_token) { nil }
  171. end
  172. context 'with a private status' do
  173. let(:status) { Fabricate(:status, account: user.account, visibility: :private) }
  174. describe 'GET #show' do
  175. it 'returns http unautharized' do
  176. get :show, params: { id: status.id }
  177. expect(response).to have_http_status(:missing)
  178. end
  179. end
  180. describe 'GET #context' do
  181. before do
  182. Fabricate(:status, account: user.account, thread: status)
  183. end
  184. it 'returns http unautharized' do
  185. get :context, params: { id: status.id }
  186. expect(response).to have_http_status(:missing)
  187. end
  188. end
  189. describe 'GET #card' do
  190. it 'returns http unautharized' do
  191. get :card, params: { id: status.id }
  192. expect(response).to have_http_status(:missing)
  193. end
  194. end
  195. describe 'GET #reblogged_by' do
  196. before do
  197. post :reblog, params: { id: status.id }
  198. end
  199. it 'returns http unautharized' do
  200. get :reblogged_by, params: { id: status.id }
  201. expect(response).to have_http_status(:missing)
  202. end
  203. end
  204. describe 'GET #favourited_by' do
  205. before do
  206. post :favourite, params: { id: status.id }
  207. end
  208. it 'returns http unautharized' do
  209. get :favourited_by, params: { id: status.id }
  210. expect(response).to have_http_status(:missing)
  211. end
  212. end
  213. end
  214. context 'with a public status' do
  215. let(:status) { Fabricate(:status, account: user.account, visibility: :public) }
  216. describe 'GET #show' do
  217. it 'returns http success' do
  218. get :show, params: { id: status.id }
  219. expect(response).to have_http_status(:success)
  220. end
  221. end
  222. describe 'GET #context' do
  223. before do
  224. Fabricate(:status, account: user.account, thread: status)
  225. end
  226. it 'returns http success' do
  227. get :context, params: { id: status.id }
  228. expect(response).to have_http_status(:success)
  229. end
  230. end
  231. describe 'GET #card' do
  232. it 'returns http success' do
  233. get :card, params: { id: status.id }
  234. expect(response).to have_http_status(:success)
  235. end
  236. end
  237. describe 'GET #reblogged_by' do
  238. before do
  239. post :reblog, params: { id: status.id }
  240. end
  241. it 'returns http success' do
  242. get :reblogged_by, params: { id: status.id }
  243. expect(response).to have_http_status(:success)
  244. end
  245. end
  246. describe 'GET #favourited_by' do
  247. before do
  248. post :favourite, params: { id: status.id }
  249. end
  250. it 'returns http success' do
  251. get :favourited_by, params: { id: status.id }
  252. expect(response).to have_http_status(:success)
  253. end
  254. end
  255. end
  256. end
  257. end