Skip to content

Commit

Permalink
WORK-360 added more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SitoCH committed Jun 1, 2016
1 parent c90d990 commit 5922eb7
Show file tree
Hide file tree
Showing 3 changed files with 307 additions and 105 deletions.
207 changes: 102 additions & 105 deletions src/test/java/io/lavagna/service/LdapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import io.lavagna.model.Key;
import io.lavagna.service.ConfigurationRepository;
import io.lavagna.service.Ldap;
import io.lavagna.service.LdapConnection;
import io.lavagna.service.LdapConnection.InitialDirContextCloseable;

import java.util.EnumMap;
Expand All @@ -46,106 +43,106 @@
@RunWith(MockitoJUnitRunner.class)
public class LdapTest {

private static final String MANAGER_PWD = "secret";
private static final String MANAGER_DN = "uid=admin,ou=system";
private static final String PROVIDER_URL = "ldap://localhost:10389";

@Mock
private ConfigurationRepository configurationRepository;
@Mock
private LdapConnection ldapConnection;

private Ldap ldap;

@Before
public void prepare() {
ldap = new Ldap(configurationRepository, ldapConnection);

Map<Key, String> conf = new EnumMap<>(Key.class);
conf.put(Key.LDAP_SERVER_URL, PROVIDER_URL);
conf.put(Key.LDAP_MANAGER_DN, MANAGER_DN);
conf.put(Key.LDAP_MANAGER_PASSWORD, MANAGER_PWD);
conf.put(Key.LDAP_USER_SEARCH_BASE, "ou=system");
conf.put(Key.LDAP_USER_SEARCH_FILTER, "uid={0}");

when(
configurationRepository.findConfigurationFor(EnumSet.of(Key.LDAP_SERVER_URL, Key.LDAP_MANAGER_DN,
Key.LDAP_MANAGER_PASSWORD, Key.LDAP_USER_SEARCH_BASE, Key.LDAP_USER_SEARCH_FILTER)))
.thenReturn(conf);
}

@Test
public void failOnFirstOpen() throws NamingException {
Throwable throwable = new NamingException("unit test :D");
when(ldapConnection.context(PROVIDER_URL, MANAGER_DN, MANAGER_PWD)).thenThrow(throwable);

Assert.assertFalse(ldap.authenticate("user", "password"));
verify(ldapConnection).context(PROVIDER_URL, MANAGER_DN, MANAGER_PWD);
}

@SuppressWarnings("unchecked")
@Test
public void nothingFound() throws NamingException {

InitialDirContextCloseable ctx = mock(InitialDirContextCloseable.class);
when(ldapConnection.context(PROVIDER_URL, MANAGER_DN, MANAGER_PWD)).thenReturn(ctx);

NamingEnumeration<SearchResult> searchRes = mock(NamingEnumeration.class);
when(ctx.search(eq("ou=system"), eq("uid=user"), any(SearchControls.class))).thenReturn(searchRes);
when(searchRes.hasMore()).thenReturn(false);

Assert.assertFalse(ldap.authenticate("user", "password"));
// first call
verify(ldapConnection).context(PROVIDER_URL, MANAGER_DN, MANAGER_PWD);
}

@SuppressWarnings("unchecked")
@Test
public void wrongPassword() throws NamingException {

InitialDirContextCloseable ctx = mock(InitialDirContextCloseable.class);
when(ldapConnection.context(PROVIDER_URL, MANAGER_DN, MANAGER_PWD)).thenReturn(ctx);

NamingEnumeration<SearchResult> searchRes = mock(NamingEnumeration.class);
when(ctx.search(eq("ou=system"), eq("uid=user\\5c\\2a\\28\\29\\00"), any(SearchControls.class))).thenReturn(
searchRes);
when(searchRes.hasMore()).thenReturn(true, false);
SearchResult sr = mock(SearchResult.class);
when(sr.getNameInNamespace()).thenReturn("HOLOYOLO");
when(searchRes.next()).thenReturn(sr);

Throwable throwable = new NamingException("unit test :D");
when(ldapConnection.context(PROVIDER_URL, "HOLOYOLO", "password")).thenThrow(throwable);

// we check the escape too..
Assert.assertFalse(ldap.authenticate("user\\*()\u0000", "password"));
verify(ctx).search(eq("ou=system"), eq("uid=user\\5c\\2a\\28\\29\\00"), any(SearchControls.class));
verify(sr).getNameInNamespace();
// first call
verify(ldapConnection).context(PROVIDER_URL, MANAGER_DN, MANAGER_PWD);
// second call with the user dn
verify(ldapConnection).context(PROVIDER_URL, "HOLOYOLO", "password");
}

@SuppressWarnings("unchecked")
@Test
public void authenticate() throws NamingException {

InitialDirContextCloseable ctx = mock(InitialDirContextCloseable.class);
when(ldapConnection.context(PROVIDER_URL, MANAGER_DN, MANAGER_PWD)).thenReturn(ctx);

NamingEnumeration<SearchResult> searchRes = mock(NamingEnumeration.class);
when(ctx.search(eq("ou=system"), eq("uid=user"), any(SearchControls.class))).thenReturn(searchRes);
when(searchRes.hasMore()).thenReturn(true, false);
SearchResult sr = mock(SearchResult.class);
when(sr.getNameInNamespace()).thenReturn("HOLOYOLO");
when(searchRes.next()).thenReturn(sr);

Assert.assertTrue(ldap.authenticate("user", "password"));
verify(sr).getNameInNamespace();
// first call
verify(ldapConnection).context(PROVIDER_URL, MANAGER_DN, MANAGER_PWD);
// second call with the user dn
verify(ldapConnection).context(PROVIDER_URL, "HOLOYOLO", "password");
}
private static final String MANAGER_PWD = "secret";
private static final String MANAGER_DN = "uid=admin,ou=system";
private static final String PROVIDER_URL = "ldap://localhost:10389";

@Mock
private ConfigurationRepository configurationRepository;
@Mock
private LdapConnection ldapConnection;

private Ldap ldap;

@Before
public void prepare() {
ldap = new Ldap(configurationRepository, ldapConnection);

Map<Key, String> conf = new EnumMap<>(Key.class);
conf.put(Key.LDAP_SERVER_URL, PROVIDER_URL);
conf.put(Key.LDAP_MANAGER_DN, MANAGER_DN);
conf.put(Key.LDAP_MANAGER_PASSWORD, MANAGER_PWD);
conf.put(Key.LDAP_USER_SEARCH_BASE, "ou=system");
conf.put(Key.LDAP_USER_SEARCH_FILTER, "uid={0}");

when(
configurationRepository.findConfigurationFor(EnumSet.of(Key.LDAP_SERVER_URL, Key.LDAP_MANAGER_DN,
Key.LDAP_MANAGER_PASSWORD, Key.LDAP_USER_SEARCH_BASE, Key.LDAP_USER_SEARCH_FILTER)))
.thenReturn(conf);
}

@Test
public void failOnFirstOpen() throws NamingException {
Throwable throwable = new NamingException("unit test :D");
when(ldapConnection.context(PROVIDER_URL, MANAGER_DN, MANAGER_PWD)).thenThrow(throwable);

Assert.assertFalse(ldap.authenticate("user", "password"));
verify(ldapConnection).context(PROVIDER_URL, MANAGER_DN, MANAGER_PWD);
}

@SuppressWarnings("unchecked")
@Test
public void nothingFound() throws NamingException {

InitialDirContextCloseable ctx = mock(InitialDirContextCloseable.class);
when(ldapConnection.context(PROVIDER_URL, MANAGER_DN, MANAGER_PWD)).thenReturn(ctx);

NamingEnumeration<SearchResult> searchRes = mock(NamingEnumeration.class);
when(ctx.search(eq("ou=system"), eq("uid=user"), any(SearchControls.class))).thenReturn(searchRes);
when(searchRes.hasMore()).thenReturn(false);

Assert.assertFalse(ldap.authenticate("user", "password"));
// first call
verify(ldapConnection).context(PROVIDER_URL, MANAGER_DN, MANAGER_PWD);
}

@SuppressWarnings("unchecked")
@Test
public void wrongPassword() throws NamingException {

InitialDirContextCloseable ctx = mock(InitialDirContextCloseable.class);
when(ldapConnection.context(PROVIDER_URL, MANAGER_DN, MANAGER_PWD)).thenReturn(ctx);

NamingEnumeration<SearchResult> searchRes = mock(NamingEnumeration.class);
when(ctx.search(eq("ou=system"), eq("uid=user\\5c\\2a\\28\\29\\00"), any(SearchControls.class))).thenReturn(
searchRes);
when(searchRes.hasMore()).thenReturn(true, false);
SearchResult sr = mock(SearchResult.class);
when(sr.getNameInNamespace()).thenReturn("HOLOYOLO");
when(searchRes.next()).thenReturn(sr);

Throwable throwable = new NamingException("unit test :D");
when(ldapConnection.context(PROVIDER_URL, "HOLOYOLO", "password")).thenThrow(throwable);

// we check the escape too..
Assert.assertFalse(ldap.authenticate("user\\*()\u0000", "password"));
verify(ctx).search(eq("ou=system"), eq("uid=user\\5c\\2a\\28\\29\\00"), any(SearchControls.class));
verify(sr).getNameInNamespace();
// first call
verify(ldapConnection).context(PROVIDER_URL, MANAGER_DN, MANAGER_PWD);
// second call with the user dn
verify(ldapConnection).context(PROVIDER_URL, "HOLOYOLO", "password");
}

@SuppressWarnings("unchecked")
@Test
public void authenticate() throws NamingException {

InitialDirContextCloseable ctx = mock(InitialDirContextCloseable.class);
when(ldapConnection.context(PROVIDER_URL, MANAGER_DN, MANAGER_PWD)).thenReturn(ctx);

NamingEnumeration<SearchResult> searchRes = mock(NamingEnumeration.class);
when(ctx.search(eq("ou=system"), eq("uid=user"), any(SearchControls.class))).thenReturn(searchRes);
when(searchRes.hasMore()).thenReturn(true, false);
SearchResult sr = mock(SearchResult.class);
when(sr.getNameInNamespace()).thenReturn("HOLOYOLO");
when(searchRes.next()).thenReturn(sr);

Assert.assertTrue(ldap.authenticate("user", "password"));
verify(sr).getNameInNamespace();
// first call
verify(ldapConnection).context(PROVIDER_URL, MANAGER_DN, MANAGER_PWD);
// second call with the user dn
verify(ldapConnection).context(PROVIDER_URL, "HOLOYOLO", "password");
}
}
178 changes: 178 additions & 0 deletions src/test/java/io/lavagna/service/MilestoneExportServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/**
* This file is part of lavagna.
*
* lavagna is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* lavagna is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with lavagna. If not, see <http://www.gnu.org/licenses/>.
*/
package io.lavagna.service;

