opencv/src/test/java/pl/edu/amu/wmi/bookapi/Integration/api/UserControllerInt.java

66 lines
2.2 KiB
Java

package pl.edu.amu.wmi.bookapi.Integration.api;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.test.web.servlet.MockMvc;
import pl.edu.amu.wmi.bookapi.fixtures.IntegrationTestUtil;
import pl.edu.amu.wmi.bookapi.fixtures.api.UserControllerRequests;
import pl.edu.amu.wmi.bookapi.models.UserDocument;
import static org.junit.Assert.assertEquals;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest
@AutoConfigureMockMvc
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class UserControllerInt{
@Autowired
MongoTemplate mongoTemplate;
@Autowired
MockMvc mvc;
@Autowired
IntegrationTestUtil testUtil;
private UserControllerRequests userRequests;
@BeforeEach
void cleanCollections() {
this.userRequests = new UserControllerRequests(mvc, new ObjectMapper());
testUtil.cleanCollections();
}
@Test
void should_register_new_user() throws Exception {
userRequests.registerUser("Abc", "def")
.andExpect(status().isOk());
assertEquals(mongoTemplate.findAll(UserDocument.class).size(), 1);
}
@Test
void user_should_not_be_able_to_create_account_with_already_existing_login() throws Exception {
userRequests.registerUser("a", "def")
.andExpect(status().isOk());
userRequests.registerUser("a", "fed")
.andExpect(status().is4xxClientError());
assertEquals(mongoTemplate.findAll(UserDocument.class).size(), 1);
}
@Test
void should_return_jwt_as_header_when_loggin_in() throws Exception {
userRequests.registerUser("a","b")
.andExpect(status().isOk());
String authHeader = userRequests.loginAsUserAndReturnAuthorizationHeader("a","b");
}
}