"Fossies" - the Fresh Open Source Software Archive

Member "LinOTP-release-2.10.5.2/linotpd/src/linotp/tests/integration/linotp_selenium_helper/token_import.py" (13 May 2019, 5259 Bytes) of package /linux/misc/LinOTP-release-2.10.5.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 # -*- coding: utf-8 -*-
    2 #
    3 #    LinOTP - the open source solution for two factor authentication
    4 #    Copyright (C) 2010 - 2019 KeyIdentity GmbH
    5 #
    6 #    This file is part of LinOTP server.
    7 #
    8 #    This program is free software: you can redistribute it and/or
    9 #    modify it under the terms of the GNU Affero General Public
   10 #    License, version 3, as published by the Free Software Foundation.
   11 #
   12 #    This program is distributed in the hope that it will be useful,
   13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
   14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   15 #    GNU Affero General Public License for more details.
   16 #
   17 #    You should have received a copy of the
   18 #               GNU Affero General Public License
   19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
   20 #
   21 #
   22 #    E-mail: linotp@keyidentity.com
   23 #    Contact: www.linotp.org
   24 #    Support: www.keyidentity.com
   25 #
   26 """Contains TokenImport class"""
   27 
   28 from manage_ui import ManageDialog
   29 from linotp_selenium_helper.manage_ui import MsgType
   30 
   31 import tempfile
   32 import os
   33 import subprocess
   34 
   35 
   36 class TokenImport(ManageDialog):
   37     """
   38     TokenImport imports files as Tokens in the LinOTP WebUI
   39     """
   40 
   41     def __init__(self, manage_ui):
   42         """
   43         Base class for all token imports. Derive from this class
   44         and implement its special behavior. You have to overwrite
   45         at least the following attributes in your derived class.
   46             menu_item_id
   47             body_id
   48             load_button_id
   49             file_name_lineedit
   50         :param manage_ui: The base manage class for the ui elements
   51         """
   52         ManageDialog.__init__(self, manage_ui)
   53         self.menu_css = manage_ui.MENU_LINOTP_IMPORT_TOKEN_CSS
   54 
   55         # Open the appropriate Token import dialog.
   56         # TopMenu->Import Token File-><safenet/aladdin,oath,yubikey,...>
   57         self.manage.activate_menu_item(self.menu_css,
   58                                        self.menu_item_id)
   59 
   60     def do_import(self, file_content=None, file_path=None):
   61         """
   62         Imports the file. Currently the only type supported is 'safenet'.
   63         Either xml_content (string) or file_path (string) has to be present.
   64         If file_content is not None and there is no path then file_content
   65         is written to a temporary file that is used for the import.
   66 
   67         :param file_content: xml string with Token import details
   68         :param file_path: the file path of provided xml token file
   69         """
   70         if(not file_content and not file_path):
   71             raise Exception("""Wrong test implementation. TokenImport.do_import
   72                             needs file_content or file_path!
   73                             """)
   74 
   75         if file_content:
   76             # Create the temp xml file with the given file_content.
   77             tf = tempfile.NamedTemporaryFile(delete=False, suffix=".xml")
   78             tf.write(file_content)
   79             tf.close()
   80             self.file_path = tf.name
   81 
   82             # We need to make the created file available in the selenium
   83             # docker container (Where the browser interaction is done).
   84             # For this reason we move the temp file in the shared
   85             # volume (integration tests dir), which is mounted inside
   86             # the selenium docker container (see docker-compose.yml).
   87             cwd = os.getcwd()
   88             subprocess.call(["mv",self.file_path,cwd])
   89 
   90             filename = os.path.basename(self.file_path)
   91             self.file_path = cwd + "/" + filename
   92             # Maybe created by root in the docker container.
   93             subprocess.call(["chmod","a+rw",self.file_path])
   94 
   95         else:
   96             # Use the provided xml token file.
   97             self.file_path = file_path
   98 
   99         # On firefox the lineedit is not cleared after dialog re-open
  100         # So we have to do this explicitly
  101         # Otherwise the token file to load will be added and
  102         # LinOTP ends up in an undefined state.
  103         self.driver.find_element_by_xpath(
  104             self.file_name_lineedit).clear()
  105 
  106         # Send the filename to the token file lineedit in the dialog.
  107         self.driver.find_element_by_xpath(
  108             self.file_name_lineedit).send_keys(self.file_path)
  109 
  110         self.driver.find_element_by_id(self.load_button_id).click()
  111         self.driver.find_element_by_id("logo").click()
  112 
  113         # delete the temp file if necessary
  114         if(not file_path):
  115             # In this case, we did not receive a pre-created file
  116             # So: delete the temp file.
  117             try:
  118                 os.unlink(self.file_path)
  119             except:
  120                 pass
  121 
  122         # Check the alert boxes on the top of the LinOTP UI
  123         return self.manage.alert_box_handler.check_message(
  124             'Failed to import token', MsgType.Error)
  125 
  126 
  127 class TokenImportAladdin(TokenImport):
  128     """
  129     Import an Aladdin Token file (xml).
  130     Create an instance and invoke the 'do_import' method.
  131     """
  132     menu_item_id = 'menu_load_aladdin_xml_tokenfile'
  133     body_id = 'dialog_import_safenet'
  134     load_button_id = 'button_aladdin_load'
  135     file_name_lineedit = '//*[@id="load_tokenfile_form_aladdin"]/p[2]/input'
  136 
  137     def __init__(self, manage_ui):
  138         TokenImport.__init__(self, manage_ui)