The code powering m.abunchtell.com https://m.abunchtell.com
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

227 lignes
6.5 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(:scopes) { '' }
  6. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  7. before do
  8. allow(controller).to receive(:doorkeeper_token) { token }
  9. end
  10. shared_examples 'forbidden for wrong scope' do |wrong_scope|
  11. let(:scopes) { wrong_scope }
  12. it 'returns http forbidden' do
  13. expect(response).to have_http_status(403)
  14. end
  15. end
  16. describe 'GET #show' do
  17. let(:scopes) { 'read:accounts' }
  18. before do
  19. get :show, params: { id: user.account.id }
  20. end
  21. it 'returns http success' do
  22. expect(response).to have_http_status(200)
  23. end
  24. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  25. end
  26. describe 'POST #follow' do
  27. let(:scopes) { 'write:follows' }
  28. let(:other_account) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob', locked: locked)).account }
  29. before do
  30. post :follow, params: { id: other_account.id }
  31. end
  32. context 'with unlocked account' do
  33. let(:locked) { false }
  34. it 'returns http success' do
  35. expect(response).to have_http_status(200)
  36. end
  37. it 'returns JSON with following=true and requested=false' do
  38. json = body_as_json
  39. expect(json[:following]).to be true
  40. expect(json[:requested]).to be false
  41. end
  42. it 'creates a following relation between user and target user' do
  43. expect(user.account.following?(other_account)).to be true
  44. end
  45. it_behaves_like 'forbidden for wrong scope', 'read:accounts'
  46. end
  47. context 'with locked account' do
  48. let(:locked) { true }
  49. it 'returns http success' do
  50. expect(response).to have_http_status(200)
  51. end
  52. it 'returns JSON with following=false and requested=true' do
  53. json = body_as_json
  54. expect(json[:following]).to be false
  55. expect(json[:requested]).to be true
  56. end
  57. it 'creates a follow request relation between user and target user' do
  58. expect(user.account.requested?(other_account)).to be true
  59. end
  60. it_behaves_like 'forbidden for wrong scope', 'read:accounts'
  61. end
  62. end
  63. describe 'POST #unfollow' do
  64. let(:scopes) { 'write:follows' }
  65. let(:other_account) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob')).account }
  66. before do
  67. user.account.follow!(other_account)
  68. post :unfollow, params: { id: other_account.id }
  69. end
  70. it 'returns http success' do
  71. expect(response).to have_http_status(200)
  72. end
  73. it 'removes the following relation between user and target user' do
  74. expect(user.account.following?(other_account)).to be false
  75. end
  76. it_behaves_like 'forbidden for wrong scope', 'read:accounts'
  77. end
  78. describe 'POST #block' do
  79. let(:scopes) { 'write:blocks' }
  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 :block, params: { id: other_account.id }
  84. end
  85. it 'returns http success' do
  86. expect(response).to have_http_status(200)
  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. it 'creates a blocking relation' do
  92. expect(user.account.blocking?(other_account)).to be true
  93. end
  94. it_behaves_like 'forbidden for wrong scope', 'read:accounts'
  95. end
  96. describe 'POST #unblock' do
  97. let(:scopes) { 'write:blocks' }
  98. let(:other_account) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob')).account }
  99. before do
  100. user.account.block!(other_account)
  101. post :unblock, params: { id: other_account.id }
  102. end
  103. it 'returns http success' do
  104. expect(response).to have_http_status(200)
  105. end
  106. it 'removes the blocking relation between user and target user' do
  107. expect(user.account.blocking?(other_account)).to be false
  108. end
  109. it_behaves_like 'forbidden for wrong scope', 'read:accounts'
  110. end
  111. describe 'POST #mute' do
  112. let(:scopes) { 'write:mutes' }
  113. let(:other_account) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob')).account }
  114. before do
  115. user.account.follow!(other_account)
  116. post :mute, params: {id: other_account.id }
  117. end
  118. it 'returns http success' do
  119. expect(response).to have_http_status(200)
  120. end
  121. it 'does not remove the following relation between user and target user' do
  122. expect(user.account.following?(other_account)).to be true
  123. end
  124. it 'creates a muting relation' do
  125. expect(user.account.muting?(other_account)).to be true
  126. end
  127. it 'mutes notifications' do
  128. expect(user.account.muting_notifications?(other_account)).to be true
  129. end
  130. it_behaves_like 'forbidden for wrong scope', 'read:accounts'
  131. end
  132. describe 'POST #mute with notifications set to false' do
  133. let(:scopes) { 'write:mutes' }
  134. let(:other_account) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob')).account }
  135. before do
  136. user.account.follow!(other_account)
  137. post :mute, params: {id: other_account.id, notifications: false }
  138. end
  139. it 'returns http success' do
  140. expect(response).to have_http_status(200)
  141. end
  142. it 'does not remove the following relation between user and target user' do
  143. expect(user.account.following?(other_account)).to be true
  144. end
  145. it 'creates a muting relation' do
  146. expect(user.account.muting?(other_account)).to be true
  147. end
  148. it 'does not mute notifications' do
  149. expect(user.account.muting_notifications?(other_account)).to be false
  150. end
  151. it_behaves_like 'forbidden for wrong scope', 'read:accounts'
  152. end
  153. describe 'POST #unmute' do
  154. let(:scopes) { 'write:mutes' }
  155. let(:other_account) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob')).account }
  156. before do
  157. user.account.mute!(other_account)
  158. post :unmute, params: { id: other_account.id }
  159. end
  160. it 'returns http success' do
  161. expect(response).to have_http_status(200)
  162. end
  163. it 'removes the muting relation between user and target user' do
  164. expect(user.account.muting?(other_account)).to be false
  165. end
  166. it_behaves_like 'forbidden for wrong scope', 'read:accounts'
  167. end
  168. end