Add Form Utils & Make all Register & Login tests pass

This commit is contained in:
jeffcheasey88 2023-09-04 16:58:55 +02:00
parent e3b18ace64
commit 9e2a4735d3
5 changed files with 103 additions and 56 deletions

View file

@ -35,6 +35,7 @@ public class DatabaseRepository {
private void ensureConnection() throws SQLException {
if (con == null || (!con.isValid(5))){
System.out.println("connecting "+config.getDbHost()+" on "+config.getDbDatabase());
this.con = DriverManager.getConnection(
"jdbc:mysql://" + config.getDbHost() + ":" + config.getDbPort() + "/" + config.getDbDatabase() + "",
config.getDbUser(), config.getDbPassword());

View file

@ -14,8 +14,9 @@ import be.jeffcheasey88.peeratcode.framework.Route;
import be.jeffcheasey88.peeratcode.framework.Router;
import be.jeffcheasey88.peeratcode.framework.User;
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
import be.jeffcheasey88.peeratcode.utils.FormResponse;
public class Login implements Response{
public class Login extends FormResponse{
private DatabaseRepository databaseRepo;
private Router router;
@ -31,23 +32,24 @@ public class Login implements Response{
@Route(path = "^\\/login$", type = POST)
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
if (user != null) {
if(user != null){
writer.response(403, "Access-Control-Allow-Origin: *");
return;
}
JSONObject informations = reader.readJson();
if (informations != null) {
String pseudo = (String) informations.get("pseudo");
String password = (String) informations.get("passwd");
int id;
if ((id = databaseRepo.login(pseudo, password)) >= 0) {
writer.response(200, "Access-Control-Allow-Origin: *",
"Access-Control-Expose-Headers: Authorization",
"Authorization: Bearer " + this.router.createAuthUser(id));
return;
}
JSONObject json = json(reader);
if(!areValids("pseudo", "passwd")){
writer.response(400, "Access-Control-Allow-Origin: *");
return;
}
int id;
if((id = databaseRepo.login((String)json.get("pseudo"), (String)json.get("passwd"))) >= 0){
writer.response(200,
"Access-Control-Allow-Origin: *",
"Access-Control-Expose-Headers: Authorization",
"Authorization: Bearer " + this.router.createAuthUser(id));
}else{
writer.response(400,"Access-Control-Allow-Origin: *");
}
writer.response(400, "Access-Control-Allow-Origin: *");
}
}

View file

@ -17,8 +17,9 @@ import be.jeffcheasey88.peeratcode.framework.Route;
import be.jeffcheasey88.peeratcode.framework.Router;
import be.jeffcheasey88.peeratcode.framework.User;
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
import be.jeffcheasey88.peeratcode.utils.FormResponse;
public class Register implements Response {
public class Register extends FormResponse {
private DatabaseRepository databaseRepo;
private Router router;
@ -40,47 +41,42 @@ public class Register implements Response {
writer.response(403, "Access-Control-Allow-Origin: *");
return;
}
JSONObject informations = reader.readJson();
if (informations != null) {
boolean allFieldsFilled = informations.containsKey("pseudo") && informations.containsKey("email")
&& informations.containsKey("passwd") && informations.containsKey("firstname")
&& informations.containsKey("lastname") && informations.containsKey("description")
&& informations.containsKey("sgroup") && informations.containsKey("avatar");
if (!allFieldsFilled) {
writer.response(403, "Access-Control-Allow-Origin: *");
return;
}
String pseudo = (String) informations.get("pseudo");
String email = (String) informations.get("email");
String password = (String) informations.get("passwd");
String firstname = (String) informations.get("firstname");
String lastname = (String) informations.get("lastname");
String description = (String) informations.get("description");
String group = (String) informations.get("sgroup");
String avatar = (String) informations.get("avatar");
boolean pseudoAvailable = databaseRepo.checkPseudoAvailability(pseudo);
boolean emailAvailable = databaseRepo.checkEmailAvailability(email);
if (pseudoAvailable && emailAvailable) {
int id;
if ((id = databaseRepo.register(pseudo, email, password, firstname, lastname, description, group,
avatar)) >= 0) {
writer.response(200, "Access-Control-Allow-Origin: *",
"Access-Control-Expose-Headers: Authorization",
"Authorization: Bearer " + this.router.createAuthUser(id));
createFolderToSaveSourceCode(pseudo);
return;
}
} else {
writer.response(400, "Access-Control-Allow-Origin: *");
JSONObject error = new JSONObject();
error.put("username_valid", pseudoAvailable);
error.put("email_valid", emailAvailable);
writer.write(error.toJSONString());
return;
}
JSONObject json = json(reader);
if(!areValids("pseudo","email","passwd","firstname","lastname")){
writer.response(400, "Access-Control-Allow-Origin: *");
return;
}
String pseudo = (String) json.get("pseudo");
String email = (String) json.get("email");
String password = (String) json.get("passwd");
String firstname = (String) json.get("firstname");
String lastname = (String) json.get("lastname");
String description = (String) json.get("description");
String group = (String) json.get("sgroup");
String avatar = (String) json.get("avatar");
boolean pseudoAvailable = databaseRepo.checkPseudoAvailability(pseudo);
boolean emailAvailable = databaseRepo.checkEmailAvailability(email);
if(pseudoAvailable && emailAvailable){
int id;
if((id = databaseRepo.register(pseudo, email, password, firstname, lastname, description, group,
avatar)) >= 0) {
writer.response(200,
"Access-Control-Allow-Origin: *",
"Access-Control-Expose-Headers: Authorization",
"Authorization: Bearer " + this.router.createAuthUser(id));
createFolderToSaveSourceCode(pseudo);
}else{
writer.response(400, "Access-Control-Allow-Origin: *");
}
}else{
writer.response(400, "Access-Control-Allow-Origin: *");
JSONObject error = new JSONObject();
error.put("username_valid", pseudoAvailable);
error.put("email_valid", emailAvailable);
writer.write(error.toJSONString());
}
writer.response(400, "Access-Control-Allow-Origin: *");
}
private void createFolderToSaveSourceCode(String pseudo) throws IOException {

View file

@ -0,0 +1,49 @@
package be.jeffcheasey88.peeratcode.utils;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
import org.json.simple.JSONAware;
import be.jeffcheasey88.peeratcode.framework.HttpReader;
import be.jeffcheasey88.peeratcode.framework.Response;
public abstract class FormResponse implements Response{
private JSONAware json;
private Map<String, Pattern> checker;
public FormResponse(){
this.checker = new HashMap<>();
}
public void validator(String key, Pattern regex){
this.checker.put(key, regex);
}
public <T extends JSONAware> T json(HttpReader reader) throws Exception{
return (T) (this.json = reader.readJson());
}
public boolean hasFields(String... fields){
Map<?,?> map = (Map<?,?>)json;
for(String field : fields){
if(!map.containsKey(field)) return false;
}
return true;
}
public boolean areValids(String... fields){
Map<?,?> map = (Map<?,?>)json;
for(String field : fields){
String value = (String) map.get(field);
if(value == null) return false;
if(value.isEmpty()) return false;
Pattern pattern = checker.get(field);
if(pattern == null) continue;
if(!pattern.matcher(value).matches()) return false;
}
return true;
}
}

View file

@ -38,7 +38,6 @@ public class TestDatabaseRepository extends DatabaseRepository{
Field field = DatabaseRepository.class.getDeclaredField("con");
field.setAccessible(true);
this.con = (Connection) field.get(this);
System.out.println("connected !");
}catch(Exception e){
e.getCause().printStackTrace();
}