/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2005 Bull S.A.
 * Contact: jonas-team@objectweb.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or 1any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas.jonasadmin.test.util;

import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebResponse;

/**
 * JonasAdmin authentification
 * @author kemlerp
 *
 */
public class JonasAdminAuth {

    /**
     * Constructor
     *
     */
    private JonasAdminAuth() {
        super();
    }
    /**
     * Authenticate with a valid login/password
     * @param wc Web Conversation to use
     * @param url url to use
     * @return the response
     * @throws Exception if an error occurs
     */
    public static WebResponse doValidAuth(WebConversation wc, String url) throws Exception {
        return doAuth(wc, url, "jonas", "jonas");
    }

    /**
     * Authenticate with the specified login/password
     * @param wc Web Conversation to use
     * @param url url to use
     * @param login login to use
     * @param password password to use
     * @return the response after authentication
     * @throws Exception if an error occurs
     */
    public static WebResponse doAuth(WebConversation wc, String url, String login, String password) throws Exception {
        WebResponse wr = wc.getResponse(url);
        WebForm[] webForms = wr.getForms();
        WebForm webForm = webForms[0];

        webForm.setParameter("j_username", login);
        webForm.setParameter("j_password", password);
        return webForm.submit();
    }
}
