Skip to content

Commit 2bf7bb5

Browse files
committed
Require agreement param to be true in the API when creating an account
1 parent 232e3dc commit 2bf7bb5

9 files changed

Lines changed: 37 additions & 23 deletions

File tree

app/controllers/api/v1/accounts_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def check_account_suspension
7676
end
7777

7878
def account_params
79-
params.permit(:username, :email, :password)
79+
params.permit(:username, :email, :password, :agreement)
8080
end
8181

8282
def check_enabled_registrations

app/controllers/auth/registrations_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def build_resource(hash = nil)
2626

2727
resource.locale = I18n.locale
2828
resource.invite_code = params[:invite_code] if resource.invite_code.blank?
29+
resource.agreement = true
2930

3031
resource.build_account if resource.account.nil?
3132
end

app/models/user.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class User < ApplicationRecord
7676
validates :locale, inclusion: I18n.available_locales.map(&:to_s), if: :locale?
7777
validates_with BlacklistedEmailValidator, if: :email_changed?
7878
validates_with EmailMxValidator, if: :validate_email_dns?
79+
validates :agreement, acceptance: { allow_nil: false, accept: [true, 'true', '1'] }, on: :create
7980

8081
scope :recent, -> { order(id: :desc) }
8182
scope :admins, -> { where(admin: true) }
@@ -296,7 +297,7 @@ def self.pam_get_user(attributes = {})
296297
end
297298

298299
if resource.blank?
299-
resource = new(email: attributes[:email])
300+
resource = new(email: attributes[:email], agreement: true)
300301
if Devise.check_at_sign && !resource[:email].index('@')
301302
resource[:email] = Rpam2.getenv(resource.find_pam_service, attributes[:email], attributes[:password], 'email', false)
302303
resource[:email] = "#{attributes[:email]}@#{resource.find_pam_suffix}" unless resource[:email]
@@ -309,7 +310,7 @@ def self.ldap_get_user(attributes = {})
309310
resource = joins(:account).find_by(accounts: { username: attributes[Devise.ldap_uid.to_sym].first })
310311

311312
if resource.blank?
312-
resource = new(email: attributes[:mail].first, account_attributes: { username: attributes[Devise.ldap_uid.to_sym].first })
313+
resource = new(email: attributes[:mail].first, agreement: true, account_attributes: { username: attributes[Devise.ldap_uid.to_sym].first })
313314
resource.ldap_setup(attributes)
314315
end
315316

app/services/app_sign_up_service.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class AppSignUpService < BaseService
44
def call(app, params)
55
return unless allowed_registrations?
66

7-
user_params = params.slice(:email, :password)
7+
user_params = params.slice(:email, :password, :agreement)
88
account_params = params.slice(:username)
99
user = User.create!(user_params.merge(created_by_application: app, password_confirmation: user_params[:password], account_attributes: account_params))
1010

lib/mastodon/accounts_cli.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def rotate(username = nil)
7373
def create(username)
7474
account = Account.new(username: username)
7575
password = SecureRandom.hex
76-
user = User.new(email: options[:email], password: password, admin: options[:role] == 'admin', moderator: options[:role] == 'moderator', confirmed_at: Time.now.utc)
76+
user = User.new(email: options[:email], password: password, agreement: true, admin: options[:role] == 'admin', moderator: options[:role] == 'moderator', confirmed_at: options[:confirmed] ? Time.now.utc : nil)
7777

7878
if options[:reattach]
7979
account = Account.find_local(username) || Account.new(username: username)

spec/controllers/api/v1/accounts_controller_spec.rb

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,34 @@
2222
describe 'POST #create' do
2323
let(:app) { Fabricate(:application) }
2424
let(:token) { Doorkeeper::AccessToken.find_or_create_for(app, nil, 'read write', nil, false) }
25+
let(:agreement) { nil }
2526

2627
before do
27-
post :create, params: { username: 'test', password: '12345678', email: 'hello@world.tld' }
28+
post :create, params: { username: 'test', password: '12345678', email: 'hello@world.tld', agreement: agreement }
2829
end
2930

30-
it 'returns http success' do
31-
expect(response).to have_http_status(200)
32-
end
31+
context 'given truthy agreement' do
32+
let(:agreement) { 'true' }
3333

