fixed a non-404 responce in the case when the customer is not found

This commit is contained in:
dartpopikyardo
2016-09-27 16:08:11 +03:00
parent 7260376b17
commit 4b74d2c1f1
5 changed files with 107 additions and 4 deletions

View File

@@ -8,4 +8,5 @@ dependencies {
compile "org.springframework.boot:spring-boot-starter-security:$springBootVersion" compile "org.springframework.boot:spring-boot-starter-security:$springBootVersion"
testCompile "junit:junit:4.11" testCompile "junit:junit:4.11"
testCompile "org.springframework.boot:spring-boot-starter-test:$springBootVersion"
} }

View File

@@ -1,12 +1,13 @@
package net.chrisrichardson.eventstore.javaexamples.banking.commonauth.controller; package net.chrisrichardson.eventstore.javaexamples.banking.commonauth.controller;
import com.fasterxml.jackson.databind.ObjectMapper; 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.common.customers.UserCredentials;
import net.chrisrichardson.eventstore.javaexamples.banking.commonauth.CustomerAuthService; 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.ErrorResponse;
import net.chrisrichardson.eventstore.javaexamples.banking.commonauth.model.User; 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.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.dao.IncorrectResultSizeDataAccessException; import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@@ -46,7 +47,7 @@ public class AuthController {
} }
@ResponseStatus(value = HttpStatus.NOT_FOUND) @ResponseStatus(value = HttpStatus.NOT_FOUND)
@ExceptionHandler(IncorrectResultSizeDataAccessException.class) @ExceptionHandler({EmptyResultDataAccessException.class, IncorrectResultSizeDataAccessException.class})
public ErrorResponse customersNotFound() { public ErrorResponse customersNotFound() {
return new ErrorResponse("Customer not found"); return new ErrorResponse("Customer not found");
} }

View File

@@ -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());
}
}

View File

@@ -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 {
}

View File

@@ -1,6 +1,7 @@
package net.chrisrichardson.eventstore.javaexamples.banking.commonauth; package net.chrisrichardson.eventstore.javaexamples.banking.commonauth;
import net.chrisrichardson.eventstore.javaexamples.banking.web.customers.queryside.common.QuerySideCustomer; import net.chrisrichardson.eventstore.javaexamples.banking.web.customers.queryside.common.QuerySideCustomer;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.dao.support.DataAccessUtils; import org.springframework.dao.support.DataAccessUtils;
/** /**
@@ -14,10 +15,18 @@ public class CustomerAuthService {
} }
public QuerySideCustomer findByEmail(String email) { 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) { 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;
} }
} }