tests.rake 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 claned 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. end
  39. desc 'Populate the database with test data for 2.4.3'
  40. task populate_v2_4_3: :environment do # rubocop:disable Naming/VariableNumber
  41. ActiveRecord::Base.connection.execute(<<~SQL)
  42. INSERT INTO "custom_filters"
  43. (id, account_id, phrase, context, whole_word, irreversible, created_at, updated_at)
  44. VALUES
  45. (1, 2, 'test', '{ "home", "public" }', true, true, now(), now()),
  46. (2, 2, 'take', '{ "home" }', false, false, now(), now());
  47. SQL
  48. end
  49. desc 'Populate the database with test data for 2.4.0'
  50. task populate_v2_4: :environment do # rubocop:disable Naming/VariableNumber
  51. ActiveRecord::Base.connection.execute(<<~SQL)
  52. INSERT INTO "settings"
  53. (id, thing_type, thing_id, var, value, created_at, updated_at)
  54. VALUES
  55. (1, 'User', 1, 'hide_network', E'--- false\n', now(), now()),
  56. (2, 'User', 2, 'hide_network', E'--- true\n', now(), now());
  57. SQL
  58. end
  59. desc 'Populate the database with test data for 2.0.0'
  60. task populate_v2: :environment do
  61. admin_key = OpenSSL::PKey::RSA.new(2048)
  62. user_key = OpenSSL::PKey::RSA.new(2048)
  63. remote_key = OpenSSL::PKey::RSA.new(2048)
  64. remote_key2 = OpenSSL::PKey::RSA.new(2048)
  65. remote_key3 = OpenSSL::PKey::RSA.new(2048)
  66. admin_private_key = ActiveRecord::Base.connection.quote(admin_key.to_pem)
  67. admin_public_key = ActiveRecord::Base.connection.quote(admin_key.public_key.to_pem)
  68. user_private_key = ActiveRecord::Base.connection.quote(user_key.to_pem)
  69. user_public_key = ActiveRecord::Base.connection.quote(user_key.public_key.to_pem)
  70. remote_public_key = ActiveRecord::Base.connection.quote(remote_key.public_key.to_pem)
  71. remote_public_key2 = ActiveRecord::Base.connection.quote(remote_key2.public_key.to_pem)
  72. remote_public_key_ap = ActiveRecord::Base.connection.quote(remote_key3.public_key.to_pem)
  73. local_domain = ActiveRecord::Base.connection.quote(Rails.configuration.x.local_domain)
  74. ActiveRecord::Base.connection.execute(<<~SQL)
  75. -- accounts
  76. INSERT INTO "accounts"
  77. (id, username, domain, private_key, public_key, created_at, updated_at)
  78. VALUES
  79. (1, 'admin', NULL, #{admin_private_key}, #{admin_public_key}, now(), now()),
  80. (2, 'user', NULL, #{user_private_key}, #{user_public_key}, now(), now());
  81. INSERT INTO "accounts"
  82. (id, username, domain, private_key, public_key, created_at, updated_at, remote_url, salmon_url)
  83. VALUES
  84. (3, 'remote', 'remote.com', NULL, #{remote_public_key}, now(), now(),
  85. 'https://remote.com/@remote', 'https://remote.com/salmon/1'),
  86. (4, 'Remote', 'remote.com', NULL, #{remote_public_key}, now(), now(),
  87. 'https://remote.com/@Remote', 'https://remote.com/salmon/1'),
  88. (5, 'REMOTE', 'Remote.com', NULL, #{remote_public_key2}, now() - interval '1 year', now() - interval '1 year',
  89. 'https://remote.com/stale/@REMOTE', 'https://remote.com/stale/salmon/1');
  90. INSERT INTO "accounts"
  91. (id, username, domain, private_key, public_key, created_at, updated_at, protocol, inbox_url, outbox_url, followers_url)
  92. VALUES
  93. (6, 'bob', 'activitypub.com', NULL, #{remote_public_key_ap}, now(), now(),
  94. 1, 'https://activitypub.com/users/bob/inbox', 'https://activitypub.com/users/bob/outbox', 'https://activitypub.com/users/bob/followers');
  95. INSERT INTO "accounts"
  96. (id, username, domain, private_key, public_key, created_at, updated_at)
  97. VALUES
  98. (7, 'user', #{local_domain}, #{user_private_key}, #{user_public_key}, now(), now()),
  99. (8, 'pt_user', NULL, #{user_private_key}, #{user_public_key}, now(), now());
  100. INSERT INTO "accounts"
  101. (id, username, domain, private_key, public_key, created_at, updated_at, protocol, inbox_url, outbox_url, followers_url, suspended)
  102. VALUES
  103. (9, 'evil', 'activitypub.com', NULL, #{remote_public_key_ap}, now(), now(),
  104. 1, 'https://activitypub.com/users/evil/inbox', 'https://activitypub.com/users/evil/outbox',
  105. 'https://activitypub.com/users/evil/followers', true);
  106. -- users
  107. INSERT INTO "users"
  108. (id, account_id, email, created_at, updated_at, admin)
  109. VALUES
  110. (1, 1, 'admin@localhost', now(), now(), true),
  111. (2, 2, 'user@localhost', now(), now(), false);
  112. INSERT INTO "users"
  113. (id, account_id, email, created_at, updated_at, admin, locale)
  114. VALUES
  115. (3, 7, 'ptuser@localhost', now(), now(), false, 'pt');
  116. -- conversations
  117. INSERT INTO "conversations" (id, created_at, updated_at) VALUES (1, now(), now());
  118. -- statuses
  119. INSERT INTO "statuses"
  120. (id, account_id, text, created_at, updated_at)
  121. VALUES
  122. (1, 1, 'test', now(), now()),
  123. (2, 1, '@remote@remote.com hello', now(), now()),
  124. (3, 1, '@Remote@remote.com hello', now(), now()),
  125. (4, 1, '@REMOTE@remote.com hello', now(), now());
  126. INSERT INTO "statuses"
  127. (id, account_id, text, created_at, updated_at, uri, local)
  128. VALUES
  129. (5, 1, 'activitypub status', now(), now(), 'https://localhost/users/admin/statuses/4', true);
  130. INSERT INTO "statuses"
  131. (id, account_id, text, created_at, updated_at)
  132. VALUES
  133. (6, 3, 'test', now(), now());
  134. INSERT INTO "statuses"
  135. (id, account_id, text, created_at, updated_at, in_reply_to_id, in_reply_to_account_id)
  136. VALUES
  137. (7, 4, '@admin hello', now(), now(), 3, 1);
  138. INSERT INTO "statuses"
  139. (id, account_id, text, created_at, updated_at)
  140. VALUES
  141. (8, 5, 'test', now(), now());
  142. INSERT INTO "statuses"
  143. (id, account_id, reblog_of_id, created_at, updated_at)
  144. VALUES
  145. (9, 1, 2, now(), now());
  146. INSERT INTO "statuses"
  147. (id, account_id, text, in_reply_to_id, conversation_id, visibility, created_at, updated_at)
  148. VALUES
  149. (10, 2, '@admin hey!', NULL, 1, 3, now(), now()),
  150. (11, 1, '@user hey!', 10, 1, 3, now(), now());
  151. -- mentions (from previous statuses)
  152. INSERT INTO "mentions"
  153. (id, status_id, account_id, created_at, updated_at)
  154. VALUES
  155. (1, 2, 3, now(), now()),
  156. (2, 3, 4, now(), now()),
  157. (3, 4, 5, now(), now()),
  158. (4, 10, 1, now(), now()),
  159. (5, 11, 2, now(), now());
  160. -- stream entries
  161. INSERT INTO "stream_entries"
  162. (activity_id, account_id, activity_type, created_at, updated_at)
  163. VALUES
  164. (1, 1, 'status', now(), now()),
  165. (2, 1, 'status', now(), now()),
  166. (3, 1, 'status', now(), now()),
  167. (4, 1, 'status', now(), now()),
  168. (5, 1, 'status', now(), now()),
  169. (6, 3, 'status', now(), now()),
  170. (7, 4, 'status', now(), now()),
  171. (8, 5, 'status', now(), now()),
  172. (9, 1, 'status', now(), now());
  173. -- custom emoji
  174. INSERT INTO "custom_emojis"
  175. (shortcode, created_at, updated_at)
  176. VALUES
  177. ('test', now(), now()),
  178. ('Test', now(), now()),
  179. ('blobcat', now(), now());
  180. INSERT INTO "custom_emojis"
  181. (shortcode, domain, uri, created_at, updated_at)
  182. VALUES
  183. ('blobcat', 'remote.org', 'https://remote.org/emoji/blobcat', now(), now()),
  184. ('blobcat', 'Remote.org', 'https://remote.org/emoji/blobcat', now(), now()),
  185. ('Blobcat', 'remote.org', 'https://remote.org/emoji/Blobcat', now(), now());
  186. -- favourites
  187. INSERT INTO "favourites"
  188. (account_id, status_id, created_at, updated_at)
  189. VALUES
  190. (1, 1, now(), now()),
  191. (1, 7, now(), now()),
  192. (4, 1, now(), now()),
  193. (3, 1, now(), now()),
  194. (5, 1, now(), now());
  195. -- pinned statuses
  196. INSERT INTO "status_pins"
  197. (account_id, status_id, created_at, updated_at)
  198. VALUES
  199. (1, 1, now(), now()),
  200. (3, 6, now(), now()),
  201. (4, 7, now(), now());
  202. -- follows
  203. INSERT INTO "follows"
  204. (id, account_id, target_account_id, created_at, updated_at)
  205. VALUES
  206. (1, 1, 5, now(), now()),
  207. (2, 6, 2, now(), now()),
  208. (3, 5, 2, now(), now()),
  209. (4, 6, 1, now(), now());
  210. -- follow requests
  211. INSERT INTO "follow_requests"
  212. (account_id, target_account_id, created_at, updated_at)
  213. VALUES
  214. (2, 5, now(), now()),
  215. (5, 1, now(), now());
  216. -- notifications
  217. INSERT INTO "notifications"
  218. (id, from_account_id, account_id, activity_type, activity_id, created_at, updated_at)
  219. VALUES
  220. (1, 6, 2, 'Follow', 2, now(), now()),
  221. (2, 2, 1, 'Mention', 4, now(), now()),
  222. (3, 1, 2, 'Mention', 5, now(), now());
  223. SQL
  224. end
  225. end
  226. end