com.formkiq.web.OAuthFederationIntegrationTest.java Source code

Java tutorial

Introduction

Here is the source code for com.formkiq.web.OAuthFederationIntegrationTest.java

Source

/*
 * Copyright (C) 2017 FormKiQ Inc.
 *
 * 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 com.formkiq.web;

import static org.junit.Assert.assertEquals;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT;

import java.util.Arrays;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.google.common.base.Predicate;

/**
 * WorkflowController Edit Integration Test.
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = DEFINED_PORT)
@ActiveProfiles("dev")
public class OAuthFederationIntegrationTest extends SeleniumTestBase {

    /**
     * assert No OAuth Federations is setup.
     */
    private void assertNoOAuthSetup() {
        waitUntil(new Predicate<WebDriver>() {
            @Override
            public boolean apply(final WebDriver d) {
                WebElement e = findElements(By.xpath("//table[@id='results']")).get(0);
                return e.getText().contains("No OAuth Federation(s) setup");
            }
        });
    }

    @Override
    @Before
    public void before() throws Exception {
        super.before();

        truncateTables("oauth_federation");

        login(getDefaultEmail());
        // TODO remove when new ui is complete
        getDriver().navigate().to(getDefaultHostAndPort() + "/admin/oauth");
        assertEquals("FormKiQ Server - OAuth Federation", getTitle());
    }

    /**
     * testOauth01().
     * save blank oauth federation
     *
     * @throws Exception Exception
     */
    @Test
    public void testOauth01() throws Exception {
        // given

        // when
        findElementBy(By.id("oauth_add_button")).click();

        getWait().until(ExpectedConditions.visibilityOfElementLocated(By.id("form-modal")));

        click(By.name("_eventId_next"));

        // then
        waitUntilPageSourceText("Field required");
    }

    /**
     * testOauth02().
     * add oauth federation
     *
     * @throws Exception Exception
     */
    @Test
    public void testOauth02() throws Exception {
        // given

        // when
        findElementBy(By.id("oauth_add_button")).click();

        getWait().until(ExpectedConditions.visibilityOfElementLocated(By.id("form-modal")));

        findElementBy("input", "data-valuekey", "domain").sendKeys("222");
        findElementBy("input", "data-valuekey", "username").sendKeys("333");
        findElementBy("input", "data-valuekey", "host").sendKeys("444");

        click(By.name("_eventId_next"));

        // then
        getWait().until(ExpectedConditions.invisibilityOfElementLocated(By.id("form-modal")));

        assertEquals(1, getTableRowCount("results"));
        List<WebElement> tablecells = findElements(By.tagName("td"));
        assertRowEquals(tablecells, Arrays.asList("333", "444", ""));

        // when
        findElementBy(By.id("delete_0")).click();

        // then
        assertNoOAuthSetup();
    }

    /**
     * testOauth03().
     * add oauth federation, domain only
     *
     * @throws Exception Exception
     */
    @Test
    public void testOauth03() throws Exception {
        // given

        // when
        findElementBy(By.id("oauth_add_button")).click();

        getWait().until(ExpectedConditions.visibilityOfElementLocated(By.id("form-modal")));

        findElementBy("input", "data-valuekey", "domain").sendKeys("222");
        findElementBy("input", "data-valuekey", "host").sendKeys("444");

        click(By.name("_eventId_next"));

        // then
        getWait().until(ExpectedConditions.invisibilityOfElementLocated(By.id("form-modal")));

        assertEquals(1, getTableRowCount("results"));
        List<WebElement> tablecells = findElements(By.tagName("td"));
        assertRowEquals(tablecells, Arrays.asList("222", "444", ""));

        // when
        findElementBy(By.id("delete_0")).click();

        // then
        assertNoOAuthSetup();
    }

    // TODO add validation rule for domain & host (domain must start with @)
}