Skip to content

Commit

Permalink
Merge pull request #63 from BUMETCS673/AddServerTestsToPipeline
Browse files Browse the repository at this point in the history
Add server tests to pipeline
  • Loading branch information
amanda-yee authored Oct 6, 2024
2 parents 001ceaa + faf9378 commit 202f10f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 44 deletions.
32 changes: 28 additions & 4 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ on:
push:
branches:
- dev
- AddServerTestsToPipeline

jobs:
# Step 1: Testing using Jest
test:
Test-Client:
runs-on: ubuntu-latest
steps:
- name: Check out repository
Expand All @@ -28,10 +29,33 @@ jobs:
cd code/client
npm test
# Step 2: Deployment job depends on successful completion of tests
deploy:
# Step 2: Testing using Jest
Test-Server:
runs-on: ubuntu-latest
needs: test # This ensures that the deploy job runs only if the test job succeeds
needs: Test-Client
steps:
- name: Check out repository
uses: actions/checkout@v2

- name: Setup Node.js environment
uses: actions/setup-node@v3
with:
node-version: '16.x' # Use the version of Node.js your project requires

- name: Install dependencies for the server
run: |
cd code/server
npm ci
- name: Run Jest tests
run: |
cd code/server
npm test
# Step 3: Deployment job depends on successful completion of tests
Deploy:
runs-on: ubuntu-latest
needs: Test-Server # This ensures that the deploy job runs only if the test job succeeds

env:
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
Expand Down
69 changes: 29 additions & 40 deletions code/server/__tests__/goalController.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 () => {
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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...
});

0 comments on commit 202f10f

Please sign in to comment.