|
| 1 | +import { User, Course, Subscription } from '@prisma/client' |
| 2 | +import { describe, it, expect } from 'vitest' |
| 3 | +import { |
| 4 | + requireAdmin, |
| 5 | + requireAuthor, |
| 6 | + requireCourseAuthor, |
| 7 | + requireActiveSubscription, |
| 8 | +} from '../permissions' |
| 9 | +import { ROLES, SUBSCRIPTION_STATUS } from '~/models/enum' |
| 10 | +import { UserWithSubscriptions } from '~/models/user' |
| 11 | + |
| 12 | +const createMockUser = (overrides: Partial<User>): User => |
| 13 | + ({ |
| 14 | + id: '1', |
| 15 | + createdAt: new Date(), |
| 16 | + updatedAt: new Date(), |
| 17 | + name: 'Test User', |
| 18 | + email: 'test@example.com', |
| 19 | + phoneNumber: null, |
| 20 | + telegram: null, |
| 21 | + instagram: null, |
| 22 | + role: ROLES.MEMBER, |
| 23 | + ...overrides, |
| 24 | + } as User) |
| 25 | + |
| 26 | +const createMockSubscription = ( |
| 27 | + overrides: Partial<Subscription> |
| 28 | +): Subscription => |
| 29 | + ({ |
| 30 | + id: 'sub-1', |
| 31 | + createdAt: new Date(), |
| 32 | + updatedAt: new Date(), |
| 33 | + authorId: 'author-1', |
| 34 | + courseId: 'course-1', |
| 35 | + userId: 'user-1', |
| 36 | + status: SUBSCRIPTION_STATUS.ACTIVE, |
| 37 | + ...overrides, |
| 38 | + } as Subscription) |
| 39 | + |
| 40 | +const createMockUserWithSubscriptions = ( |
| 41 | + userOverrides: Partial<User>, |
| 42 | + subscriptions: Partial<Subscription>[] = [] |
| 43 | +): UserWithSubscriptions => { |
| 44 | + return { |
| 45 | + ...createMockUser(userOverrides), |
| 46 | + subscriptions: subscriptions.map((sub) => createMockSubscription(sub)), |
| 47 | + } as UserWithSubscriptions |
| 48 | +} |
| 49 | + |
| 50 | +const createMockCourse = (overrides: Partial<Course>): Course => |
| 51 | + ({ |
| 52 | + id: 'course-1', |
| 53 | + createdAt: new Date(), |
| 54 | + updatedAt: new Date(), |
| 55 | + name: 'Test Course', |
| 56 | + description: null, |
| 57 | + price: 100000, |
| 58 | + authorId: '2', |
| 59 | + ...overrides, |
| 60 | + } as Course) |
| 61 | + |
| 62 | +describe('Permission functions', () => { |
| 63 | + const adminUser = createMockUser({ id: '1', role: ROLES.ADMIN }) |
| 64 | + const authorUser = createMockUser({ id: '2', role: ROLES.AUTHOR }) |
| 65 | + const memberUser = createMockUser({ id: '3', role: ROLES.MEMBER }) |
| 66 | + |
| 67 | + const course = createMockCourse({ id: 'course-1', authorId: '2' }) |
| 68 | + const otherCourse = createMockCourse({ id: 'course-2', authorId: '99' }) |
| 69 | + |
| 70 | + describe('requireAdmin', () => { |
| 71 | + it('returns true for admin user', () => { |
| 72 | + expect(requireAdmin(adminUser)).toBe(true) |
| 73 | + }) |
| 74 | + |
| 75 | + it('returns false for author user', () => { |
| 76 | + expect(requireAdmin(authorUser)).toBe(false) |
| 77 | + }) |
| 78 | + |
| 79 | + it('returns false for member user', () => { |
| 80 | + expect(requireAdmin(memberUser)).toBe(false) |
| 81 | + }) |
| 82 | + }) |
| 83 | + |
| 84 | + describe('requireAuthor', () => { |
| 85 | + it('returns true for admin user', () => { |
| 86 | + expect(requireAuthor(adminUser)).toBe(true) |
| 87 | + }) |
| 88 | + |
| 89 | + it('returns true for author user', () => { |
| 90 | + expect(requireAuthor(authorUser)).toBe(true) |
| 91 | + }) |
| 92 | + |
| 93 | + it('returns false for member user', () => { |
| 94 | + expect(requireAuthor(memberUser)).toBe(false) |
| 95 | + }) |
| 96 | + }) |
| 97 | + |
| 98 | + describe('requireCourseAuthor', () => { |
| 99 | + it('returns true for admin regardless of course', () => { |
| 100 | + expect(requireCourseAuthor(adminUser, course)).toBe(true) |
| 101 | + expect(requireCourseAuthor(adminUser, otherCourse)).toBe(true) |
| 102 | + expect(requireCourseAuthor(adminUser)).toBe(true) |
| 103 | + }) |
| 104 | + |
| 105 | + it('returns true for author of the course', () => { |
| 106 | + expect(requireCourseAuthor(authorUser, course)).toBe(true) |
| 107 | + }) |
| 108 | + |
| 109 | + it('returns false for author of a different course', () => { |
| 110 | + expect(requireCourseAuthor(authorUser, otherCourse)).toBe(false) |
| 111 | + }) |
| 112 | + |
| 113 | + it('returns false for member user', () => { |
| 114 | + expect(requireCourseAuthor(memberUser, course)).toBe(false) |
| 115 | + }) |
| 116 | + }) |
| 117 | + |
| 118 | + describe('requireActiveSubscription', () => { |
| 119 | + const memberWithActiveSubscription = createMockUserWithSubscriptions( |
| 120 | + { id: '3', role: ROLES.MEMBER }, |
| 121 | + [{ courseId: 'course-1', status: SUBSCRIPTION_STATUS.ACTIVE }] |
| 122 | + ) |
| 123 | + |
| 124 | + const memberWithInactiveSubscription = createMockUserWithSubscriptions( |
| 125 | + { id: '3', role: ROLES.MEMBER }, |
| 126 | + [{ courseId: 'course-1', status: SUBSCRIPTION_STATUS.INACTIVE }] |
| 127 | + ) |
| 128 | + |
| 129 | + const memberWithNoSubscription = createMockUserWithSubscriptions( |
| 130 | + { id: '3', role: ROLES.MEMBER }, |
| 131 | + [] |
| 132 | + ) |
| 133 | + |
| 134 | + const authorWithSubscriptions = createMockUserWithSubscriptions( |
| 135 | + { id: '2', role: ROLES.AUTHOR }, |
| 136 | + [] |
| 137 | + ) |
| 138 | + |
| 139 | + const adminWithSubscriptions = createMockUserWithSubscriptions( |
| 140 | + { id: '1', role: ROLES.ADMIN }, |
| 141 | + [] |
| 142 | + ) |
| 143 | + |
| 144 | + it('returns true for admin regardless of subscription', () => { |
| 145 | + expect(requireActiveSubscription(adminWithSubscriptions, course)).toBe( |
| 146 | + true |
| 147 | + ) |
| 148 | + }) |
| 149 | + |
| 150 | + it('returns true for course author regardless of subscription', () => { |
| 151 | + expect(requireActiveSubscription(authorWithSubscriptions, course)).toBe( |
| 152 | + true |
| 153 | + ) |
| 154 | + }) |
| 155 | + |
| 156 | + it('returns true for member with active subscription to the course', () => { |
| 157 | + expect( |
| 158 | + requireActiveSubscription(memberWithActiveSubscription, course) |
| 159 | + ).toBe(true) |
| 160 | + }) |
| 161 | + |
| 162 | + it('returns false for member with inactive subscription', () => { |
| 163 | + expect( |
| 164 | + requireActiveSubscription(memberWithInactiveSubscription, course) |
| 165 | + ).toBe(false) |
| 166 | + }) |
| 167 | + |
| 168 | + it('returns false for member with no subscription', () => { |
| 169 | + expect(requireActiveSubscription(memberWithNoSubscription, course)).toBe( |
| 170 | + false |
| 171 | + ) |
| 172 | + }) |
| 173 | + |
| 174 | + it('returns false for member with subscription to different course', () => { |
| 175 | + expect( |
| 176 | + requireActiveSubscription(memberWithActiveSubscription, otherCourse) |
| 177 | + ).toBe(false) |
| 178 | + }) |
| 179 | + }) |
| 180 | +}) |
0 commit comments