app_sign_up_service_spec.rb 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. require 'rails_helper'
  2. RSpec.describe AppSignUpService, type: :service do
  3. let(:app) { Fabricate(:application, scopes: 'read write') }
  4. let(:good_params) { { username: 'alice', password: '12345678', email: 'good@email.com', agreement: true } }
  5. let(:remote_ip) { IPAddr.new('198.0.2.1') }
  6. subject { described_class.new }
  7. describe '#call' do
  8. it 'returns nil when registrations are closed' do
  9. tmp = Setting.registrations_mode
  10. Setting.registrations_mode = 'none'
  11. expect { subject.call(app, remote_ip, good_params) }.to raise_error Mastodon::NotPermittedError
  12. Setting.registrations_mode = tmp
  13. end
  14. it 'raises an error when params are missing' do
  15. expect { subject.call(app, remote_ip, {}) }.to raise_error ActiveRecord::RecordInvalid
  16. end
  17. it 'creates an unconfirmed user with access token' do
  18. access_token = subject.call(app, remote_ip, good_params)
  19. expect(access_token).to_not be_nil
  20. user = User.find_by(id: access_token.resource_owner_id)
  21. expect(user).to_not be_nil
  22. expect(user.confirmed?).to be false
  23. end
  24. it 'creates access token with the app\'s scopes' do
  25. access_token = subject.call(app, remote_ip, good_params)
  26. expect(access_token).to_not be_nil
  27. expect(access_token.scopes.to_s).to eq 'read write'
  28. end
  29. it 'creates an account' do
  30. access_token = subject.call(app, remote_ip, good_params)
  31. expect(access_token).to_not be_nil
  32. user = User.find_by(id: access_token.resource_owner_id)
  33. expect(user).to_not be_nil
  34. expect(user.account).to_not be_nil
  35. expect(user.invite_request).to be_nil
  36. end
  37. it 'creates an account with invite request text' do
  38. access_token = subject.call(app, remote_ip, good_params.merge(reason: 'Foo bar'))
  39. expect(access_token).to_not be_nil
  40. user = User.find_by(id: access_token.resource_owner_id)
  41. expect(user).to_not be_nil
  42. expect(user.invite_request&.text).to eq 'Foo bar'
  43. end
  44. end
  45. end