-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from jeffreybakker/feature/extension_2
Feature/extension 2
- Loading branch information
Showing
11 changed files
with
231 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package honours.ing.banq.auth; | ||
|
||
/** | ||
* @author jeffrey | ||
* @since 3-8-17 | ||
*/ | ||
public class CardBlockedError extends Exception { | ||
|
||
public CardBlockedError() { | ||
super(); | ||
} | ||
|
||
public CardBlockedError(String s) { | ||
super(s); | ||
} | ||
|
||
public CardBlockedError(String s, Throwable throwable) { | ||
super(s, throwable); | ||
} | ||
|
||
public CardBlockedError(Throwable throwable) { | ||
super(throwable); | ||
} | ||
|
||
protected CardBlockedError(String s, Throwable throwable, boolean b, boolean b1) { | ||
super(s, throwable, b, b1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package honours.ing.banq.card; | ||
|
||
import com.googlecode.jsonrpc4j.JsonRpcParam; | ||
import com.googlecode.jsonrpc4j.JsonRpcService; | ||
import honours.ing.banq.InvalidParamValueError; | ||
import honours.ing.banq.access.NoEffectError; | ||
import honours.ing.banq.auth.NotAuthorizedError; | ||
|
||
/** | ||
* @author jeffrey | ||
* @since 3-8-17 | ||
*/ | ||
@JsonRpcService("/api/card") | ||
public interface CardService { | ||
|
||
/** | ||
* A PIN card that has been blocked can be unblocked if the user logs in and calls this unblock card method. | ||
* @param authToken the authentication token, obtained with {@code getAuthToken()} | ||
* @param iBAN the number of the bank account | ||
* @param pinCard the number of the pin card | ||
* @return an empty dictionary if successful | ||
* @throws InvalidParamValueError one or more parameter has an invalid value. See the message | ||
* @throws NotAuthorizedError the authenticated user is not authorized to perform this action | ||
* @throws NoEffectError if the card is not blocked this method will have no effect | ||
*/ | ||
Object unblockCard(@JsonRpcParam("authToken") String authToken, @JsonRpcParam("iBAN") String iBAN, | ||
@JsonRpcParam("pinCard") String pinCard) | ||
throws InvalidParamValueError, NotAuthorizedError, NoEffectError; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package honours.ing.banq.card; | ||
|
||
import com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl; | ||
import honours.ing.banq.InvalidParamValueError; | ||
import honours.ing.banq.access.NoEffectError; | ||
import honours.ing.banq.account.BankAccount; | ||
import honours.ing.banq.account.BankAccountRepository; | ||
import honours.ing.banq.auth.AuthService; | ||
import honours.ing.banq.auth.NotAuthorizedError; | ||
import honours.ing.banq.customer.Customer; | ||
import honours.ing.banq.util.IBANUtil; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@AutoJsonRpcServiceImpl | ||
@Transactional(readOnly = true) | ||
public class CardServiceImpl implements CardService { | ||
|
||
// Services | ||
@Autowired | ||
private AuthService auth; | ||
|
||
// Repositories | ||
@Autowired | ||
private CardRepository repository; | ||
|
||
@Autowired | ||
private BankAccountRepository accountRepository; | ||
|
||
@Transactional | ||
@Override | ||
public Object unblockCard(String authToken, String iBAN, String pinCard) | ||
throws InvalidParamValueError, NotAuthorizedError, NoEffectError { | ||
Customer customer = auth.getAuthorizedCustomer(authToken); | ||
|
||
// Retrieve the bank account and check whether we are authorized to access it | ||
BankAccount account = accountRepository.findOne((int) IBANUtil.getAccountNumber(iBAN)); | ||
if (account == null) { | ||
throw new InvalidParamValueError("There is no bank account with the given iBAN"); | ||
} | ||
|
||
if (account.getPrimaryHolder() != customer && !account.getHolders().contains(customer)) { | ||
throw new NotAuthorizedError(); | ||
} | ||
|
||
// Retrieve the pin card and check whether we are authorized to access it | ||
Card card = repository.findByAccountAndCardNumber(account, pinCard); | ||
if (card == null) { | ||
throw new InvalidParamValueError("There is no pin card with the given card number"); | ||
} | ||
|
||
if (card.getHolder() != customer) { | ||
throw new NotAuthorizedError(); | ||
} | ||
|
||
if (!card.isBlocked()) { | ||
throw new NoEffectError(); | ||
} | ||
|
||
card.resetAttempts(); | ||
repository.save(card); | ||
|
||
return new Object(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package honours.ing.banq.card; | ||
|
||
import honours.ing.banq.BoilerplateTest; | ||
import honours.ing.banq.auth.CardBlockedError; | ||
import honours.ing.banq.auth.InvalidPINError; | ||
import honours.ing.banq.transaction.TransactionService; | ||
import org.junit.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* @author jeffrey | ||
* @since 3-8-17 | ||
*/ | ||
public class CardServiceTest extends BoilerplateTest { | ||
|
||
@Autowired | ||
private CardService service; | ||
|
||
@Autowired | ||
private TransactionService transactionService; | ||
|
||
@Test(expected = CardBlockedError.class) | ||
public void blockCard() throws Exception { | ||
for (int i = 0; i < 3; i++) { | ||
try { | ||
transactionService.depositIntoAccount(account1.iBan, account1.cardNumber, "0000", 10.0); | ||
} catch (InvalidPINError ignored) { } | ||
} | ||
|
||
transactionService.depositIntoAccount(account1.iBan, account1.cardNumber, account1.pin, 10.0); | ||
} | ||
|
||
@Test | ||
public void unblockCard() throws Exception { | ||
try { | ||
blockCard(); | ||
} catch (CardBlockedError ignored) { } | ||
|
||
service.unblockCard(account1.token, account1.iBan, account1.cardNumber); | ||
|
||
// Now test if the card is usable again | ||
transactionService.depositIntoAccount(account1.iBan, account1.cardNumber, account1.pin, 10.0); | ||
} | ||
|
||
} |