accounts_controller_spec.rb 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Api::V1::AccountsController do
  4. render_views
  5. let(:user) { Fabricate(:user) }
  6. let(:scopes) { '' }
  7. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  8. before do
  9. allow(controller).to receive(:doorkeeper_token) { token }
  10. end
  11. describe 'POST #create' do
  12. let(:app) { Fabricate(:application) }
  13. let(:token) { Doorkeeper::AccessToken.find_or_create_for(application: app, resource_owner: nil, scopes: 'read write', use_refresh_token: false) }
  14. let(:agreement) { nil }
  15. before do
  16. post :create, params: { username: 'test', password: '12345678', email: 'hello@world.tld', agreement: agreement }
  17. end
  18. context 'when given truthy agreement' do
  19. let(:agreement) { 'true' }
  20. it 'creates a user', :aggregate_failures do
  21. expect(response).to have_http_status(200)
  22. expect(body_as_json[:access_token]).to_not be_blank
  23. user = User.find_by(email: 'hello@world.tld')
  24. expect(user).to_not be_nil
  25. expect(user.created_by_application_id).to eq app.id
  26. end
  27. end
  28. context 'when given no agreement' do
  29. it 'returns http unprocessable entity' do
  30. expect(response).to have_http_status(422)
  31. end
  32. end
  33. end
  34. describe 'POST #follow' do
  35. let(:scopes) { 'write:follows' }
  36. let(:other_account) { Fabricate(:account, username: 'bob', locked: locked) }
  37. context 'when posting to an other account' do
  38. before do
  39. post :follow, params: { id: other_account.id }
  40. end
  41. context 'with unlocked account' do
  42. let(:locked) { false }
  43. it 'creates a following relation between user and target user', :aggregate_failures do
  44. expect(response).to have_http_status(200)
  45. json = body_as_json
  46. expect(json[:following]).to be true
  47. expect(json[:requested]).to be false
  48. expect(user.account.following?(other_account)).to be true
  49. end
  50. it_behaves_like 'forbidden for wrong scope', 'read:accounts'
  51. end
  52. context 'with locked account' do
  53. let(:locked) { true }
  54. it 'creates a follow request relation between user and target user', :aggregate_failures do
  55. expect(response).to have_http_status(200)
  56. json = body_as_json
  57. expect(json[:following]).to be false
  58. expect(json[:requested]).to be true
  59. expect(user.account.requested?(other_account)).to be true
  60. end
  61. it_behaves_like 'forbidden for wrong scope', 'read:accounts'
  62. end
  63. end
  64. context 'when modifying follow options' do
  65. let(:locked) { false }
  66. before do
  67. user.account.follow!(other_account, reblogs: false, notify: false)
  68. end
  69. it 'changes reblogs option' do
  70. post :follow, params: { id: other_account.id, reblogs: true }
  71. json = body_as_json
  72. expect(json[:following]).to be true
  73. expect(json[:showing_reblogs]).to be true
  74. expect(json[:notifying]).to be false
  75. end
  76. it 'changes notify option' do
  77. post :follow, params: { id: other_account.id, notify: true }
  78. json = body_as_json
  79. expect(json[:following]).to be true
  80. expect(json[:showing_reblogs]).to be false
  81. expect(json[:notifying]).to be true
  82. end
  83. it 'changes languages option' do
  84. post :follow, params: { id: other_account.id, languages: %w(en es) }
  85. json = body_as_json
  86. expect(json[:following]).to be true
  87. expect(json[:showing_reblogs]).to be false
  88. expect(json[:notifying]).to be false
  89. expect(json[:languages]).to match_array %w(en es)
  90. end
  91. end
  92. end
  93. describe 'POST #unfollow' do
  94. let(:scopes) { 'write:follows' }
  95. let(:other_account) { Fabricate(:account, username: 'bob') }
  96. before do
  97. user.account.follow!(other_account)
  98. post :unfollow, params: { id: other_account.id }
  99. end
  100. it 'removes the following relation between user and target user', :aggregate_failures do
  101. expect(response).to have_http_status(200)
  102. expect(user.account.following?(other_account)).to be false
  103. end
  104. it_behaves_like 'forbidden for wrong scope', 'read:accounts'
  105. end
  106. describe 'POST #remove_from_followers' do
  107. let(:scopes) { 'write:follows' }
  108. let(:other_account) { Fabricate(:account, username: 'bob') }
  109. before do
  110. other_account.follow!(user.account)
  111. post :remove_from_followers, params: { id: other_account.id }
  112. end
  113. it 'removes the followed relation between user and target user', :aggregate_failures do
  114. expect(response).to have_http_status(200)
  115. expect(user.account.followed_by?(other_account)).to be false
  116. end
  117. it_behaves_like 'forbidden for wrong scope', 'read:accounts'
  118. end
  119. describe 'POST #block' do
  120. let(:scopes) { 'write:blocks' }
  121. let(:other_account) { Fabricate(:account, username: 'bob') }
  122. before do
  123. user.account.follow!(other_account)
  124. post :block, params: { id: other_account.id }
  125. end
  126. it 'creates a blocking relation', :aggregate_failures do
  127. expect(response).to have_http_status(200)
  128. expect(user.account.following?(other_account)).to be false
  129. expect(user.account.blocking?(other_account)).to be true
  130. end
  131. it_behaves_like 'forbidden for wrong scope', 'read:accounts'
  132. end
  133. describe 'POST #unblock' do
  134. let(:scopes) { 'write:blocks' }
  135. let(:other_account) { Fabricate(:account, username: 'bob') }
  136. before do
  137. user.account.block!(other_account)
  138. post :unblock, params: { id: other_account.id }
  139. end
  140. it 'removes the blocking relation between user and target user', :aggregate_failures do
  141. expect(response).to have_http_status(200)
  142. expect(user.account.blocking?(other_account)).to be false
  143. end
  144. it_behaves_like 'forbidden for wrong scope', 'read:accounts'
  145. end
  146. describe 'POST #mute' do
  147. let(:scopes) { 'write:mutes' }
  148. let(:other_account) { Fabricate(:account, username: 'bob') }
  149. before do
  150. user.account.follow!(other_account)
  151. post :mute, params: { id: other_account.id }
  152. end
  153. it 'mutes notifications', :aggregate_failures do
  154. expect(response).to have_http_status(200)
  155. expect(user.account.following?(other_account)).to be true
  156. expect(user.account.muting?(other_account)).to be true
  157. expect(user.account.muting_notifications?(other_account)).to be true
  158. end
  159. it_behaves_like 'forbidden for wrong scope', 'read:accounts'
  160. end
  161. describe 'POST #mute with notifications set to false' do
  162. let(:scopes) { 'write:mutes' }
  163. let(:other_account) { Fabricate(:account, username: 'bob') }
  164. before do
  165. user.account.follow!(other_account)
  166. post :mute, params: { id: other_account.id, notifications: false }
  167. end
  168. it 'does not mute notifications', :aggregate_failures do
  169. expect(response).to have_http_status(200)
  170. expect(user.account.following?(other_account)).to be true
  171. expect(user.account.muting?(other_account)).to be true
  172. expect(user.account.muting_notifications?(other_account)).to be false
  173. end
  174. it_behaves_like 'forbidden for wrong scope', 'read:accounts'
  175. end
  176. describe 'POST #mute with nonzero duration set' do
  177. let(:scopes) { 'write:mutes' }
  178. let(:other_account) { Fabricate(:account, username: 'bob') }
  179. before do
  180. user.account.follow!(other_account)
  181. post :mute, params: { id: other_account.id, duration: 300 }
  182. end
  183. it 'mutes notifications', :aggregate_failures do
  184. expect(response).to have_http_status(200)
  185. expect(user.account.following?(other_account)).to be true
  186. expect(user.account.muting?(other_account)).to be true
  187. expect(user.account.muting_notifications?(other_account)).to be true
  188. end
  189. it_behaves_like 'forbidden for wrong scope', 'read:accounts'
  190. end
  191. describe 'POST #unmute' do
  192. let(:scopes) { 'write:mutes' }
  193. let(:other_account) { Fabricate(:account, username: 'bob') }
  194. before do
  195. user.account.mute!(other_account)
  196. post :unmute, params: { id: other_account.id }
  197. end
  198. it 'removes the muting relation between user and target user', :aggregate_failures do
  199. expect(response).to have_http_status(200)
  200. expect(user.account.muting?(other_account)).to be false
  201. end
  202. it_behaves_like 'forbidden for wrong scope', 'read:accounts'
  203. end
  204. end