From a96a3e49cd472aa902c22143358b87562603d47c Mon Sep 17 00:00:00 2001 From: "Kishore Reddy, Gujja (kg811t)" Date: Mon, 9 Jul 2018 13:41:00 -0400 Subject: Adding User Auth and permission aaf services Issue-ID: PORTAL-334 Change-Id: I2826f2a06f7d818d918ae5f45b500a8da78cec42 Signed-off-by: Kishore Reddy, Gujja (kg811t) --- .../service/UserApiServiceImplTest.java | 46 ++++++++++++++++++++-- .../util/EcompExternalAuthUtilsTest.java | 16 ++++---- 2 files changed, 50 insertions(+), 12 deletions(-) (limited to 'ecomp-sdk/epsdk-aaf/src/test/java/org/onap/portalsdk/external') diff --git a/ecomp-sdk/epsdk-aaf/src/test/java/org/onap/portalsdk/external/authorization/service/UserApiServiceImplTest.java b/ecomp-sdk/epsdk-aaf/src/test/java/org/onap/portalsdk/external/authorization/service/UserApiServiceImplTest.java index d93b03c1..9836c5f1 100644 --- a/ecomp-sdk/epsdk-aaf/src/test/java/org/onap/portalsdk/external/authorization/service/UserApiServiceImplTest.java +++ b/ecomp-sdk/epsdk-aaf/src/test/java/org/onap/portalsdk/external/authorization/service/UserApiServiceImplTest.java @@ -37,6 +37,7 @@ */ package org.onap.portalsdk.external.authorization.service; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; @@ -83,6 +84,7 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.RestTemplate; import com.fasterxml.jackson.databind.ObjectMapper; @@ -132,7 +134,7 @@ public class UserApiServiceImplTest { PowerMockito.mockStatic(SystemProperties.class); Mockito.when(EcompExternalAuthProperties.getProperty(EcompExternalAuthProperties.EXTERNAL_AUTH_NAMESPACE)) .thenReturn("com.test.app2"); - Mockito.when(EcompExternalAuthUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(new HttpHeaders()); + Mockito.when(EcompExternalAuthUtils.base64encodeKeyForAAFBasicAuth(Matchers.anyString(), Matchers.anyString())).thenReturn(new HttpHeaders()); Mockito.when(EcompExternalAuthProperties.getProperty(EcompExternalAuthProperties.EXTERNAL_AUTH_USER_DOMAIN)) .thenReturn("@test.com"); MockitoAnnotations.initMocks(this); @@ -286,6 +288,15 @@ public class UserApiServiceImplTest { @Test public void getRoleFunctionsTest() throws Exception { + JSONObject mockJsonObjectPerms = mockUserPerms(); + ResponseEntity response = new ResponseEntity<>(mockJsonObjectPerms.toString(), HttpStatus.OK); + Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET), + Matchers.>any(), Matchers.eq(String.class))).thenReturn(response); + List actual = UserApiServiceImpl.getRoleFunctions("test123"); + assertNotNull(actual); + } + + private JSONObject mockUserPerms() { JSONObject mockJsonObjectPerms = new JSONObject(); JSONObject mockJsonObjectPerm1 = new JSONObject(); JSONObject mockJsonObjectPerm2 = new JSONObject(); @@ -304,10 +315,39 @@ public class UserApiServiceImplTest { permsList.add(mockJsonObjectPerm1); permsList.add(mockJsonObjectPerm2); mockJsonObjectPerms.put("perm", permsList); + return mockJsonObjectPerms; + } + + @Test + public void checkUserExistsTest() throws Exception { + ResponseEntity response = new ResponseEntity<>(HttpStatus.OK); + Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST), + Matchers.>any(), Matchers.eq(String.class))).thenReturn(response); + ResponseEntity actual = UserApiServiceImpl.checkUserExists("test", "test"); + assertNotNull(actual); + } + + @Test(expected = HttpClientErrorException.class) + public void checkUserExistsExceptionTest() throws Exception { + Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST), + Matchers.>any(), Matchers.eq(String.class))).thenThrow(new HttpClientErrorException(HttpStatus.UNAUTHORIZED)); + UserApiServiceImpl.checkUserExists("test", "test"); + } + + @Test + public void getIfUserPermsExistsTest() throws Exception { + JSONObject mockJsonObjectPerms = mockUserPerms(); ResponseEntity response = new ResponseEntity<>(mockJsonObjectPerms.toString(), HttpStatus.OK); Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET), Matchers.>any(), Matchers.eq(String.class))).thenReturn(response); - List actual = UserApiServiceImpl.getRoleFunctions("test123"); - assertNotNull(actual); + UserApiServiceImpl.getIfUserPermsExists("test123@test.com"); + } + + @Test(expected = HttpClientErrorException.class) + public void getIfUserPermsExistsInvalidUserTest() throws Exception { + Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET), + Matchers.>any(), Matchers.eq(String.class))).thenThrow(new HttpClientErrorException(HttpStatus.UNAUTHORIZED)); + UserApiServiceImpl.getIfUserPermsExists("test1"); } + } diff --git a/ecomp-sdk/epsdk-aaf/src/test/java/org/onap/portalsdk/external/authorization/util/EcompExternalAuthUtilsTest.java b/ecomp-sdk/epsdk-aaf/src/test/java/org/onap/portalsdk/external/authorization/util/EcompExternalAuthUtilsTest.java index b35a1cda..4357b0a5 100644 --- a/ecomp-sdk/epsdk-aaf/src/test/java/org/onap/portalsdk/external/authorization/util/EcompExternalAuthUtilsTest.java +++ b/ecomp-sdk/epsdk-aaf/src/test/java/org/onap/portalsdk/external/authorization/util/EcompExternalAuthUtilsTest.java @@ -41,20 +41,21 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import javax.xml.bind.DatatypeConverter; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.onap.portalsdk.core.onboarding.util.CipherUtil; -import org.onap.portalsdk.core.util.SystemProperties; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import org.springframework.http.HttpHeaders; @RunWith(PowerMockRunner.class) -@PrepareForTest({ EcompExternalAuthProperties.class, CipherUtil.class }) +@PrepareForTest({ EcompExternalAuthProperties.class, CipherUtil.class, DatatypeConverter.class }) public class EcompExternalAuthUtilsTest { public static final String EXT_EMPTY_JSON_STRING = "{}"; @@ -73,6 +74,7 @@ public class EcompExternalAuthUtilsTest { public void setup() { PowerMockito.mockStatic(EcompExternalAuthProperties.class); PowerMockito.mockStatic(CipherUtil.class); + PowerMockito.mockStatic(DatatypeConverter.class); Mockito.when(EcompExternalAuthProperties.getProperty(EcompExternalAuthProperties.EXTERNAL_AUTH_USER_NAME)) .thenReturn("test_username"); Mockito.when(EcompExternalAuthProperties.getProperty(EcompExternalAuthProperties.EXTERNAL_AUTH_PASSWORD)) @@ -82,19 +84,15 @@ public class EcompExternalAuthUtilsTest { @Test public void base64encodeKeyForAAFBasicAuthTest() throws Exception { - Mockito.when( - CipherUtil.decryptPKC("test_password", SystemProperties.getProperty(SystemProperties.Decryption_Key))) - .thenReturn("test_decrypted_password"); - HttpHeaders actual = EcompExternalAuthUtils.base64encodeKeyForAAFBasicAuth(); + HttpHeaders actual = EcompExternalAuthUtils.base64encodeKeyForAAFBasicAuth("test", "test"); assertNotNull(actual); } @Test(expected = NullPointerException.class) public void base64encodeKeyForAAFBasicAuthDecryptPassExceptionTest() throws Exception { - Mockito.when( - CipherUtil.decryptPKC("test_password", SystemProperties.getProperty(SystemProperties.Decryption_Key))) + Mockito.when(DatatypeConverter.printBase64Binary("test:test".getBytes())) .thenThrow(new NullPointerException()); - EcompExternalAuthUtils.base64encodeKeyForAAFBasicAuth(); + EcompExternalAuthUtils.base64encodeKeyForAAFBasicAuth("test", "test"); } @Test -- cgit 1.2.3-korg