PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/jira-project/jira-components/jira-tests-parent/jira-tests-unit/src/test/java/com/atlassian/jira/onboarding/TestUserCheckerImpl.java

https://bitbucket.org/ahmed_bilal_360factors/jira7-core
Java | 151 lines | 129 code | 21 blank | 1 comment | 0 complexity | 1c48a2632def186d7ba645fadf277fc7 MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.jira.onboarding;
  2. import com.atlassian.jira.bc.security.login.LoginInfo;
  3. import com.atlassian.jira.bc.security.login.LoginService;
  4. import com.atlassian.jira.config.FeatureManager;
  5. import com.atlassian.jira.config.properties.PropertiesManager;
  6. import com.atlassian.jira.permission.GlobalPermissionKey;
  7. import com.atlassian.jira.security.GlobalPermissionManager;
  8. import com.atlassian.jira.user.ApplicationUser;
  9. import com.atlassian.jira.user.util.UserKeyStore;
  10. import com.atlassian.jira.web.HttpServletVariables;
  11. import com.opensymphony.module.propertyset.PropertySet;
  12. import org.junit.Before;
  13. import org.junit.Test;
  14. import org.junit.runner.RunWith;
  15. import org.mockito.Mock;
  16. import org.mockito.runners.MockitoJUnitRunner;
  17. import javax.servlet.http.Cookie;
  18. import javax.servlet.http.HttpServletRequest;
  19. import static org.hamcrest.MatcherAssert.assertThat;
  20. import static org.hamcrest.Matchers.is;
  21. import static org.mockito.Matchers.any;
  22. import static org.mockito.Matchers.anyString;
  23. import static org.mockito.Matchers.eq;
  24. import static org.mockito.Mockito.mock;
  25. import static org.mockito.Mockito.when;
  26. @RunWith(MockitoJUnitRunner.class)
  27. public class TestUserCheckerImpl {
  28. private UserChecker userChecker;
  29. @Mock
  30. private UserKeyStore userKeyStore;
  31. @Mock
  32. private LoginService loginService;
  33. @Mock
  34. private LoginInfo loginInfo;
  35. @Mock
  36. private FeatureManager featureManager;
  37. @Mock
  38. private GlobalPermissionManager permissionManager;
  39. @Mock
  40. private PropertiesManager propertiesManager;
  41. @Mock
  42. private HttpServletVariables httpServletVariables;
  43. @Mock
  44. private PropertySet propertySet;
  45. @Mock
  46. private ApplicationUser user;
  47. @Before
  48. public void init() {
  49. when(propertiesManager.getPropertySet()).thenReturn(propertySet);
  50. when(loginService.getLoginInfo(anyString())).thenReturn(loginInfo);
  51. when(userKeyStore.getIdForUserKey(anyString())).thenReturn(1000L);
  52. userChecker = new UserCheckerImpl(userKeyStore, loginService, featureManager, permissionManager, propertiesManager, httpServletVariables);
  53. }
  54. @Test
  55. public void shouldNotReturnTrueForServerSysAdmin() {
  56. assertThat("should return false when instance is Server", userChecker.isOnDemandSysAdmin(user), is(false));
  57. }
  58. @Test
  59. public void shouldReturnTrueAsFirstLoginWhenBelowIdThresholdAndLoginCountIsOne() {
  60. when(loginInfo.getLoginCount()).thenReturn(1L);
  61. assertThat("should return true when user id is below threshold AND loginCount is exactly one", userChecker.firstTimeLoggingIn(user), is(true));
  62. }
  63. @Test
  64. public void shouldReturnTrueAsFirstLoginWhenAboveIdThresholdAndLoginCountIsOne() {
  65. when(propertySet.exists(anyString())).thenReturn(true);
  66. when(propertySet.getString(anyString())).thenReturn("500");
  67. when(loginInfo.getLoginCount()).thenReturn(1L);
  68. assertThat("should return true when user id is above threshold AND loginCount is exactly one", userChecker.firstTimeLoggingIn(user), is(true));
  69. }
  70. @Test
  71. public void shouldReturnFalseAsFirstLoginWhenBelowIdThresholdAndLoginCountIsNull() {
  72. when(loginInfo.getLoginCount()).thenReturn(null);
  73. assertThat("should return false when user id is below threshold AND loginCount is null", userChecker.firstTimeLoggingIn(user), is(false));
  74. }
  75. @Test
  76. public void shouldReturnTrueAsFirstLoginWhenAboveIdThresholdAndLoginCountIsNull() {
  77. when(propertySet.exists(anyString())).thenReturn(true);
  78. when(propertySet.getString(anyString())).thenReturn("500");
  79. when(loginInfo.getLoginCount()).thenReturn(null);
  80. assertThat("should return true when user id is above threshold AND loginCount is null", userChecker.firstTimeLoggingIn(user), is(true));
  81. }
  82. @Test
  83. public void shouldReturnFalseAsFirstLoginWhenBelowIdThresholdAndLoginCountIsMoreThanOne() {
  84. when(loginInfo.getLoginCount()).thenReturn(42L);
  85. assertThat("should return false when user id is below threshold AND loginCount is more than one", userChecker.firstTimeLoggingIn(user), is(false));
  86. }
  87. @Test
  88. public void shouldReturnFalseAsFirstLoginWhenAboveIdThresholdAndLoginCountIsMoreThanOne() {
  89. when(propertySet.exists(anyString())).thenReturn(true);
  90. when(propertySet.getString(anyString())).thenReturn("500");
  91. when(loginInfo.getLoginCount()).thenReturn(42L);
  92. assertThat("should return false when user id is above threshold AND loginCount is more than one", userChecker.firstTimeLoggingIn(user), is(false));
  93. }
  94. @Test
  95. public void shouldReturnImpersonationFalseWhenCookieIsNotPresent() {
  96. // returns an empty Cookie array
  97. final HttpServletRequest mockHttpRequest = mock(HttpServletRequest.class);
  98. when(mockHttpRequest.getCookies()).thenReturn(new Cookie[]{});
  99. when(httpServletVariables.getHttpRequest()).thenReturn(mockHttpRequest);
  100. when(user.getUsername()).thenReturn("anything");
  101. assertThat("if impersonation cookie is not present, impersonation should be false", userChecker.isImpersonationActive(user), is(false));
  102. }
  103. @Test
  104. public void shouldReturnTrueWhenImpersonationCookieIsPresentAndItsValueMatchesUserCurrentlyLoggedIn() {
  105. Cookie impersonationCookie = new Cookie("um.user.impersonated.username", "username-currently-logged-in");
  106. final HttpServletRequest mockHttpRequest = mock(HttpServletRequest.class);
  107. when(mockHttpRequest.getCookies()).thenReturn(new Cookie[]{impersonationCookie});
  108. when(httpServletVariables.getHttpRequest()).thenReturn(mockHttpRequest);
  109. when(user.getUsername()).thenReturn("username-currently-logged-in");
  110. assertThat("if impersonation cookie is present AND current user matches the cookie value, impersonation should be true", userChecker.isImpersonationActive(user), is(true));
  111. }
  112. @Test
  113. public void shouldReturnTrueWhenImpersonationCookieIsPresentAndItsValueMatchesUserCurrentlyLoggedInWithSpaceInUsername() {
  114. Cookie impersonationCookie = new Cookie("um.user.impersonated.username", "username%20currently-logged-in");
  115. final HttpServletRequest mockHttpRequest = mock(HttpServletRequest.class);
  116. when(mockHttpRequest.getCookies()).thenReturn(new Cookie[]{impersonationCookie});
  117. when(httpServletVariables.getHttpRequest()).thenReturn(mockHttpRequest);
  118. when(user.getUsername()).thenReturn("username currently-logged-in");
  119. assertThat("if impersonation cookie is present AND current user with space in username matches the cookie value, impersonation should be true", userChecker.isImpersonationActive(user), is(true));
  120. }
  121. @Test
  122. public void shouldReturnFalseWhenImpersonationCookieIsPresentAndButItsValueDoesNotMatchUserCurrentlyLoggedIn() {
  123. Cookie impersonationCookie = new Cookie("um.user.impersonated.username", "username-currently-logged-in");
  124. final HttpServletRequest mockHttpRequest = mock(HttpServletRequest.class);
  125. when(mockHttpRequest.getCookies()).thenReturn(new Cookie[]{impersonationCookie});
  126. when(httpServletVariables.getHttpRequest()).thenReturn(mockHttpRequest);
  127. when(user.getUsername()).thenReturn("another-username-logged-in");
  128. assertThat("if impersonation cookie is present AND current user does not match the cookie value, impersonation should be false", userChecker.isImpersonationActive(user), is(false));
  129. }
  130. }