34-
it 'returns a new access token as JSON' do
35-
expect(body_as_json[:access_token]).to_not be_blank
34+
it 'returns http success' do
35+
expect(response).to have_http_status(200)
36+
end
37+
38+
it 'returns a new access token as JSON' do
39+
expect(body_as_json[:access_token]).to_not be_blank
40+
end
41+
42+
it 'creates a user' do
43+
user = User.find_by(email: 'hello@world.tld')
44+
expect(user).to_not be_nil
45+
expect(user.created_by_application_id).to eq app.id
46+
end
3647
end
3748

38-
it 'creates a user' do
39-
user = User.find_by(email: 'hello@world.tld')
40-
expect(user).to_not be_nil
41-
expect(user.created_by_application_id).to eq app.id
49+
context 'given no agreement' do
50+
it 'returns http unprocessable entity' do
51+
expect(response).to have_http_status(422)
52+
end
4253
end
4354
end
4455

spec/fabricators/user_fabricator.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
email { sequence(:email) { |i| "#{i}#{Faker::Internet.email}" } }
44
password "123456789"
55
confirmed_at { Time.zone.now }
6+
agreement true
67
end

spec/models/user_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,19 +106,19 @@
106106
end
107107

108108
it 'should allow a non-blacklisted user to be created' do
109-
user = User.new(email: 'foo@example.com', account: account, password: password)
109+
user = User.new(email: 'foo@example.com', account: account, password: password, agreement: true)
110110

111111
expect(user.valid?).to be_truthy
112112
end
113113

114114
it 'should not allow a blacklisted user to be created' do
115-
user = User.new(email: 'foo@mvrht.com', account: account, password: password)
115+
user = User.new(email: 'foo@mvrht.com', account: account, password: password, agreement: true)
116116

117117
expect(user.valid?).to be_falsey
118118
end
119119

120120
it 'should not allow a subdomain blacklisted user to be created' do
121-
user = User.new(email: 'foo@mvrht.com.topdomain.tld', account: account, password: password)
121+
user = User.new(email: 'foo@mvrht.com.topdomain.tld', account: account, password: password, agreement: true)
122122

123123
expect(user.valid?).to be_falsey
124124
end
@@ -210,17 +210,17 @@
210210
end
211211

212212
it 'should not allow a user to be created unless they are whitelisted' do
213-
user = User.new(email: 'foo@example.com', account: account, password: password)
213+
user = User.new(email: 'foo@example.com', account: account, password: password, agreement: true)
214214
expect(user.valid?).to be_falsey
215215
end
216216

217217
it 'should allow a user to be created if they are whitelisted' do
218-
user = User.new(email: 'foo@mastodon.space', account: account, password: password)
218+
user = User.new(email: 'foo@mastodon.space', account: account, password: password, agreement: true)
219219
expect(user.valid?).to be_truthy
220220
end
221221

222222
it 'should not allow a user with a whitelisted top domain as subdomain in their email address to be created' do
223-
user = User.new(email: 'foo@mastodon.space.userdomain.com', account: account, password: password)
223+
user = User.new(email: 'foo@mastodon.space.userdomain.com', account: account, password: password, agreement: true)
224224
expect(user.valid?).to be_falsey
225225
end
226226

@@ -242,7 +242,7 @@
242242

243243
it_behaves_like 'Settings-extended' do
244244
def create!
245-
User.create!(account: Fabricate(:account), email: 'foo@mastodon.space', password: 'abcd1234')
245+
User.create!(account: Fabricate(:account), email: 'foo@mastodon.space', password: 'abcd1234', agreement: true)
246246
end
247247

248248
def fabricate

spec/services/app_sign_up_service_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
RSpec.describe AppSignUpService, type: :service do
44
let(:app) { Fabricate(:application, scopes: 'read write') }
5-
let(:good_params) { { username: 'alice', password: '12345678', email: 'good@email.com' } }
5+
let(:good_params) { { username: 'alice', password: '12345678', email: 'good@email.com', agreement: true } }
66

77
subject { described_class.new }
88

0 commit comments

Comments
 (0)