1
0
Fork 0
mirror of https://github.com/mastodon/mastodon.git synced 2024-08-20 21:08:15 -07:00

Add more cases to search spec

This commit is contained in:
Angus McLeod 2024-05-23 18:21:52 +02:00
parent f6307d2c3d
commit a3d78540bd

View file

@ -85,27 +85,66 @@ describe 'Search API' do
end end
context 'when a remote actor username has changed' do context 'when a remote actor username has changed' do
let(:remote_actor_original_username) { 'original_username' } let!(:remote_actor_keypair) do
let(:remote_actor) do OpenSSL::PKey.read(<<~PEM_TEXT)
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAqIAYvNFGbZ5g4iiK6feSdXD4bDStFM58A7tHycYXaYtzZQpI
eHXAmaXuZzXIwtrP4N0gIk8JNwZvXj2UPS+S07t0V9wNK94he01LV5EMz/GN4eNn
FmDL64HIEuKLvV8TvgjbUPRD6Y5X0UpKi2ZIFLSb96Q5w0Z/k7ntpVKV52y8kz5F
jr/O/0JuHryZe0yItzJh8kzFfeMf0EXzfSnaKvT7P9jhgC6uTre+jXyvVZjiHDrn
qvvucdI3I7DRfXo1OqARBrLjy+TdseUAjNYJ+OuPRI1URIWQI01DCHqcohVu9+Ar
+BiCjFp3ua+XMuJvrvbD61d1Fvig/9nbBRR+8QIDAQABAoIBAAgySHnFWI6gItR3
fkfiqIm80cHCN3Xk1C6iiVu+3oBOZbHpW9R7vl9e/WOA/9O+LPjiSsQOegtWnVvd
RRjrl7Hj20VDlZKv5Mssm6zOGAxksrcVbqwdj+fUJaNJCL0AyyseH0x/IE9T8rDC
I1GH+3tB3JkhkIN/qjipdX5ab8MswEPu8IC4ViTpdBgWYY/xBcAHPw4xuL0tcwzh
FBlf4DqoEVQo8GdK5GAJ2Ny0S4xbXHUURzx/R4y4CCts7niAiLGqd9jmLU1kUTMk
QcXfQYK6l+unLc7wDYAz7sFEHh04M48VjWwiIZJnlCqmQbLda7uhhu8zkF1DqZTu
ulWDGQECgYEA0TIAc8BQBVab979DHEEmMdgqBwxLY3OIAk0b+r50h7VBGWCDPRsC
STD73fQY3lNet/7/jgSGwwAlAJ5PpMXxXiZAE3bUwPmHzgF7pvIOOLhA8O07tHSO
L2mvQe6NPzjZ+6iAO2U9PkClxcvGvPx2OBvisfHqZLmxC9PIVxzruQECgYEAzjM6
BTUXa6T/qHvLFbN699BXsUOGmHBGaLRapFDBfVvgZrwqYQcZpBBhesLdGTGSqwE7
gWsITPIJ+Ldo+38oGYyVys+w/V67q6ud7hgSDTW3hSvm+GboCjk6gzxlt9hQ0t9X
8vfDOYhEXvVUJNv3mYO60ENqQhILO4bQ0zi+VfECgYBb/nUccfG+pzunU0Cb6Dp3
qOuydcGhVmj1OhuXxLFSDG84Tazo7juvHA9mp7VX76mzmDuhpHPuxN2AzB2SBEoE
cSW0aYld413JRfWukLuYTc6hJHIhBTCRwRQFFnae2s1hUdQySm8INT2xIc+fxBXo
zrp+Ljg5Wz90SAnN5TX0AQKBgDaatDOq0o/r+tPYLHiLtfWoE4Dau+rkWJDjqdk3
lXWn/e3WyHY3Vh/vQpEqxzgju45TXjmwaVtPATr+/usSykCxzP0PMPR3wMT+Rm1F
rIoY/odij+CaB7qlWwxj0x/zRbwB7x1lZSp4HnrzBpxYL+JUUwVRxPLIKndSBTza
GvVRAoGBAIVBcNcRQYF4fvZjDKAb4fdBsEuHmycqtRCsnkGOz6ebbEQznSaZ0tZE
+JuouZaGjyp8uPjNGD5D7mIGbyoZ3KyG4mTXNxDAGBso1hrNDKGBOrGaPhZx8LgO
4VXJ+ybXrATf4jr8ccZYsZdFpOphPzz+j55Mqg5vac5P1XjmsGTb
-----END RSA PRIVATE KEY-----
PEM_TEXT
end
let!(:remote_actor_inbox_url) { 'https://remote.domain/users/bob/inbox' }
let!(:remote_actor_original_username) { 'original_username' }
let!(:remote_actor) do
Fabricate(:account, Fabricate(:account,
domain: 'remote.domain', domain: 'remote.domain',
uri: 'https://remote.domain/users/bob', uri: 'https://remote.domain/users/bob',
private_key: nil, private_key: nil,
public_key: remote_actor_keypair.public_key.to_pem,
username: remote_actor_original_username, username: remote_actor_original_username,
protocol: 1) # activitypub protocol: 1, # activitypub
inbox_url: remote_actor_inbox_url)
end end
let(:remote_actor_new_username) { 'new_username' } let!(:remote_actor_old_handle) { "#{remote_actor_original_username}@remote.domain" }
let(:remote_actor_json) do let!(:remote_actor_new_username) { 'new_username' }
let!(:remote_actor_json) do
{ {
'@context': 'https://www.w3.org/ns/activitystreams', '@context': 'https://www.w3.org/ns/activitystreams',
id: remote_actor.uri, id: remote_actor.uri,
type: 'Person', type: 'Person',
preferredUsername: remote_actor_new_username, preferredUsername: remote_actor_new_username,
inbox: "#{remote_actor.uri}#inbox", inbox: remote_actor.inbox_url,
publicKey: {
id: "#{remote_actor.uri}#main-key",
owner: remote_actor.uri,
publicKeyPem: remote_actor.public_key,
},
}.with_indifferent_access }.with_indifferent_access
end end
let(:remote_actor_new_handle) { "#{remote_actor_new_username}@remote.domain" } let!(:remote_actor_new_handle) { "#{remote_actor_new_username}@remote.domain" }
let(:params) { { q: remote_actor_new_handle, resolve: '1' } }
let(:webfinger_response) do let(:webfinger_response) do
{ {
subject: "acct:#{remote_actor_new_handle}", subject: "acct:#{remote_actor_new_handle}",
@ -121,6 +160,7 @@ describe 'Search API' do
before do before do
sign_in(user) sign_in(user)
tom.follow!(remote_actor)
stub_request(:get, "https://remote.domain/.well-known/webfinger?resource=acct:#{remote_actor_new_handle}") stub_request(:get, "https://remote.domain/.well-known/webfinger?resource=acct:#{remote_actor_new_handle}")
.to_return( .to_return(
body: webfinger_response.to_json, body: webfinger_response.to_json,
@ -137,26 +177,109 @@ describe 'Search API' do
}, },
status: 200 status: 200
) )
Sidekiq::Testing.inline!
end end
it 'does not increase the number of accounts' do context 'when requesting the old handle' do
Sidekiq::Testing.inline! let!(:params) { { q: remote_actor_old_handle, resolve: '1' } }
expect do
it 'does not increase the number of accounts' do
expect do
get '/api/v2/search', headers: headers, params: params
end.to(not_change { Account.count })
end
it 'does not change the remote actor account' do
get '/api/v2/search', headers: headers, params: params get '/api/v2/search', headers: headers, params: params
end.to(not_change { Account.count }) expect(remote_actor.reload.username).to eq(remote_actor_original_username)
end
it 'returns the remote actor account' do
get '/api/v2/search', headers: headers, params: params
expect(body_as_json[:accounts].pluck(:id)).to contain_exactly(remote_actor.id.to_s)
end
end end
it 'merges the old account with the new account' do context 'when requesting the old handle of a stale account' do
Sidekiq::Testing.inline! let!(:params) { { q: remote_actor_old_handle, resolve: '1' } }
tom.follow!(remote_actor)
get '/api/v2/search', headers: headers, params: params before do
expect(Account.exists?(id: remote_actor.id)).to be(false) stub_request(:get, 'https://remote.domain/.well-known/host-meta').to_return(status: 404)
new_remote_actor = Account.find_by( remote_actor.update(last_webfingered_at: 2.days.ago)
uri: remote_actor.uri, end
username: remote_actor_new_username
) it 'makes a webfinger request with the old handle' do
expect(new_remote_actor.present?).to be(true) stub_request(:get, "https://remote.domain/.well-known/webfinger?resource=acct:#{remote_actor_old_handle}")
expect(tom.following?(new_remote_actor)).to be(true) get '/api/v2/search', headers: headers, params: params
expect(
a_request(
:get,
"https://remote.domain/.well-known/webfinger?resource=acct:#{remote_actor_old_handle}"
)
).to have_been_made.once
end
it 'does nothing if the webfinger request returns not found' do
stub_request(:get, "https://remote.domain/.well-known/webfinger?resource=acct:#{remote_actor_old_handle}")
.to_return(
status: 404
)
get '/api/v2/search', headers: headers, params: params
expect(body_as_json[:accounts].empty?).to be(true)
expect(remote_actor.reload.username).to eq(remote_actor_original_username)
end
it 'merges the old account with the new account if the webfinger request succeeds' do
stub_request(:get, "https://remote.domain/.well-known/webfinger?resource=acct:#{remote_actor_old_handle}")
.to_return(
body: {
subject: "acct:#{remote_actor_old_handle}",
links: [
{
rel: 'self',
type: 'application/activity+json',
href: remote_actor.uri,
},
],
}.to_json,
headers: {
'Content-Type' => 'application/json',
},
status: 200
)
expect do
get '/api/v2/search', headers: headers, params: params
end.to(not_change { Account.count })
expect(Account.exists?(id: remote_actor.id)).to be(false)
new_remote_actor = Account.find_by(
uri: remote_actor.uri,
username: remote_actor_new_username
)
expect(new_remote_actor.present?).to be(true)
expect(tom.following?(new_remote_actor)).to be(true)
end
end
context 'when requesting the new handle' do
let(:params) { { q: remote_actor_new_handle, resolve: '1' } }
it 'does not increase the number of accounts' do
expect do
get '/api/v2/search', headers: headers, params: params
end.to(not_change { Account.count })
end
it 'merges the old account with the new account' do
get '/api/v2/search', headers: headers, params: params
expect(Account.exists?(id: remote_actor.id)).to be(false)
new_remote_actor = Account.find_by(
uri: remote_actor.uri,
username: remote_actor_new_username
)
expect(new_remote_actor.present?).to be(true)
expect(tom.following?(new_remote_actor)).to be(true)
end
end end
end end
end end