Merge commit '68a126908895da2e62ddc8955e1c8790195a6e9b' into private-event-sourcing-examples-46
* commit '68a126908895da2e62ddc8955e1c8790195a6e9b': fix AuthControllerIntegrationTest fixed a non-404 responce in the case when the customer is not found fixed issue #54
This commit is contained in:
@@ -40,7 +40,7 @@ public class AccountInfoUpdateService {
|
||||
.set("description", description)
|
||||
.set("balance", toIntegerRepr(initialBalance))
|
||||
.push("changes", ci)
|
||||
.set("date", getFromEventId(version))
|
||||
.set("creationDate", getFromEventId(version))
|
||||
.set("version", version),
|
||||
AccountInfo.class);
|
||||
logger.info("Saved in mongo");
|
||||
|
||||
@@ -8,4 +8,5 @@ dependencies {
|
||||
compile "org.springframework.boot:spring-boot-starter-security:$springBootVersion"
|
||||
|
||||
testCompile "junit:junit:4.11"
|
||||
testCompile "org.springframework.boot:spring-boot-starter-test:$springBootVersion"
|
||||
}
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
package net.chrisrichardson.eventstore.javaexamples.banking.commonauth.controller;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import net.chrisrichardson.eventstore.javaexamples.banking.web.customers.queryside.common.QuerySideCustomer;
|
||||
import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.UserCredentials;
|
||||
import net.chrisrichardson.eventstore.javaexamples.banking.commonauth.CustomerAuthService;
|
||||
import net.chrisrichardson.eventstore.javaexamples.banking.commonauth.model.ErrorResponse;
|
||||
import net.chrisrichardson.eventstore.javaexamples.banking.commonauth.model.User;
|
||||
import net.chrisrichardson.eventstore.javaexamples.banking.web.customers.queryside.common.QuerySideCustomer;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import org.springframework.dao.IncorrectResultSizeDataAccessException;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
@@ -46,7 +47,7 @@ public class AuthController {
|
||||
}
|
||||
|
||||
@ResponseStatus(value = HttpStatus.NOT_FOUND)
|
||||
@ExceptionHandler(IncorrectResultSizeDataAccessException.class)
|
||||
@ExceptionHandler({EmptyResultDataAccessException.class, IncorrectResultSizeDataAccessException.class})
|
||||
public ErrorResponse customersNotFound() {
|
||||
return new ErrorResponse("Customer not found");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
package net.chrisrichardson.eventstore.javaexamples.banking.commonauth.controller;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.Address;
|
||||
import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.Name;
|
||||
import net.chrisrichardson.eventstore.javaexamples.banking.commonauth.CustomerAuthService;
|
||||
import net.chrisrichardson.eventstore.javaexamples.banking.commonauth.model.User;
|
||||
import net.chrisrichardson.eventstore.javaexamples.banking.web.customers.queryside.common.QuerySideCustomer;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.springframework.boot.test.IntegrationTest;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.security.core.token.DefaultToken;
|
||||
import org.springframework.security.core.token.TokenService;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = AuthControllerIntegrationTestConfiguration.class)
|
||||
@IntegrationTest
|
||||
@WebAppConfiguration
|
||||
public class AuthControllerIntegrationTest {
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Mock
|
||||
TokenService tokenService;
|
||||
|
||||
@Mock
|
||||
CustomerAuthService customerAuthService;
|
||||
|
||||
@InjectMocks
|
||||
AuthController authController;
|
||||
|
||||
private static ObjectMapper om = new ObjectMapper();
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
this.mockMvc = MockMvcBuilders.standaloneSetup(authController).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldLogin() throws Exception {
|
||||
when(customerAuthService.findByEmailAndPassword("my@email.com", "my_password")).thenReturn(new QuerySideCustomer("id", new Name("test", "test"), "my@email.com", "my_password", "ssn", "", new Address(), null));
|
||||
when(customerAuthService.findByEmailAndPassword("not_my@email.com", "not_my_password")).thenThrow(new EmptyResultDataAccessException(1));
|
||||
|
||||
when(tokenService.allocateToken(om.writeValueAsString(new User("my@email.com")))).thenReturn(new DefaultToken("key", System.currentTimeMillis(), ""));
|
||||
|
||||
mockMvc.perform(post("/api/login")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content("{\"email\" : \"my@email.com\", \"password\" : \"my_password\"}")
|
||||
.accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(result -> {
|
||||
assertTrue(result.getResponse().getContentAsString().contains("id"));
|
||||
assertTrue(result.getResponse().getContentAsString().contains("my@email.com"));
|
||||
assertTrue(result.getResponse().getContentAsString().contains("my_password"));
|
||||
});
|
||||
|
||||
mockMvc.perform(post("/api/login")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content("{\"email\" : \"not_my@email.com\", \"password\" : \"not_my_password\"}")
|
||||
.accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isNotFound());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package net.chrisrichardson.eventstore.javaexamples.banking.commonauth.controller;
|
||||
|
||||
import net.chrisrichardson.eventstore.javaexamples.banking.commonauth.AuthConfiguration;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
@Configuration
|
||||
@Import(AuthConfiguration.class)
|
||||
@EnableAutoConfiguration
|
||||
public class AuthControllerIntegrationTestConfiguration {
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package net.chrisrichardson.eventstore.javaexamples.banking.commonauth;
|
||||
|
||||
import net.chrisrichardson.eventstore.javaexamples.banking.web.customers.queryside.common.QuerySideCustomer;
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import org.springframework.dao.support.DataAccessUtils;
|
||||
|
||||
/**
|
||||
@@ -14,10 +15,18 @@ public class CustomerAuthService {
|
||||
}
|
||||
|
||||
public QuerySideCustomer findByEmail(String email) {
|
||||
return DataAccessUtils.uniqueResult(customerAuthRepository.findByEmail(email));
|
||||
QuerySideCustomer result = DataAccessUtils.uniqueResult(customerAuthRepository.findByEmail(email));
|
||||
if (result==null)
|
||||
throw new EmptyResultDataAccessException(1);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public QuerySideCustomer findByEmailAndPassword(String email, String password) {
|
||||
return DataAccessUtils.uniqueResult(customerAuthRepository.findByEmailAndPassword(email, password));
|
||||
QuerySideCustomer result = DataAccessUtils.uniqueResult(customerAuthRepository.findByEmailAndPassword(email, password));
|
||||
if (result==null)
|
||||
throw new EmptyResultDataAccessException(1);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user