Skip to content

Commit

Permalink
Merge pull request #392 from aureamunoz/issue-286
Browse files Browse the repository at this point in the history
fixes #286
  • Loading branch information
aureamunoz authored Sep 6, 2023
2 parents 1c420fd + c046a5a commit 8d8b85c
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-panache-mock</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
Expand Down
13 changes: 12 additions & 1 deletion app/src/main/java/io/halkyon/services/CredentialService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.halkyon.services;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.transaction.Transactional;
import jakarta.ws.rs.NotFoundException;
Expand All @@ -10,6 +13,7 @@
import io.halkyon.model.CredentialParameter;
import io.halkyon.model.Service;
import io.halkyon.resource.requests.CredentialRequest;
import io.sundr.utils.Strings;

@ApplicationScoped
public class CredentialService {
Expand Down Expand Up @@ -57,7 +61,14 @@ public Credential initializeCredential(CredentialRequest request) {
credential.name = request.name;
credential.username = request.username;
credential.password = request.password;
credential.vaultKvPath = request.vaultKvPath;
if (request.vaultKvPath != null) {
Pattern stringPattern = Pattern.compile("^/*");
Matcher matcher = stringPattern.matcher(request.vaultKvPath);
if (Strings.isNotNullOrEmpty(request.vaultKvPath) && matcher.find()) {
request.vaultKvPath = matcher.replaceFirst("");
}
credential.vaultKvPath = request.vaultKvPath;
}
credential.service = Service.findById(request.serviceId);
credential.params.clear();
if (request.params != null) {
Expand Down
194 changes: 194 additions & 0 deletions app/src/test/java/io/halkyon/CredentialServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
package io.halkyon;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;

import java.util.Arrays;
import java.util.List;

import jakarta.inject.Inject;

import org.hibernate.Session;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import io.halkyon.model.Credential;
import io.halkyon.model.CredentialParameter;
import io.halkyon.model.Service;
import io.halkyon.resource.requests.CredentialRequest;
import io.halkyon.services.CredentialService;
import io.quarkus.panache.mock.PanacheMock;
import io.quarkus.test.InjectMock;
import io.quarkus.test.junit.QuarkusTest;

@QuarkusTest
public class CredentialServiceTest extends BaseTest {

@Inject
CredentialService credentialService;

@InjectMock
Session session;

@Test
public void shouldPersistCredential() {

Credential mock = mock(Credential.class);
when(mock.id).thenReturn(null);

credentialService.doSave(mock);

verify(mock, times(1)).persist();

}

@Test
public void shouldPersistEditedCredential() {

// Prepare
PanacheMock.mock(Credential.class);

Credential credential = new Credential();
credential.id = 1L;
credential.name = "TestCredential";
credential.service = mock(Service.class);
credential.username = "testuser";
credential.password = "testpassword";
credential.vaultKvPath = "some/path";
CredentialParameter cp = new CredentialParameter();
cp.paramName = "foo";
cp.paramValue = "bar";
credential.params.add(cp);

Credential edited = new Credential();
edited.id = 1L;
edited.name = "TestEditedCredential";
edited.service = mock(Service.class);
edited.username = "testEditeduser";
edited.password = "testEditedpassword";
edited.vaultKvPath = "another/path";
CredentialParameter editedParams = new CredentialParameter();
editedParams.paramName = "foz";
editedParams.paramValue = "baz";
edited.params = Arrays.asList(editedParams);

when(Credential.findById(1L)).thenReturn(credential);

credentialService.doSave(edited);

// PanacheMock.verify(Credential.class, times(1)).findById(1L);
Mockito.verify(session, Mockito.times(1)).persist(credential);

assertEquals("TestEditedCredential", credential.name);
assertEquals("testEditeduser", credential.username);
assertEquals("testEditedpassword", credential.password);
assertEquals(credential.service, edited.service);
assertEquals("another/path", credential.vaultKvPath);
assertEquals(1, credential.params.size());
assertEquals("foz", credential.params.get(0).paramName);
assertEquals("baz", credential.params.get(0).paramValue);

}

@Test
public void shouldCreateUsernameAndPasswordCredentialFromRequest() {

// Prepare
PanacheMock.mock(Service.class);
Service mock = mock(Service.class);
when(Service.findById(1L)).thenReturn(mock);

// Given a CredentialRequest
CredentialRequest request = new CredentialRequest();
request.name = "TestCredential";
request.username = "testuser";
request.password = "testpassword";
request.serviceId = 1L;

// When Call the initializeCredential method
Credential credential = credentialService.initializeCredential(request);

// Then Verify that the method returns the expected Credential
assertEquals("TestCredential", credential.name);
assertEquals("testuser", credential.username);
assertEquals("testpassword", credential.password);
assertEquals(mock, credential.service);
assertEquals(null, credential.vaultKvPath);
}

@Test
public void shouldCreateVaultKvPathCredentialFromRequest() {

// Prepare
PanacheMock.mock(Service.class);
Service mock = mock(Service.class);
when(Service.findById(1L)).thenReturn(mock);

// Given a CredentialRequest
CredentialRequest request = new CredentialRequest();
request.name = "TestCredential";
request.vaultKvPath = "some/path";
request.serviceId = 1L;

// When Call the initializeCredential method
Credential credential = credentialService.initializeCredential(request);

// Then Verify that the method returns the expected Credential
assertEquals("TestCredential", credential.name);
assertEquals(null, credential.username);
assertEquals(null, credential.password);
assertEquals(mock, credential.service);
assertEquals("some/path", credential.vaultKvPath);
}

@Test
public void shouldCreateVaultKvPathCredentialFromRequestWhenPathStartsWithSlash() {

// Prepare
PanacheMock.mock(Service.class);
Service mock = mock(Service.class);
when(Service.findById(1L)).thenReturn(mock);

// Given a CredentialRequest
CredentialRequest request = new CredentialRequest();
request.name = "TestCredential";
request.vaultKvPath = "/some/path";
request.serviceId = 1L;

// When Call the initializeCredential method
Credential credential = credentialService.initializeCredential(request);

// Then Verify that the method returns the expected Credential
assertEquals("TestCredential", credential.name);
assertEquals(null, credential.username);
assertEquals(null, credential.password);
assertEquals(mock, credential.service);
assertEquals("some/path", credential.vaultKvPath);
}

@Test
public void shouldCreateCredentialWithParams() {

// Prepare
PanacheMock.mock(Service.class);
Service mock = mock(Service.class);
when(Service.findById(1L)).thenReturn(mock);

// Given a CredentialRequest
CredentialRequest request = new CredentialRequest();
request.name = "TestCredential";
request.serviceId = 1L;
List<String> params = Arrays.asList("foo:bar");
request.params = params;

// When Call the initializeCredential method
Credential credential = credentialService.initializeCredential(request);

// Then Verify that the method returns the expected Credential
assertEquals("TestCredential", credential.name);
assertEquals(1, credential.params.size());
assertEquals("foo", credential.params.get(0).paramName);
assertEquals("bar", credential.params.get(0).paramValue);
}

}

0 comments on commit 8d8b85c

Please sign in to comment.