2023-02-22 01:55:31 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-12-24 19:12:38 +01:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe AppSignUpService, type: :service do
|
2023-02-20 05:24:14 +01:00
|
|
|
subject { described_class.new }
|
|
|
|
|
2018-12-24 19:12:38 +01:00
|
|
|
let(:app) { Fabricate(:application, scopes: 'read write') }
|
|
|
|
let(:good_params) { { username: 'alice', password: '12345678', email: 'good@email.com', agreement: true } }
|
2020-10-12 16:33:49 +02:00
|
|
|
let(:remote_ip) { IPAddr.new('198.0.2.1') }
|
2018-12-24 19:12:38 +01:00
|
|
|
|
|
|
|
describe '#call' do
|
2023-11-13 14:27:00 +01:00
|
|
|
let(:params) { good_params }
|
2018-12-24 19:12:38 +01:00
|
|
|
|
2023-11-13 14:27:00 +01:00
|
|
|
shared_examples 'successful registration' do
|
|
|
|
it 'creates an unconfirmed user with access token and the app\'s scope', :aggregate_failures do
|
|
|
|
access_token = subject.call(app, remote_ip, params)
|
|
|
|
expect(access_token).to_not be_nil
|
|
|
|
expect(access_token.scopes.to_s).to eq 'read write'
|
2018-12-24 19:12:38 +01:00
|
|
|
|
2023-11-13 14:27:00 +01:00
|
|
|
user = User.find_by(id: access_token.resource_owner_id)
|
|
|
|
expect(user).to_not be_nil
|
|
|
|
expect(user.confirmed?).to be false
|
|
|
|
|
|
|
|
expect(user.account).to_not be_nil
|
|
|
|
expect(user.invite_request).to be_nil
|
|
|
|
end
|
2018-12-24 19:12:38 +01:00
|
|
|
end
|
|
|
|
|
2023-11-13 14:27:00 +01:00
|
|
|
context 'when registrations are closed' do
|
|
|
|
around do |example|
|
|
|
|
tmp = Setting.registrations_mode
|
|
|
|
Setting.registrations_mode = 'none'
|
|
|
|
|
|
|
|
example.run
|
|
|
|
|
|
|
|
Setting.registrations_mode = tmp
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error', :aggregate_failures do
|
|
|
|
expect { subject.call(app, remote_ip, good_params) }.to raise_error Mastodon::NotPermittedError
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when using a valid invite' do
|
|
|
|
let(:params) { good_params.merge({ invite_code: invite.code }) }
|
|
|
|
let(:invite) { Fabricate(:invite) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
invite.user.approve!
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'successful registration'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when using an invalid invite' do
|
|
|
|
let(:params) { good_params.merge({ invite_code: invite.code }) }
|
|
|
|
let(:invite) { Fabricate(:invite, uses: 1, max_uses: 1) }
|
|
|
|
|
|
|
|
it 'raises an error', :aggregate_failures do
|
|
|
|
expect { subject.call(app, remote_ip, params) }.to raise_error Mastodon::NotPermittedError
|
|
|
|
end
|
|
|
|
end
|
2018-12-24 19:12:38 +01:00
|
|
|
end
|
|
|
|
|
2023-11-13 14:27:00 +01:00
|
|
|
it 'raises an error when params are missing' do
|
|
|
|
expect { subject.call(app, remote_ip, {}) }.to raise_error ActiveRecord::RecordInvalid
|
2019-10-03 17:50:59 +02:00
|
|
|
end
|
|
|
|
|
2023-11-13 14:27:00 +01:00
|
|
|
it_behaves_like 'successful registration'
|
|
|
|
|
|
|
|
context 'when given an invite request text' do
|
|
|
|
it 'creates an account with invite request text' do
|
|
|
|
access_token = subject.call(app, remote_ip, good_params.merge(reason: 'Foo bar'))
|
|
|
|
expect(access_token).to_not be_nil
|
|
|
|
user = User.find_by(id: access_token.resource_owner_id)
|
|
|
|
expect(user).to_not be_nil
|
|
|
|
expect(user.invite_request&.text).to eq 'Foo bar'
|
|
|
|
end
|
2018-12-24 19:12:38 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|