|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +RSpec.describe 'Deliverability probe sign-up (imastodon)', type: :request do |
| 6 | + let(:probe_username) { 'bp8d064b8ce361cf9f' } |
| 7 | + let(:probe_reason) { 'Automated protocol deliverability probe' } |
| 8 | + |
| 9 | + before { Setting.registrations_mode = 'approved' } |
| 10 | + |
| 11 | + describe 'POST /api/v1/accounts' do |
| 12 | + subject do |
| 13 | + post '/api/v1/accounts', headers: { 'Authorization' => "Bearer #{token.token}" }, params: { username: username, reason: reason, password: '12345678', email: 'probe@example.com', agreement: 'true' } |
| 14 | + end |
| 15 | + |
| 16 | + let(:token) { Fabricate(:client_credentials_token, application: Fabricate(:application, scopes: 'read write'), scopes: 'read write') } |
| 17 | + |
| 18 | + context 'when the username is bp + 16 hex digits and the reason is the probe text' do |
| 19 | + let(:username) { probe_username } |
| 20 | + let(:reason) { probe_reason } |
| 21 | + |
| 22 | + it 'returns 200 without creating an account' do |
| 23 | + expect { subject } |
| 24 | + .to not_change(User, :count) |
| 25 | + .and not_change(Account, :count) |
| 26 | + |
| 27 | + expect(response).to have_http_status(200) |
| 28 | + end |
| 29 | + |
| 30 | + it 'returns a dummy token response shaped like a real sign-up' do |
| 31 | + post '/api/v1/accounts', headers: { 'Authorization' => "Bearer #{token.token}" }, params: { username: 'fuyuko', password: '12345678', email: 'fuyuko@example.com', agreement: 'true' } |
| 32 | + real_body = response.parsed_body |
| 33 | + real_headers = response.headers.slice('Cache-Control', 'Content-Type', 'Deprecation', 'Pragma') |
| 34 | + |
| 35 | + expect { subject } |
| 36 | + .to not_change(Doorkeeper::AccessToken, :count) |
| 37 | + |
| 38 | + expect(response.parsed_body.keys).to match_array(real_body.keys) |
| 39 | + expect(response.parsed_body) |
| 40 | + .to include( |
| 41 | + 'access_token' => be_a(String).and(have_attributes(length: real_body['access_token'].length)), |
| 42 | + 'token_type' => real_body['token_type'], |
| 43 | + 'scope' => real_body['scope'], |
| 44 | + 'created_at' => be_within(5).of(Time.now.to_i) |
| 45 | + ) |
| 46 | + expect(response.parsed_body['access_token']).to_not eq(real_body['access_token']) |
| 47 | + expect(response.headers.slice('Cache-Control', 'Content-Type', 'Deprecation', 'Pragma')).to eq(real_headers) |
| 48 | + end |
| 49 | + |
| 50 | + it 'logs the username, IP and application of the rejected probe' do |
| 51 | + allow(Rails.logger).to receive(:info) |
| 52 | + |
| 53 | + subject |
| 54 | + |
| 55 | + expect(Rails.logger) |
| 56 | + .to have_received(:info) |
| 57 | + .with("Rejected deliverability probe sign-up: username=#{probe_username} ip=127.0.0.1 app=#{token.application.name}") |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + context 'when only the username matches' do |
| 62 | + let(:username) { probe_username } |
| 63 | + let(:reason) { 'アイマスが好きです' } |
| 64 | + |
| 65 | + it 'creates an account with the probe-like username' do |
| 66 | + expect { subject } |
| 67 | + .to change(User, :count).by(1) |
| 68 | + |
| 69 | + expect(Account.find_local(probe_username)).to be_present |
| 70 | + end |
| 71 | + end |
| 72 | + |
| 73 | + context 'when only the reason matches' do |
| 74 | + let(:username) { 'fuyuko' } |
| 75 | + let(:reason) { probe_reason } |
| 76 | + |
| 77 | + it 'creates an account with the probe reason' do |
| 78 | + expect { subject } |
| 79 | + .to change(User, :count).by(1) |
| 80 | + |
| 81 | + expect(Account.find_local('fuyuko')).to be_present |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + context 'when the hex part of the username is not 16 digits' do |
| 86 | + let(:username) { 'bp8d064b8ce361cf9f0' } |
| 87 | + let(:reason) { probe_reason } |
| 88 | + |
| 89 | + it 'creates an account with the 17-digit username' do |
| 90 | + expect { subject } |
| 91 | + .to change(User, :count).by(1) |
| 92 | + |
| 93 | + expect(Account.find_local('bp8d064b8ce361cf9f0')).to be_present |
| 94 | + end |
| 95 | + end |
| 96 | + end |
| 97 | +end |
0 commit comments