podd.server.authn.impl.PoddUserDetailsServiceImplUnitTest.java Source code

Java tutorial

Introduction

Here is the source code for podd.server.authn.impl.PoddUserDetailsServiceImplUnitTest.java

Source

/*
 * Copyright (c) 2009 - 2010. School of Information Technology and Electrical
 * Engineering, The University of Queensland.  This software is being developed
 * for the "Phenomics Ontoogy Driven Data Management Project (PODD)" project.
 * PODD is a National e-Research Architecture Taskforce (NeAT) project
 * co-funded by ANDS and ARCS.
 *
 * PODD 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.
 *
 * PODD 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 PODD.  If not, see <http://www.gnu.org/licenses/>.
 */

package podd.server.authn.impl;

import org.junit.Before;
import org.junit.Test;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.GrantedAuthorityImpl;
import org.springframework.security.userdetails.UserDetails;
import org.springframework.security.userdetails.UserDetailsService;
import podd.dataaccess.UserDAO;
import podd.dataaccess.exception.DataAccessException;
import podd.model.user.User;
import podd.repository.exceptions.RepositoryObjectHandlingException;
import podd.util.PoddApplicationContext;
import podd.util.PoddWebApplicationContext;
import podd.util.PoddWebappTestUtil;
import podd.util.fedora.FedoraRepositoryUtil;

import java.io.IOException;
import java.util.Collections;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static podd.model.user.UserStatus.BLOCKED;
import static podd.util.PoddWebappTestUtil.clearDB;
import static podd.util.PoddWebappTestUtil.getAdminProperty;
import static podd.util.PoddWebappTestUtil.populateUser;
import static podd.util.PoddWebappTestUtil.setupDefaultRepoRoles;
import static podd.util.common.vocabulary.PoddUserNamespace.PODD_USER;

/**
 * @author Yuan-Fang Li
 * @version $Id$
 */

public class PoddUserDetailsServiceImplUnitTest {
    private static final PoddApplicationContext PODD_CONTEXT = PoddApplicationContext.getPoddContext();
    private static final PoddWebApplicationContext PODD_WEB_CONTEXT = PoddWebApplicationContext.getPoddContext();

    private UserDetailsService dsService;
    private User user;
    private UserDAO dao;

    @Before
    public void setUp() throws RepositoryObjectHandlingException, DataAccessException, IOException {
        dao = PODD_CONTEXT.getHibernateUserDAO();

        clearDB("PoddEntity");
        clearDB("User");
        clearDB("RepositoryRole");
        setupDefaultRepoRoles();

        user = populateUser(getAdminProperty("username"), getAdminProperty("email"));
        FedoraRepositoryUtil fedoraUtil = PODD_CONTEXT.getFedoraUtil();
        fedoraUtil.ensureDeleted(PODD_USER.prefix + ":" + user.getUserName());
        UserDAO userDao = PODD_CONTEXT.getUserDAO();
        userDao.save(user);

        dsService = new PoddUserDetailsServiceImpl(PODD_CONTEXT.getHibernateUserDAO(),
                PODD_CONTEXT.getRepositoryRoleDAO());
    }

    @Test
    public void testExistingUser() {
        final UserDetails details = dsService.loadUserByUsername(user.getUserName());
        assertEquals(user.getUserName(), details.getUsername());
        assertEquals(user.getPasswordHash(), details.getPassword());
        final GrantedAuthority[] authorities = details.getAuthorities();
        GrantedAuthority authority = new GrantedAuthorityImpl(user.getRepositoryRole().getName());
        PoddWebappTestUtil.checkMembers(Collections.singleton(authority), authorities);
    }

    @Test
    public void testUserStatus() throws DataAccessException {
        user.setStatus(BLOCKED);
        dao.update(user);
        final UserDetails details = dsService.loadUserByUsername(user.getUserName());
        assertFalse(details.isEnabled());
    }

    @Test(expected = org.springframework.security.userdetails.UsernameNotFoundException.class)
    public void testNonExistingUser() {
        dsService.loadUserByUsername("chuck");
    }
}