Skip to content

Commit

Permalink
test: Add tests for register method (#60)
Browse files Browse the repository at this point in the history
* feat: init test to register and fixing format issues in CtrlAccountRegister

* fix: changing location of register test

* fix: adding requested changes in code and tests

* fix: validating format in code
  • Loading branch information
SilviaPabon authored Oct 8, 2023
1 parent ad23c94 commit bad63bf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
21 changes: 15 additions & 6 deletions app/src/main/java/gateway/controller/CtrlAccountRegister.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;
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 @@ -17,6 +18,12 @@ public static ResSession account_register (Credentials credentials)
{
ResSession res = new ResSession ();

// validations of all fields
ResStatus resValidate = UtilValidator.validate (credentials);
if (resValidate.error) {
return ResStatus.downCast (ResSession.class, resValidate);
}

String url = Config.getAuthBaseUrl () + "/register";

// Request
Expand All @@ -26,7 +33,6 @@ public static ResSession account_register (Credentials credentials)

try {
// Configs and make HTTP POST request to user register.

HttpResponse<String> response = HttpClient.newHttpClient ().send (
HttpRequest.newBuilder ()
.uri (URI.create (url))
Expand All @@ -41,15 +47,18 @@ public static ResSession account_register (Credentials credentials)

if (res.code == 201) {
res.auth = new Authorization ();
res.error = false;
res.auth.token = jsonObject.getString ("jwt");
res.error = false;
res.msg = "Register succeed";
} else {
res.error = true;
res.msg = jsonObject.getString ("msg");
}

} catch (IOException | InterruptedException e) {
System.err.println (e);
} catch (Exception e) {
e.printStackTrace ();
res.code = 500;
res.error = true;
res.msg = "Internal error, try again later";
}

return res;
Expand Down
25 changes: 25 additions & 0 deletions app/src/test/java/gateway/ITServiceAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,31 @@ class ITServiceAuth
{
@BeforeEach void setup () { Config.initializeFromEnv (); }

@Test void Register ()
{

Credentials credentials = new Credentials (UUID.randomUUID ().toString (), "pass");

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

// 500 - user already exists
res = CtrlAccountRegister.account_register (credentials);
assertEquals (500, res.code, "Internal server error");

// 500 - without some fields
credentials.username = null;
res = CtrlAccountRegister.account_register (credentials);
assertEquals (400, res.code, "Field: must not be null");

// 500 - all fails
TestUtilConfig.makeInvalidAll ();
credentials.username = UUID.randomUUID ().toString ();
res = CtrlAccountRegister.account_register (credentials);
assertEquals (500, res.code, "Internal error");
}

@Test void Authenticate ()
{
// 200
Expand Down

0 comments on commit bad63bf

Please sign in to comment.