-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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 <[email protected]>
- Loading branch information
1 parent
56b6efb
commit ff0efa6
Showing
5 changed files
with
171 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String> 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,4 +79,5 @@ services: | |
ADMINER_DEFAULT_PORT: 5432 | ||
ADMINER_DEFAULT_DB: metadatadb | ||
networks: | ||
- net | ||
- net | ||
|