global-setup.js 3.2 KB

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