#! /usr/bin/python2
# -*- coding: utf-8 -*-

# This file is part of Cockpit.
#
# Copyright (C) 2013 Red Hat, Inc.
#
# Cockpit 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
# (at your option) any later version.
#
# Cockpit 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 Cockpit; If not, see <http://www.gnu.org/licenses/>.

import parent
from testlib import *

class TestEmbed(MachineCase):
    def testBasic(self):
        b = self.browser
        m = self.machine

        m.start_cockpit()

        b.open("file://%s/verify/files/embed-cockpit.html" % TEST_DIR)
        b.wait_present("#embed-loaded")
        b.set_val("#embed-address", "http://{0}:{1}".format(m.web_address, m.web_port))
        b.click("#embed-full")
        b.wait_present("iframe[name='embed-full'][loaded]")
        b.switch_to_frame("embed-full")
        b.wait_visible("#login")
        b.set_val('#login-user-input', "admin")
        b.set_val('#login-password-input', "foobar")
        b.set_checked('#authorized-input', True)
        b.click('#login-button')
        # don't use expect_load() here as the outside page doesn't reload, just the iframe
        b.expect_load_frame("cockpit1:localhost/system")

        # Page should show automatically now that other frame logged in
        b.switch_to_top()
        b.click("#embed-terminal")
        b.wait_present("iframe[name='embed-terminal'][loaded]")
        b.switch_to_frame("embed-terminal")
        b.wait_visible("#terminal")

        # Clicking on the link with separate auth, shouldn't log in automatically
        b.switch_to_top()
        b.click("#embed-auth")
        b.wait_present("iframe[name='embed-auth'][loaded]")
        b.switch_to_frame("embed-auth")
        b.wait_present("#login-user-input")

if __name__ == '__main__':
    test_main()
