Skip to content

Commit

Permalink
Merge commit '859a01a4bc2372a11a5377d1206235ca12da4417' into wip-cust…
Browse files Browse the repository at this point in the history
…omer

* commit '859a01a4bc2372a11a5377d1206235ca12da4417':
  - fixed tests fix issue cer#24, fix issue cer#26, fix issue cer#27, fix issue cer#28
  - added password to CustomerInfo - added unique email constraint to CustomerQuerySide - updated authorization logic
  removed transferStates from AccountInfo cannot reproduce issue cer#37
  Revert "wip-customer small issues fixes"
  • Loading branch information
dartandrevinsky committed Sep 9, 2016
2 parents 8869563 + 859a01a commit 12cc48b
Show file tree
Hide file tree
Showing 20 changed files with 123 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -77,12 +76,4 @@ public String getVersion() {
public Date getDate() {
return date;
}

public Map<String, TransferState> getTransferStates() {
return transferStates;
}

public void setTransferStates(Map<String, TransferState> transferStates) {
this.transferStates = transferStates;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ public void create(String accountId, String customerId, String title, BigDecimal


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


Expand All @@ -72,9 +74,11 @@ public void updateBalance(String accountId, String changeId, long balanceDelta,
}

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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ public AccountInfo findByAccountId(String accountId) {
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -26,4 +30,12 @@ public String getEmail() {
public void setEmail(String email) {
this.email = email;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,6 +19,7 @@
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;
Expand Down Expand Up @@ -50,15 +52,13 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
@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));
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
interface CustomerAuthRepository extends MongoRepository<QuerySideCustomer, String> {

List<QuerySideCustomer> findByEmail(String email);

List<QuerySideCustomer> findByEmailAndPassword(String email, String password);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ public QuerySideCustomer findByEmail(String email) {
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);
}
Expand Down
1 change: 1 addition & 0 deletions java-spring/common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class CustomerInfo {
@NotNull
protected String email;
@NotNull
protected String password;
@NotNull
protected String ssn;
@NotNull
protected String phoneNumber;
Expand All @@ -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;
Expand All @@ -37,6 +40,10 @@ public String getEmail() {
return email;
}

public String getPassword() {
return password;
}

public String getSsn() {
return ssn;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -51,6 +57,14 @@ public void setEmail(String email) {
this.email = email;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getSsn() {
return ssn;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public void create(String id, CustomerInfo customerInfo) {
querySideCustomerRepository.save(new QuerySideCustomer(id,
customerInfo.getName(),
customerInfo.getEmail(),
customerInfo.getPassword(),
customerInfo.getSsn(),
customerInfo.getPhoneNumber(),
customerInfo.getAddress(),
Expand Down
1 change: 1 addition & 0 deletions java-spring/customers-query-side-service/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ public void shouldGetCustomerById() {

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

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,7 +17,7 @@
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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,14 @@ public void shouldCreateCustomerAndLogin() {

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

Expand Down
Loading

0 comments on commit 12cc48b

Please sign in to comment.