Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update login and Add tests #47

Merged
merged 6 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions app/src/main/java/gateway/controller/CtrlAuthLogin.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package gateway.controller;

import gateway.config.Config;
import gateway.services.UtilValidator;
andres123dbh marked this conversation as resolved.
Show resolved Hide resolved
import gateway.soap.request.*;
import gateway.soap.response.*;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
Expand All @@ -15,8 +16,14 @@ public class CtrlAuthLogin
public static ResSession auth_login (Credentials credentials)
{
// Create a new ResSession object to hold the response data
ResFileNew resFileNew = new ResFileNew ();
ResSession res = new ResSession ();

ResStatus resValidate = UtilValidator.validate (credentials);
if (resValidate.error) {
return ResStatus.downCast (ResSession.class, resValidate);
}

// Define the URL for the authentication request
String url = Config.getAuthBaseUrl () + "/login";

Expand All @@ -42,25 +49,28 @@ public static ResSession auth_login (Credentials credentials)
JSONObject jsonObject = new JSONObject (response.body ());

// Get the HTTP status code from the response.
int statusCode = response.statusCode ();
res.code = response.statusCode ();

// Check if the response status code is 201 (Login succeed)
if (statusCode == 201) {
if (res.code == 201) {
// If the status code is 201, indicating login succeed, initialize an Authorization
// object in the response and extract the JWT token.
res.auth = new Authorization ();
res.error = false;
res.auth.token = jsonObject.getString ("jwt");
res.error = false;
res.msg = "Login succeed";
} else {
// If the status code is different from 201, indicating an error response, extract
// success status and message from the JSON object.
res.error = true;
res.msg = jsonObject.getString ("msg");
}

} catch (IOException | InterruptedException e) {
} catch (Exception e) {
// Handle exceptions such as IOException and InterruptedException, if they occur.
e.printStackTrace ();
resFileNew.code = 500;
resFileNew.error = true;
resFileNew.msg = "Internal error, try again later";
}

// Return the res object containing the response data.
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/gateway/controller/CtrlFileUpload.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ public static ResFileNew file_upload (ReqFileUpload args)

try {
IWorkerService server = ServiceWorker.getServer ();
UploadFileArgs queryUpload =
new UploadFileArgs (s.fileUUID.toString (), args.fileContent);
UploadFileArgs queryUpload = new UploadFileArgs (s.fileUUID, args.fileContent);
server.uploadFile (queryUpload);

s.code = 201;
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/java/gateway/soap/request/Credentials.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package gateway.soap.request;

import jakarta.validation.constraints.NotNull;

public class Credentials
{
public String username;
public String password;
@NotNull public String username;
@NotNull public String password;

public Credentials () {}
public Credentials (String username, String password)
Expand Down
26 changes: 26 additions & 0 deletions app/src/test/java/gateway/ITServiceAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import gateway.config.Config;
import gateway.controller.CtrlAccountRegister;
import gateway.controller.CtrlAuthLogin;
import gateway.services.ServiceAuth;
import gateway.soap.request.Credentials;
import gateway.soap.response.ResSession;
Expand Down Expand Up @@ -53,4 +54,29 @@ class ITServiceAuth
500, ServiceAuth.getUserUUID (res.auth.token, cred.username).code,
"Can't reach Auth Server");
}

@Test void Login ()
{
Credentials cred = new Credentials (UUID.randomUUID ().toString (), "pass");
// register

ResSession res = CtrlAccountRegister.account_register (cred);
assertEquals (201, res.code, "Register successfully");

ResSession login = CtrlAuthLogin.auth_login (cred);
assertEquals (201, login.code, "Login succeed");

cred.username = UUID.randomUUID ().toString ();
login = CtrlAuthLogin.auth_login (cred);
assertEquals (401, login.code, "Invalid credentials");

cred.username = null;
login = CtrlAuthLogin.auth_login (cred);
assertEquals (400, login.code, "Bad Request: Invalid Field");

TestUtilConfig.makeInvalidAll ();
cred.username = UUID.randomUUID ().toString ();
login = CtrlAuthLogin.auth_login (cred);
assertEquals (500, login.code, "Internal error, try again later");
}
}
2 changes: 1 addition & 1 deletion docs/spec.openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ paths:
schema:
$ref: '#/components/schemas/Credentials'
responses:
'200':
'201':
description: Login succeeded
content:
Object:
Expand Down