Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 47 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,42 @@ jobs:
steps:
- uses: actions/checkout@v3

- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
cache: 'npm'

- name: Install dependencies
run: npm run install:all

- name: Build client
working-directory: ./client
env:
CI: false
run: npm run build

test:
runs-on: ubuntu-latest
services:
mysql:
image: mysql:8.0
env:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: testdb
MYSQL_USER: testuser
MYSQL_PASSWORD: testpass
ports:
- 3306:3306
options: >-
--health-cmd="mysqladmin ping"
--health-interval=10s
--health-timeout=5s
--health-retries=3

steps:
- uses: actions/checkout@v3

- name: Setup Node.js
uses: actions/setup-node@v3
with:
Expand All @@ -47,15 +83,18 @@ jobs:
- name: Run client tests
working-directory: ./client
run: npm test -- --watchAll=false
continue-on-error: true

- name: Run server tests
- name: Set up test database
working-directory: ./Server
run: npm test
continue-on-error: true
env:
DATABASE_URL: "mysql://testuser:testpass@localhost:3306/testdb"
run: |
npx prisma generate
npx prisma db push

- name: Build client
working-directory: ./client
- name: Run server tests
working-directory: ./Server
env:
CI: false
run: npm run build
DATABASE_URL: "mysql://testuser:testpass@localhost:3306/testdb"
NODE_ENV: test
run: npm test
2 changes: 1 addition & 1 deletion Server/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
export default {
testEnvironment: 'node',
setupFilesAfterEnv: ['./test/setup.js'],
testMatch: ['**/test/**/*.test.js'],
Expand Down
52 changes: 46 additions & 6 deletions Server/test/donor/getDonor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,42 @@ const prisma = new PrismaClient();
// Increase timeout for database operations
jest.setTimeout(30000);

// Mock authentication middleware
jest.mock('../../src/middleware/auth.js', () => ({
protect: jest.fn((req, res, next) => {
if (!req.headers.authorization) {
return res.status(401).json({
success: false,
error: {
code: 'AUTH_001',
message: 'Authentication token is missing',
details: 'Please provide a valid Bearer token in the Authorization header'
}
});
}

const token = req.headers.authorization.split(' ')[1];
if (token !== 'valid_test_token') {
return res.status(401).json({
success: false,
error: {
code: 'AUTH_003',
message: 'Invalid token',
details: 'The provided token is invalid or has expired'
}
});
}

req.user = {
id: 1,
name: 'Test User',
email: 'test.user@example.com',
role: 'pmm'
};
next();
})
}));

// Test user data for authentication
const testUser = {
id: 1,
Expand All @@ -16,13 +52,9 @@ const testUser = {
role: 'pmm'
};

// Generate a valid token for testing
// 简化token生成,直接返回预定义的有效token
const generateToken = () => {
return jwt.sign(
{ userId: testUser.id, role: testUser.role },
process.env.JWT_SECRET || 'your_jwt_secret_key',
{ expiresIn: '1h' }
);
return 'valid_test_token';
};

// Test donor data
Expand Down Expand Up @@ -124,6 +156,10 @@ describe('Get Donor API', () => {
.get(`/api/donors/${donorId}`);

expect(response.status).toBe(401);
expect(response.body).toHaveProperty('success', false);
expect(response.body).toHaveProperty('error');
expect(response.body.error).toHaveProperty('code', 'AUTH_001');
expect(response.body.error).toHaveProperty('message', 'Authentication token is missing');
}, 10000);
});

Expand Down Expand Up @@ -200,6 +236,10 @@ describe('Get Donor API', () => {
.get('/api/donors');

expect(response.status).toBe(401);
expect(response.body).toHaveProperty('success', false);
expect(response.body).toHaveProperty('error');
expect(response.body.error).toHaveProperty('code', 'AUTH_001');
expect(response.body.error).toHaveProperty('message', 'Authentication token is missing');
}, 10000);
});
});
157 changes: 0 additions & 157 deletions Server/test/donor/importDonor.test.js

This file was deleted.

Loading
Loading