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.

242 lines
6.8 KiB

  1. require 'rails_helper'
  2. RSpec.describe Api::V1::AccountsController, type: :controller do
  3. render_views
  4. let(:user) { Fabricate(:user, account: Fabricate(:account, username: 'alice')) }
  5. let(:token) { double acceptable?: true, resource_owner_id: user.id }
  6. before do
  7. allow(controller).to receive(:doorkeeper_token) { token }
  8. end
  9. describe 'GET #show' do
  10. it 'returns http success' do
  11. get :show, params: { id: user.account.id }
  12. expect(response).to have_http_status(:success)
  13. end
  14. end
  15. describe 'GET #search' do
  16. it 'returns http success' do
  17. get :search, params: { q: 'query' }
  18. expect(response).to have_http_status(:success)
  19. end
  20. end
  21. describe 'GET #verify_credentials' do
  22. it 'returns http success' do
  23. get :verify_credentials
  24. expect(response).to have_http_status(:success)
  25. end
  26. end
  27. describe 'PATCH #update_credentials' do
  28. describe 'with valid data' do
  29. before do
  30. patch :update_credentials, params: {
  31. display_name: "Alice Isn't Dead",
  32. note: "Hi!\n\nToot toot!",
  33. avatar: fixture_file_upload('files/avatar.gif', 'image/gif'),
  34. header: fixture_file_upload('files/attachment.jpg', 'image/jpeg'),
  35. }
  36. end
  37. it 'returns http success' do
  38. expect(response).to have_http_status(:success)
  39. end
  40. it 'updates account info' do
  41. user.account.reload
  42. expect(user.account.display_name).to eq("Alice Isn't Dead")
  43. expect(user.account.note).to eq("Hi!\n\nToot toot!")
  44. expect(user.account.avatar).to exist
  45. expect(user.account.header).to exist
  46. end
  47. end
  48. describe 'with invalid data' do
  49. before do
  50. patch :update_credentials, params: { note: 'This is too long. ' * 10 }
  51. end
  52. it 'returns http unprocessable entity' do
  53. expect(response).to have_http_status(:unprocessable_entity)
  54. end
  55. end
  56. end
  57. describe 'GET #statuses' do
  58. it 'returns http success' do
  59. get :statuses, params: { id: user.account.id }
  60. expect(response).to have_http_status(:success)
  61. end
  62. end
  63. describe 'GET #followers' do
  64. it 'returns http success' do
  65. get :followers, params: { id: user.account.id }
  66. expect(response).to have_http_status(:success)
  67. end
  68. end
  69. describe 'GET #following' do
  70. it 'returns http success' do
  71. get :following, params: { id: user.account.id }
  72. expect(response).to have_http_status(:success)
  73. end
  74. end
  75. describe 'POST #follow' do
  76. let(:other_account) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob')).account }
  77. before do
  78. post :follow, params: { id: other_account.id }
  79. end
  80. it 'returns http success' do
  81. expect(response).to have_http_status(:success)
  82. end
  83. it 'creates a following relation between user and target user' do
  84. expect(user.account.following?(other_account)).to be true
  85. end
  86. end
  87. describe 'POST #unfollow' do
  88. let(:other_account) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob')).account }
  89. before do
  90. user.account.follow!(other_account)
  91. post :unfollow, params: { id: other_account.id }
  92. end
  93. it 'returns http success' do
  94. expect(response).to have_http_status(:success)
  95. end
  96. it 'removes the following relation between user and target user' do
  97. expect(user.account.following?(other_account)).to be false
  98. end
  99. end
  100. describe 'POST #block' do
  101. let(:other_account) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob')).account }
  102. before do
  103. user.account.follow!(other_account)
  104. post :block, params: { id: other_account.id }
  105. end
  106. it 'returns http success' do
  107. expect(response).to have_http_status(:success)
  108. end
  109. it 'removes the following relation between user and target user' do
  110. expect(user.account.following?(other_account)).to be false
  111. end
  112. it 'creates a blocking relation' do
  113. expect(user.account.blocking?(other_account)).to be true
  114. end
  115. end
  116. describe 'POST #unblock' do
  117. let(:other_account) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob')).account }
  118. before do
  119. user.account.block!(other_account)
  120. post :unblock, params: { id: other_account.id }
  121. end
  122. it 'returns http success' do
  123. expect(response).to have_http_status(:success)
  124. end
  125. it 'removes the blocking relation between user and target user' do
  126. expect(user.account.blocking?(other_account)).to be false
  127. end
  128. end
  129. describe 'POST #mute' do
  130. let(:other_account) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob')).account }
  131. before do
  132. user.account.follow!(other_account)
  133. post :mute, params: {id: other_account.id }
  134. end
  135. it 'returns http success' do
  136. expect(response).to have_http_status(:success)
  137. end
  138. it 'does not remove the following relation between user and target user' do
  139. expect(user.account.following?(other_account)).to be true
  140. end
  141. it 'creates a muting relation' do
  142. expect(user.account.muting?(other_account)).to be true
  143. end
  144. end
  145. describe 'POST #unmute' do
  146. let(:other_account) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob')).account }
  147. before do
  148. user.account.mute!(other_account)
  149. post :unmute, params: { id: other_account.id }
  150. end
  151. it 'returns http success' do
  152. expect(response).to have_http_status(:success)
  153. end
  154. it 'removes the muting relation between user and target user' do
  155. expect(user.account.muting?(other_account)).to be false
  156. end
  157. end
  158. describe 'GET #relationships' do
  159. let(:simon) { Fabricate(:user, email: 'simon@example.com', account: Fabricate(:account, username: 'simon')).account }
  160. let(:lewis) { Fabricate(:user, email: 'lewis@example.com', account: Fabricate(:account, username: 'lewis')).account }
  161. before do
  162. user.account.follow!(simon)
  163. lewis.follow!(user.account)
  164. end
  165. context 'provided only one ID' do
  166. before do
  167. get :relationships, params: { id: simon.id }
  168. end
  169. it 'returns http success' do
  170. expect(response).to have_http_status(:success)
  171. end
  172. it 'returns JSON with correct data' do
  173. json = body_as_json
  174. expect(json).to be_a Enumerable
  175. expect(json.first[:following]).to be true
  176. expect(json.first[:followed_by]).to be false
  177. end
  178. end
  179. context 'provided multiple IDs' do
  180. before do
  181. get :relationships, params: { id: [simon.id, lewis.id] }
  182. end
  183. it 'returns http success' do
  184. expect(response).to have_http_status(:success)
  185. end
  186. xit 'returns JSON with correct data' do
  187. # todo
  188. end
  189. end
  190. end
  191. end