global-setup.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 { 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. mongoose.connect(getMongoUri(), mongoOptions);
  15. // drop database
  16. await mongoose.connection.dropDatabase();
  17. // init DB
  18. const pageCollection = mongoose.connection.collection('pages');
  19. const userCollection = mongoose.connection.collection('users');
  20. const userGroupCollection = mongoose.connection.collection('usergroups');
  21. const userGroupRelationsCollection = mongoose.connection.collection('usergrouprelations');
  22. // create global user & rootPage
  23. const globalUser = (await userCollection.insertMany([{ name: 'globalUser', username: 'globalUser', email: 'globalUser@example.com' }]))[0];
  24. const gGroupUserId1 = new mongoose.Types.ObjectId();
  25. const gGroupUserId2 = new mongoose.Types.ObjectId();
  26. const gGroupUserId3 = new mongoose.Types.ObjectId();
  27. await userCollection.insertMany([
  28. { name: 'v5DummyUser1', username: 'v5DummyUser1', email: 'v5DummyUser1@example.com' },
  29. { name: 'v5DummyUser2', username: 'v5DummyUser2', email: 'v5DummyUser2@example.com' },
  30. {
  31. _id: gGroupUserId1, name: 'gGroupUser1', username: 'gGroupUser1', email: 'gGroupUser1@example.com',
  32. },
  33. {
  34. _id: gGroupUserId2, name: 'gGroupUser2', username: 'gGroupUser2', email: 'gGroupUser2@example.com',
  35. },
  36. {
  37. _id: gGroupUserId3, name: 'gGroupUser3', username: 'gGroupUser3', email: 'gGroupUser3@example.com',
  38. },
  39. ]);
  40. const gGroupIdIsolate = new mongoose.Types.ObjectId();
  41. const gGroupIdA = new mongoose.Types.ObjectId();
  42. const gGroupIdB = new mongoose.Types.ObjectId();
  43. const gGroupIdC = new mongoose.Types.ObjectId();
  44. await userGroupCollection.insertMany([
  45. {
  46. _id: gGroupIdIsolate,
  47. name: 'globalGroupIsolate',
  48. },
  49. {
  50. _id: gGroupIdA,
  51. name: 'globalGroupA',
  52. },
  53. {
  54. _id: gGroupIdB,
  55. name: 'globalGroupB',
  56. parent: gGroupIdA,
  57. },
  58. {
  59. _id: gGroupIdC,
  60. name: 'globalGroupC',
  61. parent: gGroupIdB,
  62. },
  63. ]);
  64. await userGroupRelationsCollection.insertMany([
  65. {
  66. relatedGroup: gGroupIdIsolate,
  67. relatedUser: gGroupUserId1,
  68. createdAt: new Date(),
  69. },
  70. {
  71. relatedGroup: gGroupIdIsolate,
  72. relatedUser: gGroupUserId2,
  73. createdAt: new Date(),
  74. },
  75. {
  76. relatedGroup: gGroupIdA,
  77. relatedUser: gGroupUserId1,
  78. createdAt: new Date(),
  79. },
  80. {
  81. relatedGroup: gGroupIdA,
  82. relatedUser: gGroupUserId2,
  83. createdAt: new Date(),
  84. },
  85. {
  86. relatedGroup: gGroupIdA,
  87. relatedUser: gGroupUserId3,
  88. createdAt: new Date(),
  89. },
  90. {
  91. relatedGroup: gGroupIdB,
  92. relatedUser: gGroupUserId2,
  93. createdAt: new Date(),
  94. },
  95. {
  96. relatedGroup: gGroupIdB,
  97. relatedUser: gGroupUserId3,
  98. createdAt: new Date(),
  99. },
  100. {
  101. relatedGroup: gGroupIdC,
  102. relatedUser: gGroupUserId3,
  103. createdAt: new Date(),
  104. },
  105. ]);
  106. await pageCollection.insertMany([{
  107. path: '/',
  108. grant: 1,
  109. creator: globalUser,
  110. lastUpdateUser: globalUser,
  111. }]);
  112. await mongoose.disconnect();
  113. };