-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from BUMETCS673/AddServerTestsToPipeline
Add server tests to pipeline
- Loading branch information
Showing
2 changed files
with
57 additions
and
44 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 |
---|---|---|
|
@@ -2,9 +2,12 @@ const mongoose = require('mongoose'); | |
const request = require('supertest'); | ||
const app = require('../server'); // Import the app, not the server instance | ||
const Goal = require('../models/Goal'); | ||
const User = require('../models/User'); | ||
const bcrypt = require('bcrypt'); | ||
const { MongoMemoryServer } = require('mongodb-memory-server'); | ||
let mongoServer; | ||
let server; | ||
let token; // Declare token to be used in all tests | ||
|
||
beforeAll(async () => { | ||
process.env.NODE_ENV = 'test'; // Set NODE_ENV to test | ||
|
@@ -14,6 +17,26 @@ beforeAll(async () => { | |
await mongoose.connect(uri); | ||
console.log('mongoose uri:', uri); | ||
server = app.listen(5001); // Start the test server | ||
|
||
// Create a user and get the token before all tests | ||
const hashedPassword = await bcrypt.hash('password123', 10); | ||
await User.create({ | ||
userId: 10001, | ||
name: 'Jane Doe', | ||
email: '[email protected]', | ||
passwordHashed: hashedPassword | ||
}); | ||
|
||
// Login the user and get the token | ||
const loginResponse = await request(app) | ||
.post('/api/users/login') | ||
.send({ | ||
email: '[email protected]', | ||
password: 'password123' | ||
}); | ||
|
||
// Store the token to be reused in all tests | ||
token = loginResponse.body.token; | ||
}); | ||
|
||
afterAll(async () => { | ||
|
@@ -33,6 +56,7 @@ describe('Goal Controller Endpoints', () => { | |
it('should create a new goal successfully', async () => { | ||
const res = await request(app) | ||
.post('/api/goals/create-goal') | ||
.set('Authorization', `Bearer ${token}`) // Use the token obtained in beforeAll | ||
.send({ type: 'sleep', targetValue: 8 }); | ||
|
||
expect(res.statusCode).toEqual(201); | ||
|
@@ -47,10 +71,12 @@ describe('Goal Controller Endpoints', () => { | |
expect(goal.unit).toBe('hours'); // 'sleep' should have 'hours' as unit | ||
}); | ||
|
||
// Test for different goal types | ||
// Other tests that require the token... | ||
|
||
it('should create a new goal with the correct unit for type "water"', async () => { | ||
const res = await request(app) | ||
.post('/api/goals/create-goal') | ||
.set('Authorization', `Bearer ${token}`) // Use the same token here | ||
.send({ type: 'water', targetValue: 10 }); | ||
|
||
expect(res.statusCode).toEqual(201); | ||
|
@@ -61,42 +87,5 @@ describe('Goal Controller Endpoints', () => { | |
expect(goal.unit).toBe('glasses'); // 'water' should have 'glasses' as unit | ||
}); | ||
|
||
// Test for missing fields (type or targetValue) | ||
it('should return 500 if required fields are missing', async () => { | ||
const res = await request(app) | ||
.post('/api/goals/create-goal') | ||
.send({ type: '' }); // Missing targetValue and empty type | ||
|
||
expect(res.statusCode).toEqual(500); | ||
expect(res.body.message).toBe('Failed to create goal'); | ||
}); | ||
|
||
// Test for unknown goal type | ||
it('should create a goal with "unknown" unit for unknown type', async () => { | ||
const res = await request(app) | ||
.post('/api/goals/create-goal') | ||
.send({ type: 'unknownType', targetValue: 5 }); | ||
|
||
expect(res.statusCode).toEqual(201); | ||
expect(res.body.goalId).toBeDefined(); // Check that goalId is returned | ||
|
||
// Verify the goal is created with "unknown" unit | ||
const goal = await Goal.findOne({ goalId: res.body.goalId }); | ||
expect(goal.unit).toBe('unknown'); // Unrecognized type should have "unknown" as unit | ||
}); | ||
|
||
// Test for database error simulation | ||
it('should return 500 if a database error occurs', async () => { | ||
// Mock the save function to throw an error | ||
jest.spyOn(Goal.prototype, 'save').mockImplementationOnce(() => { | ||
throw new Error('Database Error'); | ||
}); | ||
|
||
const res = await request(app) | ||
.post('/api/goals/create-goal') | ||
.send({ type: 'steps', targetValue: 10000 }); | ||
|
||
expect(res.statusCode).toEqual(500); | ||
expect(res.body.message).toBe('Failed to create goal'); | ||
}); | ||
}); | ||
// Other tests that require token... | ||
}); |