finder_concern_spec.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Account::FinderConcern do
  4. describe 'local finders' do
  5. let!(:account) { Fabricate(:account, username: 'Alice') }
  6. describe '.find_local' do
  7. it 'returns case-insensitive result' do
  8. expect(Account.find_local('alice')).to eq(account)
  9. end
  10. it 'returns correctly cased result' do
  11. expect(Account.find_local('Alice')).to eq(account)
  12. end
  13. it 'returns nil without a match' do
  14. expect(Account.find_local('a_ice')).to be_nil
  15. end
  16. it 'returns nil for regex style username value' do
  17. expect(Account.find_local('al%')).to be_nil
  18. end
  19. it 'returns nil for nil username value' do
  20. expect(Account.find_local(nil)).to be_nil
  21. end
  22. it 'returns nil for blank username value' do
  23. expect(Account.find_local('')).to be_nil
  24. end
  25. end
  26. describe '.find_local!' do
  27. it 'returns matching result' do
  28. expect(Account.find_local!('alice')).to eq(account)
  29. end
  30. it 'raises on non-matching result' do
  31. expect { Account.find_local!('missing') }.to raise_error(ActiveRecord::RecordNotFound)
  32. end
  33. it 'raises with blank username' do
  34. expect { Account.find_local!('') }.to raise_error(ActiveRecord::RecordNotFound)
  35. end
  36. it 'raises with nil username' do
  37. expect { Account.find_local!(nil) }.to raise_error(ActiveRecord::RecordNotFound)
  38. end
  39. end
  40. end
  41. describe 'remote finders' do
  42. let!(:account) { Fabricate(:account, username: 'Alice', domain: 'mastodon.social') }
  43. describe '.find_remote' do
  44. it 'returns exact match result' do
  45. expect(Account.find_remote('alice', 'mastodon.social')).to eq(account)
  46. end
  47. it 'returns case-insensitive result' do
  48. expect(Account.find_remote('ALICE', 'MASTODON.SOCIAL')).to eq(account)
  49. end
  50. it 'returns nil when username does not match' do
  51. expect(Account.find_remote('a_ice', 'mastodon.social')).to be_nil
  52. end
  53. it 'returns nil when domain does not match' do
  54. expect(Account.find_remote('alice', 'm_stodon.social')).to be_nil
  55. end
  56. it 'returns nil for regex style domain value' do
  57. expect(Account.find_remote('alice', 'm%')).to be_nil
  58. end
  59. it 'returns nil for nil username value' do
  60. expect(Account.find_remote(nil, 'domain')).to be_nil
  61. end
  62. it 'returns nil for blank username value' do
  63. expect(Account.find_remote('', 'domain')).to be_nil
  64. end
  65. end
  66. describe '.find_remote!' do
  67. it 'returns matching result' do
  68. expect(Account.find_remote!('alice', 'mastodon.social')).to eq(account)
  69. end
  70. it 'raises on non-matching result' do
  71. expect { Account.find_remote!('missing', 'mastodon.host') }.to raise_error(ActiveRecord::RecordNotFound)
  72. end
  73. it 'raises with blank username' do
  74. expect { Account.find_remote!('', '') }.to raise_error(ActiveRecord::RecordNotFound)
  75. end
  76. it 'raises with nil username' do
  77. expect { Account.find_remote!(nil, nil) }.to raise_error(ActiveRecord::RecordNotFound)
  78. end
  79. end
  80. end
  81. end