From 6d99b5c331bc1e7868cb1e72779c8c2277743bac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Silvia=20Pab=C3=B3n?= <62608580+SilviaPabon@users.noreply.github.com> Date: Fri, 20 Oct 2023 11:45:31 -0500 Subject: [PATCH] Feat: list shared (#82) * feat: init feat list shared * feat: advance in feat, iterating array of sharefiles * fix: format * feat: adding get shared list in py client * refactor: generating array of files * feat: adding changes in feat and tests * fix: updating file of services where is createFileArray method * fix: format * refactor: move all related to share tests to ITShareManagement * fix: adding missing auth, typo and unnecesary field * fix: format --- .../java/gateway/controller/CtrlFileList.java | 21 +-- .../gateway/controller/CtrlShareList.java | 66 ++++++++ .../java/gateway/services/UtilsFiles.java | 30 ++++ .../main/java/gateway/soap/ServiceImp.java | 5 +- .../gateway/soap/response/ResShareList.java | 2 +- .../gateway/soap/response/SharedFile.java | 6 - app/src/test/java/gateway/ITShareFile.java | 63 -------- .../test/java/gateway/ITShareListWhitWho.java | 73 --------- .../test/java/gateway/ITShareManagement.java | 149 ++++++++++++++++++ .../java/gateway/testutils/TestUtilShare.java | 17 ++ cli-client/cli.py | 1 + cli-client/handlers.py | 12 ++ 12 files changed, 282 insertions(+), 163 deletions(-) create mode 100644 app/src/main/java/gateway/controller/CtrlShareList.java create mode 100644 app/src/main/java/gateway/services/UtilsFiles.java delete mode 100644 app/src/main/java/gateway/soap/response/SharedFile.java delete mode 100644 app/src/test/java/gateway/ITShareFile.java delete mode 100644 app/src/test/java/gateway/ITShareListWhitWho.java create mode 100644 app/src/test/java/gateway/ITShareManagement.java create mode 100644 app/src/test/java/gateway/testutils/TestUtilShare.java diff --git a/app/src/main/java/gateway/controller/CtrlFileList.java b/app/src/main/java/gateway/controller/CtrlFileList.java index 074ac76..eb25012 100644 --- a/app/src/main/java/gateway/controller/CtrlFileList.java +++ b/app/src/main/java/gateway/controller/CtrlFileList.java @@ -2,6 +2,7 @@ import gateway.config.Config; import gateway.services.ServiceAuth; +import gateway.services.UtilsFiles; import gateway.soap.request.ReqFileList; import gateway.soap.response.File; import gateway.soap.response.ResFileList; @@ -54,25 +55,7 @@ public static ResFileList file_list (ReqFileList args) if (resFileList.code == 200) { // If the response code is 200, process the received files JSONArray filesArray = responseBody.getJSONArray ("files"); - File[] files = new File[filesArray.length ()]; - - for (int i = 0; i < filesArray.length (); i++) { - // Process each file in the response JSON - JSONObject fileObject = filesArray.getJSONObject (i); - - // Create a File object and assign it the file information - File file = new File (); - - file.uuid = UUID.fromString (fileObject.getString ("uuid")); - file.name = fileObject.getString ("fileName"); - file.extension = fileObject.isNull ("fileExtension") - ? null - : fileObject.getString ("fileExtension"); - file.isFile = fileObject.getString ("fileType").equals ("archive"); - file.size = fileObject.getInt ("fileSize"); - - files[i] = file; - } + File[] files = UtilsFiles.createFileArray (filesArray); resFileList.files = files; resFileList.error = false; diff --git a/app/src/main/java/gateway/controller/CtrlShareList.java b/app/src/main/java/gateway/controller/CtrlShareList.java new file mode 100644 index 0000000..4809294 --- /dev/null +++ b/app/src/main/java/gateway/controller/CtrlShareList.java @@ -0,0 +1,66 @@ +package gateway.controller; + +import gateway.config.Config; +import gateway.services.ServiceAuth; +import gateway.services.UtilsFiles; +import gateway.soap.request.Authorization; +import gateway.soap.response.File; +import gateway.soap.response.ResShareList; +import gateway.soap.response.ResStatus; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.util.UUID; +import org.json.JSONArray; +import org.json.JSONObject; + +public class CtrlShareList +{ + public static ResShareList share_list (Authorization authorization) + { + ResShareList resShareList = new ResShareList (); + + ResStatus resAuth = ServiceAuth.authenticate (authorization.token); + if (resAuth.error) { + return ResStatus.downCast (ResShareList.class, resAuth); + } + + // obtain uuid from user + UUID userUUID = UUID.fromString (ServiceAuth.tokenGetClaim (authorization.token, "uuid")); + + String url = Config.getMetadataBaseUrl () + "/files/shared_with_me/" + userUUID; + + try { + + HttpResponse response = HttpClient.newHttpClient ().send ( + HttpRequest.newBuilder ().uri (URI.create (url)).GET ().build (), + HttpResponse.BodyHandlers.ofString ()); + + // Response + JSONObject responseBody = new JSONObject (response.body ()); + resShareList.code = response.statusCode (); + + if (resShareList.code == 200) { + // If the response code is 200, process the received files + JSONArray shareFilesArray = responseBody.getJSONArray ("files"); + File[] shareFiles = UtilsFiles.createFileArray (shareFilesArray); + + resShareList.error = false; + resShareList.sharedFiles = shareFiles; + resShareList.msg = "Ok. The directory was listed."; + } else { + resShareList.error = true; + resShareList.msg = responseBody.getString ("message"); + } + + } catch (Exception e) { + e.printStackTrace (); + resShareList.code = 500; + resShareList.error = true; + resShareList.msg = "Internal error, try again later"; + } + + return resShareList; + } +} diff --git a/app/src/main/java/gateway/services/UtilsFiles.java b/app/src/main/java/gateway/services/UtilsFiles.java new file mode 100644 index 0000000..e9afc0b --- /dev/null +++ b/app/src/main/java/gateway/services/UtilsFiles.java @@ -0,0 +1,30 @@ +package gateway.services; + +import gateway.soap.response.File; +import java.util.UUID; +import org.json.JSONArray; +import org.json.JSONObject; + +public class UtilsFiles +{ + public static File[] createFileArray (JSONArray jsonArray) + { + File[] files = new File[jsonArray.length ()]; + + for (int i = 0; i < jsonArray.length (); i++) { + JSONObject fileObject = jsonArray.getJSONObject (i); + File file = new File (); + + file.uuid = UUID.fromString (fileObject.getString ("uuid")); + file.name = fileObject.getString ("fileName"); + file.extension = + fileObject.isNull ("fileExtension") ? null : fileObject.getString ("fileExtension"); + file.isFile = fileObject.getString ("fileType").equals ("archive"); + file.size = fileObject.getInt ("fileSize"); + + files[i] = file; + } + + return files; + } +} diff --git a/app/src/main/java/gateway/soap/ServiceImp.java b/app/src/main/java/gateway/soap/ServiceImp.java index 105cc4e..b62a22a 100644 --- a/app/src/main/java/gateway/soap/ServiceImp.java +++ b/app/src/main/java/gateway/soap/ServiceImp.java @@ -82,7 +82,10 @@ @WebMethod public ResStatus unshare_file (ReqShareRemove args) { return null; } - @WebMethod public ResShareList share_list (Authorization auth) { return null; } + @WebMethod public ResShareList share_list (Authorization auth) + { + return CtrlShareList.share_list (auth); + } @WebMethod public ResShareListWithWho share_list_with_who (ReqFile args) { diff --git a/app/src/main/java/gateway/soap/response/ResShareList.java b/app/src/main/java/gateway/soap/response/ResShareList.java index 707ce76..ecd4150 100644 --- a/app/src/main/java/gateway/soap/response/ResShareList.java +++ b/app/src/main/java/gateway/soap/response/ResShareList.java @@ -2,5 +2,5 @@ public class ResShareList extends ResStatus { - public SharedFile[] sharedFiles; + public File[] sharedFiles; } diff --git a/app/src/main/java/gateway/soap/response/SharedFile.java b/app/src/main/java/gateway/soap/response/SharedFile.java deleted file mode 100644 index d61e4dc..0000000 --- a/app/src/main/java/gateway/soap/response/SharedFile.java +++ /dev/null @@ -1,6 +0,0 @@ -package gateway.soap.response; - -public class SharedFile extends File -{ - public String ownerUsername; -} diff --git a/app/src/test/java/gateway/ITShareFile.java b/app/src/test/java/gateway/ITShareFile.java deleted file mode 100644 index 4145d80..0000000 --- a/app/src/test/java/gateway/ITShareFile.java +++ /dev/null @@ -1,63 +0,0 @@ -package gateway; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import gateway.config.Config; -import gateway.controller.CtrlShareFile; -import gateway.services.ServiceMetadata.ResSaveFile; -import gateway.soap.request.ReqShareFile; -import gateway.soap.response.ResStatus; -import gateway.testutils.TestUtilAuth; -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 = TestUtilAuth.registerAndLoginUserSuccess (username1); - String tokenUser2 = TestUtilAuth.registerAndLoginUserSuccess (username2); - - // create a file and aux file - ResSaveFile resSaveFile1 = TestUtilAuth.createFile (tokenUser1); - ResSaveFile resSaveFile2 = TestUtilAuth.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."); - } -} diff --git a/app/src/test/java/gateway/ITShareListWhitWho.java b/app/src/test/java/gateway/ITShareListWhitWho.java deleted file mode 100644 index 4eaca7b..0000000 --- a/app/src/test/java/gateway/ITShareListWhitWho.java +++ /dev/null @@ -1,73 +0,0 @@ -package gateway; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import gateway.config.Config; -import gateway.controller.CtrlShareFile; -import gateway.controller.CtrlShareListWithWho; -import gateway.services.ServiceMetadata.ResSaveFile; -import gateway.soap.request.ReqFile; -import gateway.soap.request.ReqShareFile; -import gateway.soap.response.ResStatus; -import gateway.testutils.TestUtilAuth; -import gateway.testutils.TestUtilConfig; -import java.util.UUID; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -public class ITShareListWhitWho -{ - @BeforeEach void setup () { Config.initializeFromEnv (); } - - @Test void shareListWhitWho () - { - String username1 = UUID.randomUUID ().toString (); - String username2 = UUID.randomUUID ().toString (); - - // Register - - String tokenUser1 = TestUtilAuth.registerAndLoginUserSuccess (username1); - TestUtilAuth.registerAndLoginUserSuccess (username2); - - // Create a file - ResSaveFile resSaveFile1 = TestUtilAuth.createFile (tokenUser1); - - // Share file - 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"); - - // List of shared with users successfully - ReqFile reqFile = new ReqFile (); - reqFile.fileUUID = resSaveFile1.fileUUID; - reqFile.token = tokenUser1; - assertEquals ( - 200, CtrlShareListWithWho.share_list_with_who (reqFile).code, - "List of shared with users successfully"); - - // errors - reqFile.fileUUID = null; - assertEquals ( - 400, CtrlShareListWithWho.share_list_with_who (reqFile).code, - "Bad Request: Invalid Field"); - - reqFile.token = "invalid token"; - reqFile.fileUUID = resSaveFile1.fileUUID; - assertEquals ( - 401, CtrlShareListWithWho.share_list_with_who (reqFile).code, "Authorization failed"); - - reqFile.fileUUID = UUID.randomUUID (); - reqFile.token = tokenUser1; - assertEquals ( - 404, CtrlShareListWithWho.share_list_with_who (reqFile).code, - "There is no file with the given fileUUID"); - - reqFile.fileUUID = resSaveFile1.fileUUID; - TestUtilConfig.makeInvalidMetadata (); - assertEquals ( - 500, CtrlShareListWithWho.share_list_with_who (reqFile).code, "Can't reach metadata"); - } -} diff --git a/app/src/test/java/gateway/ITShareManagement.java b/app/src/test/java/gateway/ITShareManagement.java new file mode 100644 index 0000000..ddc5e2d --- /dev/null +++ b/app/src/test/java/gateway/ITShareManagement.java @@ -0,0 +1,149 @@ +package gateway; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import gateway.config.Config; +import gateway.controller.CtrlShareFile; +import gateway.controller.CtrlShareList; +import gateway.controller.CtrlShareListWithWho; +import gateway.services.ServiceMetadata.ResSaveFile; +import gateway.soap.request.Authorization; +import gateway.soap.request.ReqFile; +import gateway.soap.request.ReqShareFile; +import gateway.soap.response.ResStatus; +import gateway.testutils.TestUtilAuth; +import gateway.testutils.TestUtilConfig; +import gateway.testutils.TestUtilShare; +import java.util.UUID; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class ITShareManagement +{ + + @BeforeEach void setup () { Config.initializeFromEnv (); } + + @Test void ShareFile () + { + + String username1 = UUID.randomUUID ().toString (); + String username2 = UUID.randomUUID ().toString (); + + // register + String tokenUser1 = TestUtilAuth.registerAndLoginUserSuccess (username1); + String tokenUser2 = TestUtilAuth.registerAndLoginUserSuccess (username2); + + // create a file and aux file + ResSaveFile resSaveFile1 = TestUtilAuth.createFile (tokenUser1); + ResSaveFile resSaveFile2 = TestUtilAuth.createFile (tokenUser2); + + // 204 + ReqShareFile reqShareFile = + TestUtilShare.createShareFile (resSaveFile1, username2, 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."); + } + + @Test void shareList () + { + String username1 = UUID.randomUUID ().toString (); + String username2 = UUID.randomUUID ().toString (); + + // Register + String tokenUser1 = TestUtilAuth.registerAndLoginUserSuccess (username1); + Authorization authorization = new Authorization (tokenUser1); + TestUtilAuth.registerAndLoginUserSuccess (username2); + + // Create a file + ResSaveFile resSaveFile1 = TestUtilAuth.createFile (tokenUser1); + + // Share file + ReqShareFile reqShareFile = + TestUtilShare.createShareFile (resSaveFile1, username2, tokenUser1); + ResStatus res = CtrlShareFile.share_file (reqShareFile); + assertEquals (204, res.code, "The file have been shared"); + + // List of shared with me + assertEquals ( + 200, CtrlShareList.share_list (authorization).code, "Ok. The directory was listed."); + + // 401 + authorization.token = "invalid_token"; + assertEquals (401, CtrlShareList.share_list (authorization).code, "unauthorized"); + + authorization.token = tokenUser1; + TestUtilConfig.makeInvalidMetadata (); + assertEquals (500, CtrlShareList.share_list (authorization).code, "Can't reach metadata"); + } + + @Test void shareListWithWho () + { + String username1 = UUID.randomUUID ().toString (); + String username2 = UUID.randomUUID ().toString (); + + // Register + + String tokenUser1 = TestUtilAuth.registerAndLoginUserSuccess (username1); + TestUtilAuth.registerAndLoginUserSuccess (username2); + + // Create a file + ResSaveFile resSaveFile1 = TestUtilAuth.createFile (tokenUser1); + + // Share file + ReqShareFile reqShareFile = + TestUtilShare.createShareFile (resSaveFile1, username2, tokenUser1); + ResStatus res = CtrlShareFile.share_file (reqShareFile); + assertEquals (204, res.code, "The file have been shared"); + + // List of shared with users successfully + ReqFile reqFile = new ReqFile (); + reqFile.fileUUID = resSaveFile1.fileUUID; + reqFile.token = tokenUser1; + assertEquals ( + 200, CtrlShareListWithWho.share_list_with_who (reqFile).code, + "List of shared with users successfully"); + + // errors + reqFile.fileUUID = null; + assertEquals ( + 400, CtrlShareListWithWho.share_list_with_who (reqFile).code, + "Bad Request: Invalid Field"); + + reqFile.token = "invalid token"; + reqFile.fileUUID = resSaveFile1.fileUUID; + assertEquals ( + 401, CtrlShareListWithWho.share_list_with_who (reqFile).code, "Authorization failed"); + + reqFile.fileUUID = UUID.randomUUID (); + reqFile.token = tokenUser1; + assertEquals ( + 404, CtrlShareListWithWho.share_list_with_who (reqFile).code, + "There is no file with the given fileUUID"); + + reqFile.fileUUID = resSaveFile1.fileUUID; + TestUtilConfig.makeInvalidMetadata (); + assertEquals ( + 500, CtrlShareListWithWho.share_list_with_who (reqFile).code, "Can't reach metadata"); + } +} diff --git a/app/src/test/java/gateway/testutils/TestUtilShare.java b/app/src/test/java/gateway/testutils/TestUtilShare.java new file mode 100644 index 0000000..15ace10 --- /dev/null +++ b/app/src/test/java/gateway/testutils/TestUtilShare.java @@ -0,0 +1,17 @@ +package gateway.testutils; + +import gateway.services.ServiceMetadata.ResSaveFile; +import gateway.soap.request.ReqShareFile; + +public class TestUtilShare +{ + public static ReqShareFile + createShareFile (ResSaveFile resSaveFile, String otherUsername, String token) + { + ReqShareFile reqShareFile = new ReqShareFile (); + reqShareFile.fileUUID = resSaveFile.fileUUID; + reqShareFile.otherUsername = otherUsername; + reqShareFile.token = token; + return reqShareFile; + } +} diff --git a/cli-client/cli.py b/cli-client/cli.py index d5ec1fe..b5d3dbd 100755 --- a/cli-client/cli.py +++ b/cli-client/cli.py @@ -15,6 +15,7 @@ {"name": "📄 Download file", "handler": handlers.downloadHandler}, {"name": "📄 Move file", "handler": handlers.moveFileHandler}, {"name": "📄 Get file by UUID", "handler": handlers.getMetadataByUUIDHandler}, + {"name": "📄 Get share with me", "handler": handlers.shareListHandler}, {"name": "🚪 Finish program", "handler": handlers.exitHandler}, ] diff --git a/cli-client/handlers.py b/cli-client/handlers.py index db325d8..82e794d 100644 --- a/cli-client/handlers.py +++ b/cli-client/handlers.py @@ -106,5 +106,17 @@ def moveFileHandler(): print(res) +def shareListHandler(): + token = input("Token : ") + + # Login with the default user + # session = client.service.auth_login({"username": "woynert", "password": "password"}) + + res = client.service.share_list( + {"token": token} + ) + + print(res) + def exitHandler(): os._exit(0)