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

440 lines
13 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 '.find_local' do
  202. before do
  203. Fabricate(:account, username: 'Alice')
  204. end
  205. it 'returns Alice for alice' do
  206. expect(Account.find_local('alice')).to_not be_nil
  207. end
  208. it 'returns Alice for Alice' do
  209. expect(Account.find_local('Alice')).to_not be_nil
  210. end
  211. it 'does not return anything for a_ice' do
  212. expect(Account.find_local('a_ice')).to be_nil
  213. end
  214. it 'does not return anything for al%' do
  215. expect(Account.find_local('al%')).to be_nil
  216. end
  217. end
  218. describe '.find_remote' do
  219. before do
  220. Fabricate(:account, username: 'Alice', domain: 'mastodon.social')
  221. end
  222. it 'returns Alice for alice@mastodon.social' do
  223. expect(Account.find_remote('alice', 'mastodon.social')).to_not be_nil
  224. end
  225. it 'returns Alice for ALICE@MASTODON.SOCIAL' do
  226. expect(Account.find_remote('ALICE', 'MASTODON.SOCIAL')).to_not be_nil
  227. end
  228. it 'does not return anything for a_ice@mastodon.social' do
  229. expect(Account.find_remote('a_ice', 'mastodon.social')).to be_nil
  230. end
  231. it 'does not return anything for alice@m_stodon.social' do
  232. expect(Account.find_remote('alice', 'm_stodon.social')).to be_nil
  233. end
  234. it 'does not return anything for alice@m%' do
  235. expect(Account.find_remote('alice', 'm%')).to be_nil
  236. end
  237. end
  238. describe '.following_map' do
  239. it 'returns an hash' do
  240. expect(Account.following_map([], 1)).to be_a Hash
  241. end
  242. end
  243. describe '.followed_by_map' do
  244. it 'returns an hash' do
  245. expect(Account.followed_by_map([], 1)).to be_a Hash
  246. end
  247. end
  248. describe '.blocking_map' do
  249. it 'returns an hash' do
  250. expect(Account.blocking_map([], 1)).to be_a Hash
  251. end
  252. end
  253. describe '.requested_map' do
  254. it 'returns an hash' do
  255. expect(Account.requested_map([], 1)).to be_a Hash
  256. end
  257. end
  258. describe 'MENTION_RE' do
  259. subject { Account::MENTION_RE }
  260. it 'matches usernames in the middle of a sentence' do
  261. expect(subject.match('Hello to @alice from me')[1]).to eq 'alice'
  262. end
  263. it 'matches usernames in the beginning of status' do
  264. expect(subject.match('@alice Hey how are you?')[1]).to eq 'alice'
  265. end
  266. it 'matches full usernames' do
  267. expect(subject.match('@alice@example.com')[1]).to eq 'alice@example.com'
  268. end
  269. it 'matches full usernames with a dot at the end' do
  270. expect(subject.match('Hello @alice@example.com.')[1]).to eq 'alice@example.com'
  271. end
  272. it 'matches dot-prepended usernames' do
  273. expect(subject.match('.@alice I want everybody to see this')[1]).to eq 'alice'
  274. end
  275. it 'does not match e-mails' do
  276. expect(subject.match('Drop me an e-mail at alice@example.com')).to be_nil
  277. end
  278. it 'does not match URLs' do
  279. expect(subject.match('Check this out https://medium.com/@alice/some-article#.abcdef123')).to be_nil
  280. end
  281. end
  282. describe 'validations' do
  283. it 'has a valid fabricator' do
  284. account = Fabricate.build(:account)
  285. account.valid?
  286. expect(account).to be_valid
  287. end
  288. it 'is invalid without a username' do
  289. account = Fabricate.build(:account, username: nil)
  290. account.valid?
  291. expect(account).to model_have_error_on_field(:username)
  292. end
  293. it 'is invalid if the username already exists' do
  294. account_1 = Fabricate(:account, username: 'the_doctor')
  295. account_2 = Fabricate.build(:account, username: 'the_doctor')
  296. account_2.valid?
  297. expect(account_2).to model_have_error_on_field(:username)
  298. end
  299. context 'when is local' do
  300. it 'is invalid if the username doesn\'t only contains letters, numbers and underscores' do
  301. account = Fabricate.build(:account, username: 'the-doctor')
  302. account.valid?
  303. expect(account).to model_have_error_on_field(:username)
  304. end
  305. it 'is invalid if the username is longer then 30 characters' do
  306. account = Fabricate.build(:account, username: Faker::Lorem.characters(31))
  307. account.valid?
  308. expect(account).to model_have_error_on_field(:username)
  309. end
  310. end
  311. end
  312. describe 'scopes' do
  313. describe 'remote' do
  314. it 'returns an array of accounts who have a domain' do
  315. account_1 = Fabricate(:account, domain: nil)
  316. account_2 = Fabricate(:account, domain: 'example.com')
  317. expect(Account.remote).to match_array([account_2])
  318. end
  319. end
  320. describe 'local' do
  321. it 'returns an array of accounts who do not have a domain' do
  322. account_1 = Fabricate(:account, domain: nil)
  323. account_2 = Fabricate(:account, domain: 'example.com')
  324. expect(Account.local).to match_array([account_1])
  325. end
  326. end
  327. describe 'silenced' do
  328. it 'returns an array of accounts who are silenced' do
  329. account_1 = Fabricate(:account, silenced: true)
  330. account_2 = Fabricate(:account, silenced: false)
  331. expect(Account.silenced).to match_array([account_1])
  332. end
  333. end
  334. describe 'suspended' do
  335. it 'returns an array of accounts who are suspended' do
  336. account_1 = Fabricate(:account, suspended: true)
  337. account_2 = Fabricate(:account, suspended: false)
  338. expect(Account.suspended).to match_array([account_1])
  339. end
  340. end
  341. end
  342. describe 'static avatars' do
  343. describe 'when GIF' do
  344. it 'creates a png static style' do
  345. subject.avatar = attachment_fixture('avatar.gif')
  346. subject.save
  347. expect(subject.avatar_static_url).to_not eq subject.avatar_original_url
  348. end
  349. end
  350. describe 'when non-GIF' do
  351. it 'does not create extra static style' do
  352. subject.avatar = attachment_fixture('attachment.jpg')
  353. subject.save
  354. expect(subject.avatar_static_url).to eq subject.avatar_original_url
  355. end
  356. end
  357. end
  358. end