Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

fix(app-routes-redirect): redirects when workspaceBlocked & onboarding having wrong redirects (#10738)

Vinicius Lourençoa8e2155bb65b
diff --git a/frontend/src/AppRoutes/Private.tsx b/frontend/src/AppRoutes/Private.tsx
index cbcca1f649..0e9c2dc934 100644
--- a/frontend/src/AppRoutes/Private.tsx
+++ b/frontend/src/AppRoutes/Private.tsx
@@ -101,6 +101,22 @@ function PrivateRoute({ children }: PrivateRouteProps): JSX.Element {
preference.name === ORG_PREFERENCES.ORG_ONBOARDING,
)?.value;
+ // Don't redirect to onboarding if workspace has issues (blocked, suspended, or restricted)
+ // User needs access to settings/billing to fix payment issues
+ const isWorkspaceBlocked = trialInfo?.workSpaceBlock;
+ const isWorkspaceSuspended = activeLicense?.state === LicenseState.DEFAULTED;
+ const isWorkspaceAccessRestricted =
+ activeLicense?.state === LicenseState.TERMINATED ||
+ activeLicense?.state === LicenseState.EXPIRED ||
+ activeLicense?.state === LicenseState.CANCELLED;
+
+ const hasWorkspaceIssue =
+ isWorkspaceBlocked || isWorkspaceSuspended || isWorkspaceAccessRestricted;
+
+ if (hasWorkspaceIssue) {
+ return;
+ }
+
const isFirstUser = checkFirstTimeUser();
if (
isFirstUser &&
@@ -119,40 +135,36 @@ function PrivateRoute({ children }: PrivateRouteProps): JSX.Element {
orgPreferences,
usersData,
pathname,
+ trialInfo?.workSpaceBlock,
+ activeLicense?.state,
]);
- const navigateToWorkSpaceBlocked = (route: any): void => {
- const { path } = route;
-
+ const navigateToWorkSpaceBlocked = useCallback((): void => {
const isRouteEnabledForWorkspaceBlockedState =
isAdmin &&
- (path === ROUTES.SETTINGS ||
- path === ROUTES.ORG_SETTINGS ||
- path === ROUTES.MEMBERS_SETTINGS ||
- path === ROUTES.BILLING ||
- path === ROUTES.MY_SETTINGS);
+ (pathname === ROUTES.SETTINGS ||
+ pathname === ROUTES.ORG_SETTINGS ||
+ pathname === ROUTES.MEMBERS_SETTINGS ||
+ pathname === ROUTES.BILLING ||
+ pathname === ROUTES.MY_SETTINGS);
if (
- path &&
- path !== ROUTES.WORKSPACE_LOCKED &&
+ pathname &&
+ pathname !== ROUTES.WORKSPACE_LOCKED &&
!isRouteEnabledForWorkspaceBlockedState
) {
history.push(ROUTES.WORKSPACE_LOCKED);
}
- };
+ }, [isAdmin, pathname]);
- const navigateToWorkSpaceAccessRestricted = (route: any): void => {
- const { path } = route;
-
- if (path && path !== ROUTES.WORKSPACE_ACCESS_RESTRICTED) {
+ const navigateToWorkSpaceAccessRestricted = useCallback((): void => {
+ if (pathname && pathname !== ROUTES.WORKSPACE_ACCESS_RESTRICTED) {
history.push(ROUTES.WORKSPACE_ACCESS_RESTRICTED);
}
- };
+ }, [pathname]);
useEffect(() => {
if (!isFetchingActiveLicense && activeLicense) {
- const currentRoute = mapRoutes.get('current');
-
const isTerminated = activeLicense.state === LicenseState.TERMINATED;
const isExpired = activeLicense.state === LicenseState.EXPIRED;
const isCancelled = activeLicense.state === LicenseState.CANCELLED;
@@ -161,61 +173,53 @@ function PrivateRoute({ children }: PrivateRouteProps): JSX.Element {
const { platform } = activeLicense;
- if (
- isWorkspaceAccessRestricted &&
- platform === LicensePlatform.CLOUD &&
- currentRoute
- ) {
- navigateToWorkSpaceAccessRestricted(currentRoute);
+ if (isWorkspaceAccessRestricted && platform === LicensePlatform.CLOUD) {
+ navigateToWorkSpaceAccessRestricted();
}
}
- }, [isFetchingActiveLicense, activeLicense, mapRoutes, pathname]);
+ }, [
+ isFetchingActiveLicense,
+ activeLicense,
+ navigateToWorkSpaceAccessRestricted,
+ ]);
useEffect(() => {
if (!isFetchingActiveLicense) {
- const currentRoute = mapRoutes.get('current');
const shouldBlockWorkspace = trialInfo?.workSpaceBlock;
if (
shouldBlockWorkspace &&
- currentRoute &&
activeLicense?.platform === LicensePlatform.CLOUD
) {
- navigateToWorkSpaceBlocked(currentRoute);
+ navigateToWorkSpaceBlocked();
}
}
- // eslint-disable-next-line react-hooks/exhaustive-deps
}, [
isFetchingActiveLicense,
trialInfo?.workSpaceBlock,
activeLicense?.platform,
- mapRoutes,
- pathname,
+ navigateToWorkSpaceBlocked,
]);
- const navigateToWorkSpaceSuspended = (route: any): void => {
- const { path } = route;
-
- if (path && path !== ROUTES.WORKSPACE_SUSPENDED) {
+ const navigateToWorkSpaceSuspended = useCallback((): void => {
+ if (pathname && pathname !== ROUTES.WORKSPACE_SUSPENDED) {
history.push(ROUTES.WORKSPACE_SUSPENDED);
}
- };
+ }, [pathname]);
useEffect(() => {
if (!isFetchingActiveLicense && activeLicense) {
- const currentRoute = mapRoutes.get('current');
const shouldSuspendWorkspace =
activeLicense.state === LicenseState.DEFAULTED;
if (
shouldSuspendWorkspace &&
- currentRoute &&
activeLicense.platform === LicensePlatform.CLOUD
) {
- navigateToWorkSpaceSuspended(currentRoute);
+ navigateToWorkSpaceSuspended();
}
}
- }, [isFetchingActiveLicense, activeLicense, mapRoutes, pathname]);
+ }, [isFetchingActiveLicense, activeLicense, navigateToWorkSpaceSuspended]);
useEffect(() => {
if (org && org.length > 0 && org[0].id !== undefined) {
diff --git a/frontend/src/AppRoutes/__tests__/Private.test.tsx b/frontend/src/AppRoutes/__tests__/Private.test.tsx
new file mode 100644
index 0000000000..a3c010c165
--- /dev/null
+++ b/frontend/src/AppRoutes/__tests__/Private.test.tsx
@@ -0,0 +1,1531 @@
+import { ReactElement } from 'react';
+import { QueryClient, QueryClientProvider } from 'react-query';
+import { MemoryRouter, Route, Switch, useLocation } from 'react-router-dom';
+import { act, render, screen, waitFor } from '@testing-library/react';
+import { FeatureKeys } from 'constants/features';
+import { LOCALSTORAGE } from 'constants/localStorage';
+import { ORG_PREFERENCES } from 'constants/orgPreferences';
+import ROUTES from 'constants/routes';
+import history from 'lib/history';
+import { AppContext } from 'providers/App/App';
+import { IAppContext, IUser } from 'providers/App/types';
+import {
+ LicenseEvent,
+ LicensePlatform,
+ LicenseResModel,
+ LicenseState,
+ LicenseStatus,
+ TrialInfo,
+} from 'types/api/licensesV3/getActive';
+import { OrgPreference } from 'types/api/preferences/preference';
+import { ROLES, USER_ROLES } from 'types/roles';
+
+import PrivateRoute from '../Private';
+
+// Mock history module
+jest.mock('lib/history', () => ({
+ __esModule: true,
+ default: {
+ push: jest.fn(),
+ location: { pathname: '/', search: '', hash: '' },
+ listen: jest.fn(),
+ createHref: jest.fn(),
+ },
+}));
+
+const mockHistoryPush = history.push as jest.Mock;
+
+// Mock localStorage APIs
+const mockLocalStorage: Record<string, string> = {};
+jest.mock('api/browser/localstorage/get', () => ({
+ __esModule: true,
+ default: (key: string): string | null => mockLocalStorage[key] || null,
+}));
+
+jest.mock('api/browser/localstorage/set', () => ({
+ __esModule: true,
+ default: (key: string, value: string): void => {
+ mockLocalStorage[key] = value;
+ },
+}));
+
+// Mock useGetTenantLicense hook
+let mockIsCloudUser = true;
+jest.mock('hooks/useGetTenantLicense', () => ({
+ useGetTenantLicense: (): {
+ isCloudUser: boolean;
+ isEnterpriseSelfHostedUser: boolean;
+ isCommunityUser: boolean;
+ isCommunityEnterpriseUser: boolean;
+ } => ({
+ isCloudUser: mockIsCloudUser,
+ isEnterpriseSelfHostedUser: !mockIsCloudUser,
+ isCommunityUser: false,
+ isCommunityEnterpriseUser: false,
+ }),
+}));
+
+// Mock react-query for users fetch
+let mockUsersData: { email: string }[] = [];
+jest.mock('api/v1/user/get', () => ({
+ __esModule: true,
+ default: jest.fn(() => Promise.resolve({ data: mockUsersData })),
+}));
+
+const queryClient = new QueryClient({
+ defaultOptions: {
+ queries: {
+ refetchOnWindowFocus: false,
+ retry: false,
+ },
+ },
+});
+
+// Component to capture current location for assertions
+function LocationDisplay(): ReactElement {
+ const location = useLocation();
+ return <div data-testid="location-display">{location.pathname}</div>;
+}
+
+// Helper to create mock user
+function createMockUser(overrides: Partial<IUser> = {}): IUser {
+ return {
+ accessJwt: 'test-token',
+ refreshJwt: 'test-refresh-token',
+ id: 'user-id',
+ email: '[email protected]',
+ displayName: 'Test User',
+ createdAt: 1732544623,
+ organization: 'Test Org',
+ orgId: 'org-id',
+ role: USER_ROLES.ADMIN as ROLES,
+ ...overrides,
+ };
+}
+
+// Helper to create mock license
+function createMockLicense(
+ overrides: Partial<LicenseResModel> = {},
+): LicenseResModel {
+ return {
+ key: 'test-key',
+ event_queue: {
+ created_at: '0',
+ event: LicenseEvent.NO_EVENT,
+ scheduled_at: '0',
+ status: '',
+ updated_at: '0',
+ },
+ state: LicenseState.ACTIVE,
+ status: LicenseStatus.VALID,
+ platform: LicensePlatform.CLOUD,
+ created_at: '0',
+ plan: {
+ created_at: '0',
+ description: '',
+ is_active: true,
+ name: '',
+ updated_at: '0',
+ },
+ plan_id: '0',
+ free_until: '0',
+ updated_at: '0',
+ valid_from: 0,
+ valid_until: 0,
+ ...overrides,
+ };
+}
+
+// Helper to create mock trial info
+function createMockTrialInfo(overrides: Partial<TrialInfo> = {}): TrialInfo {
+ return {
+ trialStart: -1,
+ trialEnd: -1,
+ onTrial: false,
+ workSpaceBlock: false,
+ trialConvertedToSubscription: false,
+ gracePeriodEnd: -1,
+ ...overrides,
+ };
+}
+
+// Helper to create mock org preferences
+function createMockOrgPreferences(onboardingComplete = true): OrgPreference[] {
+ return [
+ {
+ name: ORG_PREFERENCES.ORG_ONBOARDING,
+ description: 'Organisation Onboarding',
+ valueType: 'boolean',
+ defaultValue: false,
+ allowedValues: ['true', 'false'],
+ allowedScopes: ['org'],
+ value: onboardingComplete,
+ },
+ ];
+}
+
+// Helper to create mock app context
+function createMockAppContext(
+ overrides: Partial<IAppContext> = {},
+): IAppContext {
+ return {
+ user: createMockUser(),
+ activeLicense: createMockLicense(),
+ trialInfo: createMockTrialInfo(),
+ featureFlags: [],
+ orgPreferences: createMockOrgPreferences(),
+ userPreferences: [],
+ isLoggedIn: true,
+ org: [{ createdAt: 0, id: 'org-id', displayName: 'Test Org' }],
+ isFetchingUser: false,
+ isFetchingActiveLicense: false,
+ isFetchingFeatureFlags: false,
+ isFetchingOrgPreferences: false,
+ userFetchError: null,
+ activeLicenseFetchError: null,
+ featureFlagsFetchError: null,
+ orgPreferencesFetchError: null,
+ changelog: null,
+ showChangelogModal: false,
+ activeLicenseRefetch: jest.fn(),
+ updateUser: jest.fn(),
+ updateOrgPreferences: jest.fn(),
+ updateUserPreferenceInContext: jest.fn(),
+ updateOrg: jest.fn(),
+ updateChangelog: jest.fn(),
+ toggleChangelogModal: jest.fn(),
+ versionData: { version: '1.0.0', ee: 'Y', setupCompleted: true },
+ hasEditPermission: true,
+ ...overrides,
+ };
+}
+
+interface RenderPrivateRouteOptions {
+ initialRoute?: string;
+ appContext?: Partial<IAppContext>;
+ isCloudUser?: boolean;
+}
+
+function renderPrivateRoute(options: RenderPrivateRouteOptions = {}): void {
+ const {
+ initialRoute = ROUTES.HOME,
+ appContext = {},
+ isCloudUser = true,
+ } = options;
+
+ mockIsCloudUser = isCloudUser;
+
+ const contextValue = createMockAppContext(appContext);
+
+ render(
+ <QueryClientProvider client={queryClient}>
+ <MemoryRouter initialEntries={[initialRoute]}>
+ <AppContext.Provider value={contextValue}>
+ <PrivateRoute>
+ <Switch>
+ <Route path="*">
+ <div data-testid="children-rendered">Content</div>
+ <LocationDisplay />
+ </Route>
+ </Switch>
+ </PrivateRoute>
+ </AppContext.Provider>
+ </MemoryRouter>
+ </QueryClientProvider>,
+ );
+}
+
+// Generic assertion helpers for navigation behavior
+// Using these allows easier refactoring when switching from history.push to Redirect component
+
+async function assertRedirectsTo(targetRoute: string): Promise<void> {
+ await waitFor(() => {
+ expect(mockHistoryPush).toHaveBeenCalledWith(targetRoute);
+ });
+}
+
+function assertNoRedirect(): void {
+ expect(mockHistoryPush).not.toHaveBeenCalled();
+}
+
+function assertDoesNotRedirectTo(targetRoute: string): void {
+ expect(mockHistoryPush).not.toHaveBeenCalledWith(targetRoute);
+}
+
+function assertRendersChildren(): void {
+ expect(screen.getByTestId('children-rendered')).toBeInTheDocument();
+}
+
+describe('PrivateRoute', () => {
+ beforeEach(() => {
+ jest.clearAllMocks();
+ queryClient.clear();
+ Object.keys(mockLocalStorage).forEach((key) => delete mockLocalStorage[key]);
+ mockIsCloudUser = true;
+ mockUsersData = [];
+ });
+
+ describe('Old Routes Handling', () => {
+ it('should redirect /pipelines to /logs/pipelines preserving search params', () => {
+ renderPrivateRoute({ initialRoute: '/pipelines?foo=bar' });
+
+ expect(screen.getByTestId('location-display')).toHaveTextContent(
+ '/logs/pipelines',
+ );
+ });
+
+ it('should redirect /logs-explorer to /logs/logs-explorer', () => {
+ renderPrivateRoute({ initialRoute: '/logs-explorer' });
+
+ expect(screen.getByTestId('location-display')).toHaveTextContent(
+ '/logs/logs-explorer',
+ );
+ });
+
+ it('should redirect /logs-explorer/live to /logs/logs-explorer/live', () => {
+ renderPrivateRoute({ initialRoute: '/logs-explorer/live' });
+
+ expect(screen.getByTestId('location-display')).toHaveTextContent(
+ '/logs/logs-explorer/live',
+ );
+ });
+
+ it('should redirect /logs-save-views to /logs/saved-views', () => {
+ renderPrivateRoute({ initialRoute: '/logs-save-views' });
+
+ expect(screen.getByTestId('location-display')).toHaveTextContent(
+ '/logs/saved-views',
+ );
+ });
+
+ it('should redirect /traces-save-views to /traces/saved-views', () => {
+ renderPrivateRoute({ initialRoute: '/traces-save-views' });
+
+ expect(screen.getByTestId('location-display')).toHaveTextContent(
+ '/traces/saved-views',
+ );
+ });
+
+ it('should redirect /settings/access-tokens to /settings/api-keys', () => {
+ renderPrivateRoute({ initialRoute: '/settings/access-tokens' });
+
+ expect(screen.getByTestId('location-display')).toHaveTextContent(
+ '/settings/api-keys',
+ );
+ });
+
+ it('should redirect /messaging-queues to /messaging-queues/overview', () => {
+ renderPrivateRoute({ initialRoute: '/messaging-queues' });
+
+ expect(screen.getByTestId('location-display')).toHaveTextContent(
+ '/messaging-queues/overview',
+ );
+ });
+
+ it('should redirect /alerts/edit to /alerts/overview', () => {
+ renderPrivateRoute({ initialRoute: '/alerts/edit' });
+
+ expect(screen.getByTestId('location-display')).toHaveTextContent(
+ '/alerts/overview',
+ );
+ });
+ });
+
+ describe('Public Dashboard Route', () => {
+ it('should render children for public dashboard route without redirecting when not logged in', () => {
+ renderPrivateRoute({
+ initialRoute: '/public/dashboard/abc123',
+ appContext: { isLoggedIn: false },
+ });
+
+ assertRendersChildren();
+ assertNoRedirect();
+ });
+
+ it('should render children for public dashboard route when logged in without redirecting', () => {
+ renderPrivateRoute({
+ initialRoute: '/public/dashboard/abc123',
+ appContext: { isLoggedIn: true },
+ });
+
+ assertRendersChildren();
+ // Critical: without the isPublicDashboard early return, logged-in users
+ // would be redirected to HOME due to the non-private route handling
+ assertNoRedirect();
+ });
+ });
+
+ describe('Private Routes - User Not Logged In', () => {
+ it('should redirect to login when accessing private route without authentication', async () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.HOME,
+ appContext: { isLoggedIn: false },
+ });
+
+ await assertRedirectsTo(ROUTES.LOGIN);
+ });
+
+ it('should save current pathname to localStorage before redirecting to login', async () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.ALL_DASHBOARD,
+ appContext: { isLoggedIn: false },
+ });
+
+ await waitFor(() => {
+ expect(mockLocalStorage[LOCALSTORAGE.UNAUTHENTICATED_ROUTE_HIT]).toBe(
+ ROUTES.ALL_DASHBOARD,
+ );
+ });
+ await assertRedirectsTo(ROUTES.LOGIN);
+ });
+
+ it('should redirect to login for /services route when not logged in', async () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.APPLICATION,
+ appContext: { isLoggedIn: false },
+ });
+
+ await assertRedirectsTo(ROUTES.LOGIN);
+ });
+
+ it('should redirect to login for /alerts route when not logged in', async () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.LIST_ALL_ALERT,
+ appContext: { isLoggedIn: false },
+ });
+
+ await assertRedirectsTo(ROUTES.LOGIN);
+ });
+ });
+
+ describe('Private Routes - User Logged In', () => {
+ it('should render children when logged in user has correct permissions for route', () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.HOME,
+ appContext: {
+ isLoggedIn: true,
+ user: createMockUser({ role: USER_ROLES.ADMIN as ROLES }),
+ },
+ });
+
+ assertRendersChildren();
+ assertNoRedirect();
+ });
+
+ it('should redirect to unauthorized when VIEWER tries to access admin-only route /alerts/new', async () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.ALERTS_NEW,
+ appContext: {
+ isLoggedIn: true,
+ user: createMockUser({ role: USER_ROLES.VIEWER as ROLES }),
+ },
+ });
+
+ await assertRedirectsTo(ROUTES.UN_AUTHORIZED);
+ });
+
+ it('should not redirect VIEWER from /settings/org-settings since it matches SETTINGS route which allows all roles', () => {
+ // Note: ORG_SETTINGS is not a standalone route in routes.ts
+ // It's handled by the SETTINGS route (exact: false) which allows all roles
+ // The actual permission check happens inside SettingsPage component
+ renderPrivateRoute({
+ initialRoute: ROUTES.ORG_SETTINGS,
+ appContext: {
+ isLoggedIn: true,
+ user: createMockUser({ role: USER_ROLES.VIEWER as ROLES }),
+ },
+ });
+
+ // VIEWER can access since SETTINGS route allows all roles
+ assertRendersChildren();
+ });
+
+ it('should allow ADMIN to access /settings/org-settings', () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.ORG_SETTINGS,
+ appContext: {
+ isLoggedIn: true,
+ user: createMockUser({ role: USER_ROLES.ADMIN as ROLES }),
+ },
+ });
+
+ assertRendersChildren();
+ });
+
+ it('should allow EDITOR to access /alerts/new route', () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.ALERTS_NEW,
+ appContext: {
+ isLoggedIn: true,
+ user: createMockUser({ role: USER_ROLES.EDITOR as ROLES }),
+ },
+ });
+
+ assertRendersChildren();
+ });
+
+ it('should allow VIEWER to access /dashboard route', () => {
+ renderPrivateRoute({
+ initialRoute: '/dashboard/test-id',
+ appContext: {
+ isLoggedIn: true,
+ user: createMockUser({ role: USER_ROLES.VIEWER as ROLES }),
+ },
+ });
+
+ assertRendersChildren();
+ });
+ });
+
+ describe('Non-Private Routes - User Logged In', () => {
+ it('should redirect to saved route from localStorage when logging in', async () => {
+ mockLocalStorage[LOCALSTORAGE.UNAUTHENTICATED_ROUTE_HIT] =
+ ROUTES.ALL_DASHBOARD;
+
+ renderPrivateRoute({
+ initialRoute: ROUTES.LOGIN,
+ appContext: { isLoggedIn: true },
+ });
+
+ await assertRedirectsTo(ROUTES.ALL_DASHBOARD);
+ });
+
+ it('should clear localStorage after redirecting to saved route', async () => {
+ mockLocalStorage[LOCALSTORAGE.UNAUTHENTICATED_ROUTE_HIT] = ROUTES.HOME;
+
+ renderPrivateRoute({
+ initialRoute: ROUTES.LOGIN,
+ appContext: { isLoggedIn: true },
+ });
+
+ await waitFor(() => {
+ expect(mockLocalStorage[LOCALSTORAGE.UNAUTHENTICATED_ROUTE_HIT]).toBe('');
+ });
+ });
+
+ it('should redirect to home when logged in user visits login page with no saved route', async () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.LOGIN,
+ appContext: { isLoggedIn: true },
+ });
+
+ await assertRedirectsTo(ROUTES.HOME);
+ });
+
+ it('should not redirect when on /something-went-wrong page even when logged in', () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.SOMETHING_WENT_WRONG,
+ appContext: { isLoggedIn: true },
+ });
+
+ assertDoesNotRedirectTo(ROUTES.HOME);
+ });
+ });
+
+ describe('Non-Private Routes - User Not Logged In', () => {
+ it('should not redirect when not logged in user visits login page', () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.LOGIN,
+ appContext: { isLoggedIn: false },
+ });
+
+ // Should not redirect - login page handles its own routing
+ assertNoRedirect();
+ });
+
+ it('should not redirect when not logged in user visits signup page', () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.SIGN_UP,
+ appContext: { isLoggedIn: false },
+ });
+
+ assertNoRedirect();
+ });
+
+ it('should not redirect when not logged in user visits password reset page', () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.PASSWORD_RESET,
+ appContext: { isLoggedIn: false },
+ });
+
+ assertNoRedirect();
+ });
+
+ it('should not redirect when not logged in user visits forgot password page', () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.FORGOT_PASSWORD,
+ appContext: { isLoggedIn: false },
+ });
+
+ assertNoRedirect();
+ });
+ });
+
+ describe('Unknown Routes', () => {
+ it('should redirect to home when logged in and visiting unknown route', async () => {
+ renderPrivateRoute({
+ initialRoute: '/unknown-route-that-does-not-exist',
+ appContext: { isLoggedIn: true },
+ });
+
+ await assertRedirectsTo(ROUTES.HOME);
+ });
+
+ it('should redirect to login when not logged in and visiting unknown route', async () => {
+ renderPrivateRoute({
+ initialRoute: '/unknown-route-that-does-not-exist',
+ appContext: { isLoggedIn: false },
+ });
+
+ await assertRedirectsTo(ROUTES.LOGIN);
+ });
+
+ it('should save unknown route to localStorage before redirecting to login', async () => {
+ const unknownRoute = '/some-unknown-page';
+
+ renderPrivateRoute({
+ initialRoute: unknownRoute,
+ appContext: { isLoggedIn: false },
+ });
+
+ await waitFor(() => {
+ expect(mockLocalStorage[LOCALSTORAGE.UNAUTHENTICATED_ROUTE_HIT]).toBe(
+ unknownRoute,
+ );
+ });
+ });
+
+ it('should redirect to saved route when logged in user visits unknown route with fromPathname in localStorage', async () => {
+ // This tests the branch where:
+ // - currentRoute is null (unknown route)
+ // - isLoggedInState is true
+ // - fromPathname exists in localStorage
+ // Expected: redirect to savedRoute and clear localStorage
+ const savedRoute = ROUTES.ALL_DASHBOARD;
+ mockLocalStorage[LOCALSTORAGE.UNAUTHENTICATED_ROUTE_HIT] = savedRoute;
+
+ renderPrivateRoute({
+ initialRoute: '/unknown-route-that-does-not-exist',
+ appContext: { isLoggedIn: true },
+ });
+
+ await assertRedirectsTo(savedRoute);
+ await waitFor(() => {
+ expect(mockLocalStorage[LOCALSTORAGE.UNAUTHENTICATED_ROUTE_HIT]).toBe('');
+ });
+ });
+ });
+
+ describe('Workspace Blocked - Trial Expired (Cloud Users)', () => {
+ it('should redirect to workspace locked when workSpaceBlock is true for cloud users', async () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.HOME,
+ appContext: {
+ isLoggedIn: true,
+ isFetchingActiveLicense: false,
+ activeLicense: createMockLicense({ platform: LicensePlatform.CLOUD }),
+ trialInfo: createMockTrialInfo({ workSpaceBlock: true }),
+ },
+ isCloudUser: true,
+ });
+
+ await assertRedirectsTo(ROUTES.WORKSPACE_LOCKED);
+ });
+
+ it('should allow ADMIN to access /settings when workspace is blocked', () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.SETTINGS,
+ appContext: {
+ isLoggedIn: true,
+ isFetchingActiveLicense: false,
+ activeLicense: createMockLicense({ platform: LicensePlatform.CLOUD }),
+ trialInfo: createMockTrialInfo({ workSpaceBlock: true }),
+ user: createMockUser({ role: USER_ROLES.ADMIN as ROLES }),
+ },
+ isCloudUser: true,
+ });
+
+ // Admin should be able to access settings even when workspace is blocked
+ assertDoesNotRedirectTo(ROUTES.WORKSPACE_LOCKED);
+ });
+
+ it('should allow ADMIN to access /settings/billing when workspace is blocked', () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.BILLING,
+ appContext: {
+ isLoggedIn: true,
+ isFetchingActiveLicense: false,
+ activeLicense: createMockLicense({ platform: LicensePlatform.CLOUD }),
+ trialInfo: createMockTrialInfo({ workSpaceBlock: true }),
+ user: createMockUser({ role: USER_ROLES.ADMIN as ROLES }),
+ },
+ isCloudUser: true,
+ });
+
+ assertDoesNotRedirectTo(ROUTES.WORKSPACE_LOCKED);
+ });
+
+ it('should allow ADMIN to access /settings/org-settings when workspace is blocked', () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.ORG_SETTINGS,
+ appContext: {
+ isLoggedIn: true,
+ isFetchingActiveLicense: false,
+ activeLicense: createMockLicense({ platform: LicensePlatform.CLOUD }),
+ trialInfo: createMockTrialInfo({ workSpaceBlock: true }),
+ user: createMockUser({ role: USER_ROLES.ADMIN as ROLES }),
+ },
+ isCloudUser: true,
+ });
+
+ assertDoesNotRedirectTo(ROUTES.WORKSPACE_LOCKED);
+ });
+
+ it('should allow ADMIN to access /settings/members when workspace is blocked', () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.MEMBERS_SETTINGS,
+ appContext: {
+ isLoggedIn: true,
+ isFetchingActiveLicense: false,
+ activeLicense: createMockLicense({ platform: LicensePlatform.CLOUD }),
+ trialInfo: createMockTrialInfo({ workSpaceBlock: true }),
+ user: createMockUser({ role: USER_ROLES.ADMIN as ROLES }),
+ },
+ isCloudUser: true,
+ });
+
+ assertDoesNotRedirectTo(ROUTES.WORKSPACE_LOCKED);
+ });
+
+ it('should allow ADMIN to access /settings/my-settings when workspace is blocked', () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.MY_SETTINGS,
+ appContext: {
+ isLoggedIn: true,
+ isFetchingActiveLicense: false,
+ activeLicense: createMockLicense({ platform: LicensePlatform.CLOUD }),
+ trialInfo: createMockTrialInfo({ workSpaceBlock: true }),
+ user: createMockUser({ role: USER_ROLES.ADMIN as ROLES }),
+ },
+ isCloudUser: true,
+ });
+
+ assertDoesNotRedirectTo(ROUTES.WORKSPACE_LOCKED);
+ });
+
+ it('should redirect VIEWER to workspace locked even when trying to access settings', async () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.SETTINGS,
+ appContext: {
+ isLoggedIn: true,
+ isFetchingActiveLicense: false,
+ activeLicense: createMockLicense({ platform: LicensePlatform.CLOUD }),
+ trialInfo: createMockTrialInfo({ workSpaceBlock: true }),
+ user: createMockUser({ role: USER_ROLES.VIEWER as ROLES }),
+ },
+ isCloudUser: true,
+ });
+
+ await assertRedirectsTo(ROUTES.WORKSPACE_LOCKED);
+ });
+
+ it('should redirect VIEWER to workspace locked when trying to access billing', async () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.BILLING,
+ appContext: {
+ isLoggedIn: true,
+ isFetchingActiveLicense: false,
+ activeLicense: createMockLicense({ platform: LicensePlatform.CLOUD }),
+ trialInfo: createMockTrialInfo({ workSpaceBlock: true }),
+ user: createMockUser({ role: USER_ROLES.VIEWER as ROLES }),
+ },
+ isCloudUser: true,
+ });
+
+ await assertRedirectsTo(ROUTES.WORKSPACE_LOCKED);
+ });
+
+ it('should redirect VIEWER to workspace locked when trying to access org-settings', async () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.ORG_SETTINGS,
+ appContext: {
+ isLoggedIn: true,
+ isFetchingActiveLicense: false,
+ activeLicense: createMockLicense({ platform: LicensePlatform.CLOUD }),
+ trialInfo: createMockTrialInfo({ workSpaceBlock: true }),
+ user: createMockUser({ role: USER_ROLES.VIEWER as ROLES }),
+ },
+ isCloudUser: true,
+ });
+
+ await assertRedirectsTo(ROUTES.WORKSPACE_LOCKED);
+ });
+
+ it('should redirect VIEWER to workspace locked when trying to access members settings', async () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.MEMBERS_SETTINGS,
+ appContext: {
+ isLoggedIn: true,
+ isFetchingActiveLicense: false,
+ activeLicense: createMockLicense({ platform: LicensePlatform.CLOUD }),
+ trialInfo: createMockTrialInfo({ workSpaceBlock: true }),
+ user: createMockUser({ role: USER_ROLES.VIEWER as ROLES }),
+ },
+ isCloudUser: true,
+ });
+
+ await assertRedirectsTo(ROUTES.WORKSPACE_LOCKED);
+ });
+
+ it('should redirect VIEWER to workspace locked when trying to access my-settings', async () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.MY_SETTINGS,
+ appContext: {
+ isLoggedIn: true,
+ isFetchingActiveLicense: false,
+ activeLicense: createMockLicense({ platform: LicensePlatform.CLOUD }),
+ trialInfo: createMockTrialInfo({ workSpaceBlock: true }),
+ user: createMockUser({ role: USER_ROLES.VIEWER as ROLES }),
+ },
+ isCloudUser: true,
+ });
+
+ await assertRedirectsTo(ROUTES.WORKSPACE_LOCKED);
+ });
+
+ it('should redirect EDITOR to workspace locked when trying to access settings', async () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.SETTINGS,
+ appContext: {
+ isLoggedIn: true,
+ isFetchingActiveLicense: false,
+ activeLicense: createMockLicense({ platform: LicensePlatform.CLOUD }),
+ trialInfo: createMockTrialInfo({ workSpaceBlock: true }),
+ user: createMockUser({ role: USER_ROLES.EDITOR as ROLES }),
+ },
+ isCloudUser: true,
+ });
+
+ await assertRedirectsTo(ROUTES.WORKSPACE_LOCKED);
+ });
+
+ it('should not redirect when already on workspace locked page', () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.WORKSPACE_LOCKED,
+ appContext: {
+ isLoggedIn: true,
+ isFetchingActiveLicense: false,
+ activeLicense: createMockLicense({ platform: LicensePlatform.CLOUD }),
+ trialInfo: createMockTrialInfo({ workSpaceBlock: true }),
+ },
+ isCloudUser: true,
+ });
+
+ assertDoesNotRedirectTo(ROUTES.WORKSPACE_LOCKED);
+ });
+
+ it('should not redirect self-hosted users to workspace locked even when workSpaceBlock is true', () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.HOME,
+ appContext: {
+ isLoggedIn: true,
+ isFetchingActiveLicense: false,
+ activeLicense: createMockLicense({
+ platform: LicensePlatform.SELF_HOSTED,
+ }),
+ trialInfo: createMockTrialInfo({ workSpaceBlock: true }),
+ },
+ isCloudUser: false,
+ });
+
+ assertDoesNotRedirectTo(ROUTES.WORKSPACE_LOCKED);
+ });
+ });
+
+ describe('Workspace Access Restricted - License Terminated/Expired/Cancelled (Cloud Users)', () => {
+ it('should redirect to workspace access restricted when license is TERMINATED', async () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.HOME,
+ appContext: {
+ isLoggedIn: true,
+ isFetchingActiveLicense: false,
+ activeLicense: createMockLicense({
+ platform: LicensePlatform.CLOUD,
+ state: LicenseState.TERMINATED,
+ }),
+ },
+ isCloudUser: true,
+ });
+
+ await assertRedirectsTo(ROUTES.WORKSPACE_ACCESS_RESTRICTED);
+ });
+
+ it('should redirect to workspace access restricted when license is EXPIRED', async () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.HOME,
+ appContext: {
+ isLoggedIn: true,
+ isFetchingActiveLicense: false,
+ activeLicense: createMockLicense({
+ platform: LicensePlatform.CLOUD,
+ state: LicenseState.EXPIRED,
+ }),
+ },
+ isCloudUser: true,
+ });
+
+ await assertRedirectsTo(ROUTES.WORKSPACE_ACCESS_RESTRICTED);
+ });
+
+ it('should redirect to workspace access restricted when license is CANCELLED', async () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.HOME,
+ appContext: {
+ isLoggedIn: true,
+ isFetchingActiveLicense: false,
+ activeLicense: createMockLicense({
+ platform: LicensePlatform.CLOUD,
+ state: LicenseState.CANCELLED,
+ }),
+ },
+ isCloudUser: true,
+ });
+
+ await assertRedirectsTo(ROUTES.WORKSPACE_ACCESS_RESTRICTED);
+ });
+
+ it('should not redirect when already on workspace access restricted page', () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.WORKSPACE_ACCESS_RESTRICTED,
+ appContext: {
+ isLoggedIn: true,
+ isFetchingActiveLicense: false,
+ activeLicense: createMockLicense({
+ platform: LicensePlatform.CLOUD,
+ state: LicenseState.TERMINATED,
+ }),
+ },
+ isCloudUser: true,
+ });
+
+ assertDoesNotRedirectTo(ROUTES.WORKSPACE_ACCESS_RESTRICTED);
+ });
+
+ it('should not redirect self-hosted users to workspace access restricted when license is terminated', () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.HOME,
+ appContext: {
+ isLoggedIn: true,
+ isFetchingActiveLicense: false,
+ activeLicense: createMockLicense({
+ platform: LicensePlatform.SELF_HOSTED,
+ state: LicenseState.TERMINATED,
+ }),
+ },
+ isCloudUser: false,
+ });
+
+ assertDoesNotRedirectTo(ROUTES.WORKSPACE_ACCESS_RESTRICTED);
+ });
+
+ it('should not redirect when license is ACTIVE', () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.HOME,
+ appContext: {
+ isLoggedIn: true,
+ isFetchingActiveLicense: false,
+ activeLicense: createMockLicense({
+ platform: LicensePlatform.CLOUD,
+ state: LicenseState.ACTIVE,
+ }),
+ },
+ isCloudUser: true,
+ });
+
+ assertDoesNotRedirectTo(ROUTES.WORKSPACE_ACCESS_RESTRICTED);
+ });
+
+ it('should not redirect when license is EVALUATING', () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.HOME,
+ appContext: {
+ isLoggedIn: true,
+ isFetchingActiveLicense: false,
+ activeLicense: createMockLicense({
+ platform: LicensePlatform.CLOUD,
+ state: LicenseState.EVALUATING,
+ }),
+ },
+ isCloudUser: true,
+ });
+
+ assertDoesNotRedirectTo(ROUTES.WORKSPACE_ACCESS_RESTRICTED);
+ });
+ });
+
+ describe('Workspace Suspended - License Defaulted (Cloud Users)', () => {
+ it('should redirect to workspace suspended when license is DEFAULTED', async () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.HOME,
+ appContext: {
+ isLoggedIn: true,
+ isFetchingActiveLicense: false,
+ activeLicense: createMockLicense({
+ platform: LicensePlatform.CLOUD,
+ state: LicenseState.DEFAULTED,
+ }),
+ },
+ isCloudUser: true,
+ });
+
+ await assertRedirectsTo(ROUTES.WORKSPACE_SUSPENDED);
+ });
+
+ it('should not redirect when already on workspace suspended page', () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.WORKSPACE_SUSPENDED,
+ appContext: {
+ isLoggedIn: true,
+ isFetchingActiveLicense: false,
+ activeLicense: createMockLicense({
+ platform: LicensePlatform.CLOUD,
+ state: LicenseState.DEFAULTED,
+ }),
+ },
+ isCloudUser: true,
+ });
+
+ assertDoesNotRedirectTo(ROUTES.WORKSPACE_SUSPENDED);
+ });
+
+ it('should not redirect self-hosted users to workspace suspended when license is defaulted', () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.HOME,
+ appContext: {
+ isLoggedIn: true,
+ isFetchingActiveLicense: false,
+ activeLicense: createMockLicense({
+ platform: LicensePlatform.SELF_HOSTED,
+ state: LicenseState.DEFAULTED,
+ }),
+ },
+ isCloudUser: false,
+ });
+
+ assertDoesNotRedirectTo(ROUTES.WORKSPACE_SUSPENDED);
+ });
+ });
+
+ describe('Onboarding Flow (Cloud Users)', () => {
+ it('should redirect to onboarding when first user has not completed onboarding', async () => {
+ // Set up exactly one user (not [email protected]) to trigger first user check
+ mockUsersData = [{ email: '[email protected]' }];
+
+ renderPrivateRoute({
+ initialRoute: ROUTES.HOME,
+ appContext: {
+ isLoggedIn: true,
+ isFetchingOrgPreferences: false,
+ orgPreferences: createMockOrgPreferences(false), // Onboarding NOT complete
+ user: createMockUser({ role: USER_ROLES.ADMIN as ROLES }),
+ },
+ isCloudUser: true,
+ });
+
+ await assertRedirectsTo(ROUTES.ONBOARDING);
+ });
+
+ it('should not redirect to onboarding when org preferences are still fetching', () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.HOME,
+ appContext: {
+ isLoggedIn: true,
+ isFetchingOrgPreferences: true,
+ orgPreferences: null,
+ user: createMockUser({ role: USER_ROLES.ADMIN as ROLES }),
+ },
+ isCloudUser: true,
+ });
+
+ assertDoesNotRedirectTo(ROUTES.ONBOARDING);
+ });
+
+ it('should not redirect to onboarding when onboarding is already complete', async () => {
+ // Set up first user condition - this ensures the ONLY reason we don't redirect
+ // is because isOnboardingComplete is true
+ mockUsersData = [{ email: '[email protected]' }];
+
+ renderPrivateRoute({
+ initialRoute: ROUTES.HOME,
+ appContext: {
+ isLoggedIn: true,
+ isFetchingOrgPreferences: false,
+ orgPreferences: createMockOrgPreferences(true), // Onboarding complete
+ user: createMockUser({ role: USER_ROLES.ADMIN as ROLES }),
+ },
+ isCloudUser: true,
+ });
+
+ // Wait for async operations (useQuery, useEffect) to settle
+ await act(async () => {
+ await Promise.resolve();
+ });
+
+ // Critical: if isOnboardingComplete check is broken (always false),
+ // this test would fail because all other conditions for redirect ARE met
+ assertDoesNotRedirectTo(ROUTES.ONBOARDING);
+ });
+
+ it('should not redirect to onboarding for non-cloud users', () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.HOME,
+ appContext: {
+ isLoggedIn: true,
+ isFetchingOrgPreferences: false,
+ orgPreferences: createMockOrgPreferences(false), // Onboarding not complete
+ user: createMockUser({ role: USER_ROLES.ADMIN as ROLES }),
+ },
+ isCloudUser: false,
+ });
+
+ assertDoesNotRedirectTo(ROUTES.ONBOARDING);
+ });
+
+ it('should not redirect to onboarding when on /workspace-locked route', () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.WORKSPACE_LOCKED,
+ appContext: {
+ isLoggedIn: true,
+ isFetchingOrgPreferences: false,
+ orgPreferences: createMockOrgPreferences(false),
+ user: createMockUser({ role: USER_ROLES.ADMIN as ROLES }),
+ },
+ isCloudUser: true,
+ });
+
+ assertDoesNotRedirectTo(ROUTES.ONBOARDING);
+ });
+
+ it('should not redirect to onboarding when on /workspace-suspended route', () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.WORKSPACE_SUSPENDED,
+ appContext: {
+ isLoggedIn: true,
+ isFetchingOrgPreferences: false,
+ orgPreferences: createMockOrgPreferences(false),
+ user: createMockUser({ role: USER_ROLES.ADMIN as ROLES }),
+ },
+ isCloudUser: true,
+ });
+
+ assertDoesNotRedirectTo(ROUTES.ONBOARDING);
+ });
+
+ it('should not redirect to onboarding when workspace is blocked and accessing billing', async () => {
+ // This tests the scenario where admin tries to access billing to fix payment
+ // while workspace is blocked and onboarding is not complete
+ mockUsersData = [{ email: '[email protected]' }];
+
+ renderPrivateRoute({
+ initialRoute: ROUTES.BILLING,
+ appContext: {
+ isLoggedIn: true,
+ isFetchingOrgPreferences: false,
+ isFetchingActiveLicense: false,
+ orgPreferences: createMockOrgPreferences(false), // Onboarding NOT complete
+ activeLicense: createMockLicense({ platform: LicensePlatform.CLOUD }),
+ trialInfo: createMockTrialInfo({ workSpaceBlock: true }),
+ user: createMockUser({ role: USER_ROLES.ADMIN as ROLES }),
+ },
+ isCloudUser: true,
+ });
+
+ await act(async () => {
+ await Promise.resolve();
+ });
+
+ // Should NOT redirect to onboarding - user needs to access billing to fix payment
+ assertDoesNotRedirectTo(ROUTES.ONBOARDING);
+ });
+
+ it('should not redirect to onboarding when workspace is blocked and accessing settings', async () => {
+ mockUsersData = [{ email: '[email protected]' }];
+
+ renderPrivateRoute({
+ initialRoute: ROUTES.SETTINGS,
+ appContext: {
+ isLoggedIn: true,
+ isFetchingOrgPreferences: false,
+ isFetchingActiveLicense: false,
+ orgPreferences: createMockOrgPreferences(false),
+ activeLicense: createMockLicense({ platform: LicensePlatform.CLOUD }),
+ trialInfo: createMockTrialInfo({ workSpaceBlock: true }),
+ user: createMockUser({ role: USER_ROLES.ADMIN as ROLES }),
+ },
+ isCloudUser: true,
+ });
+
+ await act(async () => {
+ await Promise.resolve();
+ });
+
+ assertDoesNotRedirectTo(ROUTES.ONBOARDING);
+ });
+
+ it('should not redirect to onboarding when workspace is suspended (DEFAULTED)', async () => {
+ mockUsersData = [{ email: '[email protected]' }];
+
+ renderPrivateRoute({
+ initialRoute: ROUTES.HOME,
+ appContext: {
+ isLoggedIn: true,
+ isFetchingOrgPreferences: false,
+ isFetchingActiveLicense: false,
+ orgPreferences: createMockOrgPreferences(false), // Onboarding NOT complete
+ activeLicense: createMockLicense({
+ platform: LicensePlatform.CLOUD,
+ state: LicenseState.DEFAULTED,
+ }),
+ user: createMockUser({ role: USER_ROLES.ADMIN as ROLES }),
+ },
+ isCloudUser: true,
+ });
+
+ await act(async () => {
+ await Promise.resolve();
+ });
+
+ // Should redirect to WORKSPACE_SUSPENDED, not ONBOARDING
+ assertDoesNotRedirectTo(ROUTES.ONBOARDING);
+ });
+
+ it('should not redirect to onboarding when workspace is access restricted (TERMINATED)', async () => {
+ mockUsersData = [{ email: '[email protected]' }];
+
+ renderPrivateRoute({
+ initialRoute: ROUTES.HOME,
+ appContext: {
+ isLoggedIn: true,
+ isFetchingOrgPreferences: false,
+ isFetchingActiveLicense: false,
+ orgPreferences: createMockOrgPreferences(false), // Onboarding NOT complete
+ activeLicense: createMockLicense({
+ platform: LicensePlatform.CLOUD,
+ state: LicenseState.TERMINATED,
+ }),
+ user: createMockUser({ role: USER_ROLES.ADMIN as ROLES }),
+ },
+ isCloudUser: true,
+ });
+
+ await act(async () => {
+ await Promise.resolve();
+ });
+
+ // Should redirect to WORKSPACE_ACCESS_RESTRICTED, not ONBOARDING
+ assertDoesNotRedirectTo(ROUTES.ONBOARDING);
+ });
+
+ it('should not redirect to onboarding when workspace is access restricted (EXPIRED)', async () => {
+ mockUsersData = [{ email: '[email protected]' }];
+
+ renderPrivateRoute({
+ initialRoute: ROUTES.HOME,
+ appContext: {
+ isLoggedIn: true,
+ isFetchingOrgPreferences: false,
+ isFetchingActiveLicense: false,
+ orgPreferences: createMockOrgPreferences(false),
+ activeLicense: createMockLicense({
+ platform: LicensePlatform.CLOUD,
+ state: LicenseState.EXPIRED,
+ }),
+ user: createMockUser({ role: USER_ROLES.ADMIN as ROLES }),
+ },
+ isCloudUser: true,
+ });
+
+ await act(async () => {
+ await Promise.resolve();
+ });
+
+ assertDoesNotRedirectTo(ROUTES.ONBOARDING);
+ });
+ });
+
+ describe('Get Started Route Redirect', () => {
+ it('should redirect to GET_STARTED_WITH_CLOUD when on GET_STARTED and ONBOARDING_V3 feature flag is active', async () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.GET_STARTED,
+ appContext: {
+ isLoggedIn: true,
+ featureFlags: [
+ {
+ name: FeatureKeys.ONBOARDING_V3,
+ active: true,
+ usage: 0,
+ usage_limit: -1,
+ route: '',
+ },
+ ],
+ },
+ });
+
+ await assertRedirectsTo(ROUTES.GET_STARTED_WITH_CLOUD);
+ });
+
+ it('should not redirect when on GET_STARTED and ONBOARDING_V3 feature flag is inactive', () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.GET_STARTED,
+ appContext: {
+ isLoggedIn: true,
+ featureFlags: [
+ {
+ name: FeatureKeys.ONBOARDING_V3,
+ active: false,
+ usage: 0,
+ usage_limit: -1,
+ route: '',
+ },
+ ],
+ },
+ });
+
+ assertDoesNotRedirectTo(ROUTES.GET_STARTED_WITH_CLOUD);
+ });
+
+ it('should not redirect when on GET_STARTED and ONBOARDING_V3 feature flag is not present', () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.GET_STARTED,
+ appContext: {
+ isLoggedIn: true,
+ featureFlags: [],
+ },
+ });
+
+ assertDoesNotRedirectTo(ROUTES.GET_STARTED_WITH_CLOUD);
+ });
+
+ it('should not redirect when on different route even if ONBOARDING_V3 is active', () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.HOME,
+ appContext: {
+ isLoggedIn: true,
+ featureFlags: [
+ {
+ name: FeatureKeys.ONBOARDING_V3,
+ active: true,
+ usage: 0,
+ usage_limit: -1,
+ route: '',
+ },
+ ],
+ },
+ });
+
+ assertDoesNotRedirectTo(ROUTES.GET_STARTED_WITH_CLOUD);
+ });
+ });
+
+ describe('Loading States', () => {
+ it('should not redirect while license is still being fetched', () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.HOME,
+ appContext: {
+ isLoggedIn: true,
+ isFetchingActiveLicense: true,
+ activeLicense: null,
+ trialInfo: createMockTrialInfo({ workSpaceBlock: true }),
+ },
+ });
+
+ assertDoesNotRedirectTo(ROUTES.WORKSPACE_LOCKED);
+ });
+
+ it('should not fetch users when org data is not available', () => {
+ // This tests the queryFn branch where orgData is undefined
+ // so the users fetch returns undefined instead of calling getAll()
+ renderPrivateRoute({
+ initialRoute: ROUTES.HOME,
+ appContext: {
+ isLoggedIn: true,
+ org: [], // Empty org array means orgData won't be set
+ user: createMockUser({ role: USER_ROLES.ADMIN as ROLES }),
+ },
+ });
+
+ assertRendersChildren();
+ });
+
+ it('should not fetch users when org id is undefined', () => {
+ // This tests the queryFn branch where orgData exists but id is undefined
+ renderPrivateRoute({
+ initialRoute: ROUTES.HOME,
+ appContext: {
+ isLoggedIn: true,
+ // @ts-expect-error - intentionally passing undefined id to test edge case
+ org: [{ createdAt: 0, id: undefined, displayName: 'Test Org' }],
+ user: createMockUser({ role: USER_ROLES.ADMIN as ROLES }),
+ },
+ });
+
+ assertRendersChildren();
+ });
+
+ it('should not check workspace states when activeLicense is null', () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.HOME,
+ appContext: {
+ isLoggedIn: true,
+ isFetchingActiveLicense: false,
+ activeLicense: null,
+ },
+ });
+
+ assertDoesNotRedirectTo(ROUTES.WORKSPACE_LOCKED);
+ assertDoesNotRedirectTo(ROUTES.WORKSPACE_SUSPENDED);
+ assertDoesNotRedirectTo(ROUTES.WORKSPACE_ACCESS_RESTRICTED);
+ });
+ });
+
+ describe('Role-based Route Access', () => {
+ it('should allow ADMIN to access /licenses route', () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.LIST_LICENSES,
+ appContext: {
+ isLoggedIn: true,
+ user: createMockUser({ role: USER_ROLES.ADMIN as ROLES }),
+ },
+ });
+
+ assertRendersChildren();
+ });
+
+ it('should redirect VIEWER away from /licenses route', async () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.LIST_LICENSES,
+ appContext: {
+ isLoggedIn: true,
+ user: createMockUser({ role: USER_ROLES.VIEWER as ROLES }),
+ },
+ });
+
+ await assertRedirectsTo(ROUTES.UN_AUTHORIZED);
+ });
+
+ it('should redirect EDITOR away from /licenses route', async () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.LIST_LICENSES,
+ appContext: {
+ isLoggedIn: true,
+ user: createMockUser({ role: USER_ROLES.EDITOR as ROLES }),
+ },
+ });
+
+ await assertRedirectsTo(ROUTES.UN_AUTHORIZED);
+ });
+
+ it('should allow all roles to access /services route', () => {
+ const roles = [USER_ROLES.ADMIN, USER_ROLES.EDITOR, USER_ROLES.VIEWER];
+
+ roles.forEach((role) => {
+ jest.clearAllMocks();
+
+ renderPrivateRoute({
+ initialRoute: ROUTES.APPLICATION,
+ appContext: {
+ isLoggedIn: true,
+ user: createMockUser({ role: role as ROLES }),
+ },
+ });
+
+ assertDoesNotRedirectTo(ROUTES.UN_AUTHORIZED);
+ });
+ });
+
+ it('should redirect VIEWER from /onboarding route (admin only)', async () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.ONBOARDING,
+ appContext: {
+ isLoggedIn: true,
+ user: createMockUser({ role: USER_ROLES.VIEWER as ROLES }),
+ },
+ });
+
+ await assertRedirectsTo(ROUTES.UN_AUTHORIZED);
+ });
+
+ it('should not redirect VIEWER from /settings/channels/new due to route matching order (ALL_CHANNELS matches last)', () => {
+ // Note: This tests the ACTUAL behavior of Private.tsx route matching
+ // CHANNELS_NEW has path '/settings/channels/new' with permission ['ADMIN']
+ // ALL_CHANNELS has path '/settings/channels' with permission ['ADMIN', 'EDITOR', 'VIEWER']
+ // Due to non-exact matching and array order, ALL_CHANNELS matches LAST for '/settings/channels/new'
+ // This is a known limitation - actual permission enforcement happens in the page component
+ renderPrivateRoute({
+ initialRoute: ROUTES.CHANNELS_NEW,
+ appContext: {
+ isLoggedIn: true,
+ user: createMockUser({ role: USER_ROLES.VIEWER as ROLES }),
+ },
+ });
+
+ assertRendersChildren();
+ assertDoesNotRedirectTo(ROUTES.UN_AUTHORIZED);
+ });
+
+ it('should allow EDITOR to access /get-started route', () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.GET_STARTED,
+ appContext: {
+ isLoggedIn: true,
+ user: createMockUser({ role: USER_ROLES.EDITOR as ROLES }),
+ },
+ });
+
+ assertDoesNotRedirectTo(ROUTES.UN_AUTHORIZED);
+ });
+ });
+
+ describe('Edge Cases', () => {
+ it('should handle route with query parameters correctly for private routes', async () => {
+ mockLocalStorage[LOCALSTORAGE.UNAUTHENTICATED_ROUTE_HIT] = '';
+
+ renderPrivateRoute({
+ initialRoute: `${ROUTES.ALL_DASHBOARD}?tab=metrics`,
+ appContext: { isLoggedIn: false },
+ });
+
+ await assertRedirectsTo(ROUTES.LOGIN);
+ });
+
+ it('should render children when all conditions pass', () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.HOME,
+ appContext: {
+ isLoggedIn: true,
+ isFetchingActiveLicense: false,
+ activeLicense: createMockLicense({
+ platform: LicensePlatform.CLOUD,
+ state: LicenseState.ACTIVE,
+ }),
+ trialInfo: createMockTrialInfo({ workSpaceBlock: false }),
+ user: createMockUser({ role: USER_ROLES.ADMIN as ROLES }),
+ },
+ });
+
+ assertRendersChildren();
+ });
+
+ it('should handle ANONYMOUS role by redirecting from private routes', async () => {
+ renderPrivateRoute({
+ initialRoute: ROUTES.HOME,
+ appContext: {
+ isLoggedIn: true,
+ user: createMockUser({ role: USER_ROLES.ANONYMOUS as ROLES }),
+ },
+ });
+
+ await assertRedirectsTo(ROUTES.UN_AUTHORIZED);
+ });
+ });
+});