|
| 1 | +import { getUserInfo } from './getUserInfo'; |
| 2 | +import type { CachedSettings } from '../../../settings/server/CachedSettings'; |
| 3 | + |
| 4 | +jest.mock('@rocket.chat/models', () => ({ |
| 5 | + Users: { |
| 6 | + findOneById: jest.fn().mockResolvedValue({ |
| 7 | + id: '123', |
| 8 | + name: 'Test User', |
| 9 | + emails: [{ address: 'test@example.com' }], |
| 10 | + }), |
| 11 | + }, |
| 12 | +})); |
| 13 | + |
| 14 | +const settings = new Map<string, unknown>(); |
| 15 | + |
| 16 | +jest.mock('../../../settings/server', () => ({ |
| 17 | + settings: { |
| 18 | + getByRegexp(_id) { |
| 19 | + return [...settings].filter(([key]) => key.match(_id)) as any; |
| 20 | + }, |
| 21 | + get(_id) { |
| 22 | + return settings.get(_id) as any; |
| 23 | + }, |
| 24 | + set(record) { |
| 25 | + settings.set(record._id, record.value); |
| 26 | + }, |
| 27 | + } satisfies Partial<CachedSettings>, |
| 28 | +})); |
| 29 | + |
| 30 | +// @ts-expect-error __meteor_runtime_config__ is not defined in the type definitions |
| 31 | +global.__meteor_runtime_config__ = { |
| 32 | + ROOT_URL: 'http://localhost:3000', |
| 33 | + ROOT_URL_PATH_PREFIX: '', |
| 34 | +}; |
| 35 | + |
| 36 | +describe('getUserInfo', () => { |
| 37 | + let user: Parameters<typeof getUserInfo>[0]; |
| 38 | + |
| 39 | + beforeEach(() => { |
| 40 | + settings.clear(); |
| 41 | + settings.set('Site_Url', 'http://localhost:3000'); |
| 42 | + user = { |
| 43 | + _id: '123', |
| 44 | + createdAt: new Date(), |
| 45 | + roles: [], |
| 46 | + type: 'user', |
| 47 | + active: true, |
| 48 | + _updatedAt: new Date(), |
| 49 | + }; |
| 50 | + }); |
| 51 | + |
| 52 | + it('should return user info', async () => { |
| 53 | + const userInfo = await getUserInfo(user); |
| 54 | + |
| 55 | + expect(userInfo).toEqual( |
| 56 | + expect.objectContaining({ |
| 57 | + _id: '123', |
| 58 | + type: 'user', |
| 59 | + roles: [], |
| 60 | + active: true, |
| 61 | + _updatedAt: expect.any(Date), |
| 62 | + createdAt: expect.any(Date), |
| 63 | + email: undefined, |
| 64 | + avatarUrl: 'http://localhost:3000/avatar/undefined', |
| 65 | + settings: { |
| 66 | + calendar: {}, |
| 67 | + profile: {}, |
| 68 | + preferences: {}, |
| 69 | + }, |
| 70 | + }), |
| 71 | + ); |
| 72 | + }); |
| 73 | + |
| 74 | + describe('email handling', () => { |
| 75 | + it('should not include email if no emails are present', async () => { |
| 76 | + user.emails = []; |
| 77 | + const userInfo = await getUserInfo(user); |
| 78 | + expect(userInfo.email).toBe(undefined); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should include email if one email is present and verified', async () => { |
| 82 | + user.emails = [{ address: 'test@example.com', verified: true }]; |
| 83 | + const userInfo = await getUserInfo(user); |
| 84 | + expect(userInfo.email).toEqual('test@example.com'); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should not include email if one email is present and not verified', async () => { |
| 88 | + user.emails = [{ address: 'test@example.com', verified: false }]; |
| 89 | + const userInfo = await getUserInfo(user); |
| 90 | + expect(userInfo.email).toBe(undefined); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should include email if multiple emails are present and one is verified', async () => { |
| 94 | + user.emails = [ |
| 95 | + { address: 'test@example.com', verified: false }, |
| 96 | + { address: 'test2@example.com', verified: true }, |
| 97 | + ]; |
| 98 | + const userInfo = await getUserInfo(user); |
| 99 | + expect(userInfo.email).toEqual('test2@example.com'); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should not include email if multiple emails are present and none are verified', async () => { |
| 103 | + user.emails = [ |
| 104 | + { address: 'test@example.com', verified: false }, |
| 105 | + { address: 'test2@example.com', verified: false }, |
| 106 | + ]; |
| 107 | + const userInfo = await getUserInfo(user); |
| 108 | + expect(userInfo.email).toBe(undefined); |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + describe('outlook calendar settings', () => { |
| 113 | + beforeEach(() => { |
| 114 | + settings.set('Outlook_Calendar_Enabled', true); |
| 115 | + settings.set('Outlook_Calendar_Exchange_Url', 'https://outlook.office365.com/'); |
| 116 | + settings.set('Outlook_Calendar_Outlook_Url', 'https://outlook.office365.com/owa/#calendar/view/month'); |
| 117 | + settings.set('Outlook_Calendar_Url_Mapping', JSON.stringify({})); |
| 118 | + user.emails = [{ address: 'test@example.com', verified: true }]; |
| 119 | + }); |
| 120 | + |
| 121 | + it('should return empty calendar settings if Outlook is disabled', async () => { |
| 122 | + settings.set('Outlook_Calendar_Enabled', false); |
| 123 | + const userInfo = await getUserInfo(user); |
| 124 | + expect(userInfo.settings?.calendar).toEqual({}); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should return calendar settings with Outlook enabled and default URLs', async () => { |
| 128 | + const userInfo = await getUserInfo(user); |
| 129 | + expect(userInfo.settings?.calendar?.outlook).toEqual({ |
| 130 | + Enabled: true, |
| 131 | + Exchange_Url: 'https://outlook.office365.com/', |
| 132 | + Outlook_Url: 'https://outlook.office365.com/owa/#calendar/view/month', |
| 133 | + }); |
| 134 | + }); |
| 135 | + |
| 136 | + it('should return calendar settings with Outlook enabled and domain mapping', async () => { |
| 137 | + settings.set( |
| 138 | + 'Outlook_Calendar_Url_Mapping', |
| 139 | + JSON.stringify({ |
| 140 | + 'example.com': { Exchange_Url: 'https://custom.exchange.com/', Outlook_Url: 'https://custom.outlook.com/' }, |
| 141 | + }), |
| 142 | + ); |
| 143 | + const userInfo = await getUserInfo(user); |
| 144 | + expect(userInfo.settings?.calendar).toEqual({ |
| 145 | + outlook: { |
| 146 | + Enabled: true, |
| 147 | + Exchange_Url: 'https://custom.exchange.com/', |
| 148 | + Outlook_Url: 'https://custom.outlook.com/', |
| 149 | + }, |
| 150 | + }); |
| 151 | + }); |
| 152 | + |
| 153 | + it('should return calendar settings with outlook disabled but enabled for specific domain', async () => { |
| 154 | + settings.set('Outlook_Calendar_Enabled', false); |
| 155 | + settings.set( |
| 156 | + 'Outlook_Calendar_Url_Mapping', |
| 157 | + JSON.stringify({ |
| 158 | + 'specific.com': { Enabled: true, Exchange_Url: 'https://specific.exchange.com/', Outlook_Url: 'https://specific.outlook.com/' }, |
| 159 | + }), |
| 160 | + ); |
| 161 | + user.emails = [{ address: 'me@specific.com', verified: true }]; |
| 162 | + const userInfo = await getUserInfo(user); |
| 163 | + expect(userInfo.settings?.calendar).toEqual({ |
| 164 | + outlook: { |
| 165 | + Enabled: true, |
| 166 | + Exchange_Url: 'https://specific.exchange.com/', |
| 167 | + Outlook_Url: 'https://specific.outlook.com/', |
| 168 | + }, |
| 169 | + }); |
| 170 | + }); |
| 171 | + |
| 172 | + it('should return calendar settings with Outlook enabled and default mapping for unknown domain', async () => { |
| 173 | + user.emails = [{ address: 'unknown@example.com', verified: true }]; |
| 174 | + const userInfo = await getUserInfo(user); |
| 175 | + expect(userInfo.settings?.calendar).toEqual({ |
| 176 | + outlook: { |
| 177 | + Enabled: true, |
| 178 | + Exchange_Url: 'https://outlook.office365.com/', |
| 179 | + Outlook_Url: 'https://outlook.office365.com/owa/#calendar/view/month', |
| 180 | + }, |
| 181 | + }); |
| 182 | + }); |
| 183 | + |
| 184 | + it('should handle invalid JSON in Outlook_Calendar_Url_Mapping', async () => { |
| 185 | + settings.set('Outlook_Calendar_Url_Mapping', 'invalid json'); |
| 186 | + const userInfo = await getUserInfo(user); |
| 187 | + expect(userInfo.settings?.calendar).toEqual({ |
| 188 | + outlook: { |
| 189 | + Enabled: true, |
| 190 | + Exchange_Url: 'https://outlook.office365.com/', |
| 191 | + Outlook_Url: 'https://outlook.office365.com/owa/#calendar/view/month', |
| 192 | + }, |
| 193 | + }); |
| 194 | + }); |
| 195 | + }); |
| 196 | +}); |
0 commit comments