Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions backend/__tests__/userSchema.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
21 changes: 13 additions & 8 deletions backend/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand All @@ -37,10 +46,6 @@ const userSchema = new mongoose.Schema({
hasPassword: {
type: Boolean,
default: true
},
createdAt: {
type: Date,
default: Date.now
}
}, {
timestamps: true
Expand Down