From 63d688b6d7184b97b52e0bb15ba6a2fbbca9d81a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathias=20A=CC=8Ahsberg?= Date: Fri, 26 Mar 2021 09:11:03 +0000 Subject: [PATCH] Add tests for mapping Active Directory entries MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mathias Åhsberg --- .../ingestion/processors/ldap/read.test.ts | 171 +++++++++++++++++- 1 file changed, 163 insertions(+), 8 deletions(-) diff --git a/plugins/catalog-backend/src/ingestion/processors/ldap/read.test.ts b/plugins/catalog-backend/src/ingestion/processors/ldap/read.test.ts index 76dc9de0f6..e9a0410727 100644 --- a/plugins/catalog-backend/src/ingestion/processors/ldap/read.test.ts +++ b/plugins/catalog-backend/src/ingestion/processors/ldap/read.test.ts @@ -26,7 +26,7 @@ import { LDAP_UUID_ANNOTATION, } from './constants'; import { readLdapGroups, readLdapUsers, resolveRelations } from './read'; -import { DefaultLdapVendor } from './vendors'; +import { ActiveDirectoryVendor, DefaultLdapVendor } from './vendors'; function user(data: RecursivePartial): UserEntity { return merge( @@ -54,7 +54,9 @@ function group(data: RecursivePartial): GroupEntity { ); } -function searchEntry(attributes: Record): SearchEntry { +function searchEntry( + attributes: Record, +): SearchEntry { return { raw: Object.entries(attributes).reduce((obj, [key, values]) => { obj[key] = values; @@ -69,11 +71,10 @@ describe('readLdapUsers', () => { getVendor: jest.fn(), } as any; - beforeEach(() => client.getVendor.mockResolvedValue(DefaultLdapVendor)); - afterEach(() => jest.resetAllMocks()); - it('transfers all attributes', async () => { + it('transfers all attributes from a default ldap vendor', async () => { + client.getVendor.mockResolvedValue(DefaultLdapVendor); client.search.mockResolvedValue([ searchEntry({ uid: ['uid-value'], @@ -125,6 +126,79 @@ describe('readLdapUsers', () => { new Map([['dn-value', new Set(['x', 'y', 'z'])]]), ); }); + + it('transfers all attributes from Microsoft Active Directory', async () => { + client.getVendor.mockResolvedValue(ActiveDirectoryVendor); + client.search.mockResolvedValue([ + searchEntry({ + uid: ['uid-value'], + description: ['description-value'], + cn: ['cn-value'], + mail: ['mail-value'], + avatarUrl: ['avatarUrl-value'], + memberOf: ['x', 'y', 'z'], + distinguishedName: ['dn-value'], + objectGUID: [ + Buffer.from([ + 68, + 2, + 125, + 190, + 209, + 0, + 94, + 73, + 133, + 33, + 230, + 174, + 234, + 195, + 160, + 152, + ]), + ], + }), + ]); + const config: UserConfig = { + dn: 'ddd', + options: {}, + map: { + rdn: 'uid', + name: 'uid', + description: 'description', + displayName: 'cn', + email: 'mail', + picture: 'avatarUrl', + memberOf: 'memberOf', + }, + }; + const { users, userMemberOf } = await readLdapUsers(client, config); + expect(users).toEqual([ + expect.objectContaining({ + metadata: { + name: 'uid-value', + description: 'description-value', + annotations: { + [LDAP_DN_ANNOTATION]: 'dn-value', + [LDAP_RDN_ANNOTATION]: 'uid-value', + [LDAP_UUID_ANNOTATION]: 'be7d0244-00d1-495e-8521-e6aeeac3a098', + }, + }, + spec: { + profile: { + displayName: 'cn-value', + email: 'mail-value', + picture: 'avatarUrl-value', + }, + memberOf: [], + }, + }), + ]); + expect(userMemberOf).toEqual( + new Map([['dn-value', new Set(['x', 'y', 'z'])]]), + ); + }); }); describe('readLdapGroups', () => { @@ -133,11 +207,10 @@ describe('readLdapGroups', () => { getVendor: jest.fn(), } as any; - beforeEach(() => client.getVendor.mockResolvedValue(DefaultLdapVendor)); - afterEach(() => jest.resetAllMocks()); - it('transfers all attributes', async () => { + it('transfers all attributes from a default ldap vendor', async () => { + client.getVendor.mockResolvedValue(DefaultLdapVendor); client.search.mockResolvedValue([ searchEntry({ cn: ['cn-value'], @@ -199,6 +272,88 @@ describe('readLdapGroups', () => { new Map([['dn-value', new Set(['x', 'y', 'z'])]]), ); }); + it('transfers all attributes from Microsoft Active Directory', async () => { + client.getVendor.mockResolvedValue(ActiveDirectoryVendor); + client.search.mockResolvedValue([ + searchEntry({ + cn: ['cn-value'], + description: ['description-value'], + tt: ['type-value'], + mail: ['mail-value'], + avatarUrl: ['avatarUrl-value'], + memberOf: ['x', 'y', 'z'], + member: ['e', 'f', 'g'], + distinguishedName: ['dn-value'], + objectGUID: [ + Buffer.from([ + 68, + 2, + 125, + 190, + 209, + 0, + 94, + 73, + 133, + 33, + 230, + 174, + 234, + 195, + 160, + 152, + ]), + ], + }), + ]); + const config: GroupConfig = { + dn: 'ddd', + options: {}, + map: { + rdn: 'cn', + name: 'cn', + description: 'description', + displayName: 'cn', + email: 'mail', + picture: 'avatarUrl', + type: 'tt', + memberOf: 'memberOf', + members: 'member', + }, + }; + const { groups, groupMember, groupMemberOf } = await readLdapGroups( + client, + config, + ); + expect(groups).toEqual([ + expect.objectContaining({ + metadata: { + name: 'cn-value', + description: 'description-value', + annotations: { + [LDAP_DN_ANNOTATION]: 'dn-value', + [LDAP_RDN_ANNOTATION]: 'cn-value', + [LDAP_UUID_ANNOTATION]: 'be7d0244-00d1-495e-8521-e6aeeac3a098', + }, + }, + spec: { + type: 'type-value', + profile: { + displayName: 'cn-value', + email: 'mail-value', + picture: 'avatarUrl-value', + }, + children: [], + }, + }), + ]); + expect(groupMember).toEqual( + new Map([['dn-value', new Set(['e', 'f', 'g'])]]), + ); + expect(groupMemberOf).toEqual( + new Map([['dn-value', new Set(['x', 'y', 'z'])]]), + ); + }); }); describe('resolveRelations', () => {