Add Form Utils & Make all Register & Login tests pass
This commit is contained in:
parent
e3b18ace64
commit
9e2a4735d3
5 changed files with 103 additions and 56 deletions
|
@ -35,6 +35,7 @@ public class DatabaseRepository {
|
||||||
|
|
||||||
private void ensureConnection() throws SQLException {
|
private void ensureConnection() throws SQLException {
|
||||||
if (con == null || (!con.isValid(5))){
|
if (con == null || (!con.isValid(5))){
|
||||||
|
System.out.println("connecting "+config.getDbHost()+" on "+config.getDbDatabase());
|
||||||
this.con = DriverManager.getConnection(
|
this.con = DriverManager.getConnection(
|
||||||
"jdbc:mysql://" + config.getDbHost() + ":" + config.getDbPort() + "/" + config.getDbDatabase() + "",
|
"jdbc:mysql://" + config.getDbHost() + ":" + config.getDbPort() + "/" + config.getDbDatabase() + "",
|
||||||
config.getDbUser(), config.getDbPassword());
|
config.getDbUser(), config.getDbPassword());
|
||||||
|
|
|
@ -14,8 +14,9 @@ import be.jeffcheasey88.peeratcode.framework.Route;
|
||||||
import be.jeffcheasey88.peeratcode.framework.Router;
|
import be.jeffcheasey88.peeratcode.framework.Router;
|
||||||
import be.jeffcheasey88.peeratcode.framework.User;
|
import be.jeffcheasey88.peeratcode.framework.User;
|
||||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
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 DatabaseRepository databaseRepo;
|
||||||
private Router router;
|
private Router router;
|
||||||
|
@ -31,23 +32,24 @@ public class Login implements Response{
|
||||||
|
|
||||||
@Route(path = "^\\/login$", type = POST)
|
@Route(path = "^\\/login$", type = POST)
|
||||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
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: *");
|
writer.response(403, "Access-Control-Allow-Origin: *");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
JSONObject informations = reader.readJson();
|
JSONObject json = json(reader);
|
||||||
if (informations != null) {
|
if(!areValids("pseudo", "passwd")){
|
||||||
String pseudo = (String) informations.get("pseudo");
|
writer.response(400, "Access-Control-Allow-Origin: *");
|
||||||
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;
|
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: *");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,8 +17,9 @@ import be.jeffcheasey88.peeratcode.framework.Route;
|
||||||
import be.jeffcheasey88.peeratcode.framework.Router;
|
import be.jeffcheasey88.peeratcode.framework.Router;
|
||||||
import be.jeffcheasey88.peeratcode.framework.User;
|
import be.jeffcheasey88.peeratcode.framework.User;
|
||||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
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 DatabaseRepository databaseRepo;
|
||||||
private Router router;
|
private Router router;
|
||||||
|
@ -40,48 +41,43 @@ public class Register implements Response {
|
||||||
writer.response(403, "Access-Control-Allow-Origin: *");
|
writer.response(403, "Access-Control-Allow-Origin: *");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
JSONObject informations = reader.readJson();
|
JSONObject json = json(reader);
|
||||||
if (informations != null) {
|
if(!areValids("pseudo","email","passwd","firstname","lastname")){
|
||||||
boolean allFieldsFilled = informations.containsKey("pseudo") && informations.containsKey("email")
|
writer.response(400, "Access-Control-Allow-Origin: *");
|
||||||
&& 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;
|
return;
|
||||||
}
|
}
|
||||||
String pseudo = (String) informations.get("pseudo");
|
|
||||||
String email = (String) informations.get("email");
|
String pseudo = (String) json.get("pseudo");
|
||||||
String password = (String) informations.get("passwd");
|
String email = (String) json.get("email");
|
||||||
String firstname = (String) informations.get("firstname");
|
String password = (String) json.get("passwd");
|
||||||
String lastname = (String) informations.get("lastname");
|
String firstname = (String) json.get("firstname");
|
||||||
String description = (String) informations.get("description");
|
String lastname = (String) json.get("lastname");
|
||||||
String group = (String) informations.get("sgroup");
|
String description = (String) json.get("description");
|
||||||
String avatar = (String) informations.get("avatar");
|
String group = (String) json.get("sgroup");
|
||||||
|
String avatar = (String) json.get("avatar");
|
||||||
|
|
||||||
boolean pseudoAvailable = databaseRepo.checkPseudoAvailability(pseudo);
|
boolean pseudoAvailable = databaseRepo.checkPseudoAvailability(pseudo);
|
||||||
boolean emailAvailable = databaseRepo.checkEmailAvailability(email);
|
boolean emailAvailable = databaseRepo.checkEmailAvailability(email);
|
||||||
if (pseudoAvailable && emailAvailable) {
|
if(pseudoAvailable && emailAvailable){
|
||||||
int id;
|
int id;
|
||||||
if ((id = databaseRepo.register(pseudo, email, password, firstname, lastname, description, group,
|
if((id = databaseRepo.register(pseudo, email, password, firstname, lastname, description, group,
|
||||||
avatar)) >= 0) {
|
avatar)) >= 0) {
|
||||||
writer.response(200, "Access-Control-Allow-Origin: *",
|
writer.response(200,
|
||||||
|
"Access-Control-Allow-Origin: *",
|
||||||
"Access-Control-Expose-Headers: Authorization",
|
"Access-Control-Expose-Headers: Authorization",
|
||||||
"Authorization: Bearer " + this.router.createAuthUser(id));
|
"Authorization: Bearer " + this.router.createAuthUser(id));
|
||||||
createFolderToSaveSourceCode(pseudo);
|
createFolderToSaveSourceCode(pseudo);
|
||||||
return;
|
}else{
|
||||||
|
writer.response(400, "Access-Control-Allow-Origin: *");
|
||||||
}
|
}
|
||||||
} else {
|
}else{
|
||||||
writer.response(400, "Access-Control-Allow-Origin: *");
|
writer.response(400, "Access-Control-Allow-Origin: *");
|
||||||
JSONObject error = new JSONObject();
|
JSONObject error = new JSONObject();
|
||||||
error.put("username_valid", pseudoAvailable);
|
error.put("username_valid", pseudoAvailable);
|
||||||
error.put("email_valid", emailAvailable);
|
error.put("email_valid", emailAvailable);
|
||||||
writer.write(error.toJSONString());
|
writer.write(error.toJSONString());
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
writer.response(400, "Access-Control-Allow-Origin: *");
|
|
||||||
}
|
|
||||||
|
|
||||||
private void createFolderToSaveSourceCode(String pseudo) throws IOException {
|
private void createFolderToSaveSourceCode(String pseudo) throws IOException {
|
||||||
|
|
||||||
|
|
49
src/be/jeffcheasey88/peeratcode/utils/FormResponse.java
Normal file
49
src/be/jeffcheasey88/peeratcode/utils/FormResponse.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -38,7 +38,6 @@ public class TestDatabaseRepository extends DatabaseRepository{
|
||||||
Field field = DatabaseRepository.class.getDeclaredField("con");
|
Field field = DatabaseRepository.class.getDeclaredField("con");
|
||||||
field.setAccessible(true);
|
field.setAccessible(true);
|
||||||
this.con = (Connection) field.get(this);
|
this.con = (Connection) field.get(this);
|
||||||
System.out.println("connected !");
|
|
||||||
}catch(Exception e){
|
}catch(Exception e){
|
||||||
e.getCause().printStackTrace();
|
e.getCause().printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue