11package it .pagopa .selfcare .dashboard .security ;
22
3+ import com .fasterxml .jackson .core .JsonProcessingException ;
4+ import com .fasterxml .jackson .databind .ObjectMapper ;
35import io .jsonwebtoken .Jwts ;
46import it .pagopa .selfcare .commons .base .security .ProductGrantedAuthority ;
57import it .pagopa .selfcare .commons .base .security .SelfCareGrantedAuthority ;
68import it .pagopa .selfcare .commons .base .security .SelfCareUser ;
79import it .pagopa .selfcare .commons .web .security .JwtService ;
10+ import it .pagopa .selfcare .dashboard .client .IamExternalRestClient ;
811import it .pagopa .selfcare .dashboard .client .UserInstitutionApiRestClient ;
912import it .pagopa .selfcare .dashboard .config .ExchangeTokenProperties ;
1013import it .pagopa .selfcare .dashboard .model .ExchangedToken ;
1619import it .pagopa .selfcare .dashboard .service .InstitutionService ;
1720import it .pagopa .selfcare .dashboard .service .UserGroupV2Service ;
1821import it .pagopa .selfcare .dashboard .service .UserV2Service ;
22+ import it .pagopa .selfcare .iam .generated .openapi .v1 .dto .ProductRoles ;
23+ import it .pagopa .selfcare .iam .generated .openapi .v1 .dto .UserClaims ;
1924import it .pagopa .selfcare .product .entity .Product ;
2025import it .pagopa .selfcare .product .service .ProductService ;
2126import it .pagopa .selfcare .user .generated .openapi .v1 .dto .OnboardedProductResponse ;
@@ -58,6 +63,9 @@ class ExchangeTokenServiceV2Test {
5863 @ Mock
5964 private UserInstitutionApiRestClient userInstitutionApiRestClient ;
6065
66+ @ Mock
67+ private IamExternalRestClient iamExternalRestClient ;
68+
6169 @ Mock
6270 private InstitutionService institutionService ;
6371
@@ -88,8 +96,11 @@ class ExchangeTokenServiceV2Test {
8896
8997 private ExchangeTokenServiceV2 exchangeTokenServiceV2 ;
9098
99+ private ObjectMapper objectMapper ;
100+
91101 @ BeforeEach
92102 void setUp () throws Exception {
103+ objectMapper = new ObjectMapper ();
93104 when (exchangeTokenProperties .getBillingAudience ()).thenReturn ("aud" );
94105 when (exchangeTokenProperties .getBillingUrl ()).thenReturn ("url" );
95106 when (exchangeTokenProperties .getDuration ()).thenReturn ("PT20H30M" );
@@ -105,7 +116,7 @@ void setUp() throws Exception {
105116
106117 exchangeTokenServiceV2 = new ExchangeTokenServiceV2 (jwtService ,
107118 institutionService ,userGroupV2Service ,exchangeTokenProperties ,userV2Service ,productService ,
108- userInstitutionApiRestClient ,institutionResourceMapper ,institutionMapper , productMapper );
119+ userInstitutionApiRestClient ,iamExternalRestClient , institutionResourceMapper ,institutionMapper , productMapper , objectMapper );
109120 }
110121
111122
@@ -186,7 +197,7 @@ void exchange_noProductGrantedAuthority_throwsIllegalArgumentException() {
186197 }
187198
188199 @ Test
189- void exchangeBackofficeAdmin_validInputs_returnsExchangedToken () {
200+ void exchangeBackofficeAdmin_validInputs_returnsExchangedToken () throws JsonProcessingException {
190201 String jti = "id" ;
191202 String sub = "subject" ;
192203 String iss = "PAGOPA" ;
@@ -196,6 +207,15 @@ void exchangeBackofficeAdmin_validInputs_returnsExchangedToken() {
196207 String productId = "productId" ;
197208 String credential = "password" ;
198209 String userId = UUID .randomUUID ().toString ();
210+ UserClaims userClaims = new UserClaims ();
211+ List <ProductRoles > productRoles = List .of (
212+ ProductRoles .builder ()
213+ .productId (productId )
214+ .roles (List .of ("SUPPORT" ))
215+ .build ()
216+ );
217+ userClaims .setProductRoles (productRoles );
218+ String userClaimsJson = objectMapper .writeValueAsString (userClaims );
199219
200220 it .pagopa .selfcare .dashboard .model .institution .Institution institution = mock (it .pagopa .selfcare .dashboard .model .institution .Institution .class );
201221 Product product = mock (Product .class );
@@ -211,23 +231,55 @@ void exchangeBackofficeAdmin_validInputs_returnsExchangedToken() {
211231
212232 when (institutionService .getInstitutionById (institutionId )).thenReturn (institution );
213233 when (productService .getProduct (productId )).thenReturn (product );
234+ when (iamExternalRestClient ._getIAMUser (userId , productId ))
235+ .thenReturn (ResponseEntity .ok (userClaimsJson ));
214236
215237 ExchangedToken result = exchangeTokenServiceV2 .exchangeBackofficeAdmin (institutionId , productId , Optional .empty ());
216238
217239 assertNotNull (result );
218240 assertNotNull (result .getIdentityToken ());
219241 }
220242
221- @ Test
222- void exchangeBackofficeAdmin_noAuth () {
223- // Arrange
224- String institutionId = "validInstitutionId" ;
225- String productId = "validProductId" ;
226- Optional <String > environment = Optional .of ("validEnvironment" );
227- SecurityContextHolder .getContext ().setAuthentication (null );
243+ @ Test
244+ void exchangeBackofficeAdmin_noAuth () {
245+ // Arrange
246+ String institutionId = "validInstitutionId" ;
247+ String productId = "validProductId" ;
248+ Optional <String > environment = Optional .of ("validEnvironment" );
249+ SecurityContextHolder .getContext ().setAuthentication (null );
250+
251+ Assertions .assertThrows (IllegalStateException .class , () -> exchangeTokenServiceV2 .exchangeBackofficeAdmin (institutionId , productId , environment ), "Authentication is required" );
252+ }
253+
254+ @ Test
255+ void exchangeBackofficeAdmin_noUserClaims_throwsIllegalArgumentException () {
256+ String institutionId = "institutionId" ;
257+ String productId = "productId" ;
258+ String credential = "password" ;
259+ String userId = UUID .randomUUID ().toString ();
228260
229- Assertions .assertThrows (IllegalStateException .class , () -> exchangeTokenServiceV2 .exchangeBackofficeAdmin (institutionId , productId , environment ), "Authentication is required" );
230- }
261+ it .pagopa .selfcare .dashboard .model .institution .Institution institution = mock (it .pagopa .selfcare .dashboard .model .institution .Institution .class );
262+
263+ TestSecurityContextHolder .setAuthentication (new TestingAuthenticationToken (SelfCareUser .builder (userId ).build (), credential ));
264+
265+ when (institutionService .getInstitutionById (institutionId )).thenReturn (institution );
266+ when (iamExternalRestClient ._getIAMUser (userId , productId ))
267+ .thenReturn (ResponseEntity .ok ("invalid json response" ));
268+
269+ IllegalArgumentException exception = assertThrows (
270+ IllegalArgumentException .class ,
271+ () -> exchangeTokenServiceV2 .exchangeBackofficeAdmin (
272+ institutionId ,
273+ productId ,
274+ Optional .empty ()
275+ )
276+ );
277+
278+ Assertions .assertEquals (
279+ String .format ("User Claims are required for product '%s' and institution '%s'" , productId , institutionId ),
280+ exception .getMessage ()
281+ );
282+ }
231283
232284 @ Test
233285 void testRetrieveBillingExchangedToken_AuthenticationMissing () {
0 commit comments