Skip to content

Commit

Permalink
Refactored the gateway-service tests for removing duplicated code
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Mario committed Apr 15, 2024
1 parent 18636fe commit ab9b42c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 37 deletions.
12 changes: 6 additions & 6 deletions gatewayservice/gateway-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ app.get('/questions', async (req, res) => {

app.get('/questions/:lang/:amount/:type', async (req, res) => {
try {
const lang = req.params.lang;
const amount = req.params.amount;
const type = req.params.type;
const lang = req.params.lang.toString();
const amount = req.params.amount.toString();
const type = req.params.type.toString();
// Forward the question request to the quetion service
const questionResponse = await axios.get(questionServiceUrl+'/questions/' + lang + '/' + amount + '/' + type);

Check warning

Code scanning / SonarCloud

Server-side requests should not be vulnerable to forging attacks Medium

Change this code to not construct the URL from user-controlled data. See more on SonarCloud

Expand All @@ -76,8 +76,8 @@ app.get('/questions/:lang/:amount/:type', async (req, res) => {

app.get('/questions/:lang/:amount', async (req, res) => {
try {
const lang = req.params.lang;
const amount = req.params.amount;
const lang = req.params.lang.toString();
const amount = req.params.amount.toString();
// Forward the question request to the quetion service
const questionResponse = await axios.get(questionServiceUrl+'/questions/' + lang + '/' + amount);

Check warning

Code scanning / SonarCloud

Server-side requests should not be vulnerable to forging attacks Medium

Change this code to not construct the URL from user-controlled data. See more on SonarCloud

Expand All @@ -90,7 +90,7 @@ app.get('/questions/:lang/:amount', async (req, res) => {

app.get('/questions/:lang', async (req, res) => {
try {
const lang = req.params.lang;
const lang = req.params.lang.toString();
// Forward the question request to the quetion service
const questionResponse = await axios.get(questionServiceUrl+'/questions/' + lang);

Expand Down
64 changes: 33 additions & 31 deletions gatewayservice/gateway-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,28 @@ describe('Gateway Service', () => {
}
});

const question = { data: [{question: "¿Cuál es la población de Oviedo?",
answers: ["225089","272357","267855","231841"]}] };

//Dont need to check a good record just that it redirects the call
const record = {data : {record:'undefined'}};

axios.get.mockImplementation((url, data) => {
if (url.endsWith('/questions')){
return Promise.resolve({ data: [{question: "¿Cuál es la población de Oviedo?",
answers: ["225089","272357","267855","231841"]}] });
return Promise.resolve(question);
} else if (url.endsWith('/questions/es/1/CAPITAL')){
return Promise.resolve({ data: [{question: "¿Cuál es la población de Oviedo?",
answers: ["225089","272357","267855","231841"]}] });

return Promise.resolve(question);
} else if (url.endsWith('/questions/es/1')){
return Promise.resolve({ data: [{question: "¿Cuál es la población de Oviedo?",
answers: ["225089","272357","267855","231841"]}] });
return Promise.resolve(question);
} else if (url.endsWith('/questions/es')){
return Promise.resolve({ data: [{question: "¿Cuál es la población de Oviedo?",
answers: ["225089","272357","267855","231841"]}] });
return Promise.resolve(question);

} else if(url.endsWith('/record/testuser')){
//Dont need to check a good record just that it redirects the call
return Promise.resolve({data : {record:'undefined'}})
return Promise.resolve(record)
} else if(url.endsWith('/record/ranking/top10')){
//Dont need to check a good record just that it redirects the call
return Promise.resolve({data : {record:'undefined'}})
return Promise.resolve(record)
} else if(url.endsWith('/record/ranking/testuser')){
//Dont need to check a good record just that it redirects the call
return Promise.resolve({data : {record:'undefined'}})
return Promise.resolve(record)
}
});

Expand Down Expand Up @@ -72,35 +71,31 @@ describe('Gateway Service', () => {
const response = await request(app)
.get('/questions');

expect(response.statusCode).toBe(200);
expect(response.body[0]).toHaveProperty('question', "¿Cuál es la población de Oviedo?");
checkQuestion(response);
});

// Test /questions/:lang endpoint
it('should forward questions request to question service', async () => {
const response = await request(app)
.get('/questions/es');

expect(response.statusCode).toBe(200);
expect(response.body[0]).toHaveProperty('question', "¿Cuál es la población de Oviedo?");
checkQuestion(response);
});

// Test /questions/:lang/:amount endpoint
it('should forward questions request to question service', async () => {
const response = await request(app)
.get('/questions/es/1');

expect(response.statusCode).toBe(200);
expect(response.body[0]).toHaveProperty('question', "¿Cuál es la población de Oviedo?");
checkQuestion(response);
});

// Test /questions/:lang/:amount/:type endpoint
it('should forward questions request to question service', async () => {
const response = await request(app)
.get('/questions/es/1/CAPITAL');

expect(response.statusCode).toBe(200);
expect(response.body[0]).toHaveProperty('question', "¿Cuál es la población de Oviedo?");
checkQuestion(response);
});

// Test /record endpoint
Expand All @@ -117,25 +112,32 @@ describe('Gateway Service', () => {
const response = await request(app)
.get('/record/testuser');

expect(response.statusCode).toBe(200);
expect(response.body).toHaveProperty('record', "undefined");
checkRecord(response);
});

// Test /record/ranking/:user endpoint
it('should forward record request to record service', async () => {
const response = await request(app)
.get('/record/ranking/testuser');

expect(response.statusCode).toBe(200);
expect(response.body).toHaveProperty('record', "undefined");
checkRecord(response);
});

// Test /record/ranking/top10 endpoint
it('should forward record request to record service', async () => {
const response = await request(app)
.get('/record/ranking/top10');

expect(response.statusCode).toBe(200);
expect(response.body).toHaveProperty('record', "undefined");
checkRecord(response);

});
});
});

function checkRecord(response){
expect(response.statusCode).toBe(200);
expect(response.body).toHaveProperty('record', "undefined");
}

function checkQuestion(response){
expect(response.statusCode).toBe(200);
expect(response.body[0]).toHaveProperty('question', "¿Cuál es la población de Oviedo?");
}

0 comments on commit ab9b42c

Please sign in to comment.