credentials_controller_spec.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. require 'rails_helper'
  2. describe Api::V1::Accounts::CredentialsController do
  3. render_views
  4. let(:user) { Fabricate(:user) }
  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('avatar.gif', 'image/gif'),
  26. header: fixture_file_upload('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 empty source list' do
  50. before do
  51. patch :update, params: {
  52. display_name: "I'm a cat",
  53. source: {},
  54. }, as: :json
  55. end
  56. it 'returns http success' do
  57. expect(response).to have_http_status(200)
  58. end
  59. end
  60. describe 'with invalid data' do
  61. before do
  62. patch :update, params: { note: 'This is too long. ' * 30 }
  63. end
  64. it 'returns http unprocessable entity' do
  65. expect(response).to have_http_status(:unprocessable_entity)
  66. end
  67. end
  68. end
  69. end
  70. context 'without an oauth token' do
  71. before do
  72. allow(controller).to receive(:doorkeeper_token) { nil }
  73. end
  74. describe 'GET #show' do
  75. it 'returns http unauthorized' do
  76. get :show
  77. expect(response).to have_http_status(:unauthorized)
  78. end
  79. end
  80. describe 'PATCH #update' do
  81. it 'returns http unauthorized' do
  82. patch :update, params: { note: 'Foo' }
  83. expect(response).to have_http_status(:unauthorized)
  84. end
  85. end
  86. end
  87. end