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.

281 lines
7.7 KiB

8 years ago
8 years ago
  1. require 'rails_helper'
  2. RSpec.describe Account, type: :model do
  3. subject { Fabricate(:account, username: 'alice') }
  4. context do
  5. let(:bob) { Fabricate(:account, username: 'bob') }
  6. describe '#follow!' do
  7. it 'creates a follow' do
  8. follow = subject.follow!(bob)
  9. expect(follow).to be_instance_of Follow
  10. expect(follow.account).to eq subject
  11. expect(follow.target_account).to eq bob
  12. end
  13. end
  14. describe '#unfollow!' do
  15. before do
  16. subject.follow!(bob)
  17. end
  18. it 'destroys a follow' do
  19. unfollow = subject.unfollow!(bob)
  20. expect(unfollow).to be_instance_of Follow
  21. expect(unfollow.account).to eq subject
  22. expect(unfollow.target_account).to eq bob
  23. expect(unfollow.destroyed?).to be true
  24. end
  25. end
  26. describe '#following?' do
  27. it 'returns true when the target is followed' do
  28. subject.follow!(bob)
  29. expect(subject.following?(bob)).to be true
  30. end
  31. it 'returns false if the target is not followed' do
  32. expect(subject.following?(bob)).to be false
  33. end
  34. end
  35. end
  36. describe '#local?' do
  37. it 'returns true when the account is local' do
  38. expect(subject.local?).to be true
  39. end
  40. it 'returns false when the account is on a different domain' do
  41. subject.domain = 'foreign.tld'
  42. expect(subject.local?).to be false
  43. end
  44. end
  45. describe '#acct' do
  46. it 'returns username for local users' do
  47. expect(subject.acct).to eql 'alice'
  48. end
  49. it 'returns username@domain for foreign users' do
  50. subject.domain = 'foreign.tld'
  51. expect(subject.acct).to eql 'alice@foreign.tld'
  52. end
  53. end
  54. describe '#subscribed?' do
  55. it 'returns false when no subscription expiration information is present' do
  56. expect(subject.subscribed?).to be false
  57. end
  58. it 'returns true when subscription expiration has been set' do
  59. subject.subscription_expires_at = 30.days.from_now
  60. expect(subject.subscribed?).to be true
  61. end
  62. end
  63. describe '#keypair' do
  64. it 'returns an RSA key pair' do
  65. expect(subject.keypair).to be_instance_of OpenSSL::PKey::RSA
  66. end
  67. end
  68. describe '#subscription' do
  69. it 'returns an OStatus subscription' do
  70. expect(subject.subscription('')).to be_instance_of OStatus2::Subscription
  71. end
  72. end
  73. describe '#object_type' do
  74. it 'is always a person' do
  75. expect(subject.object_type).to be :person
  76. end
  77. end
  78. describe '#ping!' do
  79. pending
  80. end
  81. describe '#favourited?' do
  82. pending
  83. end
  84. describe '#reblogged?' do
  85. pending
  86. end
  87. describe '.find_local' do
  88. before do
  89. Fabricate(:account, username: 'Alice')
  90. end
  91. it 'returns Alice for alice' do
  92. expect(Account.find_local('alice')).to_not be_nil
  93. end
  94. it 'returns Alice for Alice' do
  95. expect(Account.find_local('Alice')).to_not be_nil
  96. end
  97. it 'does not return anything for a_ice' do
  98. expect(Account.find_local('a_ice')).to be_nil
  99. end
  100. it 'does not return anything for al%' do
  101. expect(Account.find_local('al%')).to be_nil
  102. end
  103. end
  104. describe '.find_remote' do
  105. before do
  106. Fabricate(:account, username: 'Alice', domain: 'mastodon.social')
  107. end
  108. it 'returns Alice for alice@mastodon.social' do
  109. expect(Account.find_remote('alice', 'mastodon.social')).to_not be_nil
  110. end
  111. it 'returns Alice for ALICE@MASTODON.SOCIAL' do
  112. expect(Account.find_remote('ALICE', 'MASTODON.SOCIAL')).to_not be_nil
  113. end
  114. it 'does not return anything for a_ice@mastodon.social' do
  115. expect(Account.find_remote('a_ice', 'mastodon.social')).to be_nil
  116. end
  117. it 'does not return anything for alice@m_stodon.social' do
  118. expect(Account.find_remote('alice', 'm_stodon.social')).to be_nil
  119. end
  120. it 'does not return anything for alice@m%' do
  121. expect(Account.find_remote('alice', 'm%')).to be_nil
  122. end
  123. end
  124. describe '.following_map' do
  125. it 'returns an hash' do
  126. expect(Account.following_map([], 1)).to be_a Hash
  127. end
  128. end
  129. describe '.followed_by_map' do
  130. it 'returns an hash' do
  131. expect(Account.followed_by_map([], 1)).to be_a Hash
  132. end
  133. end
  134. describe '.blocking_map' do
  135. it 'returns an hash' do
  136. expect(Account.blocking_map([], 1)).to be_a Hash
  137. end
  138. end
  139. describe '.requested_map' do
  140. it 'returns an hash' do
  141. expect(Account.requested_map([], 1)).to be_a Hash
  142. end
  143. end
  144. describe 'MENTION_RE' do
  145. subject { Account::MENTION_RE }
  146. it 'matches usernames in the middle of a sentence' do
  147. expect(subject.match('Hello to @alice from me')[1]).to eq 'alice'
  148. end
  149. it 'matches usernames in the beginning of status' do
  150. expect(subject.match('@alice Hey how are you?')[1]).to eq 'alice'
  151. end
  152. it 'matches full usernames' do
  153. expect(subject.match('@alice@example.com')[1]).to eq 'alice@example.com'
  154. end
  155. it 'matches full usernames with a dot at the end' do
  156. expect(subject.match('Hello @alice@example.com.')[1]).to eq 'alice@example.com'
  157. end
  158. it 'matches dot-prepended usernames' do
  159. expect(subject.match('.@alice I want everybody to see this')[1]).to eq 'alice'
  160. end
  161. it 'does not match e-mails' do
  162. expect(subject.match('Drop me an e-mail at alice@example.com')).to be_nil
  163. end
  164. it 'does not match URLs' do
  165. expect(subject.match('Check this out https://medium.com/@alice/some-article#.abcdef123')).to be_nil
  166. end
  167. end
  168. describe 'validations' do
  169. it 'has a valid fabricator' do
  170. account = Fabricate.build(:account)
  171. account.valid?
  172. expect(account).to be_valid
  173. end
  174. it 'is invalid without a username' do
  175. account = Fabricate.build(:account, username: nil)
  176. account.valid?
  177. expect(account).to model_have_error_on_field(:username)
  178. end
  179. it 'is invalid if the username already exists' do
  180. account_1 = Fabricate(:account, username: 'the_doctor')
  181. account_2 = Fabricate.build(:account, username: 'the_doctor')
  182. account_2.valid?
  183. expect(account_2).to model_have_error_on_field(:username)
  184. end
  185. context 'when is local' do
  186. it 'is invalid if the username doesn\'t only contains letters, numbers and underscores' do
  187. account = Fabricate.build(:account, username: 'the-doctor')
  188. account.valid?
  189. expect(account).to model_have_error_on_field(:username)
  190. end
  191. it 'is invalid if the username is longer then 30 characters' do
  192. account = Fabricate.build(:account, username: Faker::Lorem.characters(31))
  193. account.valid?
  194. expect(account).to model_have_error_on_field(:username)
  195. end
  196. end
  197. end
  198. describe 'scopes' do
  199. describe 'remote' do
  200. it 'returns an array of accounts who have a domain' do
  201. account_1 = Fabricate(:account, domain: nil)
  202. account_2 = Fabricate(:account, domain: 'example.com')
  203. expect(Account.remote).to match_array([account_2])
  204. end
  205. end
  206. describe 'local' do
  207. it 'returns an array of accounts who do not have a domain' do
  208. account_1 = Fabricate(:account, domain: nil)
  209. account_2 = Fabricate(:account, domain: 'example.com')
  210. expect(Account.local).to match_array([account_1])
  211. end
  212. end
  213. describe 'silenced' do
  214. it 'returns an array of accounts who are silenced' do
  215. account_1 = Fabricate(:account, silenced: true)
  216. account_2 = Fabricate(:account, silenced: false)
  217. expect(Account.silenced).to match_array([account_1])
  218. end
  219. end
  220. describe 'suspended' do
  221. it 'returns an array of accounts who are suspended' do
  222. account_1 = Fabricate(:account, suspended: true)
  223. account_2 = Fabricate(:account, suspended: false)
  224. expect(Account.suspended).to match_array([account_1])
  225. end
  226. end
  227. end
  228. end