global-setup.js 3.5 KB

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