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

Autologin, changes on displayed information and api-doc updated #166

Merged
merged 17 commits into from
Apr 7, 2024
Merged
19 changes: 18 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,21 @@ jobs:
uses: sonarsource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
e2e-tests:
needs: [unit-tests]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm --prefix users/authservice install
- run: npm --prefix users/userservice install
- run: npm --prefix question_generator install
- run: npm --prefix questionservice install
- run: npm --prefix gameservice install
- run: npm --prefix gatewayservice install
- run: npm --prefix webapp install
- run: npm --prefix webapp run build
- run: npm --prefix webapp run test:e2e
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,4 @@ jobs:
wget https://raw.githubusercontent.com/arquisoft/wiq_es3b/master/docker-compose.yml -O docker-compose.yml
wget https://raw.githubusercontent.com/arquisoft/wiq_es3b/master/.env -O .env
docker compose --profile prod down
docker-compose --profile prod up -d --pull always
docker compose --profile prod up -d --pull always
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
102 changes: 77 additions & 25 deletions gatewayservice/gateway-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,49 @@
app.post('/login', async (req, res) => {
try {
// Forward the login request to the authentication service
const authResponse = await axios.post(authServiceUrl+'/login', req.body);
res.json(authResponse.data);
try{
const authResponse = await axios.post(authServiceUrl+'/login', req.body);
res.json(authResponse.data);
} catch (error) {
res.status(error.response.status).json(error.response.data);
}
} catch (error) {
res.status(500).json({ error: 'Service down' });
}
});
app.get('/verify', async (req, res) => {
try {
if (req.headers.authorization) {
try{
const authResponse = await axios.get(authServiceUrl+'/verify', {
Dismissed Show dismissed Hide dismissed
headers: {
Authorization: req.headers.authorization
}
});
res.json(authResponse.data);
} catch (error) {
res.status(error.response.status).json(error.response.data);
}
} else {
res.status(401).json({ error: 'Unauthorized' });
}
} catch (error) {
res.status(500).json({ error: error.message||'Service down' });
res.status(500).json({ error: 'Service down' });
}
});


app.post('/adduser', async (req, res) => {
try {
// Forward the add user request to the user service
const userResponse = await axios.post(userServiceUrl+'/adduser', req.body);
res.json(userResponse.data);
try{
const userResponse = await axios.post(userServiceUrl+'/adduser', req.body);
res.json(userResponse.data);
} catch (error) {
res.status(error.response.status).json(error.response.data);
}
} catch (error) {
res.status(500).json({ error: error.message||'Service down' });
res.status(500).json({ error: 'Service down' });
}
});

Expand All @@ -73,13 +102,17 @@
const apiUrl = new URL('/api/questions/create', questionGenerationServiceUrl);
apiUrl.search = queryParams.toString();

// Forward the add user request to the user service
const userResponse = await axios.get(apiUrl.toString(), {
headers: {
Authorization: req.headers.authorization
}
});
res.json(userResponse.data);
try{
// Forward the add user request to the user service
const userResponse = await axios.get(apiUrl.toString(), {
headers: {
Authorization: req.headers.authorization
}
});
res.json(userResponse.data);
}catch(error){
res.status(error.response.status).json(error.response.data);
}
} catch (error) {
res.status(500).json({ error: "Service down" });
}
Expand All @@ -91,8 +124,12 @@
if(Object.keys(req.query).length > 0) {
url += '?' + new URLSearchParams(req.query).toString();
}
const infoResponse = await axios.get(url);
res.json(infoResponse.data);
try{
const infoResponse = await axios.get(url);
res.json(infoResponse.data);
}catch(error){
res.status(error.response.status).json(error.response.data);
}
} catch (error) {
res.status(500).json({ error: "Service down" });
}
Expand All @@ -104,8 +141,12 @@
if(username){
url += '?user=' + username;
}
const infoResponse = await axios.get(url);
res.json(infoResponse.data);
try{
const infoResponse = await axios.get(url);
res.json(infoResponse.data);
}catch(error){
res.status(error.response.status).json(error.response.data);
}
} catch (error) {
res.status(500).json({ error: "Service down" });
}
Expand All @@ -117,8 +158,12 @@
if(username){
url += '?user=' + username;
}
const infoResponse = await axios.get(url);
res.json(infoResponse.data);
try{
const infoResponse = await axios.get(url);
res.json(infoResponse.data);
}catch(error){
res.status(error.response.status).json(error.response.data);
}
} catch (error) {
res.status(500).json({ error: "Service down" });
}
Expand All @@ -127,9 +172,13 @@
// Ruta para agregar una nuevo game
app.post('/addgame', async (req, res) => {
try {
// Forward the add game request to the games service
const gameResponse = await axios.post(gameServiceUrl + '/addgame', req.body);
res.json(gameResponse.data);
try{
// Forward the add game request to the games service
const gameResponse = await axios.post(gameServiceUrl + '/addgame', req.body);
res.json(gameResponse.data);
}catch(error){
res.status(error.response.status).json(error.response.data);
}
} catch (error) {
res.status(500).json({ error: "Service down" });
}
Expand All @@ -144,9 +193,12 @@
return;
}
const apiUrl = `${gameServiceUrl}/getParticipation/${userId}`;
const gameResponse = await axios.get(apiUrl);

res.json(gameResponse.data);
try{
const gameResponse = await axios.get(apiUrl);
res.json(gameResponse.data);
}catch(error){
res.status(error.response.status).json(error.response.data);
}
} catch (error) {
res.status(500).json({ error: "Service down" });
}
Expand Down
10 changes: 10 additions & 0 deletions gatewayservice/gateway-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ describe('Gateway Service', () => {
}
]
});
}else if(url.endsWith('/verify')){
return Promise.resolve({ data: { username: 'testuser' } });
}
});
// Test /health endpoint
Expand All @@ -85,6 +87,14 @@ describe('Gateway Service', () => {
expect(response.statusCode).toBe(200);
expect(response.body.token).toBe('mockedToken');
});
// Test /verify endpoint
it('should verify authorization token with auth service', async () => {
const response = await request(app)
.get('/verify')
.set('Authorization', 'Bearer mockedToken');
expect(response.statusCode).toBe(200);
expect(response.body).toHaveProperty('username', 'testuser');
});

// Test /adduser endpoint
it('should forward add user request to user service', async () => {
Expand Down
41 changes: 40 additions & 1 deletion gatewayservice/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,46 @@ paths:
security:
- bearerAuth: []


/verify:
get:
summary: Verify the authorization token
responses:
'200':
description: Authorization token verified successfully
content:
application/json:
schema:
type: object
properties:
data:
type: object
properties:
username:
type: string
createdAt:
type: string
format: date-time
'401':
description: Unauthorized
content:
application/json:
schema:
type: object
properties:
error:
type: string
'500':
description: Error occurred while verifying authorization token
content:
application/json:
schema:
type: object
properties:
error:
type: string
security:
- bearerAuth: []

/api/questions/create:
get:
summary: Create a question
Expand Down
Loading