accounts_controller_spec.rb 9.6 KB

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