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.

486 lines
14 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 'Local domain user methods' do
  46. around do |example|
  47. before = Rails.configuration.x.local_domain
  48. example.run
  49. Rails.configuration.x.local_domain = before
  50. end
  51. describe '#to_webfinger_s' do
  52. it 'returns a webfinger string for the account' do
  53. Rails.configuration.x.local_domain = 'example.com'
  54. expect(subject.to_webfinger_s).to eq 'acct:alice@example.com'
  55. end
  56. end
  57. describe '#local_username_and_domain' do
  58. it 'returns the username and local domain for the account' do
  59. Rails.configuration.x.local_domain = 'example.com'
  60. expect(subject.local_username_and_domain).to eq 'alice@example.com'
  61. end
  62. end
  63. end
  64. describe '#acct' do
  65. it 'returns username for local users' do
  66. expect(subject.acct).to eql 'alice'
  67. end
  68. it 'returns username@domain for foreign users' do
  69. subject.domain = 'foreign.tld'
  70. expect(subject.acct).to eql 'alice@foreign.tld'
  71. end
  72. end
  73. describe '#subscribed?' do
  74. it 'returns false when no subscription expiration information is present' do
  75. expect(subject.subscribed?).to be false
  76. end
  77. it 'returns true when subscription expiration has been set' do
  78. subject.subscription_expires_at = 30.days.from_now
  79. expect(subject.subscribed?).to be true
  80. end
  81. end
  82. describe '#keypair' do
  83. it 'returns an RSA key pair' do
  84. expect(subject.keypair).to be_instance_of OpenSSL::PKey::RSA
  85. end
  86. end
  87. describe '#subscription' do
  88. it 'returns an OStatus subscription' do
  89. expect(subject.subscription('')).to be_instance_of OStatus2::Subscription
  90. end
  91. end
  92. describe '#object_type' do
  93. it 'is always a person' do
  94. expect(subject.object_type).to be :person
  95. end
  96. end
  97. describe '#favourited?' do
  98. let(:original_status) do
  99. author = Fabricate(:account, username: 'original')
  100. Fabricate(:status, account: author)
  101. end
  102. context 'when the status is a reblog of another status' do
  103. let(:original_reblog) do
  104. author = Fabricate(:account, username: 'original_reblogger')
  105. Fabricate(:status, reblog: original_status, account: author)
  106. end
  107. it 'is is true when this account has favourited it' do
  108. Fabricate(:favourite, status: original_reblog, account: subject)
  109. expect(subject.favourited?(original_status)).to eq true
  110. end
  111. it 'is false when this account has not favourited it' do
  112. expect(subject.favourited?(original_status)).to eq false
  113. end
  114. end
  115. context 'when the status is an original status' do
  116. it 'is is true when this account has favourited it' do
  117. Fabricate(:favourite, status: original_status, account: subject)
  118. expect(subject.favourited?(original_status)).to eq true
  119. end
  120. it 'is false when this account has not favourited it' do
  121. expect(subject.favourited?(original_status)).to eq false
  122. end
  123. end
  124. end
  125. describe '#reblogged?' do
  126. let(:original_status) do
  127. author = Fabricate(:account, username: 'original')
  128. Fabricate(:status, account: author)
  129. end
  130. context 'when the status is a reblog of another status'do
  131. let(:original_reblog) do
  132. author = Fabricate(:account, username: 'original_reblogger')
  133. Fabricate(:status, reblog: original_status, account: author)
  134. end
  135. it 'is true when this account has reblogged it' do
  136. Fabricate(:status, reblog: original_reblog, account: subject)
  137. expect(subject.reblogged?(original_reblog)).to eq true
  138. end
  139. it 'is false when this account has not reblogged it' do
  140. expect(subject.reblogged?(original_reblog)).to eq false
  141. end
  142. end
  143. context 'when the status is an original status' do
  144. it 'is true when this account has reblogged it' do
  145. Fabricate(:status, reblog: original_status, account: subject)
  146. expect(subject.reblogged?(original_status)).to eq true
  147. end
  148. it 'is false when this account has not reblogged it' do
  149. expect(subject.reblogged?(original_status)).to eq false
  150. end
  151. end
  152. end
  153. describe '#excluded_from_timeline_account_ids' do
  154. it 'includes account ids of blockings, blocked_bys and mutes' do
  155. account = Fabricate(:account)
  156. block = Fabricate(:block, account: account, block: true)
  157. mute = Fabricate(:mute, account: account, block: false)
  158. block_by = Fabricate(:block, target_account: account, block: true)
  159. results = account.excluded_from_timeline_account_ids
  160. expect(results.size).to eq 3
  161. expect(results).to include(block.target_account.id)
  162. expect(results).to include(mute.target_account.id)
  163. expect(results).to include(block_by.account.id)
  164. end
  165. end
  166. describe '.search_for' do
  167. before do
  168. @match = Fabricate(
  169. :account,
  170. display_name: "Display Name",
  171. username: "username",
  172. domain: "example.com"
  173. )
  174. _missing = Fabricate(
  175. :account,
  176. display_name: "Missing",
  177. username: "missing",
  178. domain: "missing.com"
  179. )
  180. end
  181. it 'finds accounts with matching display_name' do
  182. results = Account.search_for("display")
  183. expect(results).to eq [@match]
  184. end
  185. it 'finds accounts with matching username' do
  186. results = Account.search_for("username")
  187. expect(results).to eq [@match]
  188. end
  189. it 'finds accounts with matching domain' do
  190. results = Account.search_for("example")
  191. expect(results).to eq [@match]
  192. end
  193. it 'ranks multiple matches higher' do
  194. account = Fabricate(
  195. :account,
  196. username: "username",
  197. display_name: "username"
  198. )
  199. results = Account.search_for("username")
  200. expect(results).to eq [account, @match]
  201. end
  202. end
  203. describe '.advanced_search_for' do
  204. it 'ranks followed accounts higher' do
  205. account = Fabricate(:account)
  206. match = Fabricate(:account, username: "Matching")
  207. followed_match = Fabricate(:account, username: "Matcher")
  208. Fabricate(:follow, account: account, target_account: followed_match)
  209. results = Account.advanced_search_for("match", account)
  210. expect(results).to eq [followed_match, match]
  211. expect(results.first.rank).to be > results.last.rank
  212. end
  213. end
  214. describe '.triadic_closures' do
  215. it 'finds accounts you dont follow which are followed by accounts you do follow' do
  216. me = Fabricate(:account)
  217. friend = Fabricate(:account)
  218. friends_friend = Fabricate(:account)
  219. me.follow!(friend)
  220. friend.follow!(friends_friend)
  221. both_follow = Fabricate(:account)
  222. me.follow!(both_follow)
  223. friend.follow!(both_follow)
  224. results = Account.triadic_closures(me)
  225. expect(results).to eq [friends_friend]
  226. end
  227. end
  228. describe '.find_local' do
  229. before do
  230. Fabricate(:account, username: 'Alice')
  231. end
  232. it 'returns Alice for alice' do
  233. expect(Account.find_local('alice')).to_not be_nil
  234. end
  235. it 'returns Alice for Alice' do
  236. expect(Account.find_local('Alice')).to_not be_nil
  237. end
  238. it 'does not return anything for a_ice' do
  239. expect(Account.find_local('a_ice')).to be_nil
  240. end
  241. it 'does not return anything for al%' do
  242. expect(Account.find_local('al%')).to be_nil
  243. end
  244. end
  245. describe '.find_remote' do
  246. before do
  247. Fabricate(:account, username: 'Alice', domain: 'mastodon.social')
  248. end
  249. it 'returns Alice for alice@mastodon.social' do
  250. expect(Account.find_remote('alice', 'mastodon.social')).to_not be_nil
  251. end
  252. it 'returns Alice for ALICE@MASTODON.SOCIAL' do
  253. expect(Account.find_remote('ALICE', 'MASTODON.SOCIAL')).to_not be_nil
  254. end
  255. it 'does not return anything for a_ice@mastodon.social' do
  256. expect(Account.find_remote('a_ice', 'mastodon.social')).to be_nil
  257. end
  258. it 'does not return anything for alice@m_stodon.social' do
  259. expect(Account.find_remote('alice', 'm_stodon.social')).to be_nil
  260. end
  261. it 'does not return anything for alice@m%' do
  262. expect(Account.find_remote('alice', 'm%')).to be_nil
  263. end
  264. end
  265. describe '.following_map' do
  266. it 'returns an hash' do
  267. expect(Account.following_map([], 1)).to be_a Hash
  268. end
  269. end
  270. describe '.followed_by_map' do
  271. it 'returns an hash' do
  272. expect(Account.followed_by_map([], 1)).to be_a Hash
  273. end
  274. end
  275. describe '.blocking_map' do
  276. it 'returns an hash' do
  277. expect(Account.blocking_map([], 1)).to be_a Hash
  278. end
  279. end
  280. describe '.requested_map' do
  281. it 'returns an hash' do
  282. expect(Account.requested_map([], 1)).to be_a Hash
  283. end
  284. end
  285. describe 'MENTION_RE' do
  286. subject { Account::MENTION_RE }
  287. it 'matches usernames in the middle of a sentence' do
  288. expect(subject.match('Hello to @alice from me')[1]).to eq 'alice'
  289. end
  290. it 'matches usernames in the beginning of status' do
  291. expect(subject.match('@alice Hey how are you?')[1]).to eq 'alice'
  292. end
  293. it 'matches full usernames' do
  294. expect(subject.match('@alice@example.com')[1]).to eq 'alice@example.com'
  295. end
  296. it 'matches full usernames with a dot at the end' do
  297. expect(subject.match('Hello @alice@example.com.')[1]).to eq 'alice@example.com'
  298. end
  299. it 'matches dot-prepended usernames' do
  300. expect(subject.match('.@alice I want everybody to see this')[1]).to eq 'alice'
  301. end
  302. it 'does not match e-mails' do
  303. expect(subject.match('Drop me an e-mail at alice@example.com')).to be_nil
  304. end
  305. it 'does not match URLs' do
  306. expect(subject.match('Check this out https://medium.com/@alice/some-article#.abcdef123')).to be_nil
  307. end
  308. end
  309. describe 'validations' do
  310. it 'has a valid fabricator' do
  311. account = Fabricate.build(:account)
  312. account.valid?
  313. expect(account).to be_valid
  314. end
  315. it 'is invalid without a username' do
  316. account = Fabricate.build(:account, username: nil)
  317. account.valid?
  318. expect(account).to model_have_error_on_field(:username)
  319. end
  320. it 'is invalid if the username already exists' do
  321. account_1 = Fabricate(:account, username: 'the_doctor')
  322. account_2 = Fabricate.build(:account, username: 'the_doctor')
  323. account_2.valid?
  324. expect(account_2).to model_have_error_on_field(:username)
  325. end
  326. context 'when is local' do
  327. it 'is invalid if the username doesn\'t only contains letters, numbers and underscores' do
  328. account = Fabricate.build(:account, username: 'the-doctor')
  329. account.valid?
  330. expect(account).to model_have_error_on_field(:username)
  331. end
  332. it 'is invalid if the username is longer then 30 characters' do
  333. account = Fabricate.build(:account, username: Faker::Lorem.characters(31))
  334. account.valid?
  335. expect(account).to model_have_error_on_field(:username)
  336. end
  337. end
  338. end
  339. describe 'scopes' do
  340. describe 'remote' do
  341. it 'returns an array of accounts who have a domain' do
  342. account_1 = Fabricate(:account, domain: nil)
  343. account_2 = Fabricate(:account, domain: 'example.com')
  344. expect(Account.remote).to match_array([account_2])
  345. end
  346. end
  347. describe 'by_domain_accounts' do
  348. it 'returns accounts grouped by domain sorted by accounts' do
  349. 2.times { Fabricate(:account, domain: 'example.com') }
  350. Fabricate(:account, domain: 'example2.com')
  351. results = Account.by_domain_accounts
  352. expect(results.length).to eq 2
  353. expect(results.first.domain).to eq 'example.com'
  354. expect(results.first.accounts_count).to eq 2
  355. expect(results.last.domain).to eq 'example2.com'
  356. expect(results.last.accounts_count).to eq 1
  357. end
  358. end
  359. describe 'local' do
  360. it 'returns an array of accounts who do not have a domain' do
  361. account_1 = Fabricate(:account, domain: nil)
  362. account_2 = Fabricate(:account, domain: 'example.com')
  363. expect(Account.local).to match_array([account_1])
  364. end
  365. end
  366. describe 'silenced' do
  367. it 'returns an array of accounts who are silenced' do
  368. account_1 = Fabricate(:account, silenced: true)
  369. account_2 = Fabricate(:account, silenced: false)
  370. expect(Account.silenced).to match_array([account_1])
  371. end
  372. end
  373. describe 'suspended' do
  374. it 'returns an array of accounts who are suspended' do
  375. account_1 = Fabricate(:account, suspended: true)
  376. account_2 = Fabricate(:account, suspended: false)
  377. expect(Account.suspended).to match_array([account_1])
  378. end
  379. end
  380. end
  381. describe 'static avatars' do
  382. describe 'when GIF' do
  383. it 'creates a png static style' do
  384. subject.avatar = attachment_fixture('avatar.gif')
  385. subject.save
  386. expect(subject.avatar_static_url).to_not eq subject.avatar_original_url
  387. end
  388. end
  389. describe 'when non-GIF' do
  390. it 'does not create extra static style' do
  391. subject.avatar = attachment_fixture('attachment.jpg')
  392. subject.save
  393. expect(subject.avatar_static_url).to eq subject.avatar_original_url
  394. end
  395. end
  396. end
  397. end