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.

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