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

Add Awaitility Dependency & Import die Bibliothek #244

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
13 changes: 10 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,14 @@
de.remsfal.service.entity.dto\**
</sonar.cpd.exclusions>
</properties>

<dependencies>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dependency is only in the service module required. Therefore, please move this dependency to the remsfal-service/pom.xml

<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>4.2.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<profile>
<id>githubDeploy</id>
Expand Down Expand Up @@ -85,7 +92,7 @@
<java.util.logging.manager>
org.jboss.logmanager.LogManager
</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
<maven.home>${env.MAVEN_HOME}</maven.home>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, do not set the maven home in a pom.xml. The maven home is an environment variable that should be provided by the os system.

<quarkus.jacoco.data-file>
${maven.multiModuleProjectDirectory}/target/site/jacoco/jacoco.exec
</quarkus.jacoco.data-file>
Expand Down Expand Up @@ -114,7 +121,7 @@
${project.build.directory}/${project.build.finalName}-runner</native.image.path>
<java.util.logging.manager>
org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
<maven.home>${env.MAVEN_HOME}</maven.home>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this.

</systemPropertyVariables>
</configuration>
</execution>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.matcher.RestAssuredMatchers;

import jakarta.transaction.Transactional;
import org.awaitility.Awaitility;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;

Expand All @@ -20,6 +22,8 @@

import jakarta.ws.rs.core.Response.Status;

import java.util.concurrent.TimeUnit;

@QuarkusTest
class AuthenticationResourceMockitoTest extends AbstractResourceTest {

Expand All @@ -32,109 +36,120 @@ class AuthenticationResourceMockitoTest extends AbstractResourceTest {
void session_SUCCESS_userIsCreated() {
final String code = "anyRandomCode";
final GoogleIdToken.Payload idTokenPayload = new GoogleIdToken.Payload()
.setSubject(TestData.USER_TOKEN_1)
.setEmail(TestData.USER_EMAIL_1);
.setSubject(TestData.USER_TOKEN_1)
.setEmail(TestData.USER_EMAIL_1);

when(authenticator.getIdToken(eq(code), any()))
.thenReturn(new GoogleIdToken(new JsonWebSignature.Header(), idTokenPayload, new byte[1], new byte[1]));
.thenReturn(new GoogleIdToken(new JsonWebSignature.Header(), idTokenPayload, new byte[1], new byte[1]));

long enties = entityManager
.createQuery("SELECT count(user) FROM UserEntity user where user.email = :email", Long.class)
.setParameter("email", TestData.USER_EMAIL_1)
.getSingleResult();
.createQuery("SELECT count(user) FROM UserEntity user where user.email = :email", Long.class)
.setParameter("email", TestData.USER_EMAIL_1)
.getSingleResult();
assertEquals(0, enties);

given()
.when()
.queryParam("code", code)
.queryParam("state", "/my/callback")
.queryParam("anyOtherParam", "toBeIgnored")
.redirects().follow(false)
.get(BASE_PATH + "/session")
.then()
.statusCode(Status.FOUND.getStatusCode())
.header("location", Matchers.equalTo("http://localhost:8081/my/callback"))
.cookie("remsfal_session", RestAssuredMatchers.detailedCookie()
.path("/")
.sameSite("Strict")
.maxAge(60 * 30));
.when()
.queryParam("code", code)
.queryParam("state", "/my/callback")
.queryParam("anyOtherParam", "toBeIgnored")
.redirects().follow(false)
.get(BASE_PATH + "/session")
.then()
.statusCode(Status.FOUND.getStatusCode())
.header("location", Matchers.equalTo("http://localhost:8081/my/callback"))
.cookie("remsfal_session", RestAssuredMatchers.detailedCookie()
.path("/")
.sameSite("Strict")
.maxAge(60 * 30));

enties = entityManager
.createQuery("SELECT count(user) FROM UserEntity user where user.email = :email", Long.class)
.setParameter("email", TestData.USER_EMAIL_1)
.getSingleResult();
.createQuery("SELECT count(user) FROM UserEntity user where user.email = :email", Long.class)
.setParameter("email", TestData.USER_EMAIL_1)
.getSingleResult();
assertEquals(1, enties);
}

@Test
void session_SUCCESS_userAlreadyExists() {
void session_SUCCESS_userAlreadyExists() {
final String code = "anyRandomCode";
final GoogleIdToken.Payload idTokenPayload = new GoogleIdToken.Payload()
.setSubject(TestData.USER_TOKEN_1)
.setEmail(TestData.USER_EMAIL_1);
.setSubject(TestData.USER_TOKEN_1)
.setEmail(TestData.USER_EMAIL_1);

when(authenticator.getIdToken(eq(code), any()))
.thenReturn(new GoogleIdToken(new JsonWebSignature.Header(), idTokenPayload, new byte[1], new byte[1]));
.thenReturn(new GoogleIdToken(new JsonWebSignature.Header(), idTokenPayload, new byte[1], new byte[1]));

setupTestUsers();
long enties = entityManager
.createQuery("SELECT count(user) FROM UserEntity user where user.email = :email", Long.class)
.setParameter("email", TestData.USER_EMAIL_1)
.getSingleResult();
.createQuery("SELECT count(user) FROM UserEntity user where user.email = :email", Long.class)
.setParameter("email", TestData.USER_EMAIL_1)
.getSingleResult();
assertEquals(1, enties);

given()
.when()
.queryParam("code", code)
.queryParam("anyOtherParam", "toBeIgnored")
.redirects().follow(false)
.get(BASE_PATH + "/session")
.then()
.statusCode(Status.FOUND.getStatusCode())
.header("location", Matchers.equalTo("http://localhost:8081/"))
.cookie("remsfal_session", RestAssuredMatchers.detailedCookie()
.path("/")
.sameSite("Strict")
.maxAge(60 * 30));
.when()
.queryParam("code", code)
.queryParam("anyOtherParam", "toBeIgnored")
.redirects().follow(false)
.get(BASE_PATH + "/session")
.then()
.statusCode(Status.FOUND.getStatusCode())
.header("location", Matchers.equalTo("http://localhost:8081/"))
.cookie("remsfal_session", RestAssuredMatchers.detailedCookie()
.path("/")
.sameSite("Strict")
.maxAge(60 * 30));

enties = entityManager
.createQuery("SELECT count(user) FROM UserEntity user where user.email = :email", Long.class)
.setParameter("email", TestData.USER_EMAIL_1)
.getSingleResult();
.createQuery("SELECT count(user) FROM UserEntity user where user.email = :email", Long.class)
.setParameter("email", TestData.USER_EMAIL_1)
.getSingleResult();
assertEquals(1, enties);
// TODO: Use Awaitility to test AuthenticationEvent

// // Awaitility check with separate transactional assertion method

Awaitility.await().atMost(5, TimeUnit.SECONDS).untilAsserted(() -> assertUserCount(TestData.USER_EMAIL_1, 1));
}

@Transactional
void assertUserCount(String email, long expectedCount) {
long actualCount = entityManager
.createQuery("SELECT count(user) FROM UserEntity user where user.email = :email", Long.class)
.setParameter("email", email)
.getSingleResult();
assertEquals(expectedCount, actualCount);
}

@Test
void session_FAILED_noCode() {
given()
.when()
.get(BASE_PATH + "/session")
.then()
.statusCode(Status.UNAUTHORIZED.getStatusCode());
.when()
.get(BASE_PATH + "/session")
.then()
.statusCode(Status.UNAUTHORIZED.getStatusCode());
}

@Test
void session_FAILED_errorIsProvided() {
given()
.queryParam("error", "Any Error from Google")
.when()
.get(BASE_PATH + "/session")
.then()
.statusCode(Status.UNAUTHORIZED.getStatusCode());
.queryParam("error", "Any Error from Google")
.when()
.get(BASE_PATH + "/session")
.then()
.statusCode(Status.UNAUTHORIZED.getStatusCode());
}

@Test
void session_FAILED_invalidToken() {
when(authenticator.getIdToken(any(), any()))
.thenReturn(null);
.thenReturn(null);

given()
.when()
.queryParam("code", "anyValidCode")
.get(BASE_PATH + "/session")
.then()
.statusCode(Status.FORBIDDEN.getStatusCode());
.when()
.queryParam("code", "anyValidCode")
.get(BASE_PATH + "/session")
.then()
.statusCode(Status.FORBIDDEN.getStatusCode());
}

}
Loading