|
| 1 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 2 | + |
| 3 | +import { |
| 4 | + createEncryptedAnswersBlob, |
| 5 | + decryptAnswers, |
| 6 | + encryptAnswers, |
| 7 | +} from '@/lib/quiz/quiz-crypto'; |
| 8 | + |
| 9 | +import { |
| 10 | + createCorrectAnswersMap, |
| 11 | + createMockQuestions, |
| 12 | + resetFactoryCounters, |
| 13 | +} from '../factories/quiz/quiz'; |
| 14 | +import { cleanupQuizTestEnv, setupQuizTestEnv } from './setup'; |
| 15 | + |
| 16 | +describe('quiz-crypto', () => { |
| 17 | + // Setup: set encryption key before each test |
| 18 | + beforeEach(() => { |
| 19 | + setupQuizTestEnv(); |
| 20 | + resetFactoryCounters(); |
| 21 | + }); |
| 22 | + |
| 23 | + // Cleanup: remove encryption key after each test |
| 24 | + afterEach(() => { |
| 25 | + cleanupQuizTestEnv(); |
| 26 | + }); |
| 27 | + |
| 28 | + describe('encryptAnswers', () => { |
| 29 | + it('returns a base64 string', () => { |
| 30 | + const answers = { 'q-1': 'a-1' }; |
| 31 | + |
| 32 | + const result = encryptAnswers(answers); |
| 33 | + |
| 34 | + // Base64 pattern: letters, numbers, +, /, ends with optional = |
| 35 | + expect(result).toMatch(/^[A-Za-z0-9+/]+=*$/); |
| 36 | + }); |
| 37 | + |
| 38 | + it('returns different output for same input (random IV)', () => { |
| 39 | + const answers = { 'q-1': 'a-1' }; |
| 40 | + |
| 41 | + const result1 = encryptAnswers(answers); |
| 42 | + const result2 = encryptAnswers(answers); |
| 43 | + |
| 44 | + // Each encryption uses random IV, so outputs differ |
| 45 | + expect(result1).not.toBe(result2); |
| 46 | + }); |
| 47 | + |
| 48 | + it('handles empty object', () => { |
| 49 | + const result = encryptAnswers({}); |
| 50 | + |
| 51 | + expect(result).toBeDefined(); |
| 52 | + expect(typeof result).toBe('string'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('handles multiple questions', () => { |
| 56 | + const answers = { |
| 57 | + 'q-1': 'a-1', |
| 58 | + 'q-2': 'a-2', |
| 59 | + 'q-3': 'a-3', |
| 60 | + }; |
| 61 | + |
| 62 | + const result = encryptAnswers(answers); |
| 63 | + |
| 64 | + expect(result).toBeDefined(); |
| 65 | + expect(result.length).toBeGreaterThan(0); |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + describe('decryptAnswers', () => { |
| 70 | + it('decrypts back to original data', () => { |
| 71 | + const original = { 'q-1': 'a-1', 'q-2': 'a-2' }; |
| 72 | + |
| 73 | + const encrypted = encryptAnswers(original); |
| 74 | + const decrypted = decryptAnswers(encrypted); |
| 75 | + |
| 76 | + expect(decrypted).toEqual(original); |
| 77 | + }); |
| 78 | + |
| 79 | + it('returns null for tampered data', () => { |
| 80 | + const original = { 'q-1': 'a-1' }; |
| 81 | + const encrypted = encryptAnswers(original); |
| 82 | + |
| 83 | + // Tamper: change last 5 characters |
| 84 | + const tampered = encrypted.slice(0, -5) + 'XXXXX'; |
| 85 | + |
| 86 | + const result = decryptAnswers(tampered); |
| 87 | + |
| 88 | + expect(result).toBeNull(); |
| 89 | + }); |
| 90 | + |
| 91 | + it('returns null for invalid base64', () => { |
| 92 | + const result = decryptAnswers('not-valid-base64!!!'); |
| 93 | + |
| 94 | + expect(result).toBeNull(); |
| 95 | + }); |
| 96 | + |
| 97 | + it('returns null for empty string', () => { |
| 98 | + const result = decryptAnswers(''); |
| 99 | + |
| 100 | + expect(result).toBeNull(); |
| 101 | + }); |
| 102 | + |
| 103 | + it('returns null for truncated data', () => { |
| 104 | + const original = { 'q-1': 'a-1' }; |
| 105 | + const encrypted = encryptAnswers(original); |
| 106 | + |
| 107 | + // Truncate: remove half of the data |
| 108 | + const truncated = encrypted.slice(0, encrypted.length / 2); |
| 109 | + |
| 110 | + const result = decryptAnswers(truncated); |
| 111 | + |
| 112 | + expect(result).toBeNull(); |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + describe('createEncryptedAnswersBlob', () => { |
| 117 | + it('creates encrypted blob from questions', () => { |
| 118 | + const questions = createMockQuestions(3); |
| 119 | + |
| 120 | + const blob = createEncryptedAnswersBlob(questions); |
| 121 | + |
| 122 | + expect(blob).toBeDefined(); |
| 123 | + expect(typeof blob).toBe('string'); |
| 124 | + expect(blob.length).toBeGreaterThan(0); |
| 125 | + }); |
| 126 | + |
| 127 | + it('encrypted blob decrypts to correct answers map', () => { |
| 128 | + const questions = createMockQuestions(3); |
| 129 | + const expectedMap = createCorrectAnswersMap(questions); |
| 130 | + |
| 131 | + const blob = createEncryptedAnswersBlob(questions); |
| 132 | + const decrypted = decryptAnswers(blob); |
| 133 | + |
| 134 | + expect(decrypted).toEqual(expectedMap); |
| 135 | + }); |
| 136 | + |
| 137 | + it('handles questions with no correct answer', () => { |
| 138 | + const questions = [ |
| 139 | + { |
| 140 | + id: 'q-no-correct', |
| 141 | + answers: [ |
| 142 | + { id: 'a-1', isCorrect: false }, |
| 143 | + { id: 'a-2', isCorrect: false }, |
| 144 | + ], |
| 145 | + }, |
| 146 | + ]; |
| 147 | + |
| 148 | + const blob = createEncryptedAnswersBlob(questions); |
| 149 | + const decrypted = decryptAnswers(blob); |
| 150 | + |
| 151 | + // Question with no correct answer is not included in map |
| 152 | + expect(decrypted).toEqual({}); |
| 153 | + }); |
| 154 | + |
| 155 | + it('handles empty questions array', () => { |
| 156 | + const blob = createEncryptedAnswersBlob([]); |
| 157 | + const decrypted = decryptAnswers(blob); |
| 158 | + |
| 159 | + expect(decrypted).toEqual({}); |
| 160 | + }); |
| 161 | + }); |
| 162 | +}); |
0 commit comments