Преглед изворни кода

refs 124934: add test for ExternalUserGroup model

Futa Arai пре 2 година
родитељ
комит
0fd0024111

+ 1 - 2
apps/app/src/features/external-user-group/server/models/external-user-group-relation.integ.ts

@@ -1,5 +1,4 @@
 import mongoose from 'mongoose';
-import { expect } from 'vitest';
 
 import ExternalUserGroup from './external-user-group';
 import ExternalUserGroupRelation from './external-user-group-relation';
@@ -15,7 +14,7 @@ const userSchema = new mongoose.Schema({
 });
 const User = mongoose.model('User', userSchema);
 
-describe('ExternalUserGroup model', () => {
+describe('ExternalUserGroupRelation model', () => {
   const userId1 = new mongoose.Types.ObjectId();
   const groupId1 = new mongoose.Types.ObjectId();
   const groupId2 = new mongoose.Types.ObjectId();

+ 73 - 0
apps/app/src/features/external-user-group/server/models/external-user-group.integ.ts

@@ -0,0 +1,73 @@
+import mongoose from 'mongoose';
+
+import ExternalUserGroup from './external-user-group';
+
+describe('ExternalUserGroup model', () => {
+  describe('findAndUpdateOrCreateGroup', () => {
+    const groupId = new mongoose.Types.ObjectId();
+    beforeAll(async() => {
+      await ExternalUserGroup.create({
+        _id: groupId, name: 'test group', externalId: 'testExternalId', provider: 'testProvider',
+      });
+    });
+
+    it('finds and updates existing group', async() => {
+      const group = await ExternalUserGroup.findAndUpdateOrCreateGroup('edited test group', 'testExternalId', 'testProvider');
+      expect(group.id).toBe(groupId.toString());
+      expect(group.name).toBe('edited test group');
+    });
+
+    it('creates new group with parent', async() => {
+      expect(await ExternalUserGroup.count()).toBe(1);
+      const newGroup = await ExternalUserGroup.findAndUpdateOrCreateGroup(
+        'new group', 'nonExistentExternalId', 'testProvider', undefined, groupId.toString(),
+      );
+      expect(await ExternalUserGroup.count()).toBe(2);
+      expect(newGroup.parent.toString()).toBe(groupId.toString());
+    });
+
+    it('throws error when parent does not exist', async() => {
+      try {
+        await ExternalUserGroup.findAndUpdateOrCreateGroup(
+          'new group', 'nonExistentExternalId', 'testProvider', undefined, new mongoose.Types.ObjectId(),
+        );
+      }
+      catch (e) {
+        expect(e.message).toBe('Parent does not exist.');
+      }
+    });
+  });
+
+  describe('findGroupsWithAncestorsRecursively', () => {
+    const childGroupId = new mongoose.Types.ObjectId();
+    const parentGroupId = new mongoose.Types.ObjectId();
+    const grandParentGroupId = new mongoose.Types.ObjectId();
+
+    beforeAll(async() => {
+      await ExternalUserGroup.deleteMany();
+      await ExternalUserGroup.create({
+        _id: grandParentGroupId, name: 'grand parent group', externalId: 'grandParentExternalId', provider: 'testProvider',
+      });
+      await ExternalUserGroup.create({
+        _id: parentGroupId, name: 'parent group', externalId: 'parentExternalId', provider: 'testProvider', parent: grandParentGroupId,
+      });
+      await ExternalUserGroup.create({
+        _id: childGroupId, name: 'child group', externalId: 'childExternalId', provider: 'testProvider', parent: parentGroupId,
+      });
+    });
+
+    it('finds ancestors for child', async() => {
+      const childGroup = await ExternalUserGroup.findById(childGroupId);
+      const groups = await ExternalUserGroup.findGroupsWithAncestorsRecursively(childGroup);
+      const groupIds = groups.map(group => group.id);
+      expect(groupIds).toStrictEqual([grandParentGroupId.toString(), parentGroupId.toString(), childGroupId.toString()]);
+    });
+
+    it('finds ancestors for child, excluding child', async() => {
+      const childGroup = await ExternalUserGroup.findById(childGroupId);
+      const groups = await ExternalUserGroup.findGroupsWithAncestorsRecursively(childGroup, []);
+      const groupIds = groups.map(group => group.id);
+      expect(groupIds).toStrictEqual([grandParentGroupId.toString(), parentGroupId.toString()]);
+    });
+  });
+});

+ 2 - 2
apps/app/src/features/external-user-group/server/models/external-user-group.ts

@@ -24,13 +24,13 @@ const schema = new Schema<ExternalUserGroupDocument, ExternalUserGroupModel>({
 /**
  * Find group that has specified externalId and update, or create one if it doesn't exist.
  * @param name ExternalUserGroup name
- * @param name ExternalUserGroup description
  * @param name ExternalUserGroup externalId
  * @param name ExternalUserGroup provider
+ * @param name ExternalUserGroup description
  * @param name ExternalUserGroup parentId
  * @returns ExternalUserGroupDocument[]
  */
-schema.statics.findAndUpdateOrCreateGroup = async function(name, description, externalId, provider, parentId) {
+schema.statics.findAndUpdateOrCreateGroup = async function(name: string, externalId: string, provider: string, description?: string, parentId?: string) {
   // create without parent
   if (parentId == null) {
     return this.findOneAndUpdate({ externalId }, { name, description, provider }, { upsert: true, new: true });

+ 1 - 1
apps/app/src/features/external-user-group/server/service/external-user-group-sync-service.ts

@@ -61,7 +61,7 @@ abstract class ExternalUserGroupSyncService {
   */
   async createUpdateExternalUserGroup(node: ExternalUserGroupTreeNode, parentId?: string): Promise<IExternalUserGroupHasId> {
     const externalUserGroup = await ExternalUserGroup.findAndUpdateOrCreateGroup(
-      node.name, node.description, node.id, this.groupProviderType, parentId,
+      node.name, node.id, this.groupProviderType, node.description, parentId,
     );
     await Promise.all(node.userInfos.map((userInfo) => {
       return (async() => {