|
| 1 | +import { jest } from '@jest/globals' |
| 2 | +import express from 'express' |
| 3 | +import request from 'supertest' |
| 4 | + |
| 5 | +// Create mock functions |
| 6 | +const mockFindOne = jest.fn() |
| 7 | +const mockReplaceOne = jest.fn() |
| 8 | + |
| 9 | +// Mock the database module |
| 10 | +jest.mock('../../database/index.js', () => ({ |
| 11 | + db: { |
| 12 | + findOne: mockFindOne, |
| 13 | + replaceOne: mockReplaceOne |
| 14 | + } |
| 15 | +})) |
| 16 | + |
| 17 | +// Import controller after mocking |
| 18 | +import controller from '../../db-controller.js' |
| 19 | + |
| 20 | +// Helper to add auth to requests |
| 21 | +const addAuth = (req, res, next) => { |
| 22 | + req.user = {"http://store.rerum.io/agent": "test-user"} |
| 23 | + next() |
| 24 | +} |
| 25 | + |
| 26 | +// Create a test Express app |
| 27 | +const routeTester = express() |
| 28 | +routeTester.use(express.json()) |
| 29 | +routeTester.use(express.urlencoded({ extended: false })) |
| 30 | + |
| 31 | +// Mount our routes |
| 32 | +routeTester.use('/overwrite', [addAuth, controller.overwrite]) |
| 33 | +routeTester.use('/id/:_id', controller.id) |
| 34 | + |
| 35 | +describe('Overwrite Optimistic Locking', () => { |
| 36 | + beforeEach(() => { |
| 37 | + jest.clearAllMocks() |
| 38 | + }) |
| 39 | + |
| 40 | + test('should succeed when no version is specified (backwards compatibility)', async () => { |
| 41 | + const mockObject = { |
| 42 | + _id: 'test-id', |
| 43 | + '@id': 'http://example.com/test-id', |
| 44 | + '@context': 'http://example.com/context', |
| 45 | + '__rerum': { |
| 46 | + isOverwritten: '', |
| 47 | + generatedBy: 'test-user' |
| 48 | + }, |
| 49 | + data: 'original-data' |
| 50 | + } |
| 51 | + |
| 52 | + mockFindOne.mockResolvedValue(mockObject) |
| 53 | + mockReplaceOne.mockResolvedValue({ modifiedCount: 1 }) |
| 54 | + |
| 55 | + const response = await request(routeTester) |
| 56 | + .put('/overwrite') |
| 57 | + .send({ |
| 58 | + '@id': 'http://example.com/test-id', |
| 59 | + data: 'updated-data' |
| 60 | + }) |
| 61 | + |
| 62 | + expect(response.status).toBe(200) |
| 63 | + }) |
| 64 | + |
| 65 | + test('should succeed when correct version is provided', async () => { |
| 66 | + const mockObject = { |
| 67 | + _id: 'test-id', |
| 68 | + '@id': 'http://example.com/test-id', |
| 69 | + '@context': 'http://example.com/context', |
| 70 | + '__rerum': { |
| 71 | + isOverwritten: '2025-06-24T10:00:00', |
| 72 | + generatedBy: 'test-user' |
| 73 | + }, |
| 74 | + data: 'original-data' |
| 75 | + } |
| 76 | + |
| 77 | + mockFindOne.mockResolvedValue(mockObject) |
| 78 | + mockReplaceOne.mockResolvedValue({ modifiedCount: 1 }) |
| 79 | + |
| 80 | + const response = await request(routeTester) |
| 81 | + .put('/overwrite') |
| 82 | + .set('If-Overwritten-Version', '2025-06-24T10:00:00') |
| 83 | + .send({ |
| 84 | + '@id': 'http://example.com/test-id', |
| 85 | + data: 'updated-data' |
| 86 | + }) |
| 87 | + |
| 88 | + expect(response.status).toBe(200) |
| 89 | + }) |
| 90 | + |
| 91 | + test('should fail with 409 when version mismatch occurs', async () => { |
| 92 | + const mockObject = { |
| 93 | + _id: 'test-id', |
| 94 | + '@id': 'http://example.com/test-id', |
| 95 | + '@context': 'http://example.com/context', |
| 96 | + '__rerum': { |
| 97 | + isOverwritten: '2025-06-24T10:30:00', // Different from expected |
| 98 | + generatedBy: 'test-user' |
| 99 | + }, |
| 100 | + data: 'original-data' |
| 101 | + } |
| 102 | + |
| 103 | + mockFindOne.mockResolvedValue(mockObject) |
| 104 | + |
| 105 | + const response = await request(routeTester) |
| 106 | + .put('/overwrite') |
| 107 | + .set('If-Overwritten-Version', '2025-06-24T10:00:00') |
| 108 | + .send({ |
| 109 | + '@id': 'http://example.com/test-id', |
| 110 | + data: 'updated-data' |
| 111 | + }) |
| 112 | + |
| 113 | + expect(response.status).toBe(409) |
| 114 | + expect(response.body.message).toContain('Version conflict detected') |
| 115 | + expect(response.body.currentVersion).toBe('2025-06-24T10:30:00') |
| 116 | + }) |
| 117 | + |
| 118 | + test('should accept version via request body as fallback', async () => { |
| 119 | + const mockObject = { |
| 120 | + _id: 'test-id', |
| 121 | + '@id': 'http://example.com/test-id', |
| 122 | + '@context': 'http://example.com/context', |
| 123 | + '__rerum': { |
| 124 | + isOverwritten: '2025-06-24T10:00:00', |
| 125 | + generatedBy: 'test-user' |
| 126 | + }, |
| 127 | + data: 'original-data' |
| 128 | + } |
| 129 | + |
| 130 | + mockFindOne.mockResolvedValue(mockObject) |
| 131 | + mockReplaceOne.mockResolvedValue({ modifiedCount: 1 }) |
| 132 | + |
| 133 | + const response = await request(routeTester) |
| 134 | + .put('/overwrite') |
| 135 | + .send({ |
| 136 | + '@id': 'http://example.com/test-id', |
| 137 | + '__expectedVersion': '2025-06-24T10:00:00', |
| 138 | + data: 'updated-data' |
| 139 | + }) |
| 140 | + |
| 141 | + expect(response.status).toBe(200) |
| 142 | + }) |
| 143 | +}) |
| 144 | + |
| 145 | +describe('ID endpoint includes version header', () => { |
| 146 | + beforeEach(() => { |
| 147 | + jest.clearAllMocks() |
| 148 | + }) |
| 149 | + |
| 150 | + test('should include Current-Overwritten-Version header in GET /id response', async () => { |
| 151 | + const mockObject = { |
| 152 | + _id: 'test-id', |
| 153 | + '@id': 'http://example.com/test-id', |
| 154 | + '__rerum': { |
| 155 | + isOverwritten: '2025-06-24T10:00:00' |
| 156 | + }, |
| 157 | + data: 'some-data' |
| 158 | + } |
| 159 | + |
| 160 | + mockFindOne.mockResolvedValue(mockObject) |
| 161 | + |
| 162 | + const response = await request(routeTester) |
| 163 | + .get('/id/test-id') |
| 164 | + |
| 165 | + expect(response.status).toBe(200) |
| 166 | + }) |
| 167 | + |
| 168 | + test('should include empty string for new objects', async () => { |
| 169 | + const mockObject = { |
| 170 | + _id: 'test-id', |
| 171 | + '@id': 'http://example.com/test-id', |
| 172 | + '__rerum': { |
| 173 | + isOverwritten: '' |
| 174 | + }, |
| 175 | + data: 'some-data' |
| 176 | + } |
| 177 | + |
| 178 | + mockFindOne.mockResolvedValue(mockObject) |
| 179 | + |
| 180 | + const response = await request(routeTester) |
| 181 | + .get('/id/test-id') |
| 182 | + |
| 183 | + expect(response.status).toBe(200) |
| 184 | + }) |
| 185 | +}) |
0 commit comments