fix: add site_admin support to tournament permissions and fix session cache issues
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
* Regression tests for the issue where tournament_admin users were redirected to login
|
||||
*/
|
||||
|
||||
import { describe, test, expect, vi } from 'vitest';
|
||||
import { describe, test, expect, vi, beforeEach } from 'vitest';
|
||||
import { canManageTournament, ownsTournament, hasRole, getManageableTournaments } from '@/lib/permissions';
|
||||
import { getSession } from '@/lib/auth-simple';
|
||||
import { prisma } from '@/lib/prisma';
|
||||
@@ -44,6 +44,7 @@ const createMockUser = (id: string, email: string, role: string): User => ({
|
||||
// Helper to create mock tournament
|
||||
const createMockTournament = (id: number, ownerId: string | null): Event => ({
|
||||
id,
|
||||
event_id: null,
|
||||
name: 'Test Tournament',
|
||||
description: null,
|
||||
eventDate: new Date(),
|
||||
@@ -163,9 +164,12 @@ describe('Tournament Permissions', () => {
|
||||
describe('getManageableTournaments', () => {
|
||||
test('should return all tournaments for club_admin', async () => {
|
||||
vi.mocked(getSession).mockResolvedValue({
|
||||
user: { id: 'admin-1', email: 'admin@example.com', role: 'club_admin' },
|
||||
user: { id: 'admin-1', email: 'admin@example.com' },
|
||||
session: { token: 'test', expiresAt: new Date() }
|
||||
});
|
||||
vi.mocked(prisma.user.findUnique).mockResolvedValue(
|
||||
createMockUser('admin-1', 'admin@example.com', 'club_admin')
|
||||
);
|
||||
|
||||
const mockTournaments = [
|
||||
createMockTournament(1, 'user-1'),
|
||||
@@ -185,9 +189,12 @@ describe('Tournament Permissions', () => {
|
||||
|
||||
test('should return only owned tournaments for tournament_admin', async () => {
|
||||
vi.mocked(getSession).mockResolvedValue({
|
||||
user: { id: 'tour-admin-1', email: 'tour@example.com', role: 'tournament_admin' },
|
||||
user: { id: 'tour-admin-1', email: 'tour@example.com' },
|
||||
session: { token: 'test', expiresAt: new Date() }
|
||||
});
|
||||
vi.mocked(prisma.user.findUnique).mockResolvedValue(
|
||||
createMockUser('tour-admin-1', 'tour@example.com', 'tournament_admin')
|
||||
);
|
||||
|
||||
const mockTournaments = [
|
||||
createMockTournament(1, 'tour-admin-1'),
|
||||
@@ -209,9 +216,12 @@ describe('Tournament Permissions', () => {
|
||||
|
||||
test('should return only non-draft tournaments for players', async () => {
|
||||
vi.mocked(getSession).mockResolvedValue({
|
||||
user: { id: 'player-1', email: 'player@example.com', role: 'player' },
|
||||
user: { id: 'player-1', email: 'player@example.com' },
|
||||
session: { token: 'test', expiresAt: new Date() }
|
||||
});
|
||||
vi.mocked(prisma.user.findUnique).mockResolvedValue(
|
||||
createMockUser('player-1', 'player@example.com', 'player')
|
||||
);
|
||||
|
||||
const mockTournaments = [
|
||||
createMockTournament(1, 'user-1'),
|
||||
|
||||
+21
-12
@@ -114,8 +114,8 @@ export async function canManageTournament(tournamentId: number): Promise<Permiss
|
||||
|
||||
const userRole = user.role as UserRole;
|
||||
|
||||
// Club admins can manage all tournaments
|
||||
if (userRole === 'club_admin') {
|
||||
// Site admins and club admins can manage all tournaments
|
||||
if (userRole === 'site_admin' || userRole === 'club_admin') {
|
||||
return { allowed: true };
|
||||
}
|
||||
|
||||
@@ -188,11 +188,25 @@ export async function getManageableTournaments() {
|
||||
return [];
|
||||
}
|
||||
|
||||
const userRole = session.user?.role as UserRole;
|
||||
const userId = session.user?.id;
|
||||
if (!userId) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (userRole === 'club_admin') {
|
||||
// Club admins can see all tournaments
|
||||
// Fetch user from database to get current role (bypasses session cache)
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { id: userId },
|
||||
select: { role: true }
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const userRole = user.role as UserRole;
|
||||
|
||||
if (userRole === 'site_admin' || userRole === 'club_admin') {
|
||||
// Site admins and club admins can see all tournaments
|
||||
return prisma.event.findMany({
|
||||
where: { eventType: 'tournament' },
|
||||
include: { participants: true },
|
||||
@@ -264,13 +278,8 @@ export async function canDeleteTournament(tournamentId: number): Promise<Permiss
|
||||
|
||||
const userRole = user.role as UserRole;
|
||||
|
||||
// Site admins can delete any tournament
|
||||
if (userRole === 'site_admin') {
|
||||
return { allowed: true };
|
||||
}
|
||||
|
||||
// Club admins can delete tournaments they can manage
|
||||
if (userRole === 'club_admin') {
|
||||
// Site admins and club admins can delete any tournament
|
||||
if (userRole === 'site_admin' || userRole === 'club_admin') {
|
||||
return { allowed: true };
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user