Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TRUNK-6235: auto deactivate users after XX days of inactivity #4650

Merged
merged 33 commits into from
Jun 26, 2024

Conversation

dicksonmulli
Copy link
Contributor

@dicksonmulli dicksonmulli commented May 27, 2024

Description of what I changed

  • Creating a task for auto retiring users after XX days of inactivity.
  • Setting number-of-days-to-retire-users property in global properties.
  • Adding last login time in user properties

Issue I worked on

see https://issues.openmrs.org/browse/TRUNK-6235

Checklist: I completed these to help reviewers :)

  • My IDE is configured to follow the code style of this project.

    No? Unsure? -> configure your IDE, format the code and add the changes with git add . && git commit --amend

  • I have added tests to cover my changes. (If you refactored
    existing code that was well tested you do not have to add tests)

    No? -> write tests and add them to this commit git add . && git commit --amend

  • I ran mvn clean package right before creating this pull request and
    added all formatting changes to my commit.

    No? -> execute above command

  • All new and existing tests passed.

    No? -> figure out why and add the fix to your commit. It is your responsibility to make sure your code works.

  • My pull request is based on the latest changes of the master branch.

    No? Unsure? -> execute command git pull --rebase upstream master

Isaiah and others added 5 commits May 27, 2024 18:45
updated-dependencies:
- dependency-name: org.codehaus.mojo:build-helper-maven-plugin
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
updated-dependencies:
- dependency-name: net.bytebuddy:byte-buddy
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
updated-dependencies:
- dependency-name: net.bytebuddy:byte-buddy-agent
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Dockerfile Outdated Show resolved Hide resolved
api/pom.xml Outdated Show resolved Hide resolved
pom.xml Outdated Show resolved Hide resolved
@dicksonmulli dicksonmulli changed the title [WIP] TRUNK-6235: auto deactivate users after XX days of inactivity TRUNK-6235: auto deactivate users after XX days of inactivity May 31, 2024
pom.xml Outdated Show resolved Hide resolved
@dkayiwa
Copy link
Member

dkayiwa commented Jun 2, 2024

Can we also look into the build failures?

@dkayiwa
Copy link
Member

dkayiwa commented Jun 3, 2024

@dicksonmulli can you look into the build failures.

@dkayiwa
Copy link
Member

dkayiwa commented Jun 12, 2024

Not really, but we need to allow this ability. For instance, when an admin wants to test this feature, it will come in handy to set say half a day or few hours.

We generally tend to avoid being too permissive. Especially when it has not been explicitly requested for. 😊

@dicksonmulli
Copy link
Contributor Author

We generally tend to avoid being too permissive. Especially when it has not been explicitly requested for.

I see, however, I genuinely think this ability will be beneficial not only to testing but also preventing errors when the user accidentally adds numbers with decimal places.

@dkayiwa
Copy link
Member

dkayiwa commented Jun 12, 2024

I see, however, I genuinely think this ability will be beneficial not only to testing but also preventing errors when the user accidentally adds numbers with decimal places.

For preventing errors, our error handling should take care of it.
Avoiding a builtin library and add our custom code that increases the maintenance burden, is not worth it, when all this is done in the name of supporting decimal places, which is not part of the user requirements. 😊

@dicksonmulli
Copy link
Contributor Author

Did you intentionally set this to package-private access?

Yes, I did this to enable us to call these methods from the unit tests.

[Note] It could be that you can't see my replies anymore. I am not sure what changed.

@dkayiwa
Copy link
Member

dkayiwa commented Jun 12, 2024

Yes, I did this to enable us to call these methods from the unit tests.

Ummmmm, it does not generally smell good, to make methods more visible than they should, just for the sake of testing them. We naturally test them via their public contract. Or in the worst case, at least access them via reflection in the tests. 😊

public class AutoRetireUsersTask extends AbstractTask {

private static final Logger log = LoggerFactory.getLogger(AutoRetireUsersTask.class);
static final String AUTO_RETIRE_REASON = "User retired due to inactivity";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the above be private?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was using it in tests but I can still use anyString() instead of passing the actual reason.

administrationService.saveGlobalProperty(new GlobalProperty(OpenmrsConstants.GP_NUMBER_OF_DAYS_TO_AUTO_RETIRE_USERS, ONE_DAY_PROPERTY_VALUE));

User adminUser = getDefaultUser();
adminUser.addRole(new Role(RoleConstants.SUPERUSER));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this test fail if i remove the above line of adding the super user role?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dicksonmulli did you see the above comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commenting should be working now.

Regarding the above concern, kindly refer to the other reply that I shared with you on slack.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better to share the reply here. These are public comments that are usually read by other community members who may join the discussion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally it should fail but the reason it won't fail when you remove this line is because the algorithm will check if the inactivity exceeds the number of days to retire, of which it doesn't, unless we add unnecessary stubbings of a last login time or created date - e.g. when(userService.getLastLoginTime(any())).thenReturn(String.valueOf(System.currentTimeMillis() - TWO_DAYS_IN_MILLISECONDS));
Note that these unnecessary stubbing would cause an error.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have not properly understood your response. Let me ask differently, if i removed the line which adds the super user role to the user, will this test fail?

administrationService.saveGlobalProperty(new GlobalProperty(OpenmrsConstants.GP_NUMBER_OF_DAYS_TO_AUTO_RETIRE_USERS, ONE_DAY_PROPERTY_VALUE));

User retiredUser = getDefaultUser();
retiredUser.setRetired(true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this test fail if i remove the above line of setRetired(true)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dicksonmulli did you see the above comment?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have a response to the above comment?

Copy link
Contributor Author

@dicksonmulli dicksonmulli Jun 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I shared the response here:
#4650 (comment)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If i do not retire the user in this test, and the test still passes, what then would i be testing for? 😊

@dicksonmulli
Copy link
Contributor Author

dicksonmulli commented Jun 14, 2024

Shouldn't the above be private?

I was using it in tests but I can still use anyString() instead of passing the actual reason.

@dicksonmulli
Copy link
Contributor Author

dicksonmulli commented Jun 14, 2024

Will this test fail if i remove the above line of setRetired(true)?

Ideally it should fail but the reason it won't fail when you remove this line is because the algorithm will check if the inactivity exceeds the number of days to retire, of which it doesn't, unless we add unnecessary stubbings of a last login time or created date - e.g. when(userService.getLastLoginTime(any())).thenReturn(String.valueOf(System.currentTimeMillis() - TWO_DAYS_IN_MILLISECONDS));
Note that these unnecessary stubbing would cause an error.

Will this test fail if i remove the above line of adding the super user role?

Same case as the above

@dkayiwa
Copy link
Member

dkayiwa commented Jun 17, 2024

I was using it in tests but I can still use anyString() instead of passing the actual reason.

Would this catch a bug which changes the expected string to a different one?

@dicksonmulli
Copy link
Contributor Author

Would this catch a bug which changes the expected string to a different one?

I have updated the tests with the actual string. This may have resulted to duplication of code but on the positive side, we achieve separation of concerns.

@dkayiwa
Copy link
Member

dkayiwa commented Jun 22, 2024

@dicksonmulli can you look at these merge conflicts?

@dkayiwa dkayiwa merged commit 9241104 into openmrs:master Jun 26, 2024
6 checks passed
Wandji69 pushed a commit to Wandji69/openmrs-core that referenced this pull request Jun 27, 2024
…s#4650)

* TRUNK-6235 - auto deactivate users

* --- (openmrs#4643)

updated-dependencies:
- dependency-name: org.codehaus.mojo:build-helper-maven-plugin
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Update to Tomcat 9

* --- (openmrs#4647)

updated-dependencies:
- dependency-name: net.bytebuddy:byte-buddy
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* --- (openmrs#4648)

updated-dependencies:
- dependency-name: net.bytebuddy:byte-buddy-agent
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Updating tests

* Changes from Pull request

* Reverting refactor

* Adding property in core global property

* Using matchers in place of empty string

* Adding licence

* Adding tests for auto-retire

* Changing an access modifier

* Improving documentation

* Updating tests

* escaping superusers while auto retiring users

* Code refactor

* Improving tests

* Improving tests

* Code refactor

* Using TimeUnit for time conversion

* Changing method modifiers to private

* Fixing a failing build

* making a static variable private

* Using exact retire reason in tests

* Fixing tests

* correcting the expected values

* Removing import of a constant

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: Isaiah <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Ian <[email protected]>
Co-authored-by: Isaiah Muli <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants