credentials_controller_spec.rb 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. require 'rails_helper'
  2. describe Api::V1::Accounts::CredentialsController do
  3. render_views
  4. let(:user) { Fabricate(:user, account: Fabricate(:account, username: 'alice')) }
  5. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  6. context 'with an oauth token' do
  7. before do
  8. allow(controller).to receive(:doorkeeper_token) { token }
  9. end
  10. describe 'GET #show' do
  11. let(:scopes) { 'read:accounts' }
  12. it 'returns http success' do
  13. get :show
  14. expect(response).to have_http_status(200)
  15. end
  16. end
  17. describe 'PATCH #update' do
  18. let(:scopes) { 'write:accounts' }
  19. describe 'with valid data' do
  20. before do
  21. allow(ActivityPub::UpdateDistributionWorker).to receive(:perform_async)
  22. patch :update, params: {
  23. display_name: "Alice Isn't Dead",
  24. note: "Hi!\n\nToot toot!",
  25. avatar: fixture_file_upload('files/avatar.gif', 'image/gif'),
  26. header: fixture_file_upload('files/attachment.jpg', 'image/jpeg'),
  27. source: {
  28. privacy: 'unlisted',
  29. sensitive: true,
  30. }
  31. }
  32. end
  33. it 'returns http success' do
  34. expect(response).to have_http_status(200)
  35. end
  36. it 'updates account info' do
  37. user.account.reload
  38. expect(user.account.display_name).to eq("Alice Isn't Dead")
  39. expect(user.account.note).to eq("Hi!\n\nToot toot!")
  40. expect(user.account.avatar).to exist
  41. expect(user.account.header).to exist
  42. expect(user.setting_default_privacy).to eq('unlisted')
  43. expect(user.setting_default_sensitive).to eq(true)
  44. end
  45. it 'queues up an account update distribution' do
  46. expect(ActivityPub::UpdateDistributionWorker).to have_received(:perform_async).with(user.account_id)
  47. end
  48. end
  49. describe 'with invalid data' do
  50. before do
  51. patch :update, params: { note: 'This is too long. ' * 30 }
  52. end
  53. it 'returns http unprocessable entity' do
  54. expect(response).to have_http_status(:unprocessable_entity)
  55. end
  56. end
  57. end
  58. end
  59. context 'without an oauth token' do
  60. before do
  61. allow(controller).to receive(:doorkeeper_token) { nil }
  62. end
  63. describe 'GET #show' do
  64. it 'returns http unauthorized' do
  65. get :show
  66. expect(response).to have_http_status(:unauthorized)
  67. end
  68. end
  69. describe 'PATCH #update' do
  70. it 'returns http unauthorized' do
  71. patch :update, params: { note: 'Foo' }
  72. expect(response).to have_http_status(:unauthorized)
  73. end
  74. end
  75. end
  76. end