Skip to content

Commit

Permalink
OZ-683 - Added and updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wluyima committed Oct 29, 2024
1 parent 5a90a99 commit 4926f01
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.Person;
import org.openmrs.Provider;
import org.openmrs.User;
import org.openmrs.api.PersonService;
Expand Down Expand Up @@ -124,12 +123,11 @@ private void activateProviderAccount(User user) {
Context.addProxyPrivilege(PrivilegeConstants.GET_USERS);
Context.addProxyPrivilege(PrivilegeConstants.MANAGE_PROVIDERS);

Person person = personService.getPerson(user.getPerson().getId());
Collection<Provider> possibleProvider = ps.getProvidersByPerson(user.getPerson());
if (possibleProvider.size() == 0) {
Provider provider = new Provider();
provider.setIdentifier(user.getSystemId());
provider.setPerson(person);
provider.setPerson(personService.getPerson(user.getPerson().getId()));
provider.setCreator(userService.getUserByUsername("daemon"));
ps.saveProvider(provider);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
package org.openmrs.module.oauth2login.web.controller;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.openmrs.User;
import org.openmrs.api.context.Context;
import org.openmrs.module.oauth2login.OAuth2LoginConstants;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.security.oauth2.client.OAuth2RestOperations;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.web.RedirectStrategy;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static org.mockito.Mockito.*;

@RunWith(PowerMockRunner.class)
@PrepareForTest(Context.class)
public class CustomLogoutSuccessHandlerTest {
Expand All @@ -27,17 +31,18 @@ public CustomLogoutSuccessHandlerTest() {

@Test
public void onLogoutSuccess_redirectToLogoutURL() throws IOException, ServletException {
final String idToken = "myToken";
//setup
PowerMockito.mockStatic(Context.class);
User user = new User();
user.setUserProperty(OAuth2LoginConstants.USER_PROP_ID_TOKEN, idToken);
Mockito.when(Context.getAuthenticatedUser()).thenReturn(user);

CustomLogoutSuccessHandler customLogoutSuccessHandler = new CustomLogoutSuccessHandler();
RedirectStrategy redirectStrategy = mock(RedirectStrategy.class);
customLogoutSuccessHandler.setRedirectStrategy(redirectStrategy);
MockHttpServletRequest request = new MockHttpServletRequest();
HttpServletResponse response = mock(HttpServletResponse.class);
OAuth2RestOperations restTemplate = mock(OAuth2RestOperations.class);
customLogoutSuccessHandler.setRestTemplate(restTemplate);
when(restTemplate.getAccessToken()).thenReturn(new DefaultOAuth2AccessToken("myToken"));
//replay
customLogoutSuccessHandler.onLogoutSuccess(request, response, null);

Expand All @@ -46,7 +51,7 @@ public void onLogoutSuccess_redirectToLogoutURL() throws IOException, ServletExc
Context.logout();

verify(redirectStrategy, times(1)).sendRedirect(request, response,
"http://localhost:8081/auth/realms/demo/protocol/openid-connect/logout?id_token_hint=myToken");
"http://localhost:8081/auth/realms/demo/protocol/openid-connect/logout?id_token_hint=" + idToken);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright (C) Amiyul LLC - All Rights Reserved
*
* This source code is protected under international copyright law. All rights
* reserved and protected by the copyright holder.
*
* This file is confidential and only available to authorized individuals with the
* permission of the copyright holder. If you encounter this file and do not have
* permission, please contact the copyright holder and delete this file.
*/
package org.openmrs.module.oauth2login.web.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.openmrs.Person;
import org.openmrs.User;
import org.openmrs.api.AdministrationService;
import org.openmrs.api.PersonService;
import org.openmrs.api.ProviderService;
import org.openmrs.api.UserService;
import org.openmrs.api.context.Context;
import org.openmrs.module.oauth2login.OAuth2LoginConstants;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import org.springframework.security.oauth2.client.OAuth2RestOperations;
import org.springframework.security.oauth2.common.OAuth2AccessToken;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ Context.class })
public class OAuth2LoginControllerTest {

@Mock
private OAuth2RestOperations mockTemplate;

@Mock
private OAuth2AccessToken mockAccessToken;

@Mock
private ProviderService mockProviderService;

@Mock
private PersonService mockPersonService;

@Mock
private UserService mockUserService;

@Mock
private AdministrationService mockAdminService;

private OAuth2LoginController controller;

@Test
public void login_shouldStoreTheIdTokenAsAUserProperty() {
final String idToken = "myToken";
final String userInfUri = "http://test/userinfo";
PowerMockito.mockStatic(Context.class);
Mockito.when(Context.isAuthenticated()).thenReturn(true);
Map<String, Object> additionalInfo = new HashMap();
additionalInfo.put("id_token", idToken);
Mockito.when(mockAccessToken.getAdditionalInformation()).thenReturn(additionalInfo);
Mockito.when(mockTemplate.getAccessToken()).thenReturn(mockAccessToken);
User user = new User();
user.setPerson(new Person());
Mockito.when(Context.getAuthenticatedUser()).thenReturn(user);
controller = new OAuth2LoginController();
Properties oauth2Props = new Properties();
Whitebox.setInternalState(controller, "oauth2Props", oauth2Props);
Whitebox.setInternalState(controller, "userInfoUri", userInfUri);
Whitebox.setInternalState(controller, "restTemplate", mockTemplate);
Whitebox.setInternalState(controller, "ps", mockProviderService);
Whitebox.setInternalState(controller, "personService", mockPersonService);
Whitebox.setInternalState(controller, "userService", mockUserService);
Mockito.when(Context.getAdministrationService()).thenReturn(mockAdminService);

controller.login();

Assert.assertEquals(idToken, user.getUserProperty(OAuth2LoginConstants.USER_PROP_ID_TOKEN));
}

}

0 comments on commit 4926f01

Please sign in to comment.