From 43d498a82290174c93c8cfafe5de0ddda689580b Mon Sep 17 00:00:00 2001 From: jeffcheasey88 <66554203+jeffcheasey88@users.noreply.github.com> Date: Wed, 12 Apr 2023 17:49:35 +0200 Subject: [PATCH] Fix login & Leaderboard --- src/be/jeffcheasey88/peeratcode/Main.java | 3 - .../peeratcode/framework/Client.java | 1 - .../peeratcode/framework/HttpReader.java | 1 + .../peeratcode/framework/HttpUtil.java | 1 + .../peeratcode/repository/DatabaseQuery.java | 147 +++++++++++++++++- .../repository/DatabaseRepository.java | 2 +- .../peeratcode/routes/Leaderboard.java | 1 - .../peeratcode/routes/Login.java | 4 +- .../peeratcode/routes/TmpRoutesTests.java | 1 - .../peeratcode/routes/TriggerTests.java | 54 +++++++ 10 files changed, 205 insertions(+), 10 deletions(-) create mode 100644 test/be/jeffcheasey88/peeratcode/routes/TriggerTests.java diff --git a/src/be/jeffcheasey88/peeratcode/Main.java b/src/be/jeffcheasey88/peeratcode/Main.java index af75795..5580baf 100644 --- a/src/be/jeffcheasey88/peeratcode/Main.java +++ b/src/be/jeffcheasey88/peeratcode/Main.java @@ -5,8 +5,6 @@ import static be.jeffcheasey88.peeratcode.framework.RequestType.OPTIONS; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; -import java.util.Timer; -import java.util.TimerTask; import java.util.regex.Matcher; import javax.net.ssl.SSLServerSocket; @@ -20,7 +18,6 @@ import be.jeffcheasey88.peeratcode.framework.Response; import be.jeffcheasey88.peeratcode.framework.Route; import be.jeffcheasey88.peeratcode.framework.Router; import be.jeffcheasey88.peeratcode.framework.User; -import be.jeffcheasey88.peeratcode.model.Completion; import be.jeffcheasey88.peeratcode.repository.DatabaseRepository; import be.jeffcheasey88.peeratcode.routes.BadgeDetails; import be.jeffcheasey88.peeratcode.routes.ChapterElement; diff --git a/src/be/jeffcheasey88/peeratcode/framework/Client.java b/src/be/jeffcheasey88/peeratcode/framework/Client.java index 9aa8d1d..81a11cd 100644 --- a/src/be/jeffcheasey88/peeratcode/framework/Client.java +++ b/src/be/jeffcheasey88/peeratcode/framework/Client.java @@ -4,7 +4,6 @@ import java.net.Socket; import java.util.Arrays; import org.jose4j.jwa.AlgorithmConstraints.ConstraintType; -import org.jose4j.jwk.RsaJsonWebKey; import org.jose4j.jws.AlgorithmIdentifiers; import org.jose4j.jwt.JwtClaims; import org.jose4j.jwt.consumer.JwtConsumer; diff --git a/src/be/jeffcheasey88/peeratcode/framework/HttpReader.java b/src/be/jeffcheasey88/peeratcode/framework/HttpReader.java index 74ef71d..338beea 100644 --- a/src/be/jeffcheasey88/peeratcode/framework/HttpReader.java +++ b/src/be/jeffcheasey88/peeratcode/framework/HttpReader.java @@ -7,6 +7,7 @@ import java.io.InputStreamReader; import java.net.Socket; import java.util.HashMap; import java.util.Map; +import java.util.Map.Entry; import java.util.regex.Matcher; import java.util.regex.Pattern; diff --git a/src/be/jeffcheasey88/peeratcode/framework/HttpUtil.java b/src/be/jeffcheasey88/peeratcode/framework/HttpUtil.java index b006821..ba3ab79 100644 --- a/src/be/jeffcheasey88/peeratcode/framework/HttpUtil.java +++ b/src/be/jeffcheasey88/peeratcode/framework/HttpUtil.java @@ -56,6 +56,7 @@ Content-Type: text/javascript public static void switchToWebSocket(HttpReader reader, HttpWriter writer) throws Exception{ String key = reader.getHeader("Sec-WebSocket-Key"); + if(key == null) key = reader.getHeader("Sec-Websocket-Key"); if (key == null) throw new IllegalArgumentException(); writer.write("HTTP/1.1 101 Switching Protocols\n"); diff --git a/src/be/jeffcheasey88/peeratcode/repository/DatabaseQuery.java b/src/be/jeffcheasey88/peeratcode/repository/DatabaseQuery.java index 920027b..99880a7 100644 --- a/src/be/jeffcheasey88/peeratcode/repository/DatabaseQuery.java +++ b/src/be/jeffcheasey88/peeratcode/repository/DatabaseQuery.java @@ -59,8 +59,75 @@ public enum DatabaseQuery { // BADGES GET_BADGE("SELECT * FROM badges WHERE id_badge = ?"), GET_BADGES_OF_PLAYER( - "SELECT * FROM badges b LEFT JOIN containsBadges cb ON cb.fk_badge = b.id_badge WHERE cb.fk_player = ?"); + "SELECT * FROM badges b LEFT JOIN containsBadges cb ON cb.fk_badge = b.id_badge WHERE cb.fk_player = ?"), + //TRIGGER + FIRST_TRY("CREATE OR REPLACE TRIGGER FirstTry\r\n" + + "AFTER INSERT\r\n" + + " ON completions FOR EACH ROW\r\n" + + "BEGIN\r\n" + + " DECLARE badge INT;\r\n" + + " DECLARE player INT;\r\n" + + " DECLARE contain INT;\r\n" + + "\r\n" + + " SELECT id_badge\r\n" + + " INTO badge\r\n" + + " FROM badges\r\n" + + " WHERE name = 'FirstTry';\r\n" + + "\r\n" + + " IF @badge is not null THEN\r\n" + + "\r\n" + + " SELECT fk_player\r\n" + + " INTO player\r\n" + + " FROM inserted;\r\n" + + "\r\n" + + " SELECT count(*)\r\n" + + " INTO contain\r\n" + + " FROM containsBadges\r\n" + + " WHERE fk_badge = badge AND fk_player = player;\r\n" + + "\r\n" + + " IF (@contain = 0) THEN\r\n" + + "\r\n" + + " INSERT INTO containsBadges(fk_player, fk_badge) VALUES (@player, @badge);\r\n" + + " END IF;\r\n" + + " END IF;\r\n" + + "END;"), + EventParticipation("CREATE OR REPLACE TRIGGER EventParticipation\r\n" + + "AFTER INSERT\r\n" + + " ON completions FOR EACH ROW\r\n" + + "BEGIN\r\n" + + " DECLARE badge INT;\r\n" + + " DECLARE endDate datetime;\r\n" + + " DECLARE player INT;\r\n" + + " DECLARE contain INT;\r\n" + + "\r\n" + + " SELECT id_badge\r\n" + + " INTO badge\r\n" + + " FROM badges\r\n" + + " WHERE name = 'EventParticipation';\r\n" + + "\r\n" + + " IF @badge is not null THEN\r\n" + + "\r\n" + + " SELECT c.end_date, i.fk_player\r\n" + + " INTO endDate, player\r\n" + + " FROM inserted i\r\n" + + " JOIN puzzles p ON i.fk_puzzle = p.id_puzzle\r\n" + + " JOIN chapters c on p.fk_chapter = c.id_chapter;\r\n" + + "\r\n" + + " IF @endDate is not null THEN\r\n" + + "\r\n" + + " SELECT count(*)\r\n" + + " INTO contain\r\n" + + " FROM containsBadges\r\n" + + " WHERE fk_badge = badge AND fk_player = player;\r\n" + + "\r\n" + + " IF (@contain = 0) THEN\r\n" + + " INSERT INTO containsBadges(fk_player, fk_badge) VALUES (@player, @badge);\r\n" + + " END IF;\r\n" + + " END IF;\r\n" + + " END IF;\r\n" + + "END;"); + private String request; DatabaseQuery(DatabaseQuery parent, String request) { @@ -80,3 +147,81 @@ public enum DatabaseQuery { return this.request; } } + + +/* TRIGGER PLACE + * + +CREATE OR REPLACE TRIGGER FirstTry +AFTER INSERT + ON completions FOR EACH ROW +BEGIN + DECLARE badge INT; + DECLARE player INT; + DECLARE contain INT; + + SELECT id_badge + INTO badge + FROM badges + WHERE name = 'FirstTry'; + + IF @badge is not null THEN + + SELECT fk_player + INTO player + FROM inserted; + + SELECT count(*) + INTO contain + FROM containsBadges + WHERE fk_badge = badge AND fk_player = player; + + IF (@contain = 0) THEN + + INSERT INTO containsBadges(fk_player, fk_badge) VALUES (@player, @badge); + END IF; + END IF; +END; + + + + +CREATE OR REPLACE TRIGGER EventParticipation +AFTER INSERT + ON completions FOR EACH ROW +BEGIN + DECLARE badge INT; + DECLARE endDate datetime; + DECLARE player INT; + DECLARE contain INT; + + SELECT id_badge + INTO badge + FROM badges + WHERE name = 'EventParticipation'; + + IF @badge is not null THEN + + SELECT c.end_date, i.fk_player + INTO endDate, player + FROM inserted i + JOIN puzzles p ON i.fk_puzzle = p.id_puzzle + JOIN chapters c on p.fk_chapter = c.id_chapter; + + IF @endDate is not null THEN + + SELECT count(*) + INTO contain + FROM containsBadges + WHERE fk_badge = badge AND fk_player = player; + + IF (@contain = 0) THEN + INSERT INTO containsBadges(fk_player, fk_badge) VALUES (@player, @badge); + END IF; + END IF; + END IF; +END; + + + * + */ diff --git a/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java b/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java index 612f159..21b719e 100644 --- a/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java +++ b/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java @@ -32,7 +32,7 @@ public class DatabaseRepository { public DatabaseRepository(Configuration config) { this.config = config; } - + private void ensureConnection() throws SQLException { if (con == null || (!con.isValid(5))) { this.con = DriverManager.getConnection( diff --git a/src/be/jeffcheasey88/peeratcode/routes/Leaderboard.java b/src/be/jeffcheasey88/peeratcode/routes/Leaderboard.java index 660557c..8815dbd 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/Leaderboard.java +++ b/src/be/jeffcheasey88/peeratcode/routes/Leaderboard.java @@ -1,7 +1,6 @@ package be.jeffcheasey88.peeratcode.routes; import java.io.IOException; -import java.util.Base64; import java.util.SortedSet; import java.util.regex.Matcher; diff --git a/src/be/jeffcheasey88/peeratcode/routes/Login.java b/src/be/jeffcheasey88/peeratcode/routes/Login.java index 1b0755b..6be3f2f 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/Login.java +++ b/src/be/jeffcheasey88/peeratcode/routes/Login.java @@ -40,10 +40,10 @@ public class Login implements Response { HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *", "Access-Control-Expose-Headers: Authorization", "Authorization: Bearer " + this.router.createAuthUser(id)); + return; } - } else { - HttpUtil.responseHeaders(writer, 400, "Access-Control-Allow-Origin: *"); } + HttpUtil.responseHeaders(writer, 400, "Access-Control-Allow-Origin: *"); } } diff --git a/test/be/jeffcheasey88/peeratcode/routes/TmpRoutesTests.java b/test/be/jeffcheasey88/peeratcode/routes/TmpRoutesTests.java index e28511a..ad258b6 100644 --- a/test/be/jeffcheasey88/peeratcode/routes/TmpRoutesTests.java +++ b/test/be/jeffcheasey88/peeratcode/routes/TmpRoutesTests.java @@ -2,7 +2,6 @@ package be.jeffcheasey88.peeratcode.routes; import static org.junit.jupiter.api.Assertions.fail; -import org.json.simple.JSONObject; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; diff --git a/test/be/jeffcheasey88/peeratcode/routes/TriggerTests.java b/test/be/jeffcheasey88/peeratcode/routes/TriggerTests.java new file mode 100644 index 0000000..d570c7e --- /dev/null +++ b/test/be/jeffcheasey88/peeratcode/routes/TriggerTests.java @@ -0,0 +1,54 @@ +package be.jeffcheasey88.peeratcode.routes; + +import static org.junit.jupiter.api.Assertions.fail; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.TestInstance.Lifecycle; + +import be.jeffcheasey88.peeratcode.Main; +import be.jeffcheasey88.peeratcode.webclient.WebClient; + +@TestInstance(Lifecycle.PER_CLASS) +public class TriggerTests { + + private Thread server; + private WebClient client; + private boolean ready; + + @BeforeAll + void init(){ + server = new Thread(new Runnable(){ + @Override + public void run(){ + try { + Main.main(null); + } catch (Exception e) { + e.printStackTrace(); + }; + } + }); + server.start(); + client = new WebClient("localhost", 80); + } + + @AfterAll + void close(){ + server.interrupt(); + } + + @Test + void testOnDeployed(){ + try { + client.auth("JeffCheasey88", "TheoPueDesPieds"); + client.route("/puzzleResponse/9", "POST", WebClient.buildMultiPartData("1")); + + client.route("/player/", "GET"); + }catch(Exception e){ + e.printStackTrace(); + fail(e); + } + } +}