Skip to content

Commit

Permalink
feat: Get file by UUID (#76)
Browse files Browse the repository at this point in the history
* docs(openapi): Update spec

* fix(services): Make volume fild nullable

Since that endpoint can also be used to list directories, it's possible for the volume field to be null

* feat: Add new service to get file metadata

* test: Add tests to the service to get metadata by UUID

* chore(cli): Add new service to the CLI client

* fix: Add missing extension and size fields to File object
  • Loading branch information
PedroChaparro authored Oct 15, 2023
1 parent b8fb2c3 commit 3785327
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 51 deletions.
57 changes: 57 additions & 0 deletions app/src/main/java/gateway/controller/CtrlFileGet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package gateway.controller;

import gateway.services.ServiceAuth;
import gateway.services.ServiceMetadata;
import gateway.services.UtilValidator;
import gateway.soap.request.ReqFile;
import gateway.soap.response.File;
import gateway.soap.response.ResFileGet;
import gateway.soap.response.ResStatus;
import java.util.UUID;

public class CtrlFileGet
{
public static ResFileGet file_get (ReqFile args)
{
ResFileGet s = new ResFileGet ();

// validations
ResStatus resValidate = UtilValidator.validate (args);
if (resValidate.error) {
return ResStatus.downCast (ResFileGet.class, resValidate);
}

// Check if user is authenticated
ResStatus resAuth = ServiceAuth.authenticate (args.token);
if (resAuth.error) {
return ResStatus.downCast (ResFileGet.class, resAuth);
}

// Check if user can read the file
UUID userUUID = UUID.fromString (ServiceAuth.tokenGetClaim (args.token, "uuid"));
ResStatus resCanRead = ServiceMetadata.canRead (userUUID, args.fileUUID);
if (resCanRead.error) {
return ResStatus.downCast (ResFileGet.class, resCanRead);
}

// get metadata
ServiceMetadata.ResFileMetadata resM = ServiceMetadata.getFileMetadata (args.fileUUID);
s.code = resM.code;
s.error = resM.error;
s.msg = resM.msg;

if (s.code == 200) {
File file = new File ();
file.uuid = args.fileUUID;
file.name = resM.name;
file.isFile = resM.volume != null;
file.extension = resM.extension;
file.size = resM.size;

s.file = file;
s.msg = "File metadata has been retrieved";
}

return s;
}
}
8 changes: 4 additions & 4 deletions app/src/main/java/gateway/services/ServiceMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ public static class ResFileMetadata extends ResStatus
{
public String name;
public String extension;
public int volume;
public long size;
public Integer volume;
public int size;
public boolean isShared;
}

Expand All @@ -129,8 +129,8 @@ public static ResFileMetadata getFileMetadata (UUID fileUUID)
s.error = false;
s.name = resBody.getString ("name");
s.extension = resBody.isNull ("extension") ? null : resBody.getString ("extension");
s.volume = resBody.getInt ("volume");
s.size = resBody.getLong ("size");
s.volume = resBody.isNull ("volume") ? null : resBody.getInt ("volume");
s.size = resBody.getInt ("size");
s.isShared =
resBody.getBoolean ("is_shared"); // TODO make a test for true and false
} else {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/gateway/soap/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public interface Service {
@WebMethod ResStatus account_password (ReqAccPassword parameters);

// file system
@WebMethod ResFileGet file_get (ReqFile args);

@WebMethod ResFileList file_list (ReqFileList args);

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/gateway/soap/ServiceImp.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
return CtrlFileNewDir.file_new_dir (args);
}

@Override public ResFileGet file_get (ReqFile args) { return CtrlFileGet.file_get (args); }

@WebMethod public ResFileCheck file_check (ReqFile args)
{
return CtrlFileCheck.file_check (args);
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/gateway/soap/response/File.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

public class File
{
public UUID uuid;
public String name;
public String extension;
public boolean isFile;
public UUID uuid;
public int size; // byte size
public int size;
}
6 changes: 6 additions & 0 deletions app/src/main/java/gateway/soap/response/ResFileGet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package gateway.soap.response;

public class ResFileGet extends ResStatus
{
public File file;
}
60 changes: 51 additions & 9 deletions app/src/test/java/gateway/ITFileManagment.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
package gateway;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.*;

import gateway.config.Config;
import gateway.controller.CtrlAccountRegister;
import gateway.controller.CtrlFileNewDir;
import gateway.controller.CtrlFileRename;
import gateway.controller.CtrlFileUpload;
import gateway.soap.request.Credentials;
import gateway.soap.request.ReqFileNewDir;
import gateway.soap.request.ReqFileRename;
import gateway.soap.request.ReqFileUpload;
import gateway.controller.*;
import gateway.soap.request.*;
import gateway.soap.response.ResFileGet;
import gateway.soap.response.ResFileNew;
import gateway.soap.response.ResSession;
import gateway.testutils.TestUtilConfig;
Expand Down Expand Up @@ -107,4 +102,51 @@
reqD.directoryName = null;
assertEquals (400, CtrlFileNewDir.file_new_dir (reqD).code, "Field validation failed");
}

@Test @Order (3) void getFileMetadata () throws InterruptedException
{
// register
ResSession resR = CtrlAccountRegister.account_register (
new Credentials (UUID.randomUUID ().toString (), "pass"));
assertEquals (201, resR.code, "Register successfully");

// upload file
ReqFileUpload reqU = new ReqFileUpload ();
reqU.fileContent = TestUtilGenerator.randomBytes (1);
reqU.fileName = UUID.randomUUID ().toString ();
reqU.location = null;
reqU.token = resR.auth.token;

ResFileNew resU = CtrlFileUpload.file_upload (reqU);
assertEquals (201, resU.code, "File upload success");

// wait for upload
Thread.sleep (2_000);

// get file metadata
ReqFile reqF = new ReqFile ();
reqF.fileUUID = resU.fileUUID;
reqF.token = resR.auth.token;

ResFileGet resF = CtrlFileGet.file_get (reqF);

// Check SOAP response fields
assertEquals (200, resF.code, "Metadata retrieved successfully");
assertFalse (resF.error, "There is no error in the SOAP response");

// Check file metadata fields
assertEquals (resU.fileUUID, resF.file.uuid, "File UUID matches");
assertEquals (reqU.fileName, resF.file.name, "File name matches");
assertTrue (resF.file.isFile, "File is an archive");

// errors
TestUtilConfig.makeInvalidMetadata ();
assertEquals (500, CtrlFileGet.file_get (reqF).code, "Can't reach metadata");

reqF.token = "invalid-token";
assertEquals (401, CtrlFileGet.file_get (reqF).code, "Unauthorized");

reqF.fileUUID = null;
assertEquals (400, CtrlFileGet.file_get (reqF).code, "Field validation failed");
}
}
1 change: 1 addition & 0 deletions cli-client/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
{"name": "πŸ”‘ Auth refresh", "handler": handlers.authRefreshHandler},
{"name": "πŸ“„ Upload file", "handler": handlers.uploadHandler},
{"name": "πŸ“„ Download file", "handler": handlers.downloadHandler},
{"name": "πŸ“„ Get file by UUID", "handler": handlers.getMetadataByUUIDHandler},
{"name": "πŸšͺ Finish program", "handler": handlers.exitHandler},
]

Expand Down
13 changes: 12 additions & 1 deletion cli-client/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ def uploadHandler():

with open(filePath, "rb") as in_file:
fileName = os.path.basename(filePath)
fileExtension = os.path.splitext(fileName)[1]

# Login with the default user
session = client.service.auth_login(
Expand Down Expand Up @@ -81,6 +80,18 @@ def downloadHandler():
res.fileContent = f"{len(res.fileContent)} bytes"
print(res)

def getMetadataByUUIDHandler():
fileUUID = input("File UUID: ")

# Login with the default user
session = client.service.auth_login({"username": "woynert", "password": "password"})

# Get the file
res = client.service.file_get(
{"fileUUID": fileUUID, "token": session.auth.token}
)

print(res)

def exitHandler():
os._exit(0)
Loading

0 comments on commit 3785327

Please sign in to comment.