forked from DEVRhylme-Foundation/expressify
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Write unit tests for user registration and login
Fixes DEVRhylme-Foundation#4 Add unit tests for user registration and login functionalities. * **Tests**: Add `tests/auth.test.js` to test `POST /auth/register` and `POST /auth/login` endpoints using Jest. * **Dependencies**: Add `jest` and `supertest` as dev dependencies in `package.json`. Add a test script to run Jest. * **Configuration**: Add `jest.config.js` to configure Jest for the Node.js environment. * **CI**: Modify `.github/workflows/build.yaml` to include steps for setting up Node.js, installing dependencies, and running Jest tests.
- Loading branch information
1 parent
7e57b5d
commit 9c86ceb
Showing
4 changed files
with
99 additions
and
0 deletions.
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
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,7 @@ | ||
module.exports = { | ||
testEnvironment: 'node', | ||
verbose: true, | ||
collectCoverage: true, | ||
coverageDirectory: 'coverage', | ||
testMatch: ['**/tests/**/*.test.js'] | ||
}; |
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,18 @@ | ||
{ | ||
"name": "expressify", | ||
"version": "1.0.0", | ||
"description": "A CLI tool to generate a production-grade scaffold for Express applications.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "jest" | ||
}, | ||
"dependencies": { | ||
"express": "^4.17.1" | ||
}, | ||
"devDependencies": { | ||
"jest": "^27.0.6", | ||
"supertest": "^6.1.3" | ||
}, | ||
"author": "DEVRhylme-Foundation", | ||
"license": "MIT" | ||
} |
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,63 @@ | ||
const request = require('supertest'); | ||
const app = require('../src/app'); // Adjust the path as necessary | ||
|
||
describe('User Registration and Login', () => { | ||
describe('POST /auth/register', () => { | ||
it('should register a user with valid data', async () => { | ||
const response = await request(app) | ||
.post('/auth/register') | ||
.send({ | ||
username: 'testuser', | ||
password: 'password123', | ||
email: '[email protected]' | ||
}); | ||
expect(response.status).toBe(201); | ||
expect(response.body).toHaveProperty('id'); | ||
expect(response.body).toHaveProperty('username', 'testuser'); | ||
}); | ||
|
||
it('should reject registration with invalid data', async () => { | ||
const response = await request(app) | ||
.post('/auth/register') | ||
.send({ | ||
username: '', | ||
password: 'short', | ||
email: 'invalidemail' | ||
}); | ||
expect(response.status).toBe(400); | ||
}); | ||
}); | ||
|
||
describe('POST /auth/login', () => { | ||
it('should return a token for valid credentials', async () => { | ||
// First, register a user | ||
await request(app) | ||
.post('/auth/register') | ||
.send({ | ||
username: 'testuser', | ||
password: 'password123', | ||
email: '[email protected]' | ||
}); | ||
|
||
// Then, login with the same user | ||
const response = await request(app) | ||
.post('/auth/login') | ||
.send({ | ||
username: 'testuser', | ||
password: 'password123' | ||
}); | ||
expect(response.status).toBe(200); | ||
expect(response.body).toHaveProperty('token'); | ||
}); | ||
|
||
it('should reject login with invalid credentials', async () => { | ||
const response = await request(app) | ||
.post('/auth/login') | ||
.send({ | ||
username: 'nonexistentuser', | ||
password: 'wrongpassword' | ||
}); | ||
expect(response.status).toBe(401); | ||
}); | ||
}); | ||
}); |