Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gateway and authservice test #104

Merged
merged 6 commits into from
Apr 7, 2024
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
230 changes: 229 additions & 1 deletion gatewayservice/gateway-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,87 @@ afterAll(async () => {

jest.mock('axios');

// Define a function to generate mocked responses based on the URL
const generateMockResponse = (url, items) => {
if (url.endsWith('/question')) {
return Promise.resolve({
data: {
questionData: {
items: items
}
}
});
}
};



describe('Gateway Service', () => {
const mockUserData = [
{
username: 'user1',
ranking: {
category1: { points: 100, questions: 10, correct: 8, wrong: 2 },
category2: { points: 90, questions: 10, correct: 7, wrong: 3 }
}
},
{
username: 'user2',
ranking: {
category1: { points: 80, questions: 10, correct: 6, wrong: 4 },
category2: { points: 70, questions: 10, correct: 5, wrong: 5 }
}
}
];

// Mock responses from external services
axios.post.mockImplementation((url, data) => {
if (url.endsWith('/login')) {
return Promise.resolve({ data: { token: 'mockedToken' } });
} else if (url.endsWith('/adduser')) {
}
else if (url.endsWith('/adduser')) {
return Promise.resolve({ data: { userId: 'mockedUserId' } });
}
else if (url.endsWith('/imgs/answer')) {
// Simulate response from question service based on request data
if (data.question === 'expected_question' && data.answer === 'expected_answer') {
return Promise.resolve({ data: { correct: 'true' } });
} else {
return Promise.resolve({ data: { correct: 'false', associate: 'expected_associate' } });
}
}
});

axios.get.mockImplementation((url) => {
if (url.endsWith('/imgs/flags/question')) {
return generateMockResponse(url, [
{ item: 'Flag0', itemLabel: 'Country0', image: 'flag0.jpg' },
{ item: 'Flag1', itemLabel: 'Country1', image: 'flag1.jpg' },
]);
} else if (url.endsWith('/imgs/cities/question')) {
return generateMockResponse(url, [
{ item: 'City0', itemLabel: 'Country0', image: 'city0.jpg' },
{ item: 'City1', itemLabel: 'Country1', image: 'city1.jpg' },
]);
} else if (url.endsWith('/imgs/monuments/question')) {
return generateMockResponse(url, [
{ item: 'Monum0', itemLabel: 'Country0', image: 'monum0.jpg' },
{ item: 'Monum1', itemLabel: 'Country1', image: 'monum1.jpg' },
]);
} else if (url.endsWith('/imgs/tourist_attractions/question')) {
return generateMockResponse(url, [
{ item: 'TAtract0', itemLabel: 'Country0', image: 'tatract0.jpg' },
{ item: 'TAtract1', itemLabel: 'Country1', image: 'tatract1.jpg' },
]);
} else if (url.endsWith('/imgs/foods/question')) {
return generateMockResponse(url, [
{ item: 'Food0', itemLabel: 'Plate0', image: 'food0.jpg' },
{ item: 'Food1', itemLabel: 'Plate1', image: 'food1.jpg' },
]);
} else if (url.endsWith('/rankings/category1')) {
return Promise.resolve({ data: mockUserData });
}

});

// Test /login endpoint
Expand All @@ -37,4 +110,159 @@ describe('Gateway Service', () => {
expect(response.statusCode).toBe(200);
expect(response.body.userId).toBe('mockedUserId');
});

const testEndpoint = async (category, expectedItems) => {
const response = await request(app)
.get(`/imgs/${category}/question`);

// Verify response status code
expect(response.statusCode).toBe(200);

// Verify response body structure and content
expect(response.body.questionData).toBeDefined();
expect(response.body.questionData.items).toHaveLength(expectedItems.length);

// Iterate over items
expectedItems.forEach((expectedItem, index) => {
expect(response.body.questionData.items[index].item).toBe(expectedItem.item);
expect(response.body.questionData.items[index].itemLabel).toBe(expectedItem.itemLabel);
expect(response.body.questionData.items[index].image).toBe(expectedItem.image);
});
};

// Test /imgs/flags/question endpoint
it('should forward get Flags request to question service', async () => {
await testEndpoint('flags', [
{ item: 'Flag0', itemLabel: 'Country0', image: 'flag0.jpg' },
{ item: 'Flag1', itemLabel: 'Country1', image: 'flag1.jpg' },
]);
});

// Test /imgs/cities/question endpoint
it('should forward get Cities request to question service', async () => {
await testEndpoint('cities', [
{ item: 'City0', itemLabel: 'Country0', image: 'city0.jpg' },
{ item: 'City1', itemLabel: 'Country1', image: 'city1.jpg' },
]);
});


// Test /imgs/monuments/question endpoint
it('should forward get Monuments request to question service', async () => {
await testEndpoint('monuments', [
{ item: 'Monum0', itemLabel: 'Country0', image: 'monum0.jpg' },
{ item: 'Monum1', itemLabel: 'Country1', image: 'monum1.jpg' },
]);
});

// Test /imgs/tourist_attractions/question endpoint
it('should forward get Tourist Attractions request to question service', async () => {
await testEndpoint('tourist_attractions', [
{ item: 'TAtract0', itemLabel: 'Country0', image: 'tatract0.jpg' },
{ item: 'TAtract1', itemLabel: 'Country1', image: 'tatract1.jpg' },
]);
});

// Test /imgs/foods/question endpoint
it('should forward get Foods request to question service', async () => {
await testEndpoint('foods', [
{ item: 'Food0', itemLabel: 'Plate0', image: 'food0.jpg' },
{ item: 'Food1', itemLabel: 'Plate1', image: 'food1.jpg' },
]);
});

// Test /imgs/answer endpoint, when true
it('should forward answer request to question service and return correct response', async () => {
// Simulate request data
const requestData = {
answer: 'expected_answer',
username: 'testuser',
category: 'testcategory',
question: 'expected_question'
};

// Send POST request to gateway endpoint
const response = await request(app)
.post('/imgs/answer')
.send(requestData);

// Verify response status code
expect(response.statusCode).toBe(200);

// Verify response body
expect(response.body).toEqual({ correct: 'true' });
});

// Test /imgs/answer endpoint, when false
it('should forward answer request to question service and return correct response', async () => {
// Simulate request data
const requestData = {
answer: 'expected_answer',
username: 'testuser',
category: 'testcategory',
question: 'expected_question'
};

// Send POST request to gateway endpoint
const response = await request(app)
.post('/imgs/answer')
.send(requestData);

// Verify response status code
expect(response.statusCode).toBe(200);

// Verify response body
expect(response.body).toEqual({ correct: 'true' });
});

it('should forward answer request to question service and return incorrect response', async () => {
// Simulate request data
const requestData = {
answer: 'incorrect_answer',
username: 'testuser',
category: 'testcategory',
question: 'expected_question'
};

// Send POST request to gateway endpoint
const response = await request(app)
.post('/imgs/answer')
.send(requestData);

// Verify response status code
expect(response.statusCode).toBe(200);

// Verify response body
expect(response.body).toEqual({ correct: 'false', associate: 'expected_associate' });
});

// Test /rankings/:filter endpoint
it('should forward get rankings request to user service and return sorted rankings', async () => {
// Simulate request data
const filter = 'category1';

// Send GET request to gateway endpoint
const response = await request(app)
.get(`/rankings/${filter}`);

expect(response.statusCode).toBe(200);

expect(response.body).toHaveLength(mockUserData.length);
expect(response.body[0].username).toBe('user1');
expect(response.body[1].username).toBe('user2');

expect(response.body[0].ranking.category1.points).toBe(100);
expect(response.body[0].ranking.category1.questions).toBe(10);
expect(response.body[0].ranking.category1.correct).toBe(8);
expect(response.body[0].ranking.category1.wrong).toBe(2);

expect(response.body[1].ranking.category1.points).toBe(80);
expect(response.body[1].ranking.category1.questions).toBe(10);
expect(response.body[1].ranking.category1.correct).toBe(6);
expect(response.body[1].ranking.category1.wrong).toBe(4);

});



});
22 changes: 22 additions & 0 deletions users/authservice/auth-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,32 @@ afterAll(async () => {
await mongoServer.stop();
});

// Test /login
describe('Auth Service', () => {
// Test for successful login
it('Should perform a login operation /login', async () => {
const response = await request(app).post('/login').send(user);
expect(response.status).toBe(200);
expect(response.body).toHaveProperty('username', 'testuser');
});

// Test for invalid credentials
it('Should respond with 401 for invalid credentials', async () => {
const response = await request(app)
.post('/login')
.send({ username: 'invaliduser', password: 'invalidpassword' });
expect(response.status).toBe(401);
expect(response.body).toHaveProperty('error', 'Invalid credentials');
});

// Test for missing fields
it('Should respond with 400 for missing fields', async () => {
const response = await request(app).post('/login').send({ username: 'testuser' });
expect(response.status).toBe(500);
expect(response.body).toHaveProperty('error', 'Internal Server Error');
});


});

//