-
Notifications
You must be signed in to change notification settings - Fork 209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat/be/tests-recovery-module #1145
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
import sinon from "sinon"; | ||
import RecoveryToken from "../../db/models/RecoveryToken.js"; | ||
import User from "../../db/models/User.js"; | ||
import { | ||
requestRecoveryToken, | ||
validateRecoveryToken, | ||
resetPassword, | ||
} from "../../db/mongo/modules/recoveryModule.js"; | ||
import { errorMessages } from "../../utils/messages.js"; | ||
|
||
const mockRecoveryToken = { | ||
email: "[email protected]", | ||
token: "1234567890", | ||
}; | ||
|
||
const mockUser = { | ||
email: "[email protected]", | ||
password: "oldPassword", | ||
}; | ||
|
||
const mockUserWithoutPassword = { | ||
email: "[email protected]", | ||
}; | ||
|
||
// Create a query builder that logs | ||
const createQueryChain = (finalResult, comparePasswordResult = false) => ({ | ||
select: () => ({ | ||
select: async () => { | ||
if (finalResult === mockUser) { | ||
// Return a new object with all required methods | ||
return { | ||
email: "[email protected]", | ||
password: "oldPassword", | ||
comparePassword: sinon.stub().resolves(comparePasswordResult), | ||
save: sinon.stub().resolves(), | ||
}; | ||
} | ||
return finalResult; | ||
}, | ||
}), | ||
// Add methods to the top level too | ||
comparePassword: sinon.stub().resolves(comparePasswordResult), | ||
save: sinon.stub().resolves(), | ||
}); | ||
|
||
describe("recoveryModule", () => { | ||
let deleteManyStub, | ||
saveStub, | ||
findOneStub, | ||
userCompareStub, | ||
userSaveStub, | ||
userFindOneStub; | ||
let req, res; | ||
beforeEach(() => { | ||
req = { | ||
body: { email: "[email protected]" }, | ||
}; | ||
deleteManyStub = sinon.stub(RecoveryToken, "deleteMany"); | ||
saveStub = sinon.stub(RecoveryToken.prototype, "save"); | ||
findOneStub = sinon.stub(RecoveryToken, "findOne"); | ||
userCompareStub = sinon.stub(User.prototype, "comparePassword"); | ||
userSaveStub = sinon.stub(User.prototype, "save"); | ||
userFindOneStub = sinon.stub().resolves(); | ||
}); | ||
|
||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
|
||
describe("requestRecoveryToken", () => { | ||
it("should return a recovery token", async () => { | ||
deleteManyStub.resolves(); | ||
saveStub.resolves(mockRecoveryToken); | ||
const result = await requestRecoveryToken(req, res); | ||
expect(result.email).to.equal(mockRecoveryToken.email); | ||
}); | ||
it("should handle an error", async () => { | ||
const err = new Error("Test error"); | ||
deleteManyStub.rejects(err); | ||
try { | ||
await requestRecoveryToken(req, res); | ||
} catch (error) { | ||
expect(error).to.exist; | ||
expect(error).to.deep.equal(err); | ||
} | ||
}); | ||
}); | ||
describe("validateRecoveryToken", () => { | ||
it("should return a recovery token if found", async () => { | ||
findOneStub.resolves(mockRecoveryToken); | ||
const result = await validateRecoveryToken(req, res); | ||
expect(result).to.deep.equal(mockRecoveryToken); | ||
}); | ||
it("should thrown an error if a token is not found", async () => { | ||
findOneStub.resolves(null); | ||
try { | ||
await validateRecoveryToken(req, res); | ||
} catch (error) { | ||
expect(error).to.exist; | ||
expect(error.message).to.equal(errorMessages.DB_TOKEN_NOT_FOUND); | ||
} | ||
}); | ||
it("should handle DB errors", async () => { | ||
const err = new Error("Test error"); | ||
findOneStub.rejects(err); | ||
try { | ||
await validateRecoveryToken(req, res); | ||
} catch (error) { | ||
expect(error).to.exist; | ||
expect(error).to.deep.equal(err); | ||
} | ||
}); | ||
}); | ||
Comment on lines
+88
to
+113
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's vomit on his sweater already - we forgot to test token expiration! 😰 The Add a test case for expired tokens: it("should throw an error if token is expired", async () => {
const expiredToken = {
...mockRecoveryToken,
createdAt: new Date(Date.now() - 24 * 60 * 60 * 1000) // 24 hours ago
};
findOneStub.resolves(expiredToken);
try {
await validateRecoveryToken(req, res);
} catch (error) {
expect(error.message).to.equal(errorMessages.TOKEN_EXPIRED);
}
}); |
||
|
||
describe("resetPassword", () => { | ||
beforeEach(() => { | ||
req.body = { | ||
password: "test", | ||
newPassword: "test1", | ||
}; | ||
}); | ||
|
||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
|
||
it("should thrown an error if a recovery token is not found", async () => { | ||
findOneStub.resolves(null); | ||
try { | ||
await resetPassword(req, res); | ||
} catch (error) { | ||
expect(error).to.exist; | ||
expect(error.message).to.equal(errorMessages.DB_TOKEN_NOT_FOUND); | ||
} | ||
}); | ||
it("should throw an error if a user is not found", async () => { | ||
findOneStub.resolves(mockRecoveryToken); | ||
userFindOneStub = sinon.stub(User, "findOne").resolves(null); | ||
try { | ||
await resetPassword(req, res); | ||
} catch (error) { | ||
expect(error).to.exist; | ||
expect(error.message).to.equal(errorMessages.DB_USER_NOT_FOUND); | ||
} | ||
}); | ||
it("should throw an error if the passwords match", async () => { | ||
findOneStub.resolves(mockRecoveryToken); | ||
saveStub.resolves(); | ||
userFindOneStub = sinon | ||
.stub(User, "findOne") | ||
.returns(createQueryChain(mockUser, true)); | ||
try { | ||
await resetPassword(req, res); | ||
} catch (error) { | ||
expect(error).to.exist; | ||
expect(error.message).to.equal(errorMessages.DB_RESET_PASSWORD_BAD_MATCH); | ||
} | ||
}); | ||
it("should return a user without password if successful", async () => { | ||
findOneStub.resolves(mockRecoveryToken); | ||
saveStub.resolves(); | ||
userFindOneStub = sinon | ||
.stub(User, "findOne") | ||
.returns(createQueryChain(mockUser)) // First call will resolve to mockUser | ||
.onSecondCall() | ||
.returns(createQueryChain(mockUserWithoutPassword)); | ||
const result = await resetPassword(req, res); | ||
expect(result).to.deep.equal(mockUserWithoutPassword); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Knees weak, test coverage's heavy! We're missing some cases! 🤔
The test suite for
requestRecoveryToken
is missing some important scenarios:Add these test cases:
Here's a sample test case: