Uploaded image for project: 'OpenMRS Core'
  1. OpenMRS Core
  2. TRUNK-3409

Problem with junit and authorization testing

    XMLWordPrintable

Details

    Description

      When running a unit test with context user having role other than "System Developer" I get following exception

      java.lang.RuntimeException: Database out of sync with code: Authenticated role does not exist

      its thrown from UserContext.getAuthenticatedRole(). I found that after running a test deleteAllData() method is called.This method first deletes all the data and then calls refreshAuthenticatedUser() method. This method, somewhere down the line needs to call getUser method which needs privilege "View Users".

      Now the way authentication logic works is if the user is System Developer it has all privileges and so no check is done. With any other user, there is a check. The first one being if the user is authenticated. but now, before we come here, all our data is already deleted, so the database does not have Authenticated role and so the error.

      I guess to fix this in the delete all method, refreshAuthenticatedUser() method must be shifted above database deletion code.

      But even if we do this,"View Users" privilege is available only to system developer. So basically we can not run tests with our own users unless they are "System Developers". A workaround can be to run assert statements with our users and then change user
      to admin just before we exit from the test.I tried this and there was no exception.

      Gliffy Diagrams

        Attachments

          Activity

            People

              sandeep Sandeep Joshi
              sandeep Sandeep Joshi
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: