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

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