datasurvey/src/test/java/org/datasurvey/web/rest/PublicUserResourceIT.java

100 lines
3.7 KiB
Java

package org.datasurvey.web.rest;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasItems;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import javax.persistence.EntityManager;
import org.datasurvey.IntegrationTest;
import org.datasurvey.domain.User;
import org.datasurvey.repository.UserRepository;
import org.datasurvey.security.AuthoritiesConstants;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.cache.CacheManager;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;
/**
* Integration tests for the {@link UserResource} REST controller.
*/
@AutoConfigureMockMvc
@WithMockUser(authorities = AuthoritiesConstants.ADMIN)
@IntegrationTest
class PublicUserResourceIT {
private static final String DEFAULT_LOGIN = "johndoe";
@Autowired
private UserRepository userRepository;
@Autowired
private EntityManager em;
@Autowired
private CacheManager cacheManager;
@Autowired
private MockMvc restUserMockMvc;
private User user;
@BeforeEach
public void setup() {
cacheManager.getCache(UserRepository.USERS_BY_LOGIN_CACHE).clear();
cacheManager.getCache(UserRepository.USERS_BY_EMAIL_CACHE).clear();
}
@BeforeEach
public void initTest() {
user = UserResourceIT.initTestUser(userRepository, em);
}
@Test
@Transactional
void getAllPublicUsers() throws Exception {
// Initialize the database
userRepository.saveAndFlush(user);
// Get all the users
restUserMockMvc
.perform(get("/api/users?sort=id,desc").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$.[*].login").value(hasItem(DEFAULT_LOGIN)))
.andExpect(jsonPath("$.[*].email").doesNotExist())
.andExpect(jsonPath("$.[*].imageUrl").doesNotExist())
.andExpect(jsonPath("$.[*].langKey").doesNotExist());
}
@Test
@Transactional
void getAllAuthorities() throws Exception {
restUserMockMvc
.perform(get("/api/authorities").accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$").isArray())
.andExpect(jsonPath("$").value(hasItems(AuthoritiesConstants.USER, AuthoritiesConstants.ADMIN)));
}
@Test
@Transactional
void getAllUsersSortedByParameters() throws Exception {
// Initialize the database
userRepository.saveAndFlush(user);
restUserMockMvc.perform(get("/api/users?sort=resetKey,desc").accept(MediaType.APPLICATION_JSON)).andExpect(status().isBadRequest());
restUserMockMvc.perform(get("/api/users?sort=password,desc").accept(MediaType.APPLICATION_JSON)).andExpect(status().isBadRequest());
restUserMockMvc
.perform(get("/api/users?sort=resetKey,id,desc").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest());
restUserMockMvc.perform(get("/api/users?sort=id,desc").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk());
}
}