credentials_controller_spec.rb 2.9 KB

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