tests.rake 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. # frozen_string_literal: true
  2. namespace :tests do
  3. namespace :migrations do
  4. desc 'Check that database state is consistent with a successful migration from populated data'
  5. task check_database: :environment do
  6. unless Account.find_by(username: 'admin', domain: nil)&.hide_collections? == false
  7. puts 'Unexpected value for Account#hide_collections? for user @admin'
  8. exit(1)
  9. end
  10. unless Account.find_by(username: 'user', domain: nil)&.hide_collections? == true
  11. puts 'Unexpected value for Account#hide_collections? for user @user'
  12. exit(1)
  13. end
  14. unless Account.find_by(username: 'evil', domain: 'activitypub.com')&.suspended?
  15. puts 'Unexpected value for Account#suspended? for user @evil@activitypub.com'
  16. exit(1)
  17. end
  18. unless Status.find(6).account_id == Status.find(7).account_id
  19. puts 'Users @remote@remote.com and @Remote@remote.com not properly merged'
  20. exit(1)
  21. end
  22. if Account.where(domain: Rails.configuration.x.local_domain).exists?
  23. puts 'Faux remote accounts not properly cleaned up'
  24. exit(1)
  25. end
  26. unless AccountConversation.first&.last_status_id == 11
  27. puts 'AccountConversation records not created as expected'
  28. exit(1)
  29. end
  30. if Account.find(-99).private_key.blank?
  31. puts 'Instance actor does not have a private key'
  32. exit(1)
  33. end
  34. unless Account.find_by(username: 'user', domain: nil).custom_filters.map { |filter| filter.keywords.pluck(:keyword) } == [['test'], ['take']]
  35. puts 'CustomFilterKeyword records not created as expected'
  36. exit(1)
  37. end
  38. unless Admin::ActionLog.find_by(target_type: 'DomainBlock', target_id: 1).human_identifier == 'example.org'
  39. puts 'Admin::ActionLog domain block records not updated as expected'
  40. exit(1)
  41. end
  42. unless Admin::ActionLog.find_by(target_type: 'EmailDomainBlock', target_id: 1).human_identifier == 'example.org'
  43. puts 'Admin::ActionLog email domain block records not updated as expected'
  44. exit(1)
  45. end
  46. unless User.find(1).settings['notification_emails.favourite'] == true && User.find(1).settings['notification_emails.mention'] == false
  47. puts 'User settings not kept as expected'
  48. exit(1)
  49. end
  50. unless User.find(1).settings['web.trends'] == false
  51. puts 'User settings not kept as expected'
  52. exit(1)
  53. end
  54. unless Account.find_remote('bob', 'ActivityPub.com').domain == 'activitypub.com'
  55. puts 'Account domains not properly normalized'
  56. exit(1)
  57. end
  58. unless Status.find(12).preview_cards.pluck(:url) == ['https://joinmastodon.org/']
  59. puts 'Preview cards not deduplicated as expected'
  60. exit(1)
  61. end
  62. unless Account.find_local('kmruser').user.chosen_languages == %w(en ku ckb)
  63. puts 'Chosen languages not migrated as expected for kmr users'
  64. exit(1)
  65. end
  66. unless Account.find_local('kmruser').user.settings['default_language'] == 'ku'
  67. puts 'Default posting language not migrated as expected for kmr users'
  68. exit(1)
  69. end
  70. end
  71. desc 'Populate the database with test data for 2.4.3'
  72. task populate_v2_4_3: :environment do # rubocop:disable Naming/VariableNumber
  73. user_key = OpenSSL::PKey::RSA.new(2048)
  74. user_private_key = ActiveRecord::Base.connection.quote(user_key.to_pem)
  75. user_public_key = ActiveRecord::Base.connection.quote(user_key.public_key.to_pem)
  76. ActiveRecord::Base.connection.execute(<<~SQL)
  77. INSERT INTO "custom_filters"
  78. (id, account_id, phrase, context, whole_word, irreversible, created_at, updated_at)
  79. VALUES
  80. (1, 2, 'test', '{ "home", "public" }', true, true, now(), now()),
  81. (2, 2, 'take', '{ "home" }', false, false, now(), now());
  82. -- Orphaned admin action logs
  83. INSERT INTO "admin_action_logs"
  84. (account_id, action, target_type, target_id, created_at, updated_at)
  85. VALUES
  86. (1, 'destroy', 'Account', 1312, now(), now()),
  87. (1, 'destroy', 'User', 1312, now(), now()),
  88. (1, 'destroy', 'Report', 1312, now(), now()),
  89. (1, 'destroy', 'DomainBlock', 1312, now(), now()),
  90. (1, 'destroy', 'EmailDomainBlock', 1312, now(), now()),
  91. (1, 'destroy', 'Status', 1312, now(), now()),
  92. (1, 'destroy', 'CustomEmoji', 1312, now(), now());
  93. -- Admin action logs with linked objects
  94. INSERT INTO "domain_blocks"
  95. (id, domain, created_at, updated_at)
  96. VALUES
  97. (1, 'example.org', now(), now());
  98. INSERT INTO "email_domain_blocks"
  99. (id, domain, created_at, updated_at)
  100. VALUES
  101. (1, 'example.org', now(), now());
  102. INSERT INTO "admin_action_logs"
  103. (account_id, action, target_type, target_id, created_at, updated_at)
  104. VALUES
  105. (1, 'destroy', 'Account', 1, now(), now()),
  106. (1, 'destroy', 'User', 1, now(), now()),
  107. (1, 'destroy', 'DomainBlock', 1, now(), now()),
  108. (1, 'destroy', 'EmailDomainBlock', 1, now(), now()),
  109. (1, 'destroy', 'Status', 1, now(), now()),
  110. (1, 'destroy', 'CustomEmoji', 3, now(), now());
  111. INSERT INTO "settings"
  112. (id, thing_type, thing_id, var, value, created_at, updated_at)
  113. VALUES
  114. (3, 'User', 1, 'notification_emails', E'--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess\nfollow: false\nreblog: true\nfavourite: true\nmention: false\nfollow_request: true\ndigest: true\nreport: true\npending_account: false\ntrending_tag: true\nappeal: true\n', now(), now()),
  115. (4, 'User', 1, 'trends', E'--- false\n', now(), now());
  116. INSERT INTO "accounts"
  117. (id, username, domain, private_key, public_key, created_at, updated_at)
  118. VALUES
  119. (10, 'kmruser', NULL, #{user_private_key}, #{user_public_key}, now(), now());
  120. INSERT INTO "users"
  121. (id, account_id, email, created_at, updated_at, admin, locale, chosen_languages)
  122. VALUES
  123. (4, 10, 'kmruser@localhost', now(), now(), false, 'ku', '{en,kmr,ku,ckb}');
  124. INSERT INTO "settings"
  125. (id, thing_type, thing_id, var, value, created_at, updated_at)
  126. VALUES
  127. (5, 'User', 4, 'default_language', E'--- kmr\n', now(), now());
  128. SQL
  129. end
  130. desc 'Populate the database with test data for 2.4.0'
  131. task populate_v2_4: :environment do # rubocop:disable Naming/VariableNumber
  132. ActiveRecord::Base.connection.execute(<<~SQL.squish)
  133. INSERT INTO "settings"
  134. (id, thing_type, thing_id, var, value, created_at, updated_at)
  135. VALUES
  136. (1, 'User', 1, 'hide_network', E'--- false\n', now(), now()),
  137. (2, 'User', 2, 'hide_network', E'--- true\n', now(), now());
  138. SQL
  139. end
  140. desc 'Populate the database with test data for 2.0.0'
  141. task populate_v2: :environment do
  142. admin_key = OpenSSL::PKey::RSA.new(2048)
  143. user_key = OpenSSL::PKey::RSA.new(2048)
  144. remote_key = OpenSSL::PKey::RSA.new(2048)
  145. remote_key2 = OpenSSL::PKey::RSA.new(2048)
  146. remote_key3 = OpenSSL::PKey::RSA.new(2048)
  147. admin_private_key = ActiveRecord::Base.connection.quote(admin_key.to_pem)
  148. admin_public_key = ActiveRecord::Base.connection.quote(admin_key.public_key.to_pem)
  149. user_private_key = ActiveRecord::Base.connection.quote(user_key.to_pem)
  150. user_public_key = ActiveRecord::Base.connection.quote(user_key.public_key.to_pem)
  151. remote_public_key = ActiveRecord::Base.connection.quote(remote_key.public_key.to_pem)
  152. remote_public_key2 = ActiveRecord::Base.connection.quote(remote_key2.public_key.to_pem)
  153. remote_public_key_ap = ActiveRecord::Base.connection.quote(remote_key3.public_key.to_pem)
  154. local_domain = ActiveRecord::Base.connection.quote(Rails.configuration.x.local_domain)
  155. ActiveRecord::Base.connection.execute(<<~SQL)
  156. -- accounts
  157. INSERT INTO "accounts"
  158. (id, username, domain, private_key, public_key, created_at, updated_at)
  159. VALUES
  160. (1, 'admin', NULL, #{admin_private_key}, #{admin_public_key}, now(), now()),
  161. (2, 'user', NULL, #{user_private_key}, #{user_public_key}, now(), now());
  162. INSERT INTO "accounts"
  163. (id, username, domain, private_key, public_key, created_at, updated_at, remote_url, salmon_url)
  164. VALUES
  165. (3, 'remote', 'remote.com', NULL, #{remote_public_key}, now(), now(),
  166. 'https://remote.com/@remote', 'https://remote.com/salmon/1'),
  167. (4, 'Remote', 'remote.com', NULL, #{remote_public_key}, now(), now(),
  168. 'https://remote.com/@Remote', 'https://remote.com/salmon/1'),
  169. (5, 'REMOTE', 'Remote.com', NULL, #{remote_public_key2}, now() - interval '1 year', now() - interval '1 year',
  170. 'https://remote.com/stale/@REMOTE', 'https://remote.com/stale/salmon/1');
  171. INSERT INTO "accounts"
  172. (id, username, domain, private_key, public_key, created_at, updated_at, protocol, inbox_url, outbox_url, followers_url)
  173. VALUES
  174. (6, 'bob', 'ActivityPub.com', NULL, #{remote_public_key_ap}, now(), now(),
  175. 1, 'https://activitypub.com/users/bob/inbox', 'https://activitypub.com/users/bob/outbox', 'https://activitypub.com/users/bob/followers');
  176. INSERT INTO "accounts"
  177. (id, username, domain, private_key, public_key, created_at, updated_at)
  178. VALUES
  179. (7, 'user', #{local_domain}, #{user_private_key}, #{user_public_key}, now(), now()),
  180. (8, 'pt_user', NULL, #{user_private_key}, #{user_public_key}, now(), now());
  181. INSERT INTO "accounts"
  182. (id, username, domain, private_key, public_key, created_at, updated_at, protocol, inbox_url, outbox_url, followers_url, suspended)
  183. VALUES
  184. (9, 'evil', 'activitypub.com', NULL, #{remote_public_key_ap}, now(), now(),
  185. 1, 'https://activitypub.com/users/evil/inbox', 'https://activitypub.com/users/evil/outbox',
  186. 'https://activitypub.com/users/evil/followers', true);
  187. -- users
  188. INSERT INTO "users"
  189. (id, account_id, email, created_at, updated_at, admin)
  190. VALUES
  191. (1, 1, 'admin@localhost', now(), now(), true),
  192. (2, 2, 'user@localhost', now(), now(), false);
  193. INSERT INTO "users"
  194. (id, account_id, email, created_at, updated_at, admin, locale)
  195. VALUES
  196. (3, 8, 'ptuser@localhost', now(), now(), false, 'pt');
  197. -- conversations
  198. INSERT INTO "conversations" (id, created_at, updated_at) VALUES (1, now(), now());
  199. -- statuses
  200. INSERT INTO "statuses"
  201. (id, account_id, text, created_at, updated_at)
  202. VALUES
  203. (1, 1, 'test', now(), now()),
  204. (2, 1, '@remote@remote.com hello', now(), now()),
  205. (3, 1, '@Remote@remote.com hello', now(), now()),
  206. (4, 1, '@REMOTE@remote.com hello', now(), now());
  207. INSERT INTO "statuses"
  208. (id, account_id, text, created_at, updated_at, uri, local)
  209. VALUES
  210. (5, 1, 'activitypub status', now(), now(), 'https://localhost/users/admin/statuses/4', true);
  211. INSERT INTO "statuses"
  212. (id, account_id, text, created_at, updated_at)
  213. VALUES
  214. (6, 3, 'test', now(), now());
  215. INSERT INTO "statuses"
  216. (id, account_id, text, created_at, updated_at, in_reply_to_id, in_reply_to_account_id)
  217. VALUES
  218. (7, 4, '@admin hello', now(), now(), 3, 1);
  219. INSERT INTO "statuses"
  220. (id, account_id, text, created_at, updated_at)
  221. VALUES
  222. (8, 5, 'test', now(), now());
  223. INSERT INTO "statuses"
  224. (id, account_id, reblog_of_id, created_at, updated_at)
  225. VALUES
  226. (9, 1, 2, now(), now());
  227. INSERT INTO "statuses"
  228. (id, account_id, text, in_reply_to_id, conversation_id, visibility, created_at, updated_at)
  229. VALUES
  230. (10, 2, '@admin hey!', NULL, 1, 3, now(), now()),
  231. (11, 1, '@user hey!', 10, 1, 3, now(), now());
  232. INSERT INTO "statuses"
  233. (id, account_id, text, created_at, updated_at)
  234. VALUES
  235. (12, 1, 'check out https://joinmastodon.org/', now(), now());
  236. -- mentions (from previous statuses)
  237. INSERT INTO "mentions"
  238. (id, status_id, account_id, created_at, updated_at)
  239. VALUES
  240. (1, 2, 3, now(), now()),
  241. (2, 3, 4, now(), now()),
  242. (3, 4, 5, now(), now()),
  243. (4, 10, 1, now(), now()),
  244. (5, 11, 2, now(), now());
  245. -- stream entries
  246. INSERT INTO "stream_entries"
  247. (activity_id, account_id, activity_type, created_at, updated_at)
  248. VALUES
  249. (1, 1, 'status', now(), now()),
  250. (2, 1, 'status', now(), now()),
  251. (3, 1, 'status', now(), now()),
  252. (4, 1, 'status', now(), now()),
  253. (5, 1, 'status', now(), now()),
  254. (6, 3, 'status', now(), now()),
  255. (7, 4, 'status', now(), now()),
  256. (8, 5, 'status', now(), now()),
  257. (9, 1, 'status', now(), now());
  258. -- custom emoji
  259. INSERT INTO "custom_emojis"
  260. (id, shortcode, created_at, updated_at)
  261. VALUES
  262. (1, 'test', now(), now()),
  263. (2, 'Test', now(), now()),
  264. (3, 'blobcat', now(), now());
  265. INSERT INTO "custom_emojis"
  266. (id, shortcode, domain, uri, created_at, updated_at)
  267. VALUES
  268. (4, 'blobcat', 'remote.org', 'https://remote.org/emoji/blobcat', now(), now()),
  269. (5, 'blobcat', 'Remote.org', 'https://remote.org/emoji/blobcat', now(), now()),
  270. (6, 'Blobcat', 'remote.org', 'https://remote.org/emoji/Blobcat', now(), now());
  271. -- favourites
  272. INSERT INTO "favourites"
  273. (account_id, status_id, created_at, updated_at)
  274. VALUES
  275. (1, 1, now(), now()),
  276. (1, 7, now(), now()),
  277. (4, 1, now(), now()),
  278. (3, 1, now(), now()),
  279. (5, 1, now(), now());
  280. -- pinned statuses
  281. INSERT INTO "status_pins"
  282. (account_id, status_id, created_at, updated_at)
  283. VALUES
  284. (1, 1, now(), now()),
  285. (3, 6, now(), now()),
  286. (4, 7, now(), now());
  287. -- follows
  288. INSERT INTO "follows"
  289. (id, account_id, target_account_id, created_at, updated_at)
  290. VALUES
  291. (1, 1, 5, now(), now()),
  292. (2, 6, 2, now(), now()),
  293. (3, 5, 2, now(), now()),
  294. (4, 6, 1, now(), now());
  295. -- follow requests
  296. INSERT INTO "follow_requests"
  297. (account_id, target_account_id, created_at, updated_at)
  298. VALUES
  299. (2, 5, now(), now()),
  300. (5, 1, now(), now());
  301. -- notifications
  302. INSERT INTO "notifications"
  303. (id, from_account_id, account_id, activity_type, activity_id, created_at, updated_at)
  304. VALUES
  305. (1, 6, 2, 'Follow', 2, now(), now()),
  306. (2, 2, 1, 'Mention', 4, now(), now()),
  307. (3, 1, 2, 'Mention', 5, now(), now());
  308. -- preview cards
  309. INSERT INTO "preview_cards"
  310. (id, url, title, created_at, updated_at)
  311. VALUES
  312. (1, 'https://joinmastodon.org/', 'Mastodon - Decentralized social media', now(), now());
  313. -- many-to-many association between preview cards and statuses
  314. INSERT INTO "preview_cards_statuses"
  315. (status_id, preview_card_id)
  316. VALUES
  317. (12, 1),
  318. (12, 1);
  319. SQL
  320. end
  321. end
  322. end