app_sign_up_service_spec.rb 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe AppSignUpService do
  4. subject { described_class.new }
  5. let(:app) { Fabricate(:application, scopes: 'read write') }
  6. let(:good_params) { { username: 'alice', password: '12345678', email: 'good@email.com', agreement: true } }
  7. let(:remote_ip) { IPAddr.new('198.0.2.1') }
  8. describe '#call' do
  9. let(:params) { good_params }
  10. shared_examples 'successful registration' do
  11. it 'creates an unconfirmed user with access token and the app\'s scope', :aggregate_failures do
  12. access_token = subject.call(app, remote_ip, params)
  13. expect(access_token).to_not be_nil
  14. expect(access_token.scopes.to_s).to eq 'read write'
  15. user = User.find_by(id: access_token.resource_owner_id)
  16. expect(user).to_not be_nil
  17. expect(user.confirmed?).to be false
  18. expect(user.account).to_not be_nil
  19. expect(user.invite_request).to be_nil
  20. end
  21. end
  22. context 'when the email address requires approval' do
  23. before do
  24. Setting.registrations_mode = 'open'
  25. Fabricate(:email_domain_block, allow_with_approval: true, domain: 'email.com')
  26. end
  27. it 'creates an unapproved user', :aggregate_failures do
  28. access_token = subject.call(app, remote_ip, params)
  29. expect(access_token).to_not be_nil
  30. expect(access_token.scopes.to_s).to eq 'read write'
  31. user = User.find_by(id: access_token.resource_owner_id)
  32. expect(user).to_not be_nil
  33. expect(user.confirmed?).to be false
  34. expect(user.approved?).to be false
  35. expect(user.account).to_not be_nil
  36. expect(user.invite_request).to be_nil
  37. end
  38. end
  39. context 'when the email address requires approval through MX records' do
  40. before do
  41. Setting.registrations_mode = 'open'
  42. Fabricate(:email_domain_block, allow_with_approval: true, domain: 'smtp.email.com')
  43. allow(User).to receive(:skip_mx_check?).and_return(false)
  44. configure_mx(domain: 'email.com', exchange: 'smtp.email.com')
  45. end
  46. it 'creates an unapproved user', :aggregate_failures do
  47. access_token = subject.call(app, remote_ip, params)
  48. expect(access_token).to_not be_nil
  49. expect(access_token.scopes.to_s).to eq 'read write'
  50. user = User.find_by(id: access_token.resource_owner_id)
  51. expect(user).to_not be_nil
  52. expect(user.confirmed?).to be false
  53. expect(user.approved?).to be false
  54. expect(user.account).to_not be_nil
  55. expect(user.invite_request).to be_nil
  56. end
  57. end
  58. context 'when registrations are closed' do
  59. before do
  60. Setting.registrations_mode = 'none'
  61. end
  62. it 'raises an error', :aggregate_failures do
  63. expect { subject.call(app, remote_ip, good_params) }.to raise_error Mastodon::NotPermittedError
  64. end
  65. context 'when using a valid invite' do
  66. let(:params) { good_params.merge({ invite_code: invite.code }) }
  67. let(:invite) { Fabricate(:invite) }
  68. before do
  69. invite.user.approve!
  70. end
  71. it_behaves_like 'successful registration'
  72. end
  73. context 'when using an invalid invite' do
  74. let(:params) { good_params.merge({ invite_code: invite.code }) }
  75. let(:invite) { Fabricate(:invite, uses: 1, max_uses: 1) }
  76. it 'raises an error', :aggregate_failures do
  77. expect { subject.call(app, remote_ip, params) }.to raise_error Mastodon::NotPermittedError
  78. end
  79. end
  80. end
  81. it 'raises an error when params are missing' do
  82. expect { subject.call(app, remote_ip, {}) }.to raise_error ActiveRecord::RecordInvalid
  83. end
  84. it_behaves_like 'successful registration'
  85. context 'when given an invite request text' do
  86. it 'creates an account with invite request text' do
  87. access_token = subject.call(app, remote_ip, good_params.merge(reason: 'Foo bar'))
  88. expect(access_token).to_not be_nil
  89. user = User.find_by(id: access_token.resource_owner_id)
  90. expect(user).to_not be_nil
  91. expect(user.invite_request&.text).to eq 'Foo bar'
  92. end
  93. end
  94. end
  95. end