accounts_controller_spec.rb 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 'returns http success' do
  21. expect(response).to have_http_status(200)
  22. end
  23. it 'returns a new access token as JSON' do
  24. expect(body_as_json[:access_token]).to_not be_blank
  25. end
  26. it 'creates a user' do
  27. user = User.find_by(email: 'hello@world.tld')
  28. expect(user).to_not be_nil
  29. expect(user.created_by_application_id).to eq app.id
  30. end
  31. end
  32. context 'when given no agreement' do
  33. it 'returns http unprocessable entity' do
  34. expect(response).to have_http_status(422)
  35. end
  36. end
  37. end
  38. describe 'POST #follow' do
  39. let(:scopes) { 'write:follows' }
  40. let(:other_account) { Fabricate(:account, username: 'bob', locked: locked) }
  41. context 'when posting to an other account' do
  42. before do
  43. post :follow, params: { id: other_account.id }
  44. end
  45. context 'with unlocked account' do
  46. let(:locked) { false }
  47. it 'returns http success' do
  48. expect(response).to have_http_status(200)
  49. end
  50. it 'returns JSON with following=true and requested=false' do
  51. json = body_as_json
  52. expect(json[:following]).to be true
  53. expect(json[:requested]).to be false
  54. end
  55. it 'creates a following relation between user and target user' do
  56. expect(user.account.following?(other_account)).to be true
  57. end
  58. it_behaves_like 'forbidden for wrong scope', 'read:accounts'
  59. end
  60. context 'with locked account' do
  61. let(:locked) { true }
  62. it 'returns http success' do
  63. expect(response).to have_http_status(200)
  64. end
  65. it 'returns JSON with following=false and requested=true' do
  66. json = body_as_json
  67. expect(json[:following]).to be false
  68. expect(json[:requested]).to be true
  69. end
  70. it 'creates a follow request relation between user and target user' do
  71. expect(user.account.requested?(other_account)).to be true
  72. end
  73. it_behaves_like 'forbidden for wrong scope', 'read:accounts'
  74. end
  75. end
  76. context 'when modifying follow options' do
  77. let(:locked) { false }
  78. before do
  79. user.account.follow!(other_account, reblogs: false, notify: false)
  80. end
  81. it 'changes reblogs option' do
  82. post :follow, params: { id: other_account.id, reblogs: true }
  83. json = body_as_json
  84. expect(json[:following]).to be true
  85. expect(json[:showing_reblogs]).to be true
  86. expect(json[:notifying]).to be false
  87. end
  88. it 'changes notify option' do
  89. post :follow, params: { id: other_account.id, notify: true }
  90. json = body_as_json
  91. expect(json[:following]).to be true
  92. expect(json[:showing_reblogs]).to be false
  93. expect(json[:notifying]).to be true
  94. end
  95. it 'changes languages option' do
  96. post :follow, params: { id: other_account.id, languages: %w(en es) }
  97. json = body_as_json
  98. expect(json[:following]).to be true
  99. expect(json[:showing_reblogs]).to be false
  100. expect(json[:notifying]).to be false
  101. expect(json[:languages]).to match_array %w(en es)
  102. end
  103. end
  104. end
  105. describe 'POST #unfollow' do
  106. let(:scopes) { 'write:follows' }
  107. let(:other_account) { Fabricate(:account, username: 'bob') }
  108. before do
  109. user.account.follow!(other_account)
  110. post :unfollow, params: { id: other_account.id }
  111. end
  112. it 'returns http success' do
  113. expect(response).to have_http_status(200)
  114. end
  115. it 'removes the following relation between user and target user' do
  116. expect(user.account.following?(other_account)).to be false
  117. end
  118. it_behaves_like 'forbidden for wrong scope', 'read:accounts'
  119. end
  120. describe 'POST #remove_from_followers' do
  121. let(:scopes) { 'write:follows' }
  122. let(:other_account) { Fabricate(:account, username: 'bob') }
  123. before do
  124. other_account.follow!(user.account)
  125. post :remove_from_followers, params: { id: other_account.id }
  126. end
  127. it 'returns http success' do
  128. expect(response).to have_http_status(200)
  129. end
  130. it 'removes the followed relation between user and target user' do
  131. expect(user.account.followed_by?(other_account)).to be false
  132. end
  133. it_behaves_like 'forbidden for wrong scope', 'read:accounts'
  134. end
  135. describe 'POST #block' do
  136. let(:scopes) { 'write:blocks' }
  137. let(:other_account) { Fabricate(:account, username: 'bob') }
  138. before do
  139. user.account.follow!(other_account)
  140. post :block, params: { id: other_account.id }
  141. end
  142. it 'returns http success' do
  143. expect(response).to have_http_status(200)
  144. end
  145. it 'removes the following relation between user and target user' do
  146. expect(user.account.following?(other_account)).to be false
  147. end
  148. it 'creates a blocking relation' do
  149. expect(user.account.blocking?(other_account)).to be true
  150. end
  151. it_behaves_like 'forbidden for wrong scope', 'read:accounts'
  152. end
  153. describe 'POST #unblock' do
  154. let(:scopes) { 'write:blocks' }
  155. let(:other_account) { Fabricate(:account, username: 'bob') }
  156. before do
  157. user.account.block!(other_account)
  158. post :unblock, 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 blocking relation between user and target user' do
  164. expect(user.account.blocking?(other_account)).to be false
  165. end
  166. it_behaves_like 'forbidden for wrong scope', 'read:accounts'
  167. end
  168. describe 'POST #mute' do
  169. let(:scopes) { 'write:mutes' }
  170. let(:other_account) { Fabricate(:account, username: 'bob') }
  171. before do
  172. user.account.follow!(other_account)
  173. post :mute, params: { id: other_account.id }
  174. end
  175. it 'returns http success' do
  176. expect(response).to have_http_status(200)
  177. end
  178. it 'does not remove the following relation between user and target user' do
  179. expect(user.account.following?(other_account)).to be true
  180. end
  181. it 'creates a muting relation' do
  182. expect(user.account.muting?(other_account)).to be true
  183. end
  184. it 'mutes notifications' do
  185. expect(user.account.muting_notifications?(other_account)).to be true
  186. end
  187. it_behaves_like 'forbidden for wrong scope', 'read:accounts'
  188. end
  189. describe 'POST #mute with notifications set to false' do
  190. let(:scopes) { 'write:mutes' }
  191. let(:other_account) { Fabricate(:account, username: 'bob') }
  192. before do
  193. user.account.follow!(other_account)
  194. post :mute, params: { id: other_account.id, notifications: false }
  195. end
  196. it 'returns http success' do
  197. expect(response).to have_http_status(200)
  198. end
  199. it 'does not remove the following relation between user and target user' do
  200. expect(user.account.following?(other_account)).to be true
  201. end
  202. it 'creates a muting relation' do
  203. expect(user.account.muting?(other_account)).to be true
  204. end
  205. it 'does not mute notifications' do
  206. expect(user.account.muting_notifications?(other_account)).to be false
  207. end
  208. it_behaves_like 'forbidden for wrong scope', 'read:accounts'
  209. end
  210. describe 'POST #mute with nonzero duration set' do
  211. let(:scopes) { 'write:mutes' }
  212. let(:other_account) { Fabricate(:account, username: 'bob') }
  213. before do
  214. user.account.follow!(other_account)
  215. post :mute, params: { id: other_account.id, duration: 300 }
  216. end
  217. it 'returns http success' do
  218. expect(response).to have_http_status(200)
  219. end
  220. it 'does not remove the following relation between user and target user' do
  221. expect(user.account.following?(other_account)).to be true
  222. end
  223. it 'creates a muting relation' do
  224. expect(user.account.muting?(other_account)).to be true
  225. end
  226. it 'mutes notifications' do
  227. expect(user.account.muting_notifications?(other_account)).to be true
  228. end
  229. it_behaves_like 'forbidden for wrong scope', 'read:accounts'
  230. end
  231. describe 'POST #unmute' do
  232. let(:scopes) { 'write:mutes' }
  233. let(:other_account) { Fabricate(:account, username: 'bob') }
  234. before do
  235. user.account.mute!(other_account)
  236. post :unmute, params: { id: other_account.id }
  237. end
  238. it 'returns http success' do
  239. expect(response).to have_http_status(200)
  240. end
  241. it 'removes the muting relation between user and target user' do
  242. expect(user.account.muting?(other_account)).to be false
  243. end
  244. it_behaves_like 'forbidden for wrong scope', 'read:accounts'
  245. end
  246. end