Add quality gates and baseline tests for web/mobile
This commit is contained in:
25
apps/web/src/features/auth/roleUtils.test.ts
Normal file
25
apps/web/src/features/auth/roleUtils.test.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { getDashboardPath, normalizeRole } from './roleUtils'
|
||||
|
||||
describe('roleUtils', () => {
|
||||
it('normalizes lowercase and uppercase roles', () => {
|
||||
expect(normalizeRole('admin')).toBe('admin')
|
||||
expect(normalizeRole('CLIENT')).toBe('client')
|
||||
expect(normalizeRole('Vendor')).toBe('vendor')
|
||||
})
|
||||
|
||||
it('returns null for unsupported roles', () => {
|
||||
expect(normalizeRole('super_admin')).toBeNull()
|
||||
expect(normalizeRole('')).toBeNull()
|
||||
})
|
||||
|
||||
it('maps known roles to the proper dashboard path', () => {
|
||||
expect(getDashboardPath('admin')).toBe('/dashboard/admin')
|
||||
expect(getDashboardPath('CLIENT')).toBe('/dashboard/client')
|
||||
expect(getDashboardPath('vendor')).toBe('/dashboard/vendor')
|
||||
})
|
||||
|
||||
it('falls back to client dashboard for unknown roles', () => {
|
||||
expect(getDashboardPath('unknown')).toBe('/dashboard/client')
|
||||
})
|
||||
})
|
||||
30
apps/web/src/features/auth/roleUtils.ts
Normal file
30
apps/web/src/features/auth/roleUtils.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
export type Role = 'admin' | 'client' | 'vendor'
|
||||
export type RawRole = Role | Uppercase<Role> | string
|
||||
|
||||
const DEFAULT_DASHBOARD_PATH = '/dashboard/client'
|
||||
|
||||
const ROLE_TO_DASHBOARD: Record<Role, string> = {
|
||||
admin: '/dashboard/admin',
|
||||
client: '/dashboard/client',
|
||||
vendor: '/dashboard/vendor',
|
||||
}
|
||||
|
||||
export const normalizeRole = (role: RawRole): Role | null => {
|
||||
const normalizedRole = role.toLowerCase()
|
||||
|
||||
if (normalizedRole === 'admin' || normalizedRole === 'client' || normalizedRole === 'vendor') {
|
||||
return normalizedRole
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
export const getDashboardPath = (role: RawRole): string => {
|
||||
const normalizedRole = normalizeRole(role)
|
||||
|
||||
if (!normalizedRole) {
|
||||
return DEFAULT_DASHBOARD_PATH
|
||||
}
|
||||
|
||||
return ROLE_TO_DASHBOARD[normalizedRole]
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { getFirestore, doc, getDoc } from "firebase/firestore";
|
||||
import { app } from "../features/auth/firebase";
|
||||
import { getDashboardPath as getDashboardPathFromRole } from "../features/auth/roleUtils";
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore - generated dataconnect types may not be resolvable in this context
|
||||
import { getUserById } from "@/dataconnect-generated";
|
||||
@@ -68,11 +69,5 @@ export const fetchUserData = async (uid: string): Promise<UserData | null> => {
|
||||
* @returns The appropriate dashboard path
|
||||
*/
|
||||
export const getDashboardPath = (userRole: string): string => {
|
||||
const roleMap: Record<string, string> = {
|
||||
admin: "/dashboard/admin",
|
||||
client: "/dashboard/client",
|
||||
vendor: "/dashboard/vendor",
|
||||
};
|
||||
|
||||
return roleMap[userRole.toLowerCase()] || "/dashboard/client";
|
||||
return getDashboardPathFromRole(userRole);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user