Skip to content

Commit

Permalink
Merge pull request #23117 from dinu0000/AccountResourceTest
Browse files Browse the repository at this point in the history
Improve Account resource test
  • Loading branch information
deepu105 authored Aug 12, 2023
2 parents a1a06d7 + 2a7c865 commit 80e4394
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ import org.springframework.transaction.annotation.Transactional;
<%_ if (reactive && searchEngineElasticsearch) { _%>
import reactor.core.publisher.Mono;
<%_ } _%>
import java.time.Instant;
<%_ if (authenticationTypeSession && !reactive && (databaseTypeSql || databaseTypeMongodb || databaseTypeNeo4j || databaseTypeCouchbase)) { _%>
import java.time.LocalDate;
Expand All @@ -84,7 +83,6 @@ import static org.mockito.Mockito.*;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasItem;
<%_ } _%>
import static <%= packageName %>.web.rest.AccountResourceIT.TEST_USER_LOGIN;
<%_ if (reactive) { _%>
<%_ if (authenticationUsesCsrf) { _%>
import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.csrf;
Expand All @@ -108,7 +106,6 @@ import static org.springframework.security.test.web.servlet.response.SecurityMoc
<%_ } else { _%>
@AutoConfigureMockMvc
<%_ } _%>
@WithMockUser(value = TEST_USER_LOGIN)
@IntegrationTest
class AccountResourceIT {
static final String TEST_USER_LOGIN = "test";
Expand Down Expand Up @@ -172,6 +169,7 @@ class AccountResourceIT {
}
@Test
@WithMockUser(TEST_USER_LOGIN)
<%_ if (reactive) { _%>
void testAuthenticatedUser() {
accountWebTestClient
Expand All @@ -183,17 +181,15 @@ class AccountResourceIT {
<%_ } else { _%>
void testAuthenticatedUser() throws Exception {
restAccountMockMvc.perform(get("/api/authenticate")
.with(request -> {
request.setRemoteUser(TEST_USER_LOGIN);
return request;
})
.with(request -> request)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(TEST_USER_LOGIN));
<%_ } _%>
}
@Test
@WithMockUser(TEST_USER_LOGIN)
void testGetExistingAccount()<% if (!reactive) { %> throws Exception<% } %> {
Set<String> authorities = new HashSet<>();
authorities.add(AuthoritiesConstants.ADMIN);
Expand Down Expand Up @@ -250,12 +246,16 @@ class AccountResourceIT {
accountWebTestClient.get().uri("/api/account")
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus().isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
<%_ if (authenticationTypeSession) { _%>
.expectStatus().isEqualTo(HttpStatus.OK);
<%_ } else { _%>
.expectStatus().isEqualTo(HttpStatus.UNAUTHORIZED);
<%_ } _%>
<%_ } else { _%>
void testGetUnknownAccount() throws Exception {
restAccountMockMvc.perform(get("/api/account")
.accept(MediaType.APPLICATION_PROBLEM_JSON))
.andExpect(status().isInternalServerError());
.andExpect(status().isUnauthorized());
<%_ } _%>
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ import org.springframework.transaction.annotation.Transactional;
<%_ } else { _%>
@AutoConfigureMockMvc
<%_ } _%>
@WithMockUser(value = TEST_USER_LOGIN)
@IntegrationTest
class AccountResourceIT {
Expand Down Expand Up @@ -151,11 +150,11 @@ class AccountResourceIT {
webTestClient.get().uri("/api/account")
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus().is5xxServerError();
.expectStatus().is3xxRedirection();
<%_ } else { _%>
restAccountMockMvc.perform(get("/api/account")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isInternalServerError());
.andExpect(status().isUnauthorized());
<%_ } _%>
}
Expand All @@ -177,6 +176,7 @@ class AccountResourceIT {
}
@Test
@WithMockUser(TEST_USER_LOGIN)
void testAuthenticatedUser() <% if (!reactive) { %>throws Exception <% } %>{
<%_ if (reactive) { _%>
webTestClient
Expand All @@ -187,10 +187,7 @@ class AccountResourceIT {
.expectBody(String.class).isEqualTo(TEST_USER_LOGIN);
<%_ } else { _%>
restAccountMockMvc.perform(get("/api/authenticate")
.with(request -> {
request.setRemoteUser(TEST_USER_LOGIN);
return request;
})
.with(request -> request)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(TEST_USER_LOGIN));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
-%>
package <%= packageName %>.web.rest;
import <%= packageName %>.IntegrationTest;
<%_ if (authenticationTypeJwt) { _%>
import static <%= packageName %>.security.jwt.JwtAuthenticationTestUtils.createValidTokenForUser;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -31,6 +30,7 @@ import static org.springframework.http.HttpHeaders.AUTHORIZATION;
<%_ if (reactive && searchEngineElasticsearch) { _%>
import <%= packageName %>.repository.search.UserSearchRepository;
<%_ } _%>
import <%= packageName %>.IntegrationTest;
import <%= packageName %>.security.AuthoritiesConstants;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -54,7 +54,6 @@ import reactor.core.publisher.Mono;
import static org.mockito.Mockito.*;
<%_ } _%>
import static <%= packageName %>.web.rest.AccountResourceIT.TEST_USER_LOGIN;
/**
* Integration tests for the {@link AccountResource} REST controller.
Expand All @@ -64,7 +63,6 @@ import static <%= packageName %>.web.rest.AccountResourceIT.TEST_USER_LOGIN;
<%_ } else { _%>
@AutoConfigureMockMvc
<%_ } _%>
@WithMockUser(value = TEST_USER_LOGIN)
@IntegrationTest
class AccountResourceIT {
static final String TEST_USER_LOGIN = "test";
Expand Down Expand Up @@ -131,6 +129,7 @@ class AccountResourceIT {
}
@Test
@WithMockUser(TEST_USER_LOGIN)
void testAuthenticatedUser() <% if (!reactive) { %>throws Exception <% } %>{
<%_ if (reactive) { _%>
accountWebTestClient
Expand All @@ -141,10 +140,7 @@ class AccountResourceIT {
.expectBody(String.class).isEqualTo(TEST_USER_LOGIN);
<%_ } else { _%>
restAccountMockMvc.perform(get("/api/authenticate")
.with(request -> {
request.setRemoteUser(TEST_USER_LOGIN);
return request;
})
.with(request -> request)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(TEST_USER_LOGIN));
Expand Down

0 comments on commit 80e4394

Please sign in to comment.