Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/platform/cloud/onboarding/onboardingCloudRoutes.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import type {
RouteRecordSingleView,
RouteRecordSingleViewWithChildren
} from 'vue-router'
import { createMemoryHistory, createRouter } from 'vue-router'

import {
cloudOnboardingRoutes,
Expand All @@ -12,11 +17,14 @@ import {
const VALID_REQUEST_ID = '550e8400-e29b-41d4-a716-446655440000'

const createSessionOrThrow = vi.fn().mockResolvedValue(undefined)
const useCurrentUser = vi.fn(() => ({ isLoggedIn: { value: false } }))

vi.mock('@/platform/auth/session/useSessionCookie', () => ({
useSessionCookie: () => ({ createSessionOrThrow })
}))

vi.mock('@/composables/auth/useCurrentUser', () => ({ useCurrentUser }))

const oauthLayout = cloudOnboardingRoutes.find((r) => r.path === '/oauth')
const consentRoute = oauthLayout?.children?.find(
(c) => c.name === 'cloud-oauth-consent'
Expand Down Expand Up @@ -46,6 +54,61 @@ const resolvedComponents = await Promise.all(
)

describe('cloudOnboardingRoutes', () => {
it('redirects the legacy login path to Cloud login with its query', async () => {
const legacyLoginRoute = cloudOnboardingRoutes.find(
(route) => route.path === '/login'
)
const cloudLayoutRoute = cloudOnboardingRoutes.find(
(route): route is RouteRecordSingleViewWithChildren =>
route.path === '/cloud' &&
'component' in route &&
'children' in route &&
!('components' in route)
)
const cloudLoginRoute = cloudLayoutRoute?.children?.find(
(route): route is RouteRecordSingleView =>
route.name === 'cloud-login' &&
'component' in route &&
!('children' in route) &&
!('components' in route)
)
expect(legacyLoginRoute).toBeDefined()
expect(cloudLayoutRoute).toBeDefined()
expect(cloudLoginRoute).toBeDefined()
if (!legacyLoginRoute || !cloudLayoutRoute || !cloudLoginRoute) return

const router = createRouter({
history: createMemoryHistory(),
routes: [
legacyLoginRoute,
{
...cloudLayoutRoute,
component: { template: '<div />' },
children: [
{
...cloudLoginRoute,
component: { template: '<div />' }
}
]
}
]
Comment thread
dante01yoon marked this conversation as resolved.
Resolved
})
const guardedRouteNames = vi.fn()
router.beforeEach((to) => guardedRouteNames(to.name))

await router.push('/login?source=legacy&campaign=one&campaign=two')

expect(guardedRouteNames).toHaveBeenCalledOnce()
expect(guardedRouteNames).toHaveBeenCalledWith('cloud-login')
expect(useCurrentUser).toHaveBeenCalledOnce()
expect(router.currentRoute.value.name).toBe('cloud-login')
expect(router.currentRoute.value.path).toBe('/cloud/login')
expect(router.currentRoute.value.query).toEqual({
source: 'legacy',
campaign: ['one', 'two']
})
})

it('consent route is not a child of the /cloud layout', () => {
const cloudLayout = cloudOnboardingRoutes.find((r) => r.path === '/cloud')
const childPaths = (cloudLayout?.children ?? []).map((c) => c.path)
Expand Down
4 changes: 4 additions & 0 deletions src/platform/cloud/onboarding/onboardingCloudRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ export async function oauthConsentRedirect() {
}

export const cloudOnboardingRoutes: RouteRecordRaw[] = [
{
path: '/login',
redirect: (to) => ({ name: 'cloud-login', query: to.query })
},
{
path: '/cloud',
component: () =>
Expand Down
Loading