credentials_spec.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'credentials API' do
  4. let(:user) { Fabricate(:user, account_attributes: { discoverable: false, locked: true, indexable: false }) }
  5. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  6. let(:scopes) { 'read:accounts write:accounts' }
  7. let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
  8. describe 'GET /api/v1/accounts/verify_credentials' do
  9. subject do
  10. get '/api/v1/accounts/verify_credentials', headers: headers
  11. end
  12. it_behaves_like 'forbidden for wrong scope', 'write write:accounts'
  13. it 'returns http success with expected content' do
  14. subject
  15. expect(response)
  16. .to have_http_status(200)
  17. expect(response.content_type)
  18. .to start_with('application/json')
  19. expect(response.parsed_body).to include({
  20. source: hash_including({
  21. discoverable: false,
  22. indexable: false,
  23. }),
  24. locked: true,
  25. })
  26. end
  27. describe 'allows the profile scope' do
  28. let(:scopes) { 'profile' }
  29. it 'returns the response successfully' do
  30. subject
  31. expect(response).to have_http_status(200)
  32. expect(response.content_type)
  33. .to start_with('application/json')
  34. expect(response.parsed_body).to include({
  35. locked: true,
  36. })
  37. end
  38. end
  39. end
  40. describe 'PATCH /api/v1/accounts/update_credentials' do
  41. subject do
  42. patch '/api/v1/accounts/update_credentials', headers: headers, params: params
  43. end
  44. before { allow(ActivityPub::UpdateDistributionWorker).to receive(:perform_async) }
  45. let(:params) do
  46. {
  47. avatar: fixture_file_upload('avatar.gif', 'image/gif'),
  48. discoverable: true,
  49. display_name: "Alice Isn't Dead",
  50. header: fixture_file_upload('attachment.jpg', 'image/jpeg'),
  51. indexable: true,
  52. locked: false,
  53. note: 'Hello!',
  54. source: {
  55. privacy: 'unlisted',
  56. sensitive: true,
  57. },
  58. }
  59. end
  60. it_behaves_like 'forbidden for wrong scope', 'read read:accounts'
  61. describe 'with empty source list' do
  62. let(:params) { { display_name: "I'm a cat", source: {} } }
  63. it 'returns http success' do
  64. subject
  65. expect(response).to have_http_status(200)
  66. expect(response.content_type)
  67. .to start_with('application/json')
  68. end
  69. end
  70. describe 'with invalid data' do
  71. let(:params) { { note: 'This is too long. ' * 30 } }
  72. it 'returns http unprocessable entity' do
  73. subject
  74. expect(response).to have_http_status(422)
  75. expect(response.content_type)
  76. .to start_with('application/json')
  77. end
  78. end
  79. it 'returns http success with updated JSON attributes' do
  80. subject
  81. expect(response)
  82. .to have_http_status(200)
  83. expect(response.content_type)
  84. .to start_with('application/json')
  85. expect(response.parsed_body).to include({
  86. source: hash_including({
  87. discoverable: true,
  88. indexable: true,
  89. }),
  90. locked: false,
  91. })
  92. expect(ActivityPub::UpdateDistributionWorker)
  93. .to have_received(:perform_async).with(user.account_id)
  94. end
  95. def expect_account_updates
  96. expect(user.account.reload)
  97. .to have_attributes(
  98. display_name: eq("Alice Isn't Dead"),
  99. note: 'Hello!',
  100. avatar: exist,
  101. header: exist
  102. )
  103. end
  104. def expect_user_updates
  105. expect(user.reload)
  106. .to have_attributes(
  107. setting_default_privacy: eq('unlisted'),
  108. setting_default_sensitive: be(true)
  109. )
  110. end
  111. end
  112. end