class members_dashboard_my_session_item_2(TestCase):

    def setUp(self):

        # start up a tool session container
        self.hubname = self.testdata.find_url_for('https')
        self.username,self.userpass = \
            self.testdata.find_account_for('purdueworkspace')

        self.cm = ContainerManager()
        self.ws = self.cm.create(host=self.hubname,
                                 username=self.username,
                                 password=self.userpass)

        self.session_number,es = self.ws.execute('echo $SESSION')
        self.session_number = int(self.session_number)

        # setup a web browser
        self.browser.get(self.https_authority)

        self.utils.account.login_as(self.username,self.userpass)

        self.po = self.catalog.load_pageobject('GenericPage')
        self.po.header.goto_myaccount()

        self.po = self.catalog.load_pageobject('MembersDashboardPage')

        self.my_sessions_module = self.po.modules.my_sessions


    def tearDown(self):

        # get out of the workspace
        # shut down the ssh connection
        self.ws.close()
        # resync our session data because in the test,
        # we attempted to close a session being tracked by cm
        self.cm.sync_open_sessions(self.hubname,self.username)


    @hubcheck.utils.hub_version(min_version='1.1.4')
    def test_terminate_click_accept(self):
        """
        click the terminate session link and accept the confirmation
        """

        pageurl1 = self.po.current_url()
        num_sessions_1 = self.my_sessions_module.count_sessions()

        session_item = self.my_sessions_module.get_session_by_session_number(
                        self.session_number)

        self.assertTrue(session_item is not None,
            'No session exists for session numbered: %s'
            % (self.session_number))

        # open the slide down window
        if not session_item.is_slide_open():
            session_item.toggle_slide()

        session_item.terminate_session(confirm=True)

        self.my_sessions_module = self.po.modules.my_sessions
        session_numbers = self.my_sessions_module.get_session_numbers()

        self.assertTrue(self.session_number not in session_numbers,
            "after confirming the termination of a session"
            + " the session %d still appears in the list of open sessions"
            % (self.session_number))
class TestIcewmUserConfig(TestCase2):

    def setup_method(self,method):

        # get user account info
        self.username,self.userpass = \
            self.testdata.find_account_for('registeredworkspace')
        self.hubname = self.testdata.find_url_for('https')

        # get into a workspace
        self.cm = ContainerManager()
        self.ws = self.cm.access(host=self.hubname,
                                 username=self.username,
                                 password=self.userpass)


    def teardown_method(self,method):
        pass


    def _check_icewm_config_file(self,fname,points_to):

        # check the file is a link
        assert self.ws.bash_test('-L {0}'.format(fname)), \
            '"{0}" not a link'.format(fname)

        # check the file points to default config
        command = 'readlink -e {0}'.format(fname)
        output,es = self.ws.execute(command)
        assert output == points_to, \
            '"{0}" points to "{1}", expected "{2}"'.format(fname,output,points_to)

        # check the default config is readable
        assert self.ws.bash_test('-r {0}'.format(points_to)), \
            '"{0}" not a readable'.format(points_to)


    def test_icewm_config(self):
        """
        check icewm config files point to hubzero config
        """

        # check if the icewm directory was created.
        msg = "User's IceWM config directory (~/.icewm) was not recreated" + \
              " by new workspace after being deleted"
        assert self.ws.bash_test('-d {0}'.format(ICEWM_CONF_DIR)), msg

        msg = "User's IceWM config directory (~/.icewm) is not readable" + \
              " by new workspace after being deleted"
        assert self.ws.bash_test('-r {0}'.format(ICEWM_CONF_DIR)), msg


        # check if the user has the default icewm user config for hubzero

        for (fname,points_to) in ICEWM_CONF_FILE_SPEC:
            self._check_icewm_config_file(fname,points_to)


    def test_updated_icewm_config(self):
        """
        check if starting a workspace created a new icewm config directory
        after the user deleted their original one.
        """

        # remove the user's icewm config dir
        self.ws.execute('rm -rf ~/.icewm')

        session_number = 0

        # create a new workspace
        ws2 = self.cm.create(host=self.hubname,
                             username=self.username,
                             password=self.userpass)

        session_number,es = ws2.execute('echo $SESSION')

        # exit the new workspace
        ws2.close()

        # stop the new container
        self.cm.stop(self.hubname,self.username,int(session_number))


        # check if the icewm directory was created.
        msg = "User's IceWM config directory (~/.icewm) was not recreated" + \
              " by new workspace after being deleted"
        assert self.ws.bash_test('-d {0}'.format(ICEWM_CONF_DIR)), msg

        msg = "User's IceWM config directory (~/.icewm) is not readable" + \
              " by new workspace after being deleted"
        assert self.ws.bash_test('-r {0}'.format(ICEWM_CONF_DIR)), msg


        # check if the user has the default icewm user config for hubzero

        for (fname,points_to) in ICEWM_CONF_FILE_SPEC:
            self._check_icewm_config_file(fname,points_to)