11package de .adorsys .psd2 .sandbox .xs2a .ais ;
22
3+ import static de .adorsys .psd2 .xs2a .domain .MessageErrorCode .CONSENT_INVALID ;
34import static org .hamcrest .CoreMatchers .containsString ;
45import static org .hamcrest .CoreMatchers .equalTo ;
56import static org .hamcrest .MatcherAssert .assertThat ;
5253import org .junit .Ignore ;
5354import org .springframework .beans .factory .annotation .Autowired ;
5455import org .springframework .http .HttpMethod ;
56+ import org .springframework .http .HttpStatus ;
5557import org .springframework .http .ResponseEntity ;
5658
5759@ Ignore ("without this ignore intellij tries to run the step files" )
@@ -314,12 +316,31 @@ public void getAccountListWithBalance(String withBalance) {
314316 }
315317
316318 @ When ("PSU accesses the transaction list" )
317- public void getAccountList () {
319+ public void getTransactionList () {
318320 getTransactionList ("true" );
319321 }
320322
321323 @ When ("PSU accesses the transaction list withBalances (.*)" )
322324 public void getTransactionList (String withBalance ) {
325+ ResponseEntity <TransactionsResponse200Json > response = getTransactions (
326+ TransactionsResponse200Json .class , withBalance
327+ );
328+
329+ assertTrue (response .getStatusCode ().is2xxSuccessful ());
330+
331+ context .setActualResponse (response );
332+ }
333+
334+ @ When ("PSU accesses the transaction list without a valid consent" )
335+ public void getTransactionListWithoutConsent () {
336+ ResponseEntity <JsonNode > response = getTransactions (JsonNode .class , "false" );
337+
338+ assertEquals (HttpStatus .UNAUTHORIZED , response .getStatusCode ());
339+
340+ context .setActualResponse (response );
341+ }
342+
343+ private <T > ResponseEntity <T > getTransactions (Class <T > clazz , String withBalance ) {
323344 ResponseEntity <AccountList > actualResponse = context .getActualResponse ();
324345 HashMap <String , String > headers = TestUtils .createSession ();
325346 headers .put ("Consent-ID" , context .getConsentId ());
@@ -329,37 +350,47 @@ public void getTransactionList(String withBalance) {
329350 "?bookingStatus=both&dateFrom=%s&dateTo=%s&withBalance=%s" ,
330351 LocalDate .now ().minusYears (1 ), LocalDate .now (), withBalance
331352 );
332-
333353 context .setWithBalance (Boolean .parseBoolean (withBalance ));
334354
335- ResponseEntity < TransactionsResponse200Json > response = template .exchange (
355+ return template .exchange (
336356 "accounts/" + context .getAccountId () + "/transactions" + queryParams ,
337357 HttpMethod .GET ,
338358 request .toHttpEntity (),
339- TransactionsResponse200Json .class );
359+ clazz );
360+ }
361+
362+ @ When ("PSU accesses the balance list" )
363+ public void getBalanceList () {
364+ ResponseEntity <ReadAccountBalanceResponse200 > response = getBalances (
365+ ReadAccountBalanceResponse200 .class
366+ );
340367
341368 assertTrue (response .getStatusCode ().is2xxSuccessful ());
342369
343370 context .setActualResponse (response );
344371 }
345372
346- @ When ("PSU accesses the balance list" )
347- public void getBalanceList () {
373+ @ When ("PSU accesses the balance list without a valid consent" )
374+ public void psuAccessesTheBalanceListWithoutAValidConsent () {
375+ ResponseEntity <JsonNode > response = getBalances (JsonNode .class );
376+
377+ assertEquals (HttpStatus .UNAUTHORIZED , response .getStatusCode ());
378+
379+ context .setActualResponse (response );
380+ }
381+
382+ private <T > ResponseEntity <T > getBalances (Class <T > clazz ) {
348383 ResponseEntity <AccountList > actualResponse = context .getActualResponse ();
349384 HashMap <String , String > headers = TestUtils .createSession ();
350385 headers .put ("Consent-ID" , context .getConsentId ());
351386 Request <?> request = Request .emptyRequest (headers );
352387 context .setAccountId (actualResponse .getBody ().getAccounts ().get (0 ).getResourceId ());
353388
354- ResponseEntity < ReadAccountBalanceResponse200 > response = template .exchange (
389+ return template .exchange (
355390 "accounts/" + context .getAccountId () + "/balances" ,
356391 HttpMethod .GET ,
357392 request .toHttpEntity (),
358- ReadAccountBalanceResponse200 .class );
359-
360- assertTrue (response .getStatusCode ().is2xxSuccessful ());
361-
362- context .setActualResponse (response );
393+ clazz );
363394 }
364395
365396 @ When ("PSU accesses a single transaction" )
@@ -467,6 +498,27 @@ public void receiveErrorMessageAndCode(String errorMessage) {
467498 assertThat (err .get ("text" ).asText (), containsString ("channel independent blocking" ));
468499 }
469500
501+ @ Then ("the transactions are not accessible" )
502+ public void transactionsAreNotAccessible () {
503+ assertUnauthorizedBecauseConsentMissingPermissions ();
504+ }
505+
506+ @ Then ("the balances are not accessible" )
507+ public void balancesAreNotAccessible () {
508+ assertUnauthorizedBecauseConsentMissingPermissions ();
509+ }
510+
511+ private void assertUnauthorizedBecauseConsentMissingPermissions () {
512+ ResponseEntity <JsonNode > actualResponse = context .getActualResponse ();
513+ JsonNode err = actualResponse .getBody ().get ("tppMessages" ).get (0 );
514+
515+ assertEquals (HttpStatus .UNAUTHORIZED , context .getActualResponse ().getStatusCode ());
516+ assertThat (err .get ("category" ).asText (), equalTo (TppMessageCategory .ERROR .toString ()));
517+ assertThat (err .get ("code" ).asText (), equalTo (CONSENT_INVALID .toString ()));
518+ assertThat (err .get ("text" ).asText (), containsString (
519+ "The consent was created by this TPP but is not valid for the addressed service/resource" ));
520+ }
521+
470522 private <T > ResponseEntity <T > handleCredentialRequest (Class <T > clazz , String url , String psuId ,
471523 String password ) {
472524 HashMap <String , String > headers = TestUtils .createSession ();
0 commit comments