-
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 #53 from BUMETCS673/HW-176-val
Changed server structure - Added routes, controllers, and models
- Loading branch information
Showing
25 changed files
with
4,825 additions
and
1,049 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
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 |
---|---|---|
|
@@ -28,4 +28,4 @@ test('valid input form fields', () => { | |
const formData = { name: 'Valid Test', email: '[email protected]', password: 'password123' }; | ||
const result = validateRegistrationForm(formData); | ||
expect(result).toBe(true); | ||
}); | ||
}); |
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
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
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 was deleted.
Oops, something went wrong.
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,111 @@ | ||
// __tests__/userController.test.js | ||
const request = require('supertest'); | ||
const app = require('../server'); | ||
const mongoose = require('mongoose'); | ||
const User = require('../models/User'); // Your User model | ||
const jwt = require('jsonwebtoken'); | ||
|
||
// Mock the database connection before running the tests | ||
beforeAll(async () => { | ||
const url = 'mongodb://127.0.0.1:27017/testDB'; | ||
await mongoose.connect(url, { useNewUrlParser: true, useUnifiedTopology: true }); | ||
}); | ||
|
||
// Close the database connection after the tests | ||
afterAll(async () => { | ||
await mongoose.connection.close(); | ||
}); | ||
|
||
describe('User Controller Endpoints', () => { | ||
|
||
// Test the createUser endpoint | ||
describe('POST /api/users/create-user', () => { | ||
it('should create a new user', async () => { | ||
const res = await request(app) | ||
.post('/api/users/create-user') | ||
.send({ | ||
name: 'Test User', | ||
email: '[email protected]', | ||
password: 'password123', | ||
}); | ||
|
||
expect(res.statusCode).toEqual(201); | ||
expect(res.body.message).toBe('User created successfully!'); | ||
|
||
// Clean up the created user after the test | ||
await User.findOneAndDelete({ email: '[email protected]' }); | ||
}); | ||
|
||
it('should return 400 if the user already exists', async () => { | ||
// First, create a user | ||
await User.create({ | ||
userId: 1, | ||
name: 'Existing User', | ||
email: '[email protected]', | ||
passwordHashed: await bcrypt.hash('password123', 10), | ||
}); | ||
|
||
const res = await request(app) | ||
.post('/api/users/create-user') | ||
.send({ | ||
name: 'Existing User', | ||
email: '[email protected]', | ||
password: 'password123', | ||
}); | ||
|
||
expect(res.statusCode).toEqual(400); | ||
expect(res.body.message).toBe('An account with this email already exists'); | ||
|
||
// Clean up the created user after the test | ||
await User.findOneAndDelete({ email: '[email protected]' }); | ||
}); | ||
}); | ||
|
||
// Test the loginUser endpoint | ||
describe('POST /api/users/login', () => { | ||
it('should log in a user and return a token', async () => { | ||
// First, create a user to log in | ||
const user = await User.create({ | ||
userId: 1, | ||
name: 'Test User', | ||
email: '[email protected]', | ||
passwordHashed: await bcrypt.hash('password123', 10), | ||
}); | ||
|
||
const res = await request(app) | ||
.post('/api/users/login') | ||
.send({ | ||
email: '[email protected]', | ||
password: 'password123', | ||
}); | ||
|
||
expect(res.statusCode).toEqual(200); | ||
expect(res.body).toHaveProperty('token'); | ||
expect(res.body.message).toBe('Login successful'); | ||
|
||
// Clean up the created user after the test | ||
await User.findOneAndDelete({ email: '[email protected]' }); | ||
}); | ||
|
||
it('should return 401 if the email or password is incorrect', async () => { | ||
const res = await request(app) | ||
.post('/api/users/login') | ||
.send({ | ||
email: '[email protected]', | ||
password: 'wrongpassword', | ||
}); | ||
|
||
expect(res.statusCode).toEqual(401); | ||
expect(res.body.message).toBe('Invalid email or password.'); | ||
}); | ||
}); | ||
|
||
// Test the viewUsers endpoint | ||
describe('GET /api/users/view-users', () => { | ||
it('should return a list of all users', async () => { | ||
const res = await request(app).get('/api/users/view-users'); | ||
expect(res.statusCode).toEqual(200); | ||
expect(Array.isArray(res.body)).toBe(true); | ||
}); | ||
}); | ||
}); |
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,25 @@ | ||
//This file is for defining the schema and retrieving data | ||
//May turn this into two files moving forward (schema for one file, connection for another) | ||
|
||
const mongoose = require('mongoose') | ||
const dotenv = require('dotenv'); | ||
const path = require('path') | ||
|
||
// Determine the correct .env file based on the environment | ||
const envFile = process.env.NODE_ENV === 'production' ? '../.env.production' : '../.env.development'; | ||
|
||
// Load the appropriate .env file | ||
// dotenv.config({ path: `../${envFile}` }); | ||
dotenv.config({ path: path.resolve(__dirname, `../${envFile}`) }) | ||
|
||
const connectDB = async () => { | ||
try { | ||
const conn = await mongoose.connect(process.env.MONGO_URI); | ||
console.log(`MongoDB Connected: ${conn.connection.host}`); | ||
} catch (error) { | ||
console.error(`Error: ${error.message}`); | ||
process.exit(1); | ||
} | ||
}; | ||
|
||
module.exports = connectDB; |
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,28 @@ | ||
// controllers/dailyEntryController.js | ||
const DailyEntry = require('../models/DailyEntry'); | ||
|
||
// Enter daily data | ||
exports.enterDailyData = async (req, res) => { | ||
try { | ||
const { weight, steps, sleep, water, exercise } = req.body; | ||
|
||
let lastEntry = await DailyEntry.find().sort({ dailyEntryId: -1 }).limit(1); | ||
let newEntryId = lastEntry.length > 0 ? lastEntry[0].dailyEntryId + 1 : 1; | ||
|
||
const newDailyEntry = new DailyEntry({ | ||
dailyEntryId: newEntryId, | ||
userId: 10001, // TODO: Replace with logged-in user's ID | ||
entryDate: new Date(), // Adjust according to your form logic | ||
weight, | ||
steps, | ||
sleep, | ||
water, | ||
exercise, | ||
}); | ||
|
||
await newDailyEntry.save(); | ||
res.status(201).json({ message: 'Daily entry created successfully!' }); | ||
} catch (error) { | ||
res.status(500).json({ message: 'Error creating daily entry' }); | ||
} | ||
}; |
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,29 @@ | ||
// controllers/goalController.js | ||
const Goal = require('../models/Goal'); | ||
|
||
// Create a new goal | ||
exports.createGoal = async (req, res) => { | ||
try { | ||
const { type, targetValue } = req.body; | ||
|
||
let lastGoal = await Goal.find().sort({ goalId: -1 }).limit(1); | ||
let newGoalId = lastGoal.length > 0 ? lastGoal[0].goalId + 1 : 1; | ||
|
||
let unit; | ||
switch (type) { | ||
case 'sleep': unit = 'hours'; break; | ||
case 'weight': unit = 'lbs'; break; | ||
case 'steps': unit = 'steps'; break; | ||
case 'water': unit = 'glasses'; break; | ||
case 'exercise': unit = 'minutes'; break; | ||
default: unit = 'unknown'; | ||
} | ||
|
||
const newGoal = new Goal({ goalId: newGoalId, type, targetValue, unit }); | ||
await newGoal.save(); | ||
|
||
res.status(201).json({ message: 'New goal created successfully', goalId: newGoalId }); | ||
} catch (error) { | ||
res.status(500).json({ message: 'Failed to create goal' }); | ||
} | ||
}; |
Oops, something went wrong.