|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const mockFindOne = jest.fn(); |
| 4 | +const mockDeleteProjectById = jest.fn(); |
| 5 | +const mockSetProjectById = jest.fn(); |
| 6 | +const mockDeleteProjectByApiKeyCache = jest.fn(); |
| 7 | + |
| 8 | +jest.mock('@urbackend/common', () => ({ |
| 9 | + Project: { |
| 10 | + findOne: mockFindOne, |
| 11 | + }, |
| 12 | + getProjectAccessQuery: jest.fn((userId) => ({ $or: [{ owner: userId }, { "members.user": userId }] })), |
| 13 | + editCollectionSchema: { |
| 14 | + parse: jest.fn() |
| 15 | + }, |
| 16 | + deleteProjectById: mockDeleteProjectById, |
| 17 | + setProjectById: mockSetProjectById, |
| 18 | + deleteProjectByApiKeyCache: mockDeleteProjectByApiKeyCache, |
| 19 | + AppError: class AppError extends Error { |
| 20 | + constructor(statusCode, message) { |
| 21 | + super(message); |
| 22 | + this.statusCode = statusCode; |
| 23 | + this.status = statusCode; |
| 24 | + } |
| 25 | + }, |
| 26 | + getConnection: jest.fn(() => ({ |
| 27 | + db: { |
| 28 | + listCollections: jest.fn(() => ({ |
| 29 | + hasNext: jest.fn(() => Promise.resolve(false)) |
| 30 | + })) |
| 31 | + } |
| 32 | + })), |
| 33 | + getCompiledModel: jest.fn(() => ({})), |
| 34 | + createUniqueIndexes: jest.fn(() => Promise.resolve()), |
| 35 | + clearCompiledModel: jest.fn() |
| 36 | +})); |
| 37 | + |
| 38 | +const mockEmitEvent = jest.fn(); |
| 39 | + |
| 40 | +jest.mock('../utils/emitEvent', () => ({ |
| 41 | + emitEvent: mockEmitEvent |
| 42 | +})); |
| 43 | + |
| 44 | +const projectController = require('../controllers/project.controller'); |
| 45 | +const { editCollectionSchema, Project } = require('@urbackend/common'); |
| 46 | +const { z } = require('zod'); |
| 47 | + |
| 48 | +describe('projectController.updateCollection', () => { |
| 49 | + let req; |
| 50 | + let res; |
| 51 | + let next; |
| 52 | + |
| 53 | + beforeEach(() => { |
| 54 | + jest.clearAllMocks(); |
| 55 | + req = { |
| 56 | + user: { _id: 'user123' }, |
| 57 | + params: { projectId: 'project123', collectionName: 'myCollection' }, |
| 58 | + body: { |
| 59 | + schema: [ |
| 60 | + { key: 'title', type: 'String', required: true } |
| 61 | + ] |
| 62 | + } |
| 63 | + }; |
| 64 | + |
| 65 | + res = { |
| 66 | + status: jest.fn().mockReturnThis(), |
| 67 | + json: jest.fn() |
| 68 | + }; |
| 69 | + |
| 70 | + next = jest.fn(); |
| 71 | + |
| 72 | + editCollectionSchema.parse.mockReturnValue({ |
| 73 | + projectId: req.params.projectId, |
| 74 | + collectionName: req.params.collectionName, |
| 75 | + schema: req.body.schema |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should return 400 if validation fails', async () => { |
| 80 | + const zodError = new z.ZodError([{ message: 'Invalid data' }]); |
| 81 | + editCollectionSchema.parse.mockImplementation(() => { throw zodError; }); |
| 82 | + |
| 83 | + await projectController.updateCollection(req, res, next); |
| 84 | + |
| 85 | + expect(next).toHaveBeenCalledWith(expect.any(Error)); |
| 86 | + const error = next.mock.calls[0][0]; |
| 87 | + expect(error.statusCode).toBe(400); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should return 404 if project not found', async () => { |
| 91 | + Project.findOne.mockResolvedValue(null); |
| 92 | + await projectController.updateCollection(req, res, next); |
| 93 | + |
| 94 | + expect(next).toHaveBeenCalledWith(expect.any(Error)); |
| 95 | + const error = next.mock.calls[0][0]; |
| 96 | + expect(error.statusCode).toBe(404); |
| 97 | + expect(error.message).toBe('Project not found'); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should return 404 if collection not found in project', async () => { |
| 101 | + const mockProject = { |
| 102 | + _id: 'project123', |
| 103 | + collections: [ |
| 104 | + { name: 'otherCollection', model: [] } |
| 105 | + ], |
| 106 | + save: jest.fn() |
| 107 | + }; |
| 108 | + Project.findOne.mockResolvedValue(mockProject); |
| 109 | + |
| 110 | + await projectController.updateCollection(req, res, next); |
| 111 | + |
| 112 | + expect(next).toHaveBeenCalledWith(expect.any(Error)); |
| 113 | + const error = next.mock.calls[0][0]; |
| 114 | + expect(error.statusCode).toBe(404); |
| 115 | + expect(error.message).toBe('Collection not found'); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should return 422 if users validation fails when editing users collection', async () => { |
| 119 | + req.params.collectionName = 'users'; |
| 120 | + req.body.schema = [{ key: 'username', type: 'String', required: true }]; // Invalid users schema |
| 121 | + editCollectionSchema.parse.mockReturnValue({ |
| 122 | + projectId: req.params.projectId, |
| 123 | + collectionName: req.params.collectionName, |
| 124 | + schema: req.body.schema |
| 125 | + }); |
| 126 | + |
| 127 | + const mockProject = { |
| 128 | + _id: 'project123', |
| 129 | + collections: [ |
| 130 | + { name: 'users', model: [] } |
| 131 | + ], |
| 132 | + save: jest.fn() |
| 133 | + }; |
| 134 | + Project.findOne.mockResolvedValue(mockProject); |
| 135 | + |
| 136 | + await projectController.updateCollection(req, res, next); |
| 137 | + |
| 138 | + expect(next).toHaveBeenCalledWith(expect.any(Error)); |
| 139 | + const error = next.mock.calls[0][0]; |
| 140 | + expect(error.statusCode).toBe(422); |
| 141 | + expect(error.message).toContain("The 'users' collection must have required 'email' and 'password' string fields."); |
| 142 | + }); |
| 143 | + |
| 144 | + it('should successfully update a collection schema', async () => { |
| 145 | + const mockProject = { |
| 146 | + _id: 'project123', |
| 147 | + collections: [ |
| 148 | + { |
| 149 | + name: 'myCollection', |
| 150 | + model: [{ key: 'oldTitle', type: 'String' }] |
| 151 | + } |
| 152 | + ], |
| 153 | + resources: { db: { isExternal: false } }, |
| 154 | + save: jest.fn().mockResolvedValue(true), |
| 155 | + toObject: jest.fn().mockReturnValue({ |
| 156 | + _id: 'project123', |
| 157 | + collections: [ |
| 158 | + { name: 'myCollection', model: [{ key: 'title', type: 'String', required: true }] } |
| 159 | + ] |
| 160 | + }) |
| 161 | + }; |
| 162 | + Project.findOne.mockResolvedValue(mockProject); |
| 163 | + |
| 164 | + await projectController.updateCollection(req, res, next); |
| 165 | + |
| 166 | + expect(mockProject.collections[0].model).toEqual(req.body.schema); |
| 167 | + expect(mockProject.save).toHaveBeenCalled(); |
| 168 | + expect(mockDeleteProjectById).toHaveBeenCalledWith('project123'); |
| 169 | + expect(mockSetProjectById).toHaveBeenCalled(); |
| 170 | + expect(mockEmitEvent).toHaveBeenCalledWith('user123', 'collection_updated', { collectionName: 'myCollection', isUsersCollection: false }, 'project123'); |
| 171 | + expect(res.status).toHaveBeenCalledWith(200); |
| 172 | + }); |
| 173 | + |
| 174 | + it('should successfully update users collection if valid', async () => { |
| 175 | + req.params.collectionName = 'users'; |
| 176 | + req.body.schema = [ |
| 177 | + { key: 'email', type: 'String', required: true }, |
| 178 | + { key: 'password', type: 'String', required: true } |
| 179 | + ]; |
| 180 | + editCollectionSchema.parse.mockReturnValue({ |
| 181 | + projectId: req.params.projectId, |
| 182 | + collectionName: req.params.collectionName, |
| 183 | + schema: req.body.schema |
| 184 | + }); |
| 185 | + |
| 186 | + const mockProject = { |
| 187 | + _id: 'project123', |
| 188 | + collections: [ |
| 189 | + { name: 'users', model: [] } |
| 190 | + ], |
| 191 | + resources: { db: { isExternal: false } }, |
| 192 | + save: jest.fn().mockResolvedValue(true), |
| 193 | + toObject: jest.fn().mockReturnValue({ |
| 194 | + _id: 'project123', |
| 195 | + collections: [ |
| 196 | + { name: 'users', model: req.body.schema } |
| 197 | + ] |
| 198 | + }) |
| 199 | + }; |
| 200 | + Project.findOne.mockResolvedValue(mockProject); |
| 201 | + |
| 202 | + await projectController.updateCollection(req, res, next); |
| 203 | + |
| 204 | + expect(mockProject.collections[0].model).toEqual(req.body.schema); |
| 205 | + expect(mockProject.save).toHaveBeenCalled(); |
| 206 | + expect(mockEmitEvent).toHaveBeenCalledWith('user123', 'collection_updated', { collectionName: 'users', isUsersCollection: true }, 'project123'); |
| 207 | + expect(res.status).toHaveBeenCalledWith(200); |
| 208 | + }); |
| 209 | +}); |
0 commit comments