<commit_before>package edu.northwestern.bioinformatics.studycalendar.web;

import java.util.Date;

import edu.northwestern.bioinformatics.studycalendar.domain.LoginAudit;
import edu.northwestern.bioinformatics.studycalendar.domain.Site;
import edu.northwestern.bioinformatics.studycalendar.dao.LoginAuditDao;
import edu.northwestern.bioinformatics.studycalendar.testing.StudyCalendarTestCase;
import gov.nih.nci.security.AuthenticationManager;
import gov.nih.nci.security.exceptions.CSException;
import org.easymock.classextension.EasyMock;
import static org.easymock.classextension.EasyMock.expect;

/**
 * @author <NAME>
 * @author <NAME>
 */
public class LoginCommandTest extends StudyCalendarTestCase {
    private static final String USERNAME = "alice";
    private static final String PASSWORD = "<PASSWORD>";
    private static final String IPADDRESS = "172.16.31.10";

    private LoginCommand command;

    private AuthenticationManager authenticationManager;
    private LoginAuditDao auditDao;

    protected void setUp() throws Exception {
        super.setUp();
        authenticationManager = registerMockFor(AuthenticationManager.class);
        auditDao = registerMockFor(LoginAuditDao.class, LoginAuditDao.class.getMethod("save", LoginAudit.class));
        command = new LoginCommand(authenticationManager, auditDao);
        command.setUsername(USERNAME);
        command.setPassword(PASSWORD);
    }

    public void testLoginSuccessfulWhenSuccessful() throws Exception {
        expect(authenticationManager.login(USERNAME, PASSWORD)).andReturn(true);
        auditDao.save((LoginAudit) EasyMock.notNull());
        replayMocks();
        assertTrue(command.login(IPADDRESS));
        verifyMocks();
    }

    public void testLoginFailsWhenFails() throws Exception {
        expect(authenticationManager.login(USERNAME, PASSWORD)).andReturn(false);
    	auditDao.save((LoginAudit) EasyMock.notNull());
        replayMocks();
        assertFalse(command.login(IPADDRESS));
        verifyMocks();
    }
    
    public void testLoginFailsWhenException() throws Exception {
        expect(authenticationManager.login(USERNAME, PASSWORD)).andThrow(new CSException());
        auditDao.save((LoginAudit) EasyMock.notNull());
        replayMocks();
        assertFalse(command.login(IPADDRESS));
        verifyMocks();
    }
    
    /**
     * @throws Exception
     */
    public void testCreateLoginAudit() throws Exception {
    	LoginAudit loginAudit = new LoginAudit();
    	loginAudit.setIpAddress("172.16.31.10");
    	loginAudit.setLoginStatus("Success");
    	loginAudit.setTime(new Date());
    	loginAudit.setUserName("study_admin");
		auditDao.save(loginAudit);
		replayMocks();
		
		command.saveAudit(loginAudit);
		verifyMocks();
    }
}<commit_msg>changed login audit date to timestamp<commit_after>package edu.northwestern.bioinformatics.studycalendar.web;

import java.util.Date;
import java.sql.Timestamp;

import edu.northwestern.bioinformatics.studycalendar.domain.LoginAudit;
import edu.northwestern.bioinformatics.studycalendar.domain.Site;
import edu.northwestern.bioinformatics.studycalendar.dao.LoginAuditDao;
import edu.northwestern.bioinformatics.studycalendar.testing.StudyCalendarTestCase;
import gov.nih.nci.security.AuthenticationManager;
import gov.nih.nci.security.exceptions.CSException;
import org.easymock.classextension.EasyMock;
import static org.easymock.classextension.EasyMock.expect;

/**
 * @author <NAME>
 * @author <NAME>
 */
public class LoginCommandTest extends StudyCalendarTestCase {
    private static final String USERNAME = "alice";
    private static final String PASSWORD = "<PASSWORD>";
    private static final String IPADDRESS = "172.16.31.10";

    private LoginCommand command;

    private AuthenticationManager authenticationManager;
    private LoginAuditDao auditDao;

    protected void setUp() throws Exception {
        super.setUp();
        authenticationManager = registerMockFor(AuthenticationManager.class);
        auditDao = registerMockFor(LoginAuditDao.class, LoginAuditDao.class.getMethod("save", LoginAudit.class));
        command = new LoginCommand(authenticationManager, auditDao);
        command.setUsername(USERNAME);
        command.setPassword(PASSWORD);
    }

    public void testLoginSuccessfulWhenSuccessful() throws Exception {
        expect(authenticationManager.login(USERNAME, PASSWORD)).andReturn(true);
        auditDao.save((LoginAudit) EasyMock.notNull());
        replayMocks();
        assertTrue(command.login(IPADDRESS));
        verifyMocks();
    }

    public void testLoginFailsWhenFails() throws Exception {
        expect(authenticationManager.login(USERNAME, PASSWORD)).andReturn(false);
    	auditDao.save((LoginAudit) EasyMock.notNull());
        replayMocks();
        assertFalse(command.login(IPADDRESS));
        verifyMocks();
    }
    
    public void testLoginFailsWhenException() throws Exception {
        expect(authenticationManager.login(USERNAME, PASSWORD)).andThrow(new CSException());
        auditDao.save((LoginAudit) EasyMock.notNull());
        replayMocks();
        assertFalse(command.login(IPADDRESS));
        verifyMocks();
    }
    
    /**
     * @throws Exception
     */
    public void testCreateLoginAudit() throws Exception {
    	LoginAudit loginAudit = new LoginAudit();
    	loginAudit.setIpAddress("172.16.31.10");
    	loginAudit.setLoginStatus("Success");
    	loginAudit.setTime(new Timestamp(new Date().getTime()));
    	loginAudit.setUserName("study_admin");
		auditDao.save(loginAudit);
		replayMocks();
		
		command.saveAudit(loginAudit);
		verifyMocks();
    }
}
{'fraction_non_alphanumeric': 0.10539389638041163, 'fraction_numerical': 0.0063875088715401, 'mean_word_length': 4.181066176470588, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '262555', 'n_tokens_mistral': 1675, 'n_tokens_neox': 1611, 'n_words': 272}