import io.lavagna.config.PersistenceAndServiceConfig;
import io.lavagna.model.Board;
import io.lavagna.model.BoardColumn;
import io.lavagna.model.BoardColumnDefinition;
import io.lavagna.model.Card;
import io.lavagna.model.CardLabel;
import io.lavagna.model.CardLabelValue;
import io.lavagna.model.LabelListValue;
import io.lavagna.model.LabelListValueWithMetadata;
import io.lavagna.model.Permission;
import io.lavagna.model.Project;
import io.lavagna.model.Role;
import io.lavagna.model.User;
import io.lavagna.model.UserWithPermission;
import io.lavagna.service.config.TestServiceConfig;

import java.io.IOException;
import java.util.Collections;
import java.util.Date;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { TestServiceConfig.class, PersistenceAndServiceConfig.class })
@Transactional
public class MilestoneExportServiceTest {

@Autowired
private MilestoneExportService milestoneExportService;

@Autowired
private BoardRepository boardRepository;

@Autowired
private UserRepository userRepository;

@Autowired
private BoardColumnRepository boardColumnRepository;

@Autowired
private CardService cardService;

@Autowired
private PermissionService permissionService;

@Autowired
private ProjectService projectService;

@Autowired
private CardLabelRepository cardLabelRepository;

Project project;
UserWithPermission user;

@Before
public void prepare() {
Helper.createUser(userRepository, "test", "test-user");
User u = userRepository.findUserByName("test", "test-user");
Role r = new Role("TEST");
permissionService.createRole(r);
permissionService.updatePermissionsToRole(r, EnumSet.of(Permission.READ));
permissionService.assignRolesToUsers(Collections.singletonMap(r, Collections.singleton(u.getId())));

user = new UserWithPermission(u, permissionService.findBasePermissionByUserId(u.getId()),
Collections.<String, Set<Permission>>emptyMap(), Collections.<Integer, Set<Permission>>emptyMap());

project = projectService.create("test", "TEST", "desc");

CardLabel lnull = cardLabelRepository
.addLabel(project.getId(), false, CardLabel.LabelType.NULL, CardLabel.LabelDomain.USER, "null", 0);

CardLabel lstring = cardLabelRepository
.addLabel(project.getId(), false, CardLabel.LabelType.STRING, CardLabel.LabelDomain.USER, "string", 0);

CardLabel ltimestamp = cardLabelRepository
.addLabel(project.getId(), false, CardLabel.LabelType.TIMESTAMP, CardLabel.LabelDomain.USER, "date", 0);

CardLabel luser = cardLabelRepository
.addLabel(project.getId(), false, CardLabel.LabelType.USER, CardLabel.LabelDomain.USER, "user", 0);

CardLabel lcard = cardLabelRepository
.addLabel(project.getId(), false, CardLabel.LabelType.CARD, CardLabel.LabelDomain.USER, "card", 0);

CardLabel lint = cardLabelRepository
.addLabel(project.getId(), false, CardLabel.LabelType.INT, CardLabel.LabelDomain.USER, "int", 0);

CardLabel llist = cardLabelRepository
.addLabel(project.getId(), false, CardLabel.LabelType.LIST, CardLabel.LabelDomain.USER, "list", 0);
LabelListValue llistVal = cardLabelRepository.addLabelListValue(llist.getId(), "abcd");

// Init new card
Board board = boardRepository
.createNewBoard("test-label", "LABEL", "label", projectService.findByShortName("TEST").getId());
List<BoardColumnDefinition> definitions = projectService.findColumnDefinitionsByProjectId(project.getId());
BoardColumn column = boardColumnRepository.addColumnToBoard("label-column", definitions.get(0).getId(),
BoardColumn.BoardColumnLocation.BOARD, board.getId());
Card card = cardService.createCard("card", column.getId(), new Date(), user);

// Init milestone
CardLabel l = cardLabelRepository.findLabelByName(project.getId(), "MILESTONE", CardLabel.LabelDomain.SYSTEM);
LabelListValue mlv = cardLabelRepository.addLabelListValue(l.getId(), "1.0");

// Add labels
cardLabelRepository.addLabelValueToCard(l, card.getId(), new CardLabelValue.LabelValue(null, null, null, null,
null, mlv.getId()));

cardLabelRepository.addLabelValueToCard(lnull, card.getId(), new CardLabelValue.LabelValue(null, null, null,
null, null, null));
cardLabelRepository.addLabelValueToCard(lstring, card.getId(), new CardLabelValue.LabelValue("ABC"));
cardLabelRepository.addLabelValueToCard(ltimestamp, card.getId(), new CardLabelValue.LabelValue(new Date()));
cardLabelRepository.addLabelValueToCard(luser, card.getId(), new CardLabelValue.LabelValue(null, null, null,
null, u.getId(), null));
cardLabelRepository.addLabelValueToCard(lcard, card.getId(), new CardLabelValue.LabelValue(null, null, null,
card.getId(), null, null));
cardLabelRepository.addLabelValueToCard(lint, card.getId(), new CardLabelValue.LabelValue(null, null, 999,
null, null, null));
cardLabelRepository.addLabelValueToCard(llist, card.getId(), new CardLabelValue.LabelValue(null, null, null,
null, null, llistVal.getId()));
}

@Test
public void testGetWrongMilestone() {
LabelListValueWithMetadata m = milestoneExportService.getMilestone(project.getId(), "AAAA");
Assert.assertNull(m);
}

@Test
public void testGetMilestone() {
LabelListValueWithMetadata m = milestoneExportService.getMilestone(project.getId(), "1.0");
Assert.assertNotNull(m);
}

@Test(expected = IllegalArgumentException.class)
public void testExportMilestoneToExcelWrongValue() throws IOException {

milestoneExportService.exportMilestoneToExcel(project.getShortName(), "AAA", user);

}

@Test
public void testExportMilestoneToExcel() throws IOException {

HSSFWorkbook w = milestoneExportService.exportMilestoneToExcel(project.getShortName(), "1.0", user);

Assert.assertNotNull(w);
Assert.assertEquals(1, w.getSheet("1.0").getLastRowNum()); // 0 based -> 1 means 2 rows (header + 1 card)

}
}

0 comments on commit 5922eb7

Please sign in to comment.