global-setup.js 3.4 KB

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