app_sign_up_service_spec.rb 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe AppSignUpService, type: :service 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. it 'returns nil when registrations are closed' do
  10. tmp = Setting.registrations_mode
  11. Setting.registrations_mode = 'none'
  12. expect { subject.call(app, remote_ip, good_params) }.to raise_error Mastodon::NotPermittedError
  13. Setting.registrations_mode = tmp
  14. end
  15. it 'raises an error when params are missing' do
  16. expect { subject.call(app, remote_ip, {}) }.to raise_error ActiveRecord::RecordInvalid
  17. end
  18. it 'creates an unconfirmed user with access token' do
  19. access_token = subject.call(app, remote_ip, good_params)
  20. expect(access_token).to_not be_nil
  21. user = User.find_by(id: access_token.resource_owner_id)
  22. expect(user).to_not be_nil
  23. expect(user.confirmed?).to be false
  24. end
  25. it 'creates access token with the app\'s scopes' do
  26. access_token = subject.call(app, remote_ip, good_params)
  27. expect(access_token).to_not be_nil
  28. expect(access_token.scopes.to_s).to eq 'read write'
  29. end
  30. it 'creates an account' do
  31. access_token = subject.call(app, remote_ip, good_params)
  32. expect(access_token).to_not be_nil
  33. user = User.find_by(id: access_token.resource_owner_id)
  34. expect(user).to_not be_nil
  35. expect(user.account).to_not be_nil
  36. expect(user.invite_request).to be_nil
  37. end
  38. it 'creates an account with invite request text' do
  39. access_token = subject.call(app, remote_ip, good_params.merge(reason: 'Foo bar'))
  40. expect(access_token).to_not be_nil
  41. user = User.find_by(id: access_token.resource_owner_id)
  42. expect(user).to_not be_nil
  43. expect(user.invite_request&.text).to eq 'Foo bar'
  44. end
  45. end
  46. end