闭社主体 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.

471 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 '.search_for' do
  154. before do
  155. @match = Fabricate(
  156. :account,
  157. display_name: "Display Name",
  158. username: "username",
  159. domain: "example.com"
  160. )
  161. _missing = Fabricate(
  162. :account,
  163. display_name: "Missing",
  164. username: "missing",
  165. domain: "missing.com"
  166. )
  167. end
  168. it 'finds accounts with matching display_name' do
  169. results = Account.search_for("display")
  170. expect(results).to eq [@match]
  171. end
  172. it 'finds accounts with matching username' do
  173. results = Account.search_for("username")
  174. expect(results).to eq [@match]
  175. end
  176. it 'finds accounts with matching domain' do
  177. results = Account.search_for("example")
  178. expect(results).to eq [@match]
  179. end
  180. it 'ranks multiple matches higher' do
  181. account = Fabricate(
  182. :account,
  183. username: "username",
  184. display_name: "username"
  185. )
  186. results = Account.search_for("username")
  187. expect(results).to eq [account, @match]
  188. end
  189. end
  190. describe '.advanced_search_for' do
  191. it 'ranks followed accounts higher' do
  192. account = Fabricate(:account)
  193. match = Fabricate(:account, username: "Matching")
  194. followed_match = Fabricate(:account, username: "Matcher")
  195. Fabricate(:follow, account: account, target_account: followed_match)
  196. results = Account.advanced_search_for("match", account)
  197. expect(results).to eq [followed_match, match]
  198. expect(results.first.rank).to be > results.last.rank
  199. end
  200. end
  201. describe '.triadic_closures' do
  202. it 'finds accounts you dont follow which are followed by accounts you do follow' do
  203. me = Fabricate(:account)
  204. friend = Fabricate(:account)
  205. friends_friend = Fabricate(:account)
  206. me.follow!(friend)
  207. friend.follow!(friends_friend)
  208. both_follow = Fabricate(:account)
  209. me.follow!(both_follow)
  210. friend.follow!(both_follow)
  211. results = Account.triadic_closures(me)
  212. expect(results).to eq [friends_friend]
  213. end
  214. end
  215. describe '.find_local' do
  216. before do
  217. Fabricate(:account, username: 'Alice')
  218. end
  219. it 'returns Alice for alice' do
  220. expect(Account.find_local('alice')).to_not be_nil
  221. end
  222. it 'returns Alice for Alice' do
  223. expect(Account.find_local('Alice')).to_not be_nil
  224. end
  225. it 'does not return anything for a_ice' do
  226. expect(Account.find_local('a_ice')).to be_nil
  227. end
  228. it 'does not return anything for al%' do
  229. expect(Account.find_local('al%')).to be_nil
  230. end
  231. end
  232. describe '.find_remote' do
  233. before do
  234. Fabricate(:account, username: 'Alice', domain: 'mastodon.social')
  235. end
  236. it 'returns Alice for alice@mastodon.social' do
  237. expect(Account.find_remote('alice', 'mastodon.social')).to_not be_nil
  238. end
  239. it 'returns Alice for ALICE@MASTODON.SOCIAL' do
  240. expect(Account.find_remote('ALICE', 'MASTODON.SOCIAL')).to_not be_nil
  241. end
  242. it 'does not return anything for a_ice@mastodon.social' do
  243. expect(Account.find_remote('a_ice', 'mastodon.social')).to be_nil
  244. end
  245. it 'does not return anything for alice@m_stodon.social' do
  246. expect(Account.find_remote('alice', 'm_stodon.social')).to be_nil
  247. end
  248. it 'does not return anything for alice@m%' do
  249. expect(Account.find_remote('alice', 'm%')).to be_nil
  250. end
  251. end
  252. describe '.following_map' do
  253. it 'returns an hash' do
  254. expect(Account.following_map([], 1)).to be_a Hash
  255. end
  256. end
  257. describe '.followed_by_map' do
  258. it 'returns an hash' do
  259. expect(Account.followed_by_map([], 1)).to be_a Hash
  260. end
  261. end
  262. describe '.blocking_map' do
  263. it 'returns an hash' do
  264. expect(Account.blocking_map([], 1)).to be_a Hash
  265. end
  266. end
  267. describe '.requested_map' do
  268. it 'returns an hash' do
  269. expect(Account.requested_map([], 1)).to be_a Hash
  270. end
  271. end
  272. describe 'MENTION_RE' do
  273. subject { Account::MENTION_RE }
  274. it 'matches usernames in the middle of a sentence' do
  275. expect(subject.match('Hello to @alice from me')[1]).to eq 'alice'
  276. end
  277. it 'matches usernames in the beginning of status' do
  278. expect(subject.match('@alice Hey how are you?')[1]).to eq 'alice'
  279. end
  280. it 'matches full usernames' do
  281. expect(subject.match('@alice@example.com')[1]).to eq 'alice@example.com'
  282. end
  283. it 'matches full usernames with a dot at the end' do
  284. expect(subject.match('Hello @alice@example.com.')[1]).to eq 'alice@example.com'
  285. end
  286. it 'matches dot-prepended usernames' do
  287. expect(subject.match('.@alice I want everybody to see this')[1]).to eq 'alice'
  288. end
  289. it 'does not match e-mails' do
  290. expect(subject.match('Drop me an e-mail at alice@example.com')).to be_nil
  291. end
  292. it 'does not match URLs' do
  293. expect(subject.match('Check this out https://medium.com/@alice/some-article#.abcdef123')).to be_nil
  294. end
  295. end
  296. describe 'validations' do
  297. it 'has a valid fabricator' do
  298. account = Fabricate.build(:account)
  299. account.valid?
  300. expect(account).to be_valid
  301. end
  302. it 'is invalid without a username' do
  303. account = Fabricate.build(:account, username: nil)
  304. account.valid?
  305. expect(account).to model_have_error_on_field(:username)
  306. end
  307. it 'is invalid if the username already exists' do
  308. account_1 = Fabricate(:account, username: 'the_doctor')
  309. account_2 = Fabricate.build(:account, username: 'the_doctor')
  310. account_2.valid?
  311. expect(account_2).to model_have_error_on_field(:username)
  312. end
  313. context 'when is local' do
  314. it 'is invalid if the username doesn\'t only contains letters, numbers and underscores' do
  315. account = Fabricate.build(:account, username: 'the-doctor')
  316. account.valid?
  317. expect(account).to model_have_error_on_field(:username)
  318. end
  319. it 'is invalid if the username is longer then 30 characters' do
  320. account = Fabricate.build(:account, username: Faker::Lorem.characters(31))
  321. account.valid?
  322. expect(account).to model_have_error_on_field(:username)
  323. end
  324. end
  325. end
  326. describe 'scopes' do
  327. describe 'remote' do
  328. it 'returns an array of accounts who have a domain' do
  329. account_1 = Fabricate(:account, domain: nil)
  330. account_2 = Fabricate(:account, domain: 'example.com')
  331. expect(Account.remote).to match_array([account_2])
  332. end
  333. end
  334. describe 'by_domain_accounts' do
  335. it 'returns accounts grouped by domain sorted by accounts' do
  336. 2.times { Fabricate(:account, domain: 'example.com') }
  337. Fabricate(:account, domain: 'example2.com')
  338. results = Account.by_domain_accounts
  339. expect(results.length).to eq 2
  340. expect(results.first.domain).to eq 'example.com'
  341. expect(results.first.accounts_count).to eq 2
  342. expect(results.last.domain).to eq 'example2.com'
  343. expect(results.last.accounts_count).to eq 1
  344. end
  345. end
  346. describe 'local' do
  347. it 'returns an array of accounts who do not have a domain' do
  348. account_1 = Fabricate(:account, domain: nil)
  349. account_2 = Fabricate(:account, domain: 'example.com')
  350. expect(Account.local).to match_array([account_1])
  351. end
  352. end
  353. describe 'silenced' do
  354. it 'returns an array of accounts who are silenced' do
  355. account_1 = Fabricate(:account, silenced: true)
  356. account_2 = Fabricate(:account, silenced: false)
  357. expect(Account.silenced).to match_array([account_1])
  358. end
  359. end
  360. describe 'suspended' do
  361. it 'returns an array of accounts who are suspended' do
  362. account_1 = Fabricate(:account, suspended: true)
  363. account_2 = Fabricate(:account, suspended: false)
  364. expect(Account.suspended).to match_array([account_1])
  365. end
  366. end
  367. end
  368. describe 'static avatars' do
  369. describe 'when GIF' do
  370. it 'creates a png static style' do
  371. subject.avatar = attachment_fixture('avatar.gif')
  372. subject.save
  373. expect(subject.avatar_static_url).to_not eq subject.avatar_original_url
  374. end
  375. end
  376. describe 'when non-GIF' do
  377. it 'does not create extra static style' do
  378. subject.avatar = attachment_fixture('attachment.jpg')
  379. subject.save
  380. expect(subject.avatar_static_url).to eq subject.avatar_original_url
  381. end
  382. end
  383. end
  384. end