package ca.corefacility.bioinformatics.irida.ria.integration;

import org.junit.jupiter.api.Test;

import ca.corefacility.bioinformatics.irida.ria.integration.pages.AbstractPage;
import ca.corefacility.bioinformatics.irida.ria.integration.pages.LoginPage;
import ca.corefacility.bioinformatics.irida.ria.integration.pages.user.PasswordResetPage;

import com.github.springtestdbunit.annotation.DatabaseSetup;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * <p>
 * Integration test to ensure that the Login Page works and redirects the user to the dashboard.
 * </p>
 */
@DatabaseSetup("/ca/corefacility/bioinformatics/irida/ria/web/LoginPageIT.xml")
public class LoginPageIT extends AbstractIridaUIITChromeDriver {

	private static final String EXPIRED_USERNAME = "expiredGuy";
	private static final String EXPIRED_PASSWORD = "Password1!";

	@Test
	public void testBadUsername() throws Exception {
		LoginPage page = LoginPage.to(driver());
		assertFalse(page.isLoginErrorDisplayed(), "No login errors should be originally displayed");
		page.login(LoginPage.BAD_USERNAME, LoginPage.GOOD_PASSWORD);
		assertTrue(driver().getCurrentUrl().contains("login?error=true"), "Should update the url with '?error=true'");
		assertTrue(page.isLoginErrorDisplayed(), "Should display error on bad login");
	}

	@Test
	public void testBadPassword() throws Exception {
		LoginPage page = LoginPage.to(driver());
		assertFalse(page.isLoginErrorDisplayed(), "No login errors should be originally displayed");
		page.login(LoginPage.USER_USERNAME, LoginPage.BAD_PASSWORD);
		assertTrue(driver().getCurrentUrl().contains("login?error=true"), "Should update the url with '?error=true'");
		assertTrue(page.isLoginErrorDisplayed(), "Should display error on bad login");
	}

	@Test
	public void testGoodLogin() throws Exception {
		LoginPage.login(driver(), LoginPage.MANAGER_USERNAME, LoginPage.GOOD_PASSWORD);
		assertTrue(driver().getTitle().contains("Dashboard"), "The 'test' user is logged in and redirected.");
	}

	@Test
	public void testExpiredCredentialsLogin() throws Exception {
		LoginPage page = LoginPage.to(driver());
		page.login(EXPIRED_USERNAME, EXPIRED_PASSWORD);
		assertTrue(driver().getCurrentUrl().contains("password_reset/"),
				"The 'expiredGuy' user should be sent to a password reset page.");
	}

	@Test
	public void testLoginWithChangedCredentials() {
		String newPassword = "aGoodP@ssW0rD";
		LoginPage page = LoginPage.to(driver());
		page.login(EXPIRED_USERNAME, EXPIRED_PASSWORD);
		PasswordResetPage passwordResetPage = new PasswordResetPage(driver());
		passwordResetPage.enterPassword(newPassword);
		passwordResetPage.clickSubmit();
		assertTrue(passwordResetPage.checkSuccess(), "Should have succeeded in changing password.");

		AbstractPage.logout(driver());
		page = LoginPage.to(driver());
		page.login(EXPIRED_USERNAME, newPassword);
		assertTrue(driver().getTitle().contains("Dashboard"), "The user is logged in and redirected.");
	}

	@Test
	public void testSequencerLogin() throws Exception {
		LoginPage page = LoginPage.to(driver());
		assertFalse(page.isLoginErrorDisplayed(), "No login errors should be originally displayed");
		page.login(LoginPage.SEQUENCER_USERNAME, LoginPage.GOOD_PASSWORD);
		assertFalse(driver().getTitle().contains("Dashboard"),
				"The sequencer user should not be able to see the dashboard");
		assertTrue(driver().getCurrentUrl().contains("login?error=true&sequencer_login=true"),
				"Should update the url with '?error=true&sequencer_login=true'");
		assertTrue(page.isLoginErrorDisplayed(), "Should display error on bad login");
	}
}

@@ -3486,33 +3486,33 @@
 r=true&sequencer
-_
+-
 login=true%22),%0A%09%09
@@ -3563,17 +3563,17 @@
 equencer
-_
+-
 login=tr

Fix sequencer login ui test after url param rename