Merge commit '859a01a4bc2372a11a5377d1206235ca12da4417' into wip-customer

* commit '859a01a4bc2372a11a5377d1206235ca12da4417':
  - fixed tests fix issue #24, fix issue #26, fix issue #27, fix issue #28
  - added password to CustomerInfo - added unique email constraint to CustomerQuerySide - updated authorization logic
  removed transferStates from AccountInfo cannot reproduce issue #37
  Revert "wip-customer small issues fixes"
This commit is contained in:
Andrew Revinsky (DART)
2016-09-09 18:38:32 +03:00
20 changed files with 123 additions and 71 deletions

View File

@@ -18,7 +18,6 @@ public class AccountInfo {
private long balance;
private List<AccountChangeInfo> changes;
private Map<String, AccountTransactionInfo> transactions;
private Map<String, TransferState> transferStates;
private String version;
private Date date;
@@ -77,12 +76,4 @@ public class AccountInfo {
public Date getDate() {
return date;
}
public Map<String, TransferState> getTransferStates() {
return transferStates;
}
public void setTransferStates(Map<String, TransferState> transferStates) {
this.transferStates = transferStates;
}
}

View File

@@ -55,10 +55,12 @@ public class AccountInfoUpdateService {
public void addTransaction(String accountId, AccountTransactionInfo ti) {
System.out.println("Start addTransaction for: "+ti.toString());
mongoTemplate.upsert(new Query(where("id").is(accountId)),
new Update().
set("transactions." + ti.getTransactionId(), ti),
AccountInfo.class);
System.out.println("End addTransaction for: "+ti.toString());
}
@@ -72,9 +74,11 @@ public class AccountInfoUpdateService {
}
public void updateTransactionStatus(String accountId, String transactionId, TransferState status) {
System.out.println("Start updateTransactionStatus "+accountId +" "+transactionId+" "+status);
mongoTemplate.upsert(new Query(where("id").is(accountId)),
new Update().
set("transferStates." + transactionId, status),
set("transactions." + transactionId + ".status", status),
AccountInfo.class);
System.out.println("End updateTransactionStatus "+accountId +" "+transactionId+" "+status);
}
}

View File

@@ -15,8 +15,6 @@ public class AccountQueryService {
if (account == null)
throw new AccountNotFoundException(accountId);
else
if(account.getTransferStates()!=null)
account.getTransactions().stream().forEach(ati -> ati.setStatus(account.getTransferStates().get(ati.getTransactionId())));
return account;
}

View File

@@ -41,7 +41,7 @@ public class AuthController {
@RequestMapping(value = "/login", method = POST)
public ResponseEntity<QuerySideCustomer> doAuth(@RequestBody @Valid AuthRequest request) throws IOException {
QuerySideCustomer customer = customerAuthService.findByEmail(request.getEmail());
QuerySideCustomer customer = customerAuthService.findByEmailAndPassword(request.getEmail(), request.getPassword());
Token token = tokenService.allocateToken(objectMapper.writeValueAsString(new User(request.getEmail())));
return ResponseEntity.status(HttpStatus.OK).header("access-token", token.getKey())

View File

@@ -12,11 +12,15 @@ public class AuthRequest {
@Email
private String email;
@NotBlank
private String password;
public AuthRequest() {
}
public AuthRequest(String email) {
public AuthRequest(String email, String password) {
this.email = email;
this.password = password;
}
public String getEmail() {
@@ -26,4 +30,12 @@ public class AuthRequest {
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

View File

@@ -1,5 +1,6 @@
package net.chrisrichardson.eventstore.javaexamples.banking.commonauth;
import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.QuerySideCustomer;
import net.chrisrichardson.eventstore.javaexamples.banking.commonauth.filter.StatelessAuthenticationFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@@ -18,6 +19,7 @@ import org.springframework.security.core.token.KeyBasedPersistenceTokenService;
import org.springframework.security.core.token.TokenService;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
import java.security.SecureRandom;
@@ -50,15 +52,13 @@ public class AuthConfiguration extends WebSecurityConfigurerAdapter {
@Override
public UserDetailsService userDetailsServiceBean() {
return email -> {
/* QuerySideCustomer customer = customerAuthService.findByEmail(email);
if (customer != null) {
return new User(email);
} else {
throw new UsernameNotFoundException(String.format("could not find the customer '%s'", email));
}*/
//authorize everyone with basic authentication
return new User(email, "", true, true, true, true,
AuthorityUtils.createAuthorityList("USER"));
QuerySideCustomer customer = customerAuthService.findByEmail(email);
if (customer != null) {
return new User(email, customer.getPassword(), true, true, true, true,
AuthorityUtils.createAuthorityList("USER"));
} else {
throw new UsernameNotFoundException(String.format("could not find the customer '%s'", email));
}
};
}

View File

@@ -8,4 +8,6 @@ import java.util.List;
interface CustomerAuthRepository extends MongoRepository<QuerySideCustomer, String> {
List<QuerySideCustomer> findByEmail(String email);
List<QuerySideCustomer> findByEmailAndPassword(String email, String password);
}

View File

@@ -19,9 +19,14 @@ public class CustomerAuthService {
List<QuerySideCustomer> customers = customerAuthRepository.findByEmail(email);
if (customers.isEmpty())
throw new EmptyResultDataAccessException(1);
//TODO: add unique email constraint
/* else if(customers.size()>1)
throw new IncorrectResultSizeDataAccessException(1, customers.size());*/
else
return customers.get(0);
}
public QuerySideCustomer findByEmailAndPassword(String email, String password) {
List<QuerySideCustomer> customers = customerAuthRepository.findByEmailAndPassword(email, password);
if (customers.isEmpty())
throw new EmptyResultDataAccessException(1);
else
return customers.get(0);
}

View File

@@ -3,6 +3,7 @@ apply plugin: 'java'
dependencies {
compile "commons-lang:commons-lang:2.6"
compile "org.springframework.boot:spring-boot-starter-web:$springBootVersion"
compile "org.springframework.boot:spring-boot-starter-data-mongodb:$springBootVersion"
testCompile group: 'junit', name: 'junit', version: '4.11'
}

View File

@@ -13,6 +13,8 @@ public class CustomerInfo {
@NotNull
protected String email;
@NotNull
protected String password;
@NotNull
protected String ssn;
@NotNull
protected String phoneNumber;
@@ -21,9 +23,10 @@ public class CustomerInfo {
public CustomerInfo() {
}
public CustomerInfo(Name name, String email, String ssn, String phoneNumber, Address address) {
public CustomerInfo(Name name, String email, String password, String ssn, String phoneNumber, Address address) {
this.name = name;
this.email = email;
this.password = password;
this.ssn = ssn;
this.phoneNumber = phoneNumber;
this.address = address;
@@ -37,6 +40,10 @@ public class CustomerInfo {
return email;
}
public String getPassword() {
return password;
}
public String getSsn() {
return ssn;
}

View File

@@ -1,14 +1,19 @@
package net.chrisrichardson.eventstore.javaexamples.banking.common.customers;
import org.springframework.data.mongodb.core.index.Indexed;
import java.util.Map;
/**
* Created by Main on 05.02.2016.
*/
public class QuerySideCustomer {
private String id;
private Name name;
@Indexed(unique=true)
private String email;
private String password;
private String ssn;
private String phoneNumber;
private Address address;
@@ -17,10 +22,11 @@ public class QuerySideCustomer {
public QuerySideCustomer() {
}
public QuerySideCustomer(String id, Name name, String email, String ssn, String phoneNumber, Address address, Map<String, ToAccountInfo> toAccounts) {
public QuerySideCustomer(String id, Name name, String email, String password, String ssn, String phoneNumber, Address address, Map<String, ToAccountInfo> toAccounts) {
this.id = id;
this.name = name;
this.email = email;
this.password = password;
this.ssn = ssn;
this.phoneNumber = phoneNumber;
this.address = address;
@@ -51,6 +57,14 @@ public class QuerySideCustomer {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSsn() {
return ssn;
}

View File

@@ -27,6 +27,7 @@ public class CustomerInfoUpdateService {
querySideCustomerRepository.save(new QuerySideCustomer(id,
customerInfo.getName(),
customerInfo.getEmail(),
customerInfo.getPassword(),
customerInfo.getSsn(),
customerInfo.getPhoneNumber(),
customerInfo.getAddress(),

View File

@@ -14,6 +14,7 @@ dependencies {
testCompile project(":testutil")
testCompile project(":customers-command-side-service")
testCompile "org.springframework.boot:spring-boot-starter-test"
testCompile "io.eventuate.client.java:eventuate-client-java-jdbc:$eventuateClientVersion"
}
test {

View File

@@ -48,8 +48,10 @@ public class CustomersQuerySideServiceIntegrationTest {
final CustomerResponse customerResponse = restTemplate.postForEntity(baseUrl("/customers"), customerInfo, CustomerResponse.class).getBody();
final String customerId = customerResponse.getId();
final String email = customerResponse.getCustomerInfo().getEmail();
final String password = customerResponse.getCustomerInfo().getPassword();
customersTestUtils.assertCustomerResponse(customerId, customerInfo);
customersTestUtils.assertCustomerResponse(customerId, email, password, customerInfo);
}
}

View File

@@ -1,6 +1,9 @@
package net.chrisrichardson.eventstore.javaexamples.banking.web;
import io.eventuate.javaclient.spring.jdbc.EventuateJdbcEventStoreConfiguration;
import net.chrisrichardson.eventstore.javaexamples.banking.commonauth.AuthConfiguration;
import net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.customers.CustomersCommandSideWebConfiguration;
import net.chrisrichardson.eventstore.javaexamples.banking.web.customers.queryside.CustomersQuerySideWebConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
@@ -14,7 +17,7 @@ import java.util.Arrays;
import java.util.List;
@Configuration
@Import({CustomersCommandSideServiceConfiguration.class, CustomersQuerySideServiceConfiguration.class, AuthConfiguration.class})
@Import({CustomersCommandSideWebConfiguration.class, CustomersQuerySideWebConfiguration.class, EventuateJdbcEventStoreConfiguration.class, AuthConfiguration.class})
@EnableAutoConfiguration
public class CustomersQuerySideServiceTestConfiguration {

View File

@@ -53,19 +53,14 @@ public class BankingAuthTest {
final CustomerResponse customerResponse = restTemplate.postForEntity(baseUrl("/customers"), customerInfo, CustomerResponse.class).getBody();
final String customerId = customerResponse.getId();
final String password = customerResponse.getCustomerInfo().getPassword();
Assert.assertNotNull(customerId);
Assert.assertEquals(customerInfo, customerResponse.getCustomerInfo());
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
customersTestUtils.assertCustomerResponse(customerId, email, password, customerInfo);
customersTestUtils.assertCustomerResponse(customerId, customerInfo);
AuthRequest authRequest = new AuthRequest(email);
AuthRequest authRequest = new AuthRequest(email, password);
final QuerySideCustomer loginQuerySideCustomer = restTemplate.postForEntity(baseUrl("/login"), authRequest, QuerySideCustomer.class).getBody();

View File

@@ -26,6 +26,13 @@ public abstract class AbstractRestAPITest {
@Test
public void shouldCreateAccountsAndTransferMoney() {
CustomerInfo customerInfo = generateCustomerInfo();
final CustomerResponse customerResponse = getRestTemplate().postForEntity(baseUrl("/customers"), customerInfo, CustomerResponse.class).getBody();
final String customerId = customerResponse.getId();
final String email = customerResponse.getCustomerInfo().getEmail();
final String password = customerResponse.getCustomerInfo().getPassword();
BigDecimal initialFromAccountBalance = new BigDecimal(500);
BigDecimal initialToAccountBalance = new BigDecimal(100);
BigDecimal amountToTransfer = new BigDecimal(150);
@@ -34,36 +41,36 @@ public abstract class AbstractRestAPITest {
BigDecimal finalToAccountBalance = initialToAccountBalance.add(amountToTransfer);
final CreateAccountResponse fromAccount = getAuthenticatedRestTemplate().postForEntity(baseUrl("/accounts"),
new CreateAccountRequest("00000000-00000000", "My 1 Account", "", initialFromAccountBalance),
CreateAccountResponse.class);
new CreateAccountRequest(customerId, "My 1 Account", "", initialFromAccountBalance),
CreateAccountResponse.class, email, password);
final String fromAccountId = fromAccount.getAccountId();
CreateAccountResponse toAccount = getAuthenticatedRestTemplate().postForEntity(baseUrl("/accounts"),
new CreateAccountRequest("00000000-00000000", "My 2 Account", "", initialToAccountBalance),
CreateAccountResponse.class);
CreateAccountResponse.class, email, password);
String toAccountId = toAccount.getAccountId();
Assert.assertNotNull(fromAccountId);
Assert.assertNotNull(toAccountId);
assertAccountBalance(fromAccountId, initialFromAccountBalance);
assertAccountBalance(toAccountId, initialToAccountBalance);
assertAccountBalance(email, password, fromAccountId, initialFromAccountBalance);
assertAccountBalance(email, password, toAccountId, initialToAccountBalance);
final CreateMoneyTransferResponse moneyTransfer = getAuthenticatedRestTemplate().postForEntity(baseUrl("/transfers"),
new CreateMoneyTransferRequest(fromAccountId, toAccountId, amountToTransfer, ""),
CreateMoneyTransferResponse.class);
CreateMoneyTransferResponse.class, email, password);
assertAccountBalance(fromAccountId, finalFromAccountBalance);
assertAccountBalance(toAccountId, finalToAccountBalance);
assertAccountBalance(email, password, fromAccountId, finalFromAccountBalance);
assertAccountBalance(email, password, toAccountId, finalToAccountBalance);
eventually(
new Producer<AccountHistoryResponse>() {
@Override
public CompletableFuture<AccountHistoryResponse> produce() {
return CompletableFuture.completedFuture(getAuthenticatedRestTemplate().getForEntity(baseUrl("/accounts/" + fromAccountId + "/history"),
AccountHistoryResponse.class));
AccountHistoryResponse.class, email, password));
}
},
new Verifier<AccountHistoryResponse>() {
@@ -91,28 +98,30 @@ public abstract class AbstractRestAPITest {
final CustomerResponse customerResponse = getRestTemplate().postForEntity(baseUrl("/customers"), customerInfo, CustomerResponse.class).getBody();
final String customerId = customerResponse.getId();
final String email = customerResponse.getCustomerInfo().getEmail();
final String password = customerResponse.getCustomerInfo().getPassword();
Assert.assertNotNull(customerId);
assertEquals(customerInfo, customerResponse.getCustomerInfo());
getCustomersTestUtils().assertCustomerResponse(customerId, customerInfo);
getCustomersTestUtils().assertCustomerResponse(customerId, email, password, customerInfo);
final CreateAccountResponse account = getAuthenticatedRestTemplate().postForEntity(baseUrl("/accounts"),
new CreateAccountRequest(customerId, "My 1 Account", "", initialFromAccountBalance),
CreateAccountResponse.class);
CreateAccountResponse.class, email, password);
final String accountId = account.getAccountId();
Assert.assertNotNull(accountId);
assertAccountBalance(accountId, initialFromAccountBalance);
assertAccountBalance(email, password, accountId, initialFromAccountBalance);
eventually(
new Producer<GetAccountsResponse>() {
@Override
public CompletableFuture<GetAccountsResponse> produce() {
return CompletableFuture.completedFuture(getAuthenticatedRestTemplate().getForEntity(baseUrl("/customers/"+customerId+"/accounts"),
GetAccountsResponse.class));
GetAccountsResponse.class, email, password));
}
},
new Verifier<GetAccountsResponse>() {
@@ -129,33 +138,35 @@ public abstract class AbstractRestAPITest {
final CustomerResponse customerResponse = getRestTemplate().postForEntity(baseUrl("/customers"), customerInfo, CustomerResponse.class).getBody();
final String customerId = customerResponse.getId();
final String email = customerResponse.getCustomerInfo().getEmail();
final String password = customerResponse.getCustomerInfo().getPassword();
Assert.assertNotNull(customerId);
assertEquals(customerInfo, customerResponse.getCustomerInfo());
getCustomersTestUtils().assertCustomerResponse(customerId, customerInfo);
getCustomersTestUtils().assertCustomerResponse(customerId, email, password, customerInfo);
ToAccountInfo toAccountInfo = generateToAccountInfo();
getAuthenticatedRestTemplate().postForEntity(baseUrl("/customers/" + customerId + "/toaccounts"),
toAccountInfo,
null);
null, email, password);
assertToAccountsContains(customerId, toAccountInfo);
assertToAccountsContains(customerId, email, password, toAccountInfo);
}
private BigDecimal toCents(BigDecimal dollarAmount) {
return dollarAmount.multiply(new BigDecimal(100));
}
private void assertAccountBalance(final String fromAccountId, final BigDecimal expectedBalanceInDollars) {
private void assertAccountBalance(final String email, final String password, final String fromAccountId, final BigDecimal expectedBalanceInDollars) {
final BigDecimal inCents = toCents(expectedBalanceInDollars);
eventually(
new Producer<GetAccountResponse>() {
@Override
public CompletableFuture<GetAccountResponse> produce() {
return CompletableFuture.completedFuture(getAuthenticatedRestTemplate().getForEntity(baseUrl("/accounts/" + fromAccountId),
GetAccountResponse.class));
GetAccountResponse.class, email, password));
}
},
new Verifier<GetAccountResponse>() {
@@ -167,13 +178,13 @@ public abstract class AbstractRestAPITest {
});
}
private void assertToAccountsContains(final String customerId, final ToAccountInfo toAccountInfo) {
private void assertToAccountsContains(final String customerId, final String email, final String password, final ToAccountInfo toAccountInfo) {
eventually(
new Producer<QuerySideCustomer>() {
@Override
public CompletableFuture<QuerySideCustomer> produce() {
return CompletableFuture.completedFuture(getAuthenticatedRestTemplate().getForEntity(baseUrl("/customers/" + customerId),
QuerySideCustomer.class));
QuerySideCustomer.class, email, password));
}
},
new Verifier<QuerySideCustomer>() {

View File

@@ -11,19 +11,23 @@ public class AuthenticatedRestTemplate {
this.restTemplate = restTemplate;
}
public <T> T getForEntity(String url, Class<T> clazz) {
public <T> T getForEntity(String url, Class<T> clazz, String email, String password) {
return BasicAuthUtils.doBasicAuthenticatedRequest(restTemplate,
url,
HttpMethod.GET,
clazz);
clazz,
email,
password);
}
public <T> T postForEntity(String url, Object requestObject, Class<T> clazz) {
public <T> T postForEntity(String url, Object requestObject, Class<T> clazz, String email, String password) {
return BasicAuthUtils.doBasicAuthenticatedRequest(restTemplate,
url,
HttpMethod.POST,
clazz,
requestObject
requestObject,
email,
password
);
}
}

View File

@@ -12,10 +12,10 @@ import java.nio.charset.Charset;
*/
public class BasicAuthUtils {
public static HttpHeaders basicAuthHeaders(String username) {
public static HttpHeaders basicAuthHeaders(String username, String password) {
return new HttpHeaders() {
{
String auth = username + ":";
String auth = username + ":" + password;
byte[] encodedAuth = Base64.encodeBase64(
auth.getBytes(Charset.forName("US-ASCII")));
String authHeader = "Basic " + new String(encodedAuth);
@@ -24,16 +24,16 @@ public class BasicAuthUtils {
};
}
public static <T> T doBasicAuthenticatedRequest(RestTemplate restTemplate, String url, HttpMethod httpMethod, Class<T> responseType) {
return doBasicAuthenticatedRequest(restTemplate, url, httpMethod, responseType, null);
public static <T> T doBasicAuthenticatedRequest(RestTemplate restTemplate, String url, HttpMethod httpMethod, Class<T> responseType, String email, String password) {
return doBasicAuthenticatedRequest(restTemplate, url, httpMethod, responseType, null, email, password);
}
public static <T> T doBasicAuthenticatedRequest(RestTemplate restTemplate, String url, HttpMethod httpMethod, Class<T> responseType, Object requestObject) {
public static <T> T doBasicAuthenticatedRequest(RestTemplate restTemplate, String url, HttpMethod httpMethod, Class<T> responseType, Object requestObject, String email, String password) {
HttpEntity httpEntity;
if (requestObject != null) {
httpEntity = new HttpEntity<>(requestObject, BasicAuthUtils.basicAuthHeaders("test_user@mail.com"));
httpEntity = new HttpEntity<>(requestObject, BasicAuthUtils.basicAuthHeaders(email, password));
} else {
httpEntity = new HttpEntity(BasicAuthUtils.basicAuthHeaders("test_user@mail.com"));
httpEntity = new HttpEntity(BasicAuthUtils.basicAuthHeaders(email, password));
}
ResponseEntity<T> responseEntity = restTemplate.exchange(url,

View File

@@ -21,13 +21,13 @@ public class CustomersTestUtils {
this.customersBaseUrl = customersBaseUrl;
}
public void assertCustomerResponse(final String customerId, final CustomerInfo customerInfo) {
public void assertCustomerResponse(final String customerId, final String email, final String password, final CustomerInfo customerInfo) {
AuthenticatedRestTemplate art = new AuthenticatedRestTemplate(restTemplate);
eventually(
new Producer<QuerySideCustomer>() {
@Override
public CompletableFuture<QuerySideCustomer> produce() {
return CompletableFuture.completedFuture(art.getForEntity(customersBaseUrl + customerId, QuerySideCustomer.class));
return CompletableFuture.completedFuture(art.getForEntity(customersBaseUrl + customerId, QuerySideCustomer.class, email, password));
}
},
new Verifier<QuerySideCustomer>() {
@@ -55,6 +55,7 @@ public class CustomersTestUtils {
return new CustomerInfo(
new Name("John", "Doe"),
email,
"simple_password",
"000-00-0000",
"1-111-111-1111",
new Address("street 1",