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

Sciper checking #206

Merged
merged 13 commits into from
Dec 14, 2022
4 changes: 2 additions & 2 deletions internal/testing/fake/election.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ func NewForm(formID string) types.Form {
MainTitle: "dummyTitle",
},
FormID: formID,
Status: types.Closed,
Pubkey: pubKey,
Status: types.Closed,
Pubkey: pubKey,
Suffragia: types.Suffragia{
Ciphervotes: []types.Ciphervote{},
},
Expand Down
21 changes: 17 additions & 4 deletions web/backend/src/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,23 @@ app.post('/api/add_role', (req, res) => {
const { sciper } = req.body;
const { role } = req.body;

usersDB.put(sciper, role).catch((error) => {
res.status(500).send('Failed to add role');
console.log(error);
});
// The sciper has to contain 6 numbers
if (sciper < 999999 && sciper > 100000) {
// call https://search-api.epfl.ch/api/ldap?q=228271 if the answer
// empty then sciper invalid
axios.get(`https://search-api.epfl.ch/api/ldap?q=${sciper}`).then((response) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if the get fails ? it seems you don't catch that case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I missed this one, I just added a catch in a new commit

if (response.data.length === 0) {
res.status(400).send('Unknown sciper');
} else {
usersDB.put(sciper, role).catch((error) => {
res.status(500).send('Failed to add role');
console.log(error);
});
}
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For you information, you can also concatenate promises, by returning a promise from within the body of a then.

This is sometimes more readable, and you can group all the errors in one catch.

Suggested change
axios.get(`https://search-api.epfl.ch/api/ldap?q=${sciper}`).then((response) => {
if (response.data.length === 0) {
res.status(400).send('Unknown sciper');
} else {
usersDB.put(sciper, role).catch((error) => {
res.status(500).send('Failed to add role');
console.log(error);
});
}
});
axios.get(`https://search-api.epfl.ch/api/ldap?q=${sciper}`)
.then((response) => {
if (response.data.length === 0) {
res.status(400).send('Unknown sciper');
return;
}
return usersDB.put(sciper, role);
})
.catch((error) => {
res.status(500).send('Failed to add role');
console.log(error);
});;

} else {
res.status(400).send('sciper length incorrect');
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, you can reduce indentation by using guard clauses and making the code more readable.
Just a suggestion:

Suggested change
// The sciper has to contain 6 numbers
if (sciper < 999999 && sciper > 100000) {
// call https://search-api.epfl.ch/api/ldap?q=228271 if the answer
// empty then sciper invalid
axios.get(`https://search-api.epfl.ch/api/ldap?q=${sciper}`).then((response) => {
if (response.data.length === 0) {
res.status(400).send('Unknown sciper');
} else {
usersDB.put(sciper, role).catch((error) => {
res.status(500).send('Failed to add role');
console.log(error);
});
}
});
} else {
res.status(400).send('sciper length incorrect');
}
// The sciper has to contain 6 numbers
if (sciper > 999999 || sciper < 100000) {
res.status(400).send('sciper length incorrect');
return;
}
// call https://search-api.epfl.ch/api/ldap?q=228271
// if the answer is empty then the sciper is invalid
axios.get(`https://search-api.epfl.ch/api/ldap?q=${sciper}`).then((response) => {
if (response.data.length === 0) {
res.status(400).send('Unknown sciper');
} else {
usersDB.put(sciper, role).catch((error) => {
res.status(500).send('Failed to add role');
console.log(error);
});
}
});

});

// This call (only for admins) allow an admin to remove a role to a user.
Expand Down