external-user-group-sync.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. import type { IUserHasId } from '@growi/core';
  2. import mongoose, { Types } from 'mongoose';
  3. import {
  4. ExternalGroupProviderType, ExternalUserGroupTreeNode, IExternalUserGroup, IExternalUserGroupHasId,
  5. } from '../../../src/features/external-user-group/interfaces/external-user-group';
  6. import ExternalUserGroup from '../../../src/features/external-user-group/server/models/external-user-group';
  7. import ExternalUserGroupRelation from '../../../src/features/external-user-group/server/models/external-user-group-relation';
  8. import ExternalUserGroupSyncService from '../../../src/features/external-user-group/server/service/external-user-group-sync';
  9. import ExternalAccount from '../../../src/server/models/external-account';
  10. import { configManager } from '../../../src/server/service/config-manager';
  11. import { instanciate } from '../../../src/server/service/external-account';
  12. import PassportService from '../../../src/server/service/passport';
  13. import { getInstance } from '../setup-crowi';
  14. // dummy class to implement generateExternalUserGroupTrees which returns test data
  15. class TestExternalUserGroupSyncService extends ExternalUserGroupSyncService {
  16. constructor(s2sMessagingService, socketIoService) {
  17. super('ldap', s2sMessagingService, socketIoService);
  18. this.authProviderType = ExternalGroupProviderType.ldap;
  19. }
  20. async generateExternalUserGroupTrees(): Promise<ExternalUserGroupTreeNode[]> {
  21. const childNode: ExternalUserGroupTreeNode = {
  22. id: 'cn=childGroup,ou=groups,dc=example,dc=org',
  23. userInfos: [{
  24. id: 'childGroupUser',
  25. username: 'childGroupUser',
  26. name: 'Child Group User',
  27. email: 'user@childgroup.com',
  28. }],
  29. childGroupNodes: [],
  30. name: 'childGroup',
  31. description: 'this is a child group',
  32. };
  33. const parentNode: ExternalUserGroupTreeNode = {
  34. id: 'cn=parentGroup,ou=groups,dc=example,dc=org',
  35. // name is undefined
  36. userInfos: [{
  37. id: 'parentGroupUser',
  38. username: 'parentGroupUser',
  39. email: 'user@parentgroup.com',
  40. }],
  41. childGroupNodes: [childNode],
  42. name: 'parentGroup',
  43. description: 'this is a parent group',
  44. };
  45. const grandParentNode: ExternalUserGroupTreeNode = {
  46. id: 'cn=grandParentGroup,ou=groups,dc=example,dc=org',
  47. // email is undefined
  48. userInfos: [{
  49. id: 'grandParentGroupUser',
  50. username: 'grandParentGroupUser',
  51. name: 'Grand Parent Group User',
  52. }],
  53. childGroupNodes: [parentNode],
  54. name: 'grandParentGroup',
  55. description: 'this is a grand parent group',
  56. };
  57. const previouslySyncedNode: ExternalUserGroupTreeNode = {
  58. id: 'cn=previouslySyncedGroup,ou=groups,dc=example,dc=org',
  59. userInfos: [{
  60. id: 'previouslySyncedGroupUser',
  61. username: 'previouslySyncedGroupUser',
  62. name: 'Root Group User',
  63. email: 'user@previouslySyncedgroup.com',
  64. }],
  65. childGroupNodes: [],
  66. name: 'previouslySyncedGroup',
  67. description: 'this is a previouslySynced group',
  68. };
  69. return [grandParentNode, previouslySyncedNode];
  70. }
  71. }
  72. const testService = new TestExternalUserGroupSyncService(null, null);
  73. const checkGroup = (group: IExternalUserGroupHasId, expected: Omit<IExternalUserGroup, 'createdAt'>) => {
  74. const actual = {
  75. name: group.name,
  76. parent: group.parent,
  77. description: group.description,
  78. externalId: group.externalId,
  79. provider: group.provider,
  80. };
  81. expect(actual).toStrictEqual(expected);
  82. };
  83. const checkSync = async(autoGenerateUserOnGroupSync = true) => {
  84. const grandParentGroup = await ExternalUserGroup.findOne({ name: 'grandParentGroup' });
  85. checkGroup(grandParentGroup, {
  86. externalId: 'cn=grandParentGroup,ou=groups,dc=example,dc=org',
  87. name: 'grandParentGroup',
  88. description: 'this is a grand parent group',
  89. provider: 'ldap',
  90. parent: null,
  91. });
  92. const parentGroup = await ExternalUserGroup.findOne({ name: 'parentGroup' });
  93. checkGroup(parentGroup, {
  94. externalId: 'cn=parentGroup,ou=groups,dc=example,dc=org',
  95. name: 'parentGroup',
  96. description: 'this is a parent group',
  97. provider: 'ldap',
  98. parent: grandParentGroup._id,
  99. });
  100. const childGroup = await ExternalUserGroup.findOne({ name: 'childGroup' });
  101. checkGroup(childGroup, {
  102. externalId: 'cn=childGroup,ou=groups,dc=example,dc=org',
  103. name: 'childGroup',
  104. description: 'this is a child group',
  105. provider: 'ldap',
  106. parent: parentGroup._id,
  107. });
  108. const previouslySyncedGroup = await ExternalUserGroup.findOne({ name: 'previouslySyncedGroup' });
  109. checkGroup(previouslySyncedGroup, {
  110. externalId: 'cn=previouslySyncedGroup,ou=groups,dc=example,dc=org',
  111. name: 'previouslySyncedGroup',
  112. description: 'this is a previouslySynced group',
  113. provider: 'ldap',
  114. parent: null,
  115. });
  116. const grandParentGroupRelations = await ExternalUserGroupRelation
  117. .find({ relatedGroup: grandParentGroup._id });
  118. const parentGroupRelations = await ExternalUserGroupRelation
  119. .find({ relatedGroup: parentGroup._id });
  120. const childGroupRelations = await ExternalUserGroupRelation
  121. .find({ relatedGroup: childGroup._id });
  122. const previouslySyncedGroupRelations = await ExternalUserGroupRelation
  123. .find({ relatedGroup: previouslySyncedGroup._id });
  124. if (autoGenerateUserOnGroupSync) {
  125. expect(grandParentGroupRelations.length).toBe(3);
  126. const populatedGrandParentGroupRelations = await Promise.all(grandParentGroupRelations.map((relation) => {
  127. return relation.populate<{relatedUser: IUserHasId}>('relatedUser');
  128. }));
  129. expect(populatedGrandParentGroupRelations[0].relatedUser.username).toBe('grandParentGroupUser');
  130. expect(populatedGrandParentGroupRelations[1].relatedUser.username).toBe('parentGroupUser');
  131. expect(populatedGrandParentGroupRelations[2].relatedUser.username).toBe('childGroupUser');
  132. expect(parentGroupRelations.length).toBe(2);
  133. const populatedParentGroupRelations = await Promise.all(parentGroupRelations.map((relation) => {
  134. return relation.populate<{relatedUser: IUserHasId}>('relatedUser');
  135. }));
  136. expect(populatedParentGroupRelations[0].relatedUser.username).toBe('parentGroupUser');
  137. expect(populatedParentGroupRelations[1].relatedUser.username).toBe('childGroupUser');
  138. expect(childGroupRelations.length).toBe(1);
  139. const childGroupUser = (await childGroupRelations[0].populate<{relatedUser: IUserHasId}>('relatedUser'))?.relatedUser;
  140. expect(childGroupUser?.username).toBe('childGroupUser');
  141. expect(previouslySyncedGroupRelations.length).toBe(1);
  142. const previouslySyncedGroupUser = (await previouslySyncedGroupRelations[0].populate<{relatedUser: IUserHasId}>('relatedUser'))?.relatedUser;
  143. expect(previouslySyncedGroupUser?.username).toBe('previouslySyncedGroupUser');
  144. const userPages = await mongoose.model('Page').find({
  145. path: {
  146. $in: [
  147. '/user/childGroupUser', '/user/parentGroupUser', '/user/grandParentGroupUser', '/user/previouslySyncedGroupUser',
  148. ],
  149. },
  150. });
  151. expect(userPages.length).toBe(4);
  152. }
  153. else {
  154. expect(grandParentGroupRelations.length).toBe(0);
  155. expect(parentGroupRelations.length).toBe(0);
  156. expect(childGroupRelations.length).toBe(0);
  157. expect(previouslySyncedGroupRelations.length).toBe(0);
  158. }
  159. };
  160. describe('ExternalUserGroupSyncService.syncExternalUserGroups', () => {
  161. let crowi;
  162. beforeAll(async() => {
  163. crowi = await getInstance();
  164. await configManager.updateConfigsInTheSameNamespace('crowi', { 'app:isV5Compatible': true });
  165. const passportService = new PassportService(crowi);
  166. instanciate(passportService);
  167. });
  168. beforeEach(async() => {
  169. await ExternalUserGroup.create({
  170. name: 'nameBeforeEdit',
  171. description: 'this is a description before edit',
  172. externalId: 'cn=previouslySyncedGroup,ou=groups,dc=example,dc=org',
  173. provider: 'ldap',
  174. });
  175. });
  176. afterEach(async() => {
  177. await ExternalUserGroup.deleteMany();
  178. await ExternalUserGroupRelation.deleteMany();
  179. await mongoose.model('User')
  180. .deleteMany({ username: { $in: ['childGroupUser', 'parentGroupUser', 'grandParentGroupUser', 'previouslySyncedGroupUser'] } });
  181. await ExternalAccount.deleteMany({ accountId: { $in: ['childGroupUser', 'parentGroupUser', 'grandParentGroupUser', 'previouslySyncedGroupUser'] } });
  182. await mongoose.model('Page').deleteMany({
  183. path: {
  184. $in: [
  185. '/user/childGroupUser', '/user/parentGroupUser', '/user/grandParentGroupUser', '/user/previouslySyncedGroupUser',
  186. ],
  187. },
  188. });
  189. });
  190. describe('When autoGenerateUserOnGroupSync is true', () => {
  191. const configParams = {
  192. 'external-user-group:ldap:autoGenerateUserOnGroupSync': true,
  193. 'external-user-group:ldap:preserveDeletedGroups': false,
  194. };
  195. beforeAll(async() => {
  196. await configManager.updateConfigsInTheSameNamespace('crowi', configParams);
  197. });
  198. // eslint-disable-next-line jest/expect-expect
  199. it('syncs groups with new users', async() => {
  200. await testService.syncExternalUserGroups();
  201. await checkSync();
  202. });
  203. });
  204. describe('When autoGenerateUserOnGroupSync is false', () => {
  205. const configParams = {
  206. 'external-user-group:ldap:autoGenerateUserOnGroupSync': false,
  207. 'external-user-group:ldap:preserveDeletedGroups': true,
  208. };
  209. beforeAll(async() => {
  210. await configManager.updateConfigsInTheSameNamespace('crowi', configParams);
  211. });
  212. // eslint-disable-next-line jest/expect-expect
  213. it('syncs groups without new users', async() => {
  214. await testService.syncExternalUserGroups();
  215. await checkSync(false);
  216. });
  217. });
  218. describe('When preserveDeletedGroups is false', () => {
  219. const configParams = {
  220. 'external-user-group:ldap:autoGenerateUserOnGroupSync': true,
  221. 'external-user-group:ldap:preserveDeletedGroups': false,
  222. };
  223. beforeAll(async() => {
  224. await configManager.updateConfigsInTheSameNamespace('crowi', configParams);
  225. const groupId = new Types.ObjectId();
  226. const userId = new Types.ObjectId();
  227. await ExternalUserGroup.create({
  228. _id: groupId,
  229. name: 'non existent group',
  230. externalId: 'cn=nonExistentGroup,ou=groups,dc=example,dc=org',
  231. provider: 'ldap',
  232. });
  233. await mongoose.model('User').create({ _id: userId, username: 'nonExistentGroupUser' });
  234. await ExternalUserGroupRelation.create({ relatedUser: userId, relatedGroup: groupId });
  235. });
  236. it('syncs groups and deletes groups that do not exist externally', async() => {
  237. await testService.syncExternalUserGroups();
  238. await checkSync();
  239. expect(await ExternalUserGroup.countDocuments()).toBe(4);
  240. expect(await ExternalUserGroupRelation.countDocuments()).toBe(7);
  241. });
  242. });
  243. });