diff --git a/src/test/java/ch/ethz/seb/sebserver/gbl/client/ClientCredentialServiceImplTest.java b/src/test/java/ch/ethz/seb/sebserver/gbl/client/ClientCredentialServiceImplTest.java new file mode 100644 index 00000000..7ee5e07c --- /dev/null +++ b/src/test/java/ch/ethz/seb/sebserver/gbl/client/ClientCredentialServiceImplTest.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2021 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +package ch.ethz.seb.sebserver.gbl.client; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import org.junit.Test; +import org.springframework.core.env.Environment; + +import ch.ethz.seb.sebserver.gbl.util.Cryptor; +import ch.ethz.seb.sebserver.gbl.util.Result; + +public class ClientCredentialServiceImplTest { + + @Test + public void testGeneratedClientCredentials() { + final Environment envMock = mock(Environment.class); + when(envMock.getRequiredProperty(Cryptor.SEBSERVER_WEBSERVICE_INTERNAL_SECRET_KEY)) + .thenReturn("secret1"); + + final Cryptor cryptor = new Cryptor(envMock); + final String clientName = "simpleClientName"; + final ClientCredentialServiceImpl service = new ClientCredentialServiceImpl(envMock, cryptor); + + final Result clientCredentialsResult = service.generatedClientCredentials(); + assertTrue(clientCredentialsResult.hasValue()); + final ClientCredentials clientCredentials = clientCredentialsResult.get(); + assertNotNull(clientCredentials); + assertNotNull(clientCredentials.clientId); + assertNotNull(clientCredentials.secret); + assertNotNull(clientCredentials.accessToken); + +// String encrypted = +// cryptor.encrypt(clientName, "secret1").toString(); +// String decrypted = cryptor.decrypt(encrypted, "secret1").toString(); +// +// assertEquals(clientName, decrypted); +// +// final String clientSecret = "fbjreij39ru29305ruࣣàèLöäöäü65%(/%(ç87"; +// +// encrypted = +// cryptor.encrypt(clientSecret, "secret1").toString(); +// decrypted = cryptor.decrypt(encrypted, "secret1").toString(); +// +// assertEquals(clientSecret, decrypted); + } + +} diff --git a/src/test/java/ch/ethz/seb/sebserver/gbl/client/ClientCredentialServiceTest.java b/src/test/java/ch/ethz/seb/sebserver/gbl/util/CryptorTest.java similarity index 52% rename from src/test/java/ch/ethz/seb/sebserver/gbl/client/ClientCredentialServiceTest.java rename to src/test/java/ch/ethz/seb/sebserver/gbl/util/CryptorTest.java index ca95e53a..32a4dc45 100644 --- a/src/test/java/ch/ethz/seb/sebserver/gbl/client/ClientCredentialServiceTest.java +++ b/src/test/java/ch/ethz/seb/sebserver/gbl/util/CryptorTest.java @@ -6,7 +6,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -package ch.ethz.seb.sebserver.gbl.client; +package ch.ethz.seb.sebserver.gbl.util; import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.mock; @@ -15,44 +15,46 @@ import static org.mockito.Mockito.when; import org.junit.Test; import org.springframework.core.env.Environment; -import ch.ethz.seb.sebserver.gbl.util.Cryptor; - -public class ClientCredentialServiceTest { - -// @Test -// public void testEncryptSimpleSecret() { -// final Environment envMock = mock(Environment.class); -// when(envMock.getProperty(Cryptor.SEBSERVER_WEBSERVICE_INTERNAL_SECRET_KEY)) -// .thenReturn("internalSecret"); -// -// final Cryptor cryptor = new Cryptor(envMock); -// -// final CharSequence encrypt = cryptor.encrypt("testVDI"); -// assertEquals("", encrypt.toString()); -// } +public class CryptorTest { @Test - public void testEncryptDecryptClientCredentials() { - final Environment envMock = mock(Environment.class); - when(envMock.getRequiredProperty(Cryptor.SEBSERVER_WEBSERVICE_INTERNAL_SECRET_KEY)) - .thenReturn("secret1"); - - final Cryptor cryptor = new Cryptor(envMock); - + public void testEncryptDecrypt() { final String clientName = "simpleClientName"; - - final ClientCredentialServiceImpl service = new ClientCredentialServiceImpl(envMock, cryptor); String encrypted = - cryptor.encrypt(clientName, "secret1").toString(); - String decrypted = cryptor.decrypt(encrypted, "secret1").toString(); + Cryptor.encrypt(clientName, "secret1").toString(); + String decrypted = Cryptor.decrypt(encrypted, "secret1").toString(); assertEquals(clientName, decrypted); final String clientSecret = "fbjreij39ru29305ruࣣàèLöäöäü65%(/%(ç87"; encrypted = - cryptor.encrypt(clientSecret, "secret1").toString(); - decrypted = cryptor.decrypt(encrypted, "secret1").toString(); + Cryptor.encrypt(clientSecret, "secret1").toString(); + decrypted = Cryptor.decrypt(encrypted, "secret1").toString(); + + assertEquals(clientSecret, decrypted); + } + + @Test + public void testEncryptDecryptService() { + final Environment envMock = mock(Environment.class); + when(envMock.getRequiredProperty(Cryptor.SEBSERVER_WEBSERVICE_INTERNAL_SECRET_KEY)) + .thenReturn("secret1"); + + final Cryptor cryptor = new Cryptor(envMock); + final String clientName = "simpleClientName"; + + String encrypted = + cryptor.encrypt(clientName).toString(); + String decrypted = cryptor.decrypt(encrypted).toString(); + + assertEquals(clientName, decrypted); + + final String clientSecret = "fbjreij39ru29305ruࣣàèLöäöäü65%(/%(ç87"; + + encrypted = + cryptor.encrypt(clientSecret).toString(); + decrypted = cryptor.decrypt(encrypted).toString(); assertEquals(clientSecret, decrypted); }