コード例 #1
0
    def inserted_as_shim(self):
        executable = self
        saved_which = Executable.which

        def stub_which(self, program):
            full_path_to_executable = saved_which(self, program)
            if program in [full_path_to_executable, executable.name]:
                return program
            else:
                return full_path_to_executable

        saved_execute = Executable.execute

        def stub_execute(self, method, commandline_arguments, *args, **kwargs):
            if executable.name == self.name:
                return executable.execute(method, commandline_arguments, *args,
                                          **kwargs)
            else:
                return saved_execute(self, method, commandline_arguments,
                                     *args, **kwargs)

        stub_which.__name__ = ascii_as_bytes_or_str('which')
        stub_execute.__name__ = ascii_as_bytes_or_str('execute')
        with replaced(Executable.which, stub_which,
                      Executable), replaced(Executable.execute, stub_execute,
                                            Executable):
            yield executable
コード例 #2
0
def test_reading_cookies_on_initialising_a_session(web_fixture):
    fixture = web_fixture

    # Case: session cookie not set in Request
    UserSession.initialise_web_session_on(fixture.context)
    assert not fixture.context.session.is_active()
    assert not fixture.context.session.is_secured()

    # Case: session cookie set in Request
    fixture.context.session = None
    user_session = UserSession()
    user_session.set_last_activity_time()
    Session.add(user_session)

    fixture.request.headers['Cookie'] = ascii_as_bytes_or_str(
        'reahl=%s' % user_session.as_key())
    UserSession.initialise_web_session_on(fixture.context)

    assert fixture.context.session is user_session
    assert fixture.context.session.is_active()
    assert not fixture.context.session.is_secured()

    # Case: session cookie set, secure cookie also set in Request, https
    fixture.request.scheme = 'https'
    fixture.context.session = None
    user_session = UserSession()
    user_session.set_last_activity_time()
    Session.add(user_session)

    fixture.request.headers['Cookie'] = ascii_as_bytes_or_str('reahl=%s , reahl_secure=%s' % \
                                        (user_session.as_key(), user_session.secure_salt))
    UserSession.initialise_web_session_on(fixture.context)

    assert fixture.context.session is user_session
    assert fixture.context.session.is_active()
    assert fixture.context.session.is_secured()

    # Case: session cookie set, secure cookie also set in Request, http
    fixture.request.scheme = 'http'
    fixture.context.session = None
    user_session = UserSession()
    user_session.set_last_activity_time()
    Session.add(user_session)
    fixture.request.headers['Cookie'] = ascii_as_bytes_or_str('reahl=%s , reahl_secure=%s' % \
                                        (user_session.as_key(), user_session.secure_salt))

    UserSession.initialise_web_session_on(fixture.context)

    assert fixture.context.session is user_session
    assert fixture.context.session.is_active()
    assert not fixture.context.session.is_secured()
コード例 #3
0
ファイル: tools.py プロジェクト: diopib/reahl
    def click(self, locator, **kwargs):
        """Clicks on the element found by `locator`.

           :param locator: An instance of :class:`XPath` or a string containing an XPath expression.
           
           Other keyword arguments are passed directly on to 
           `Form.submit <http://webtest.readthedocs.org/en/latest/api.html#webtest.forms.Form.submit>`_.
        """
        xpath = six.text_type(locator)
        buttons = self.xpath(xpath)
        assert len(
            buttons
        ) == 1, 'Could not find one (and only one) button for %s' % locator
        button = buttons[0]
        if button.tag == 'input' and button.attrib['type'] == 'submit':
            button_name = self.xpath(xpath)[0].name
            form = self.get_form_for(xpath)
            form.action = ascii_as_bytes_or_str(self.relative(form.action))
            self.last_response = form.submit(button_name, **kwargs)
            self.follow_response()
        elif button.tag == 'a':
            self.open(button.attrib['href'], **kwargs)
        elif button.tag == 'input' and button.type == 'checkbox':
            form = self.get_form_for(xpath)
            [checkbox] = form.fields[button.name]
            checkbox.value = 'on' if not checkbox.value else None
        else:
            raise AssertionError(
                'This browser can only click on buttons, a elements, or checkboxes'
            )
コード例 #4
0
ファイル: tools.py プロジェクト: diopib/reahl
    def create_cookie(self, cookie_dict):
        """Creates a cookie from the given `cookie_dict`.

           :param cookie_dict: A dictionary with two keys: 'name' and 'value'. The values of these\
                               keys are the name of the cookie and its value, respectively.
                               The keys  'path', 'domain', 'secure', 'expiry' can also be set to values.\
                               These have the respective meanings as defined in `RFC6265 <http://tools.ietf.org/html/rfc6265#section-5.2>`
        """
        name = ascii_as_bytes_or_str(cookie_dict['name'])
        value = ascii_as_bytes_or_str(cookie_dict['value'])
        path = ascii_as_bytes_or_str(cookie_dict.get('path', ''))
        path_set = path != ''
        domain = ascii_as_bytes_or_str(cookie_dict.get('domain', ''))
        domain_set = domain != ''
        secure = cookie_dict.get('secure', False)
        expires = cookie_dict.get('expiry', None)
        cookie = Cookie(0, name, value, None, False, domain, domain_set, None,
                        path, path_set, secure, expires, None, None, None,
                        None)
        self.testapp.cookiejar.set_cookie(cookie)
コード例 #5
0
ファイル: tools.py プロジェクト: diopib/reahl
    def post(self, url_string, form_values, **kwargs):
        """POSTs the given form values to the url given.
        
           :param url_string: A string containing the URL to be posted to.
           :param form_values: A dictionary containing form data in its key/value pairs.

           Other keyword arguments are passed directly on to 
           `WebTest.post <http://webtest.readthedocs.org/en/latest/api.html#webtest.app.TestApp.post>`_.
        """
        self.last_response = self.testapp.post(
            (ascii_as_bytes_or_str(url_string)), form_values, **kwargs)
コード例 #6
0
    def __call__(self, environ, start_response):
        app = self.wrapped

        request = Request(environ, charset='utf-8')

        self.exception = None
        self.traceback = None
        try:
            to_return = b''
            for i in app(environ, start_response):
                to_return += i
        except:
            to_return = b''
            (_, self.exception, self.traceback) = sys.exc_info()
            traceback_html = six.text_type(traceback.format_exc())
            for i in HTTPInternalServerError(
                    content_type=ascii_as_bytes_or_str('text/plain'),
                    charset=ascii_as_bytes_or_str('utf-8'),
                    unicode_body=traceback_html)(environ, start_response):
                to_return += i
        yield to_return
コード例 #7
0
 def log_in(self,
            browser=None,
            session=None,
            system_account=None,
            stay_logged_in=False):
     session = session or self.session
     browser = browser or self.driver_browser
     login_session = LoginSession.for_session(session)
     login_session.set_as_logged_in(system_account or self.system_account,
                                    stay_logged_in)
     # quickly create a response so the fw sets the cookies, which we copy and explicitly set on selenium.
     response = Response()
     self.session.set_session_key(response)
     cookies = http_cookies.BaseCookie(
         ascii_as_bytes_or_str(', '.join(
             response.headers.getall('set-cookie'))))
     for name, morsel in cookies.items():
         cookie = {'name': name, 'value': morsel.value}
         cookie.update(
             dict([(key, value) for key, value in morsel.items() if value]))
         browser.create_cookie(cookie)