Skip to content

Commit

Permalink
Merge pull request #142 from Arquisoft/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
marco-qg authored May 1, 2024
2 parents 043e959 + ff8fa5a commit 5e2a49b
Show file tree
Hide file tree
Showing 22 changed files with 5,323 additions and 652 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,19 @@ jobs:
- run: npm --prefix userStatsService ci
- run: npm --prefix gameservice ci
- run: npm --prefix apisgatewayservice ci
- run: npm --prefix questionsservice ci
- run: npm --prefix questionsservice/questiongeneratorservice ci
- run: npm --prefix questionsservice/wikidataExtractor ci
- run: npm --prefix users/authservice test -- --coverage
- run: npm --prefix users/userservice test -- --coverage
- run: npm --prefix gatewayservice test -- --coverage
- run: npm --prefix webapp test -- --coverage
- run: npm --prefix storeQuestionService test -- --coverage
- run: npm --prefix userStatsService test -- --coverage
- run: npm --prefix gameservice test -- --coverage
# - run: npm --prefix gameservice test -- --coverage
- run: npm --prefix apisgatewayservice test -- --coverage
- run: npm --prefix questionsservice/questiongeneratorservice test -- --coverage
# - run: npm --prefix questionsservice/wikidataExtractor test -- --coverage
- name: Analyze with SonarCloud
uses: sonarsource/sonarcloud-github-action@master
env:
Expand Down
12 changes: 8 additions & 4 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,25 @@ jobs:
- run: npm --prefix users/authservice ci
- run: npm --prefix users/userservice ci
- run: npm --prefix gatewayservice ci
- run: npm --prefix questionsservice/questiongeneratorservice ci
- run: npm --prefix webapp ci
- run: npm --prefix storeQuestionService ci
- run: npm --prefix userStatsService ci
- run: npm --prefix webapp ci
- run: npm --prefix gameservice ci
- run: npm --prefix apisgatewayservice ci
- run: npm --prefix questionsservice ci
- run: npm --prefix questionsservice/questiongeneratorservice ci
- run: npm --prefix questionsservice/wikidataExtractor ci
- run: npm --prefix users/authservice test -- --coverage
- run: npm --prefix users/userservice test -- --coverage
- run: npm --prefix gatewayservice test -- --coverage
- run: npm --prefix webapp test -- --coverage
- run: npm --prefix storeQuestionService test -- --coverage
- run: npm --prefix userStatsService test -- --coverage
- run: npm --prefix gameservice test -- --coverage
- run: npm --prefix apisgatewayservice test -- --coverage
- run: npm --prefix questionsservice/questiongeneratorservice test -- --coverage
# - run: npm --prefix questionsservice/wikidataExtractor test -- --coverage


- name: Analyze with SonarCloud
uses: sonarsource/sonarcloud-github-action@master
env:
Expand Down Expand Up @@ -260,7 +264,7 @@ jobs:
deploy:
name: Deploy over SSH
runs-on: ubuntu-latest
needs: [docker-push-userservice,docker-push-authservice,docker-push-gatewayservice,docker-push-webapp, docker-push-questiongeneratorservice, docker-push-storequestionservice, docker-push-userstatsservice, docker-push-wikidataextractorservice, docker-push-gameservice]
needs: [docker-push-userservice,docker-push-authservice,docker-push-gatewayservice,docker-push-webapp, docker-push-questiongeneratorservice, docker-push-storequestionservice, docker-push-userstatsservice, docker-push-wikidataextractorservice, docker-push-gameservice, docker-push-apisgatewayservice]
steps:
- name: Deploy over SSH
uses: fifsky/ssh-action@master
Expand Down
24 changes: 20 additions & 4 deletions apisgatewayservice/apis-gateway-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ function catchAction(error, res) {
else if('response' in error && 'status' in error.response){
res.status(error.response.status).json({ error: 'Unknown error' });
} else {
console.log("Unknown error: " + error);
res.status(500).json({ error: 'Internal server error' });
}
// } else {
// res.status(500).json({ error: 'Internal server error' });
// }
}

app.use(metricsMiddleware);
Expand All @@ -49,6 +46,25 @@ app.get('/history/questions', async (req, res) => {
}
})

app.get('/usersStats', async (req, res) => {
try {
const users = await axios.get(userStatsServiceUrl+`/history/users`);

const usersInformation = users.data.map(user => ({
username: user.username,
tpoints: user.tpoints,
avgpoints: user.tpoints / user.ngames,
ttime: user.ttime,
avgtime: user.ttime / user.ngames,
createdAt: user.createdAt
}));

res.json(usersInformation);
} catch (error) {
catchAction(error, res)
}
})

// Start the gateway service
const server = app.listen(port, () => {
console.log(`Gateway Service listening at http://localhost:${port}`);
Expand Down
79 changes: 62 additions & 17 deletions apisgatewayservice/apis-gateway-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,23 @@ describe('User Service', () => {
describe('/users', () => {
it('should return user information', async () => {
const mockUsers = [
{ username: 'user1', createdAt: new Date() },
{ username: 'user2', createdAt: new Date() }
{ username: 'user1'},
{ username: 'user2'}
];
User.find = jest.fn().mockResolvedValue(mockUsers);


axios.get.mockResolvedValue({ data: mockUsers });

const res = await request(app).get('/users');

expect(res.statusCode).toEqual(200);

expect(res.body).toEqual(mockUsers);
});

it('should handle errors', async () => {

User.find = jest.fn().mockRejectedValue(new Error('Database error'));

const res = await request(app).get('/users');

expect(res.statusCode).toEqual(500);

expect(res.body).toEqual({ error: 'Database error' });
});
});
});


describe('Gateway Service', () => {
describe('/history/questions', () => {
it('should return all questions', async () => {
Expand All @@ -53,12 +44,66 @@ describe('Gateway Service', () => {
expect(res.body).toEqual(mockData);
});

it('should handle errors', async () => {
axios.get.mockRejectedValue(new Error('Error'));
});
});

describe('User data is send correctly', () => {
describe('/users', () => {
it('should return user information', async () => {
const mockUsers = [
{ username: 'user1', tpoints: 100, ttime: 10, ngames: 1 },
{ username: 'user2', tpoints: 100, ttime: 10, ngames: 2 }
];

const resutlsUsers = [
{ username: 'user1', avgpoints: 100, avgtime: 10, tpoints: 100, ttime: 10 },
{ username: 'user2', avgpoints: 50, avgtime: 5, tpoints: 100, ttime: 10 }
];

axios.get.mockResolvedValue({ data: mockUsers });

const res = await request(app).get('/usersStats');

expect(res.statusCode).toEqual(200);

expect(res.body).toEqual(resutlsUsers);
});


});
});

describe('Error Handling', () => {
describe('Error getting users', () => {
it('should handle error when getting users', async () => {
axios.get.mockRejectedValue(new Error('Failed to fetch users'));

const res = await request(app).get('/users');

expect(res.statusCode).toEqual(500);
expect(res.body).toEqual({ error: 'Internal server error' });
});
});

describe('Error getting history of questions', () => {
it('should handle error when getting history of questions', async () => {
axios.get.mockRejectedValue(new Error('Failed to fetch questions history'));

const res = await request(app).get('/history/questions');

expect(res.statusCode).toEqual(500);
expect(res.body).toEqual({ error: 'Error' });
expect(res.body).toEqual({ error: 'Internal server error' });
});
});

describe('Error getting history of questions', () => {
it('should handle error when getting history of questions', async () => {
axios.get.mockRejectedValue(new Error('Failed to fetch questions history'));

const res = await request(app).get('/usersStats');

expect(res.statusCode).toEqual(500);
expect(res.body).toEqual({ error: 'Internal server error' });
});
});
});
Loading

0 comments on commit 5e2a49b

Please sign in to comment.