[[{“value”:”
Introduction
Authorization is an important part of every SAP application. In real-time business scenarios, different users have different permissions to create, update, and delete business data.
In the SAP RESTful Application Programming Model (RAP), authorization control helps developers decide whether a user is permitted to perform a particular operation on a business object. We can implement authorization checks in RAP using global authorization and instance authorization methods. In many implementations, the ABAP AUTHORITY-CHECK statement is used to verify whether the current user has the required authorization object and activity.
But implementing authorization logic is only one part of development. We also need to verify that the logic behaves correctly.
In this blog, I will demonstrate how I used ABAP Unit to test the AUTHORITY-CHECK in the GET_GLOBAL_AUTHORIZATIONS method in a RAP behavior implementation. I will also show how to execute the local handler method from a local test class and measure the code coverage using the ABAP Coverage tool in ADT.
The example uses a custom authorization object named Z_RAUTH_D1 with the authorization field ACTVT.
Business Scenario
Consider a Material Management application developed using RAP.
Different users should have different permissions:
|
Operation |
Activity |
Description |
|
Create |
01 |
Create material |
|
Update |
02 |
Change material |
|
Display |
03 |
Display material |
|
Delete |
06 |
Delete material |
For this scenario, I created the authorization object Z_RAUTH_D1 and used the ACTVT field to control the permitted operations.
The requirement is to check whether the current user has the required activity before allowing the RAP operation.
Implementing Global Authorization in RAP
First, global authorization needs to be enabled in the behavior definition.
Example: authorization master ( global )
Implementing AUTHORITY-CHECK
I implemented the authorization logic in the GET_GLOBAL_AUTHORIZATIONS method.
METHOD get_global_authorizations.
IF requested_authorizations-%create = if_abap_behv=>mk-on.
AUTHORITY-CHECK OBJECT ‘Z_RAUTH_D1’ ID ‘ACTVT’ FIELD ’01’.
result-%create = COND #( WHEN sy-subrc = 0 THEN if_abap_behv=>auth-allowed
ELSE if_abap_behv=>auth-unauthorized ).
ENDIF.
IF requested_authorizations-%update = if_abap_behv=>mk-on.
AUTHORITY-CHECK OBJECT ‘Z_RAUTH_D1’ ID ‘ACTVT’ FIELD ’02’.
result-%update = COND #( WHEN sy-subrc = 0 THEN if_abap_behv=>auth-allowed
ELSE if_abap_behv=>auth-unauthorized ).
ENDIF.
IF requested_authorizations-%delete = if_abap_behv=>mk-on.
AUTHORITY-CHECK OBJECT ‘Z_RAUTH_D1’ ID ‘ACTVT’ FIELD ’06’.
result-%delete = COND #( WHEN sy-subrc = 0 THEN if_abap_behv=>auth-allowed
ELSE if_abap_behv=>auth-unauthorized ).
ENDIF.
ENDMETHOD.
Understanding the implementation in GET_GLOBAL_AUTHORIZATIONS
- First, the method checks which authorization operation has been requested.
- It executes AUTHORITY-CHECK for the corresponding activity.
- If sy-subrc = 0, the authorization is successful.
- Otherwise, the operation is marked as unauthorized.
- The result is returned to the RAP framework.
Creating the local ABAP Unit test class:
First, declare the local test class as a friend of the behavior handler.
The test class definition:
CLASS ltc_te_material_V DEFINITION FINAL
FOR TESTING
DURATION SHORT
RISK LEVEL HARMLESS.
PRIVATE SECTION.
CLASS-DATA: environment TYPE REF TO if_cds_test_environment,
environment_db TYPE REF TO if_osql_test_environment,
lt_inventory_mock TYPE STANDARD TABLE OF zte_t_material WITH EMPTY KEY,
lt_inventory_DB_mock TYPE STANDARD TABLE OF zte_t_material.
DATA: obj TYPE REF TO lhc_zi_te_material_V, ” class under test – handler
lt_imp_auth_key TYPE STRUCTURE FOR GLOBAL AUTHORIZATION REQUEST zi_te_material_v,
lt_result_auth TYPE STRUCTURE FOR GLOBAL AUTHORIZATION RESULT zi_te_material_v,
lt_reported_auth TYPE RESPONSE FOR REPORTED EARLY zi_te_material_v.
CLASS-DATA:
go_env TYPE REF TO cl_abap_behv_test_environment.
CLASS-METHODS:
class_setup,
class_teardown.
METHODS:
setup,
teardown.
METHODS:
test_global_auth FOR TESTING.
ENDCLASS.
The FRIENDS addition allows the test class to access the local handler class.
Creating the test object
In the SETUP method, create an instance of the local handler.
METHOD setup.
CREATE OBJECT obj FOR TESTING.
environment->clear_doubles( ).
* go_env->clear_doubles( ).
ENDMETHOD.
Writing the ABAP Unit test method
The following is the central part of my test implementation.
METHOD test_global_auth.
* lt_inventory_mock = VALUE #( (
* materialid = 2
* orderid = 5
* materialname = ‘PEN’
* quantity = 5
* price = ‘20.00’
* ) ).
* environment->insert_test_data( lt_inventory_mock ).
* lt_inventory_DB_mock = VALUE #( (
* materialid = 2
* orderid = 5
* materialname = ‘PEN’
* quantity = 5
* price = ‘20.00’
** ) ).
“Test CREATE authorization
me->lt_imp_auth_key-%create = ’01’.
obj->get_global_authorizations(
EXPORTING
requested_authorizations = me->lt_imp_auth_key
CHANGING
result = lt_result_auth
reported = lt_reported_auth
).
cl_abap_unit_assert=>assert_not_initial( lt_result_auth ).
cl_abap_unit_assert=>assert_equals(
act = lt_result_auth-%create
exp = ’01’ ).
me->lt_imp_auth_key-%create = ’00’.
obj->get_global_authorizations(
EXPORTING
requested_authorizations = me->lt_imp_auth_key
CHANGING
result = lt_result_auth
reported = lt_reported_auth
).
cl_abap_unit_assert=>assert_not_initial( lt_result_auth ).
“Test UPDATE authorization
me->lt_imp_auth_key-%update = ’01’.
obj->get_global_authorizations(
EXPORTING
requested_authorizations = me->lt_imp_auth_key
CHANGING
result = lt_result_auth
reported = lt_reported_auth
).
cl_abap_unit_assert=>assert_not_initial( lt_result_auth ).
cl_abap_unit_assert=>assert_equals(
act = lt_result_auth-%update
exp = ’01’ ).
me->lt_imp_auth_key-%update = ’00’.
obj->get_global_authorizations(
EXPORTING
requested_authorizations = me->lt_imp_auth_key
CHANGING
result = lt_result_auth
reported = lt_reported_auth
).
cl_abap_unit_assert=>assert_not_initial( lt_result_auth ).
“Test DELETE authorization
me->lt_imp_auth_key-%delete = ’01’.
obj->get_global_authorizations(
EXPORTING
requested_authorizations = me->lt_imp_auth_key
CHANGING
result = lt_result_auth
reported = lt_reported_auth
).
cl_abap_unit_assert=>assert_not_initial( lt_result_auth ).
cl_abap_unit_assert=>assert_equals(
act = lt_result_auth-%delete
exp = ’01’ ).
me->lt_imp_auth_key-%delete = ’00’.
obj->get_global_authorizations(
EXPORTING
requested_authorizations = me->lt_imp_auth_key
CHANGING
result = lt_result_auth
reported = lt_reported_auth
).
cl_abap_unit_assert=>assert_not_initial( lt_result_auth ).
ENDMETHOD.
code coverage
Screenshot 1 – Branch coverage
2 – Statement coverage
Conclusion:
In this practical example, I demonstrated how to test a RAP global authorization method using ABAP Unit. We implemented authorization checks using the AUTHORITY-CHECK statement and created a local test class to execute the behavior handler method. The key learning is that implementing authorization logic is not enough. We should also verify the allowed and unauthorized scenarios through automated tests. ABAP Unit helps us identify untested branches and improves the reliability and maintainability of RAP applications.
I hope this example helps developers who are working with authorization control and ABAP Unit testing in SAP RAP.
“}]]
Read More Technology Blog Posts by Members articles
#abap