global-setup.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /** **********************************************************
  2. * Caution
  3. *
  4. * Module aliases by compilerOptions.paths in tsconfig.json
  5. * are NOT available in setup scripts
  6. *********************************************************** */
  7. import 'tsconfig-paths/register';
  8. import mongoose from 'mongoose';
  9. import { initMongooseGlobalSettings, getMongoUri, mongoOptions } from '~/server/util/mongoose-utils';
  10. // check env
  11. if (process.env.NODE_ENV !== 'test') {
  12. throw new Error('\'process.env.NODE_ENV\' must be \'test\'');
  13. }
  14. module.exports = async() => {
  15. initMongooseGlobalSettings();
  16. mongoose.connect(getMongoUri(), mongoOptions);
  17. // drop database
  18. await mongoose.connection.dropDatabase();
  19. // init DB
  20. const pageCollection = mongoose.connection.collection('pages');
  21. const userCollection = mongoose.connection.collection('users');
  22. const userGroupCollection = mongoose.connection.collection('usergroups');
  23. const userGroupRelationsCollection = mongoose.connection.collection('usergrouprelations');
  24. // create global user & rootPage
  25. const globalUser = (await userCollection.insertMany([{ name: 'globalUser', username: 'globalUser', email: 'globalUser@example.com' }]))[0];
  26. const gGroupUserId1 = new mongoose.Types.ObjectId();
  27. const gGroupUserId2 = new mongoose.Types.ObjectId();
  28. const gGroupUserId3 = new mongoose.Types.ObjectId();
  29. await userCollection.insertMany([
  30. { name: 'v5DummyUser1', username: 'v5DummyUser1', email: 'v5DummyUser1@example.com' },
  31. { name: 'v5DummyUser2', username: 'v5DummyUser2', email: 'v5DummyUser2@example.com' },
  32. {
  33. _id: gGroupUserId1, name: 'gGroupUser1', username: 'gGroupUser1', email: 'gGroupUser1@example.com',
  34. },
  35. {
  36. _id: gGroupUserId2, name: 'gGroupUser2', username: 'gGroupUser2', email: 'gGroupUser2@example.com',
  37. },
  38. {
  39. _id: gGroupUserId3, name: 'gGroupUser3', username: 'gGroupUser3', email: 'gGroupUser3@example.com',
  40. },
  41. ]);
  42. const gGroupIdIsolate = new mongoose.Types.ObjectId();
  43. const gGroupIdA = new mongoose.Types.ObjectId();
  44. const gGroupIdB = new mongoose.Types.ObjectId();
  45. const gGroupIdC = new mongoose.Types.ObjectId();
  46. await userGroupCollection.insertMany([
  47. {
  48. _id: gGroupIdIsolate,
  49. name: 'globalGroupIsolate',
  50. },
  51. {
  52. _id: gGroupIdA,
  53. name: 'globalGroupA',
  54. },
  55. {
  56. _id: gGroupIdB,
  57. name: 'globalGroupB',
  58. parent: gGroupIdA,
  59. },
  60. {
  61. _id: gGroupIdC,
  62. name: 'globalGroupC',
  63. parent: gGroupIdB,
  64. },
  65. ]);
  66. await userGroupRelationsCollection.insertMany([
  67. {
  68. relatedGroup: gGroupIdIsolate,
  69. relatedUser: gGroupUserId1,
  70. createdAt: new Date(),
  71. },
  72. {
  73. relatedGroup: gGroupIdIsolate,
  74. relatedUser: gGroupUserId2,
  75. createdAt: new Date(),
  76. },
  77. {
  78. relatedGroup: gGroupIdA,
  79. relatedUser: gGroupUserId1,
  80. createdAt: new Date(),
  81. },
  82. {
  83. relatedGroup: gGroupIdA,
  84. relatedUser: gGroupUserId2,
  85. createdAt: new Date(),
  86. },
  87. {
  88. relatedGroup: gGroupIdA,
  89. relatedUser: gGroupUserId3,
  90. createdAt: new Date(),
  91. },
  92. {
  93. relatedGroup: gGroupIdB,
  94. relatedUser: gGroupUserId2,
  95. createdAt: new Date(),
  96. },
  97. {
  98. relatedGroup: gGroupIdB,
  99. relatedUser: gGroupUserId3,
  100. createdAt: new Date(),
  101. },
  102. {
  103. relatedGroup: gGroupIdC,
  104. relatedUser: gGroupUserId3,
  105. createdAt: new Date(),
  106. },
  107. ]);
  108. await pageCollection.insertMany([{
  109. path: '/',
  110. grant: 1,
  111. creator: globalUser,
  112. lastUpdateUser: globalUser,
  113. }]);
  114. await mongoose.disconnect();
  115. };