/*
 * Copyright 2009 Inspire-Software.com
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package org.yes.cart.service.domain.impl;

import org.junit.Test;
import org.yes.cart.service.domain.HashHelper;

import static org.junit.Assert.assertEquals;

/**
 * User: Igor Azarny iazarny@yahoo.com
 * Date: 09-May-2011
 * Time: 14:12:54
 */
public class MD5HashHelperImplTest {

    @Test
    public void testGetMD5Hash() throws Exception {
        HashHelper hashHelper = new MD5HashHelperImpl();

        assertEquals("Get unexpected hash",
                "5f4dcc3b5aa765d61d8327deb882cf99",
                hashHelper.getHash("password"));

        assertEquals("Get unexpected hash",
                "fcea920f7412b5da7be0cf42b8c93759",
                hashHelper.getHash("1234567"));
    }

    @Test
    public void testGetMD5HashWithSalt() throws Exception {
        MD5HashHelperImpl hashHelper = new MD5HashHelperImpl();
        hashHelper.setSalt("YCPWSALT");

        assertEquals("Get unexpected hash",
                "f71b3d55bd4b4a0b3f18f65349750c21",
                hashHelper.getHash("password"));

        assertEquals("Get unexpected hash",
                "d89c77010dedf89c10d1293bd02b53c7",
                hashHelper.getHash("1234567"));
    }


    /**
     * This is test to allow to generate password for server installation.
     *
     * Initially the server is setup with admin user, which is in initdata.sql
     * However this password is subject to md5 hash salt configuration and it
     * will not work if the salt is reconfigured.
     *
     * For such cases BEFORE inserting default admin user from initial.sql the
     * hash column in insert SQL has to be changed to value generated by this test
     * method.
     *
     * How to use:
     * 1. Replace "YCPWSALT" with desired salt value that you configured in config.properties
     * 2. Replace "1234567" with desired password value
     * 3. Modify SQL in initial.sql and insert your values for admin email and password hash.
     * 4. Run insert statement on the server to be able to login to newly installed server.
     *
     * @throws Exception  error
     */
    @Test
    public void testCreatePasswordForProdServerInitialisation() throws Exception {

        MD5HashHelperImpl hashHelper = new MD5HashHelperImpl();
        hashHelper.setSalt("YCPWSALT");

        System.out.println("Your password hash is: '" + hashHelper.getHash("1234567") + "'");

    }
}
