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.

478 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)
  157. mute = Fabricate(:mute, account: account)
  158. block_by = Fabricate(:block, target_account: account)
  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. subject { described_class.triadic_closures(me) }
  216. let!(:me) { Fabricate(:account) }
  217. let!(:friend) { Fabricate(:account) }
  218. let!(:friends_friend) { Fabricate(:account) }
  219. let!(:both_follow) { Fabricate(:account) }
  220. before do
  221. me.follow!(friend)
  222. friend.follow!(friends_friend)
  223. me.follow!(both_follow)
  224. friend.follow!(both_follow)
  225. end
  226. it 'finds accounts you dont follow which are followed by accounts you do follow' do
  227. is_expected.to eq [friends_friend]
  228. end
  229. context 'when you block account' do
  230. before do
  231. me.block!(friends_friend)
  232. end
  233. it 'rejects blocked accounts' do
  234. is_expected.to be_empty
  235. end
  236. end
  237. context 'when you mute account' do
  238. before do
  239. me.mute!(friends_friend)
  240. end
  241. it 'rejects muted accounts' do
  242. is_expected.to be_empty
  243. end
  244. end
  245. end
  246. describe '.following_map' do
  247. it 'returns an hash' do
  248. expect(Account.following_map([], 1)).to be_a Hash
  249. end
  250. end
  251. describe '.followed_by_map' do
  252. it 'returns an hash' do
  253. expect(Account.followed_by_map([], 1)).to be_a Hash
  254. end
  255. end
  256. describe '.blocking_map' do
  257. it 'returns an hash' do
  258. expect(Account.blocking_map([], 1)).to be_a Hash
  259. end
  260. end
  261. describe '.requested_map' do
  262. it 'returns an hash' do
  263. expect(Account.requested_map([], 1)).to be_a Hash
  264. end
  265. end
  266. describe 'MENTION_RE' do
  267. subject { Account::MENTION_RE }
  268. it 'matches usernames in the middle of a sentence' do
  269. expect(subject.match('Hello to @alice from me')[1]).to eq 'alice'
  270. end
  271. it 'matches usernames in the beginning of status' do
  272. expect(subject.match('@alice Hey how are you?')[1]).to eq 'alice'
  273. end
  274. it 'matches full usernames' do
  275. expect(subject.match('@alice@example.com')[1]).to eq 'alice@example.com'
  276. end
  277. it 'matches full usernames with a dot at the end' do
  278. expect(subject.match('Hello @alice@example.com.')[1]).to eq 'alice@example.com'
  279. end
  280. it 'matches dot-prepended usernames' do
  281. expect(subject.match('.@alice I want everybody to see this')[1]).to eq 'alice'
  282. end
  283. it 'does not match e-mails' do
  284. expect(subject.match('Drop me an e-mail at alice@example.com')).to be_nil
  285. end
  286. it 'does not match URLs' do
  287. expect(subject.match('Check this out https://medium.com/@alice/some-article#.abcdef123')).to be_nil
  288. end
  289. xit 'does not match URL querystring' do
  290. expect(subject.match('https://example.com/?x=@alice')).to be_nil
  291. end
  292. end
  293. describe 'validations' do
  294. it 'has a valid fabricator' do
  295. account = Fabricate.build(:account)
  296. account.valid?
  297. expect(account).to be_valid
  298. end
  299. it 'is invalid without a username' do
  300. account = Fabricate.build(:account, username: nil)
  301. account.valid?
  302. expect(account).to model_have_error_on_field(:username)
  303. end
  304. it 'is invalid if the username already exists' do
  305. account_1 = Fabricate(:account, username: 'the_doctor')
  306. account_2 = Fabricate.build(:account, username: 'the_doctor')
  307. account_2.valid?
  308. expect(account_2).to model_have_error_on_field(:username)
  309. end
  310. it 'is invalid if the username is reserved' do
  311. account = Fabricate.build(:account, username: 'support')
  312. account.valid?
  313. expect(account).to model_have_error_on_field(:username)
  314. end
  315. it 'is valid when username is reserved but record has already been created' do
  316. account = Fabricate.build(:account, username: 'support')
  317. account.save(validate: false)
  318. expect(account.valid?).to be true
  319. end
  320. context 'when is local' do
  321. it 'is invalid if the username doesn\'t only contains letters, numbers and underscores' do
  322. account = Fabricate.build(:account, username: 'the-doctor')
  323. account.valid?
  324. expect(account).to model_have_error_on_field(:username)
  325. end
  326. it 'is invalid if the username is longer then 30 characters' do
  327. account = Fabricate.build(:account, username: Faker::Lorem.characters(31))
  328. account.valid?
  329. expect(account).to model_have_error_on_field(:username)
  330. end
  331. end
  332. end
  333. describe 'scopes' do
  334. describe 'remote' do
  335. it 'returns an array of accounts who have a domain' do
  336. account_1 = Fabricate(:account, domain: nil)
  337. account_2 = Fabricate(:account, domain: 'example.com')
  338. expect(Account.remote).to match_array([account_2])
  339. end
  340. end
  341. describe 'by_domain_accounts' do
  342. it 'returns accounts grouped by domain sorted by accounts' do
  343. 2.times { Fabricate(:account, domain: 'example.com') }
  344. Fabricate(:account, domain: 'example2.com')
  345. results = Account.by_domain_accounts
  346. expect(results.length).to eq 2
  347. expect(results.first.domain).to eq 'example.com'
  348. expect(results.first.accounts_count).to eq 2
  349. expect(results.last.domain).to eq 'example2.com'
  350. expect(results.last.accounts_count).to eq 1
  351. end
  352. end
  353. describe 'local' do
  354. it 'returns an array of accounts who do not have a domain' do
  355. account_1 = Fabricate(:account, domain: nil)
  356. account_2 = Fabricate(:account, domain: 'example.com')
  357. expect(Account.local).to match_array([account_1])
  358. end
  359. end
  360. describe 'silenced' do
  361. it 'returns an array of accounts who are silenced' do
  362. account_1 = Fabricate(:account, silenced: true)
  363. account_2 = Fabricate(:account, silenced: false)
  364. expect(Account.silenced).to match_array([account_1])
  365. end
  366. end
  367. describe 'suspended' do
  368. it 'returns an array of accounts who are suspended' do
  369. account_1 = Fabricate(:account, suspended: true)
  370. account_2 = Fabricate(:account, suspended: false)
  371. expect(Account.suspended).to match_array([account_1])
  372. end
  373. end
  374. end
  375. describe 'static avatars' do
  376. describe 'when GIF' do
  377. it 'creates a png static style' do
  378. subject.avatar = attachment_fixture('avatar.gif')
  379. subject.save
  380. expect(subject.avatar_static_url).to_not eq subject.avatar_original_url
  381. end
  382. end
  383. describe 'when non-GIF' do
  384. it 'does not create extra static style' do
  385. subject.avatar = attachment_fixture('attachment.jpg')
  386. subject.save
  387. expect(subject.avatar_static_url).to eq subject.avatar_original_url
  388. end
  389. end
  390. end
  391. end