Testing with Authorization Providers

Context

The application tests are best performed with adequate test users and the matching authorizations. Since this is not always possible, SADL provides some test helpers that simulate authorizations for the current user. These test helpers can only be used in test classes.

To be able to use them, perform the following steps:

Procedure

  1. Create an instance of the condition provider.
    DATA( lo_provider ) = NEW appl_cond_provider ( ).
  2. Use the SADL test helper to inject the desired user authorizations. This can only be done within test methods.
    cl_sadl_consumer_aunit_friend => inject_auth_object_values (
    			io_provider = lo_provider
    			it_auth_values = VALUE #( ( auth = 'S_ROLE' 
    					objct = 'S_EPM_BP' field = 'EPM_BP_ID' von = '1' bis = '10' ) ) ).
  3. Execute your test and verify the result. For example, verify that the service under test returns less result lines for restricted authorizations. The method used in this example simply checks that the generated condition matches expectations.
    lo_provider -> get_condition ( 
    			EXPORTING it_elements = VALUE #( 
    				( id = 'MY_BP_ID' column = 'BP_ID' table = 'SNWD_BPA' ) ) 
    			IMPORTING et_sadl_condition = DATA( lt_condition ) ). 
    cl_abap_unit_assert => assert_equals ( 
    	act = lo_provider -> is_authorized ( ) 
    	exp = abap_true ). 
    cl_abap_unit_assert => assert_equals ( 
    	act = lt_condition 
    	exp = VALUE if_sadl_query_engine_types => tt_complex_condition ( 
    			( type = 'simpleValue' value = '1' ) 
    			( type = 'simpleValue' value = '10' ) 
    			( type = 'between' attribute = 'MY_BP_ID' ) 
    			( type = 'notIsNull' attribute = 'MY_BP_ID' ) 
    			( type = 'and' ) ) ).
    
  4. Remove injected user authorizations.
    cl_sadl_consumer_aunit_friend => reset_auth_object_values ( ).