mattermost-mobile/app/utils/server/index.test.ts
Elias Nahum 44eb76bed7
feat(iOS): Add Microsoft Intune MAM integration with multi-server support (#9312)
* refactor: implement custom ExpoImage wrapper for cache control

Add ExpoImage component with automatic cacheKey/cachePath management and replace all expo-image imports across the app

* refactor(ios): convert Gekidou to CocoaPods

Migrate from Swift Package Manager to CocoaPods, add Keychain write operations, refactor notification handler to remove react-native-notifications headers, and upgrade Swift to 5.0

* npm audit

* update fastlane

* feat(ci): integrate Intune MAM for enterprise builds with strict OSS protection

Add Intune submodule, CI actions, Fastlane configuration, developer scripts, pre-commit hooks, and validation workflows to enable internal MAM builds while protecting OSS repository

* fix tests by mocking @mattermost/intune

* feat: implement Intune MAM integration with comprehensive security enforcement

Add IntuneManager, refactor SecurityManager/SessionManager for MAM policies, implement native OIDC auth flow, add biometric enforcement, conditional launch blocking, and file protection controls

* fix alerts when no server database is present

* Unify cache strategy

* fix emit config changed after it was stored in the db

* Handle Mid-Session Enrollment Detection

* fix ADALLogOverrideDisabled missing in Fastfile

* fix flow for initial enrollment

* fix and add unit tests

* enable Intune configuration for PR and beta builds, CLIENT_ID should be changed before actual release

* Update intune submodule with addressed feedback

* fix validate-intune-clean workflow

* feat(intune): add comprehensive error handling and SAML+Entra support

Add production-ready error handling for native Entra authentication with
user-friendly i18n messages, comprehensive test coverage, and support for
Entra login when server requires SAML.

* update i18n

* update intune submodule

* update build-pr token

* fix race condition between server auth and intune enrollment

* fix CI workflow to build with intune

* use deploy key for intune submodule

* set the config directly in the submodule .git

* debug injection

* try setting GIT_SSH_COMMAND

* remove action debug

* fix server url input

* match pod cache with intune hash

* Fastfile and envs

* have workflows check for intune/.git

* have ci cache intune frameworks as well

* update Fastlane to set no-cache to artifacts uploaded

* fix s3 upload

* fix pblist template

* Attempt to remove the cache control for PR uploads to s3

* use hash from commit for S3 path

* Implement crash-resilient selective wipe with automatic retry and add removeInternetPassword to Gekidou Keychain

* Fix surface errors from intune login

* fix postinstall scripts

* use cacheKey for draft md images

* remove unnecessary double await during test

* Have isMinimumLicenseTier accept valid license sku tier as target

* Add missing Auth error messages

* remove the last period for intune errors in i18n

* do not call unenroll with wipe on manual logout

* Fix tests and Intune error messages

* do not filter any SSO type regardless of which is used for Intune

* fix 412 to not retry

* fix tests, app logs sharing and share_extension avatar cache

* apply setScreenCapturePolicy on license change

Co-authored-by: Eva Sarafianou <eva.sarafianou@mattermost.com>

* re-apply screen capture on enrollment

Co-authored-by: Eva Sarafianou <eva.sarafianou@mattermost.com>

* use userData from intunr login and prevent getMe

Co-authored-by: Eva Sarafianou <eva.sarafianou@mattermost.com>

* Check for Biometrics and Jailbreak as we used to

---------

Co-authored-by: Eva Sarafianou <eva.sarafianou@mattermost.com>
2025-12-10 13:07:28 +02:00

269 lines
8.8 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {Alert} from 'react-native';
import {Preferences, Screens, Sso} from '@constants';
import {dismissBottomSheet, showModal} from '@screens/navigation';
import {getIntlShape} from '@utils/general';
import {isMinimumServerVersion} from '@utils/helpers';
import {
addNewServer,
alertServerAlreadyConnected,
alertServerError,
alertServerLogout,
alertServerRemove,
editServer,
isSupportedServer,
loginOptions,
loginToServer,
semverFromServerVersion,
sortServersByDisplayName,
unsupportedServer,
} from './index';
import type {ServersModel} from '@database/models/app';
import type {DeepLinkWithData} from '@typings/launch';
jest.mock('@utils/helpers', () => ({
isMinimumServerVersion: jest.fn(),
isTablet: jest.fn(() => true),
}));
jest.mock('@screens/navigation', () => ({
dismissBottomSheet: jest.fn(),
showModal: jest.fn(),
}));
jest.mock('@components/compass_icon', () => ({
getImageSourceSync: jest.fn(),
}));
jest.mock('@utils/url', () => ({
tryOpenURL: jest.fn(),
}));
describe('unsupportedServer', () => {
const intl = getIntlShape();
it('should show the alert for sysadmin', () => {
const alert = jest.spyOn(Alert, 'alert');
unsupportedServer('Default Server', true, intl);
expect(alert?.mock?.calls?.[0]?.[2]?.length).toBe(2);
});
it('should not show the alert for team admin / user', () => {
const alert = jest.spyOn(Alert, 'alert');
unsupportedServer('Default Server', false, intl);
expect(alert).not.toHaveBeenCalled();
});
});
describe('isSupportedServer', () => {
it('should return true for supported server version', () => {
(isMinimumServerVersion as jest.Mock).mockReturnValue(true);
const result = isSupportedServer('5.0.0');
expect(result).toBe(true);
});
it('should return false for unsupported server version', () => {
(isMinimumServerVersion as jest.Mock).mockReturnValue(false);
const result = isSupportedServer('4.0.0');
expect(result).toBe(false);
});
});
describe('semverFromServerVersion', () => {
it('should return semver string for valid version', () => {
const result = semverFromServerVersion('5.0.0');
expect(result).toBe('5.0.0');
});
it('should return undefined for invalid version', () => {
const result = semverFromServerVersion('');
expect(result).toBeUndefined();
});
});
describe('addNewServer', () => {
it('should call dismissBottomSheet and showModal', async () => {
const theme = Preferences.THEMES.denim;
const serverUrl = 'https://server.com';
const displayName = 'Server';
const deepLinkProps = {type: 'channel', url: 'https://mattermost.com'} as DeepLinkWithData;
await addNewServer(theme, serverUrl, displayName, deepLinkProps);
expect(dismissBottomSheet).toHaveBeenCalled();
expect(showModal).toHaveBeenCalledWith(Screens.SERVER, '', expect.any(Object), expect.any(Object));
});
});
describe('loginOptions', () => {
it('should return correct login options', () => {
const config = {
EnableSaml: 'true',
EnableSignUpWithGitLab: 'true',
EnableSignUpWithGoogle: 'true',
EnableSignUpWithOffice365: 'true',
EnableSignUpWithOpenId: 'true',
EnableLdap: 'true',
EnableSignInWithEmail: 'true',
EnableSignInWithUsername: 'true',
Version: '5.0.0',
} as ClientConfig;
const license = {
IsLicensed: 'true',
SAML: 'true',
Office365OAuth: 'true',
LDAP: 'true',
} as ClientLicense;
const result = loginOptions(config, license);
expect(result).toEqual({
hasLoginForm: true,
ssoOptions: {
[Sso.SAML]: {enabled: true, text: undefined},
[Sso.GITLAB]: {enabled: true},
[Sso.GOOGLE]: {enabled: true},
[Sso.OFFICE365]: {enabled: true},
[Sso.OPENID]: {enabled: true, text: undefined},
},
enabledSSOs: [Sso.SAML, Sso.GITLAB, Sso.GOOGLE, Sso.OFFICE365, Sso.OPENID],
numberSSOs: 5,
});
});
});
describe('loginToServer', () => {
const theme = Preferences.THEMES.denim;
const serverUrl = 'https://server.com';
const displayName = 'Server';
const config = {
EnableSaml: 'true',
EnableSignUpWithGitLab: 'false',
EnableSignUpWithGoogle: 'false',
EnableSignUpWithOffice365: 'false',
EnableSignUpWithOpenId: 'false',
EnableLdap: 'false',
EnableSignInWithEmail: 'true',
EnableSignInWithUsername: 'true',
Version: '5.0.0',
} as ClientConfig;
const license = {
IsLicensed: 'true',
SAML: 'true',
Office365OAuth: 'true',
LDAP: 'true',
} as ClientLicense;
it('should call dismissBottomSheet and showModal with LOGIN screen', async () => {
await loginToServer(theme, serverUrl, displayName, config, license);
expect(dismissBottomSheet).toHaveBeenCalled();
expect(showModal).toHaveBeenCalledWith(Screens.LOGIN, '', expect.any(Object), expect.any(Object));
});
/* Commented out for now as the test is failing potentially due to incorrect logic in the function
it('should call showModal with SSO screen if redirectSSO is true', async () => {
const configWithSingleSSO = {...config, EnableSignInWithEmail: 'false', EnableSignInWithUsername: 'false'};
await loginToServer(theme, serverUrl, displayName, configWithSingleSSO, license);
expect(showModal).toHaveBeenCalledWith(Screens.SSO, '', expect.any(Object), expect.any(Object));
});*/
});
describe('editServer', () => {
const theme = Preferences.THEMES.denim;
const server = {url: 'https://server.com', displayName: 'Server'} as ServersModel;
it('should call showModal with EDIT_SERVER screen', async () => {
await editServer(theme, server);
expect(showModal).toHaveBeenCalledWith(Screens.EDIT_SERVER, '', expect.any(Object), expect.any(Object));
});
});
describe('alertServerLogout', () => {
const intl = getIntlShape();
const displayName = 'Server';
const onPress = jest.fn();
it('should show an alert with the correct title and message', () => {
const alert = jest.spyOn(Alert, 'alert');
alertServerLogout(displayName, onPress, intl);
expect(alert).toHaveBeenCalledWith(
`Are you sure you want to log out of ${displayName}?`,
'All associated data will be removed',
expect.any(Array),
);
});
});
describe('alertServerRemove', () => {
const intl = getIntlShape();
const displayName = 'Server';
const onPress = jest.fn();
it('should show an alert with the correct title and message', () => {
const alert = jest.spyOn(Alert, 'alert');
alertServerRemove(displayName, onPress, intl);
expect(alert).toHaveBeenCalledWith(
`Are you sure you want to remove ${displayName}?`,
'This will remove it from your list of servers. All associated data will be removed',
expect.any(Array),
);
});
});
describe('alertServerError', () => {
const intl = getIntlShape();
const error = new Error('Test error');
it('should show an alert with the correct title and message', () => {
const alert = jest.spyOn(Alert, 'alert');
alertServerError(intl, error);
expect(alert).toHaveBeenCalledWith(
'Server is unreachable.',
'Test error',
);
});
});
describe('alertServerAlreadyConnected', () => {
const intl = getIntlShape();
it('should show an alert with the correct message', () => {
const alert = jest.spyOn(Alert, 'alert');
alertServerAlreadyConnected(intl);
expect(alert).toHaveBeenCalledWith(
'',
'You are already connected to this server.',
);
});
});
describe('sortServersByDisplayName', () => {
const intl = getIntlShape();
const servers = [
{url: 'https://server1.com', displayName: 'Server 1'} as ServersModel,
{url: 'https://server2.com', displayName: 'Server 2'} as ServersModel,
{url: 'https://server3.com', displayName: 'https://server3.com'} as ServersModel,
];
it('should sort servers by display name', () => {
const sortedServers = sortServersByDisplayName(servers, intl);
expect(sortedServers[0].url).toBe('https://server3.com'); // Display name treated as 'Default Server'
expect(sortedServers[1].url).toBe('https://server1.com');
expect(sortedServers[2].url).toBe('https://server2.com');
});
});