|
| 1 | +import { subject } from '@casl/ability'; |
| 2 | +import { defineStudentPostAbility } from './student-post.ability.js'; |
| 3 | +import { Action } from './actions.js'; |
| 4 | +import { UserType } from '../constants/auth.constant.js'; |
| 5 | +import { AuthorRole } from '../constants/posts.constant.js'; |
| 6 | + |
| 7 | +describe('StudentPost Ability', () => { |
| 8 | + describe('STUDENT 역할', () => { |
| 9 | + const ability = defineStudentPostAbility({ |
| 10 | + userType: UserType.STUDENT, |
| 11 | + profileId: 'student-1', |
| 12 | + enrollmentIds: ['enrollment-1', 'enrollment-2'], |
| 13 | + }); |
| 14 | + |
| 15 | + it('자기 enrollment의 STUDENT 글을 읽을 수 있다', () => { |
| 16 | + const post = subject('StudentPost', { |
| 17 | + enrollmentId: 'enrollment-1', |
| 18 | + authorRole: AuthorRole.STUDENT, |
| 19 | + instructorId: 'inst-1', |
| 20 | + } as Record<string, unknown>); |
| 21 | + expect(ability.can(Action.Read, post)).toBe(true); |
| 22 | + }); |
| 23 | + |
| 24 | + it('타인 enrollment의 글은 읽을 수 없다', () => { |
| 25 | + const post = subject('StudentPost', { |
| 26 | + enrollmentId: 'other-enrollment', |
| 27 | + authorRole: AuthorRole.STUDENT, |
| 28 | + instructorId: 'inst-1', |
| 29 | + } as Record<string, unknown>); |
| 30 | + expect(ability.can(Action.Read, post)).toBe(false); |
| 31 | + }); |
| 32 | + |
| 33 | + it('PARENT가 작성한 글은 읽을 수 없다', () => { |
| 34 | + const post = subject('StudentPost', { |
| 35 | + enrollmentId: 'enrollment-1', |
| 36 | + authorRole: AuthorRole.PARENT, |
| 37 | + instructorId: 'inst-1', |
| 38 | + } as Record<string, unknown>); |
| 39 | + expect(ability.can(Action.Read, post)).toBe(false); |
| 40 | + }); |
| 41 | + |
| 42 | + it('글을 삭제할 수 있다 (자기 enrollment)', () => { |
| 43 | + const post = subject('StudentPost', { |
| 44 | + enrollmentId: 'enrollment-1', |
| 45 | + authorRole: AuthorRole.STUDENT, |
| 46 | + instructorId: 'inst-1', |
| 47 | + } as Record<string, unknown>); |
| 48 | + expect(ability.can(Action.Delete, post)).toBe(true); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + describe('INSTRUCTOR 역할', () => { |
| 53 | + const ability = defineStudentPostAbility({ |
| 54 | + userType: UserType.INSTRUCTOR, |
| 55 | + profileId: 'inst-1', |
| 56 | + effectiveInstructorId: 'inst-1', |
| 57 | + }); |
| 58 | + |
| 59 | + it('담당 학생의 글을 읽을 수 있다', () => { |
| 60 | + const post = subject('StudentPost', { |
| 61 | + instructorId: 'inst-1', |
| 62 | + enrollmentId: 'enrollment-1', |
| 63 | + authorRole: AuthorRole.STUDENT, |
| 64 | + } as Record<string, unknown>); |
| 65 | + expect(ability.can(Action.Read, post)).toBe(true); |
| 66 | + }); |
| 67 | + |
| 68 | + it('글을 수정/삭제할 수 없다', () => { |
| 69 | + const post = subject('StudentPost', { |
| 70 | + instructorId: 'inst-1', |
| 71 | + enrollmentId: 'enrollment-1', |
| 72 | + authorRole: AuthorRole.STUDENT, |
| 73 | + } as Record<string, unknown>); |
| 74 | + expect(ability.can(Action.Update, post)).toBe(false); |
| 75 | + expect(ability.can(Action.Delete, post)).toBe(false); |
| 76 | + }); |
| 77 | + |
| 78 | + it('타 강사 담당 글은 읽을 수 없다', () => { |
| 79 | + const post = subject('StudentPost', { |
| 80 | + instructorId: 'other-inst', |
| 81 | + enrollmentId: 'enrollment-1', |
| 82 | + authorRole: AuthorRole.STUDENT, |
| 83 | + } as Record<string, unknown>); |
| 84 | + expect(ability.can(Action.Read, post)).toBe(false); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe('ASSISTANT 역할', () => { |
| 89 | + const ability = defineStudentPostAbility({ |
| 90 | + userType: UserType.ASSISTANT, |
| 91 | + profileId: 'assi-1', |
| 92 | + effectiveInstructorId: 'inst-1', // 조교가 담당하는 강사 ID |
| 93 | + }); |
| 94 | + |
| 95 | + it('담당 강사의 글을 읽을 수 있다', () => { |
| 96 | + const post = subject('StudentPost', { |
| 97 | + instructorId: 'inst-1', |
| 98 | + enrollmentId: 'enrollment-1', |
| 99 | + authorRole: AuthorRole.STUDENT, |
| 100 | + } as Record<string, unknown>); |
| 101 | + expect(ability.can(Action.Read, post)).toBe(true); |
| 102 | + }); |
| 103 | + |
| 104 | + it('타 강사 담당 글은 읽을 수 없다', () => { |
| 105 | + const post = subject('StudentPost', { |
| 106 | + instructorId: 'other-inst', |
| 107 | + enrollmentId: 'enrollment-1', |
| 108 | + authorRole: AuthorRole.STUDENT, |
| 109 | + } as Record<string, unknown>); |
| 110 | + expect(ability.can(Action.Read, post)).toBe(false); |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + describe('PARENT 역할', () => { |
| 115 | + const ability = defineStudentPostAbility({ |
| 116 | + userType: UserType.PARENT, |
| 117 | + profileId: 'parent-1', |
| 118 | + parentEnrollmentIds: ['enrollment-1'], |
| 119 | + }); |
| 120 | + |
| 121 | + it('자녀 enrollment의 PARENT 글을 읽을 수 있다', () => { |
| 122 | + const post = subject('StudentPost', { |
| 123 | + enrollmentId: 'enrollment-1', |
| 124 | + authorRole: AuthorRole.PARENT, |
| 125 | + instructorId: 'inst-1', |
| 126 | + } as Record<string, unknown>); |
| 127 | + expect(ability.can(Action.Read, post)).toBe(true); |
| 128 | + }); |
| 129 | + |
| 130 | + it('자녀 enrollment의 STUDENT 글은 읽을 수 없다', () => { |
| 131 | + const post = subject('StudentPost', { |
| 132 | + enrollmentId: 'enrollment-1', |
| 133 | + authorRole: AuthorRole.STUDENT, |
| 134 | + instructorId: 'inst-1', |
| 135 | + } as Record<string, unknown>); |
| 136 | + expect(ability.can(Action.Read, post)).toBe(false); |
| 137 | + }); |
| 138 | + |
| 139 | + it('타인 자녀 enrollment의 글은 읽을 수 없다', () => { |
| 140 | + const post = subject('StudentPost', { |
| 141 | + enrollmentId: 'other-enrollment', |
| 142 | + authorRole: AuthorRole.PARENT, |
| 143 | + instructorId: 'inst-1', |
| 144 | + } as Record<string, unknown>); |
| 145 | + expect(ability.can(Action.Read, post)).toBe(false); |
| 146 | + }); |
| 147 | + }); |
| 148 | +}); |
0 commit comments