<%#
 Copyright 2013-2024 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
package <%= packageName %>.web.rest;

<%_ if (authenticationTypeJwt) { _%>
import static <%= packageName %>.security.jwt.JwtAuthenticationTestUtils.createValidTokenForUser;
import org.springframework.beans.factory.annotation.Value;
  <%_ if (!reactive) { _%>
import static <%= packageName %>.security.jwt.JwtAuthenticationTestUtils.BEARER;
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;
<%_ if (reactive) { _%>
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
<%_ } else { _%>
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
<%_ } _%>
import org.springframework.http.MediaType;
import <%= packageName %>.config.WithMockMyUser;
<%_ if (!reactive) { _%>
import org.springframework.test.web.servlet.MockMvc;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
<%_ } else { _%>
import org.springframework.test.web.reactive.server.WebTestClient;
<%_ } _%>
<%_ if (reactive && searchEngineElasticsearch) { _%>
import reactor.core.publisher.Mono;

import static org.mockito.Mockito.*;
<%_ } _%>

/**
 * Integration tests for the {@link AccountResource} REST controller.
 */
<%_ if (reactive) { _%>
@AutoConfigureWebTestClient(timeout = IntegrationTest.DEFAULT_TIMEOUT)
<%_ } else { _%>
@AutoConfigureMockMvc
<%_ } _%>
@IntegrationTest
class AccountResourceIT {
    static final String TEST_USER_LOGIN = "test";

    @Autowired
<%_ if (reactive) { _%>
    private WebTestClient accountWebTestClient;
<%_ } else { _%>
    private MockMvc restAccountMockMvc;
<%_ } _%>
<%_ if (authenticationTypeJwt) { _%>

    @Value("${jhipster.security.authentication.jwt.base64-secret}")
    private String jwtKey;
<%_ } _%>

    @Test
<%_ if (!authenticationTypeJwt) { _%>
    @WithMockMyUser(username = TEST_USER_LOGIN, authorities = AuthoritiesConstants.ADMIN)
<%_ } _%>
    void testGetExistingAccount() <% if (!reactive) { %>throws Exception <% } %>{
<%_ if (reactive) { _%>
        accountWebTestClient.get().uri("/api/account")
            .accept(MediaType.APPLICATION_JSON)
  <%_ if (authenticationTypeJwt) { _%>
            .headers(header -> header.setBearerAuth(createValidTokenForUser(jwtKey, TEST_USER_LOGIN)))
  <%_ } _%>
            .exchange()
            .expectStatus().isOk()
            .expectHeader().contentType(MediaType.APPLICATION_JSON)
            .expectBody()
            .jsonPath("$.login").isEqualTo(TEST_USER_LOGIN)
            .jsonPath("$.authorities").isEqualTo(AuthoritiesConstants.ADMIN);
<%_ } else { _%>
        restAccountMockMvc.perform(
                get("/api/account")
                    .accept(MediaType.APPLICATION_JSON)
  <%_ if (authenticationTypeJwt) { _%>
                    .header(AUTHORIZATION, BEARER + createValidTokenForUser(jwtKey, TEST_USER_LOGIN))
  <%_ } _%>
            )
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.login").value(TEST_USER_LOGIN))
            .andExpect(jsonPath("$.authorities").value(AuthoritiesConstants.ADMIN));
<%_ } _%>
    }

    @Test
    @WithUnauthenticatedMockUser
    void testNonAuthenticatedUser() <% if (!reactive) { %>throws Exception <% } %>{
<%_ if (reactive) { _%>
        accountWebTestClient.get().uri("/api/authenticate")
            .accept(MediaType.APPLICATION_JSON)
            .exchange()
            .expectStatus().isOk()
            .expectBody().isEmpty();
<%_ } else { _%>
        restAccountMockMvc.perform(get("/api/authenticate")
            .accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk())
            .andExpect(content().string(""));
<%_ } _%>
    }

    @Test
    @WithMockMyUser(TEST_USER_LOGIN)
    void testAuthenticatedUser() <% if (!reactive) { %>throws Exception <% } %>{
<%_ if (reactive) { _%>
        accountWebTestClient
            .get().uri("/api/authenticate")
            .accept(MediaType.APPLICATION_JSON)
            .exchange()
            .expectStatus().isOk()
            .expectBody(String.class).isEqualTo(TEST_USER_LOGIN);
<%_ } else { _%>
        restAccountMockMvc.perform(get("/api/authenticate")
            .with(request -> request)
            .accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk())
            .andExpect(content().string(TEST_USER_LOGIN));
<%_ } _%>
    }
}
