闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
3.2 KiB

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