From ff0efa69503dbedc6ce1b7f6f50d4b0082b1abe6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Silvia=20Pab=C3=B3n?= <62608580+SilviaPabon@users.noreply.github.com> Date: Sun, 8 Oct 2023 09:09:47 -0500 Subject: [PATCH] Feat: share file feature (#61) * fix: deleting attach from docker * feat: starting share-file feat changes * feat: init of adding error and msg according with statuscode * feat: some last adjustments for share file feat * feat: share file feat concluded and test of it added * fix: deleting repeated code and adding a check for errors after request otherUserUUID * fix: deleting unnecessary checkFile action in test * chore: remove unused import --------- Co-authored-by: woynert --- .../gateway/controller/CtrlShareFile.java | 80 ++++++++++++++++++ .../main/java/gateway/soap/ServiceImp.java | 5 +- .../gateway/soap/request/ReqShareFile.java | 5 +- app/src/test/java/gateway/ITShareFile.java | 82 +++++++++++++++++++ docker-compose.yml | 3 +- 5 files changed, 171 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/gateway/controller/CtrlShareFile.java create mode 100644 app/src/test/java/gateway/ITShareFile.java diff --git a/app/src/main/java/gateway/controller/CtrlShareFile.java b/app/src/main/java/gateway/controller/CtrlShareFile.java new file mode 100644 index 0000000..1eb31a7 --- /dev/null +++ b/app/src/main/java/gateway/controller/CtrlShareFile.java @@ -0,0 +1,80 @@ +package gateway.controller; + +import gateway.config.Config; +import gateway.services.ServiceAuth; +import gateway.services.ServiceAuth.ResUUID; +import gateway.services.UtilValidator; +import gateway.soap.request.ReqShareFile; +import gateway.soap.response.ResStatus; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpRequest.BodyPublishers; +import java.net.http.HttpResponse; +import java.util.UUID; +import org.json.JSONObject; + +public class CtrlShareFile +{ + public static ResStatus share_file (ReqShareFile args) + { + ResStatus statusRes = new ResStatus (); + + // validations of all fields + ResStatus resValidate = UtilValidator.validate (args); + if (resValidate.error) { + return ResStatus.downCast (ResStatus.class, resValidate); + } + + // validation of auth + ResStatus resAuth = ServiceAuth.authenticate (args.token); + if (resAuth.error) { + return ResStatus.downCast (ResStatus.class, resAuth); + } + + // obtain uuid from user and otheruser + UUID userUUID = UUID.fromString (ServiceAuth.tokenGetClaim (args.token, "uuid")); + ResUUID otherUserUUID = ServiceAuth.getUserUUID (args.token, args.otherUsername); + if (otherUserUUID.error) { + return ResStatus.downCast (ResStatus.class, otherUserUUID); + } + + // request to share file with otheruser + JSONObject requestBody = new JSONObject (); + requestBody.put ("otherUserUUID", otherUserUUID.uuid); + + String url = + Config.getMetadataBaseUrl () + "/files/share/" + userUUID + "/" + args.fileUUID; + + try { + + HttpClient client = HttpClient.newHttpClient (); + HttpRequest request = HttpRequest.newBuilder () + .uri (URI.create (url)) + .POST (BodyPublishers.ofString (requestBody.toString ())) + .header ("Content-Type", "application/json") + .build (); + + // Response + HttpResponse response = + client.send (request, HttpResponse.BodyHandlers.ofString ()); + statusRes.code = response.statusCode (); + + if (statusRes.code == 204) { + statusRes.error = false; + statusRes.msg = "The file have been shared"; + } else { + JSONObject responseBody = new JSONObject (response.body ()); + statusRes.error = true; + statusRes.msg = responseBody.getString ("message"); + } + } catch (Exception e) { + e.printStackTrace (); + statusRes.code = 500; + statusRes.error = true; + statusRes.msg = "Internal error, try again later"; + } + + return statusRes; + } +} diff --git a/app/src/main/java/gateway/soap/ServiceImp.java b/app/src/main/java/gateway/soap/ServiceImp.java index b004175..c26c687 100644 --- a/app/src/main/java/gateway/soap/ServiceImp.java +++ b/app/src/main/java/gateway/soap/ServiceImp.java @@ -56,7 +56,10 @@ // sharing - @WebMethod public ResStatus share_file (ReqShareFile args) { return null; } + @WebMethod public ResStatus share_file (ReqShareFile args) + { + return CtrlShareFile.share_file (args); + } @WebMethod public ResStatus unshare_file (ReqShareRemove args) { return null; } diff --git a/app/src/main/java/gateway/soap/request/ReqShareFile.java b/app/src/main/java/gateway/soap/request/ReqShareFile.java index f92fbfc..387d4c4 100644 --- a/app/src/main/java/gateway/soap/request/ReqShareFile.java +++ b/app/src/main/java/gateway/soap/request/ReqShareFile.java @@ -1,9 +1,10 @@ package gateway.soap.request; +import jakarta.validation.constraints.NotNull; import java.util.UUID; public class ReqShareFile extends Authorization { - public UUID fileUUID; - public String otherUsername; + @NotNull public UUID fileUUID; + @NotNull public String otherUsername; } diff --git a/app/src/test/java/gateway/ITShareFile.java b/app/src/test/java/gateway/ITShareFile.java new file mode 100644 index 0000000..3d94c5a --- /dev/null +++ b/app/src/test/java/gateway/ITShareFile.java @@ -0,0 +1,82 @@ +package gateway; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import gateway.config.Config; +import gateway.controller.CtrlAccountRegister; +import gateway.controller.CtrlShareFile; +import gateway.services.ServiceAuth; +import gateway.services.ServiceMetadata; +import gateway.services.ServiceMetadata.ResSaveFile; +import gateway.soap.request.Credentials; +import gateway.soap.request.ReqShareFile; +import gateway.soap.response.ResSession; +import gateway.soap.response.ResStatus; +import java.util.Random; +import java.util.UUID; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class ITShareFile +{ + + @BeforeEach void setup () { Config.initializeFromEnv (); } + + @Test void ShareFile () + { + + String username1 = UUID.randomUUID ().toString (); + String username2 = UUID.randomUUID ().toString (); + + // register + String tokenUser1 = registerAndLoginUserSuccess (username1); + String tokenUser2 = registerAndLoginUserSuccess (username2); + + // create a file and aux file + ResSaveFile resSaveFile1 = createFile (tokenUser1); + ResSaveFile resSaveFile2 = createFile (tokenUser2); + + // 204 + ReqShareFile reqShareFile = new ReqShareFile (); + reqShareFile.fileUUID = resSaveFile1.fileUUID; + reqShareFile.otherUsername = username2; + reqShareFile.token = tokenUser1; + ResStatus res = CtrlShareFile.share_file (reqShareFile); + assertEquals (204, res.code, "The file have been shared"); + + // 409 + res = CtrlShareFile.share_file (reqShareFile); + assertEquals (409, res.code, "The file is already shared with the given user."); + + // 403 + reqShareFile.fileUUID = resSaveFile2.fileUUID; + res = CtrlShareFile.share_file (reqShareFile); + assertEquals (403, res.code, "The file is not owned by the user."); + + // 401 + reqShareFile.token = "token_invalid"; + res = CtrlShareFile.share_file (reqShareFile); + assertEquals (401, res.code, "unauthorized"); + + // 400 + reqShareFile.otherUsername = null; + res = CtrlShareFile.share_file (reqShareFile); + assertEquals ( + 400, res.code, + "The owner_uuid or file_uuid were not a valid UUID or the JSON body does't fullfill the validations."); + } + + private String registerAndLoginUserSuccess (String username) + { + ResSession res = CtrlAccountRegister.account_register (new Credentials (username, "pass")); + assertEquals (201, res.code, "User registered successfully"); + return res.auth.token; + } + + private ResSaveFile createFile (String token) + { + return ServiceMetadata.saveFile ( + UUID.fromString (ServiceAuth.tokenGetClaim (token, "uuid")), null, "txt", "filename_t", + (new Random ().nextInt (3000) + 1)); + } +} diff --git a/docker-compose.yml b/docker-compose.yml index 53ffcde..2b34caf 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -79,4 +79,5 @@ services: ADMINER_DEFAULT_PORT: 5432 ADMINER_DEFAULT_DB: metadatadb networks: - - net \ No newline at end of file + - net +