diff --git a/backend/__tests__/userSchema.test.js b/backend/__tests__/userSchema.test.js new file mode 100644 index 0000000..a08ac2e --- /dev/null +++ b/backend/__tests__/userSchema.test.js @@ -0,0 +1,82 @@ +const User = require('../models/User'); + +describe('User Schema Verification (Issue #15 & Issue #92)', () => { + it('has timestamps enabled in schema options without manual createdAt duplicate', () => { + expect(User.schema.options.timestamps).toBe(true); + expect(User.schema.paths.createdAt).toBeDefined(); + expect(User.schema.paths.updatedAt).toBeDefined(); + // Verify that createdAt is handled by Mongoose timestamps, not custom default + expect(User.schema.paths.createdAt.defaultValue).toBeUndefined(); + }); + + it('declares unique indexes on email and username without duplicate specifications (Issue #92)', () => { + const indexes = User.schema.indexes(); + + const emailIndex = indexes.find( + ([fields]) => Object.keys(fields).length === 1 && fields.email === 1 + ); + const usernameIndex = indexes.find( + ([fields]) => Object.keys(fields).length === 1 && fields.username === 1 + ); + + expect(emailIndex).toBeDefined(); + expect(emailIndex[1].unique).toBe(true); + + expect(usernameIndex).toBeDefined(); + expect(usernameIndex[1].unique).toBe(true); + }); + + it('declares hot-path index on refreshToken for fast session lookups (Issue #92)', () => { + const indexes = User.schema.indexes(); + const refreshTokenIndex = indexes.find( + ([fields]) => Object.keys(fields).length === 1 && fields.refreshToken === 1 + ); + + expect(refreshTokenIndex).toBeDefined(); + expect(refreshTokenIndex[0].refreshToken).toBe(1); + }); + + it('declares sparse indexes on OAuth identifiers for external logins', () => { + const indexes = User.schema.indexes(); + const googleIndex = indexes.find( + ([fields]) => Object.keys(fields).length === 1 && fields.googleId === 1 + ); + const githubIndex = indexes.find( + ([fields]) => Object.keys(fields).length === 1 && fields.githubId === 1 + ); + + expect(googleIndex).toBeDefined(); + expect(googleIndex[1].sparse).toBe(true); + + expect(githubIndex).toBeDefined(); + expect(githubIndex[1].sparse).toBe(true); + }); + + it('contains zero duplicate index specifications', () => { + const indexes = User.schema.indexes(); + const indexedKeys = indexes.map(([fields]) => Object.keys(fields).sort().join(',')); + const uniqueKeys = new Set(indexedKeys); + expect(indexedKeys.length).toBe(uniqueKeys.size); + }); + + it('omits password from JSON representation', () => { + const user = new User({ + username: 'johndoe', + email: 'john@example.com', + password: 'SamplePassword123' + }); + + const json = user.toJSON(); + expect(json.password).toBeUndefined(); + expect(json.username).toBe('johndoe'); + expect(json.email).toBe('john@example.com'); + }); + + it('provides canUsePasswordLogin helper based on hasPassword state', () => { + const localUser = new User({ hasPassword: true }); + expect(localUser.canUsePasswordLogin()).toBe(true); + + const oauthUser = new User({ hasPassword: false }); + expect(oauthUser.canUsePasswordLogin()).toBe(false); + }); +}); diff --git a/backend/models/User.js b/backend/models/User.js index 1803014..7f88d81 100644 --- a/backend/models/User.js +++ b/backend/models/User.js @@ -25,10 +25,19 @@ const userSchema = new mongoose.Schema({ minlength: 6 }, refreshToken: { - type: String - }, // For refreshtokens to generate new accesstokens - googleId: String, - githubId: String, + type: String, + index: true + }, // For refreshtokens to generate new accesstokens (hot-path index) + googleId: { + type: String, + sparse: true, + index: true + }, + githubId: { + type: String, + sparse: true, + index: true + }, authMethod: { type: String, enum: ['local', 'google', 'github'], @@ -37,10 +46,6 @@ const userSchema = new mongoose.Schema({ hasPassword: { type: Boolean, default: true - }, - createdAt: { - type: Date, - default: Date.now } }, { timestamps: true