Skip to content
Snippets Groups Projects
Select Git revision
  • acebed8058311087b127743daf4b3bea753a128f
  • main default protected
  • f-sss4grpc
  • dev
  • 108-implement-rpc-call-for-server-side-scripting
  • f-windows-conan-create
  • f-to-string
  • f-update-requirements
  • f-related-projects
  • f-role
  • f-remote-path
  • f-rel-path
  • f-consol-message
  • v0.3.0
  • v0.2.2
  • v0.2.1
  • v0.2.0
  • v0.1.2
  • v0.1.1
  • v0.1
  • v0.0.19
  • v0.0.18
  • v0.0.16
  • v0.0.15
  • v0.0.10
  • v0.0.9
  • v0.0.8
  • v0.0.7
  • v0.0.6
  • v0.0.5
  • v0.0.4
  • v0.0.3
  • v0.0.2
33 results

test_info.cpp

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    test_authentication_auth_token.py 3.47 KiB
    # -*- coding: utf-8 -*-
    #
    # ** header v3.0
    # This file is a part of the LinkAhead Project.
    #
    # Copyright (C) 2020 Timm Fitschen <t.fitschen@indiscale.com>
    # Copyright (C) 2020 IndiScale GmbH <info@indiscale.com>
    #
    # This program is free software: you can redistribute it and/or modify
    # it under the terms of the GNU Affero General Public License as
    # published by the Free Software Foundation, either version 3 of the
    # License, or (at your option) any later version.
    #
    # This program 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 Affero General Public License for more details.
    #
    # You should have received a copy of the GNU Affero General Public License
    # along with this program. If not, see <https://www.gnu.org/licenses/>.
    #
    # ** end header
    #
    """test_authentication_auth_token
    
    Unit tests for the module linkahead.connection.authentication.auth_token
    """
    
    from __future__ import unicode_literals
    from pytest import raises
    from unittest.mock import Mock
    from linkahead.connection.authentication import auth_token as at
    from linkahead.connection.mockup import MockUpServerConnection, MockUpResponse
    from linkahead.connection.utils import parse_auth_token
    from linkahead.exceptions import LoginFailedError
    from linkahead import configure_connection
    
    
    def test_get_authentication_provider():
        ap = at.get_authentication_provider()
        assert isinstance(ap, at.AuthTokenAuthenticator)
    
    
    def response_with_auth_token():
        token = "SessionToken=[response token];"
        assert parse_auth_token(token) is not None, "cookie not ok"
    
        return MockUpResponse(200, {"Set-Cookie": token}, "ok")
    
    
    def test_configure_connection():
        def request_has_auth_token(**kwargs):
            """test resources"""
            cookie = kwargs["headers"]["Cookie"]
            assert cookie is not None
            assert cookie == "SessionToken=%5Brequest%20token%5D;"
    
            return response_with_auth_token()
    
        c = configure_connection(password_method="auth_token",
                                 auth_token="[request token]",
                                 implementation=MockUpServerConnection)
        assert isinstance(c._authenticator, at.AuthTokenAuthenticator)
    
        c._delegate_connection.resources.append(request_has_auth_token)
        assert c._authenticator.auth_token == "[request token]"
        response = c._http_request(method="GET", path="test")
        assert response.read().decode() == "ok"
        assert c._authenticator.auth_token == "[response token]"
    
    
    def test_login_raises():
        c = configure_connection(url="https://example.com",
                                 password_method="auth_token",
                                 auth_token="[auth_token]")
        with raises(LoginFailedError):
            c._login()
    
    
    def test_logout_calls_delete():
        mock = Mock()
    
        def logout_resource(**kwargs):
            """logout with auth_token"""
            mock.method()
            assert kwargs["path"] == "logout"
            assert kwargs["method"] == "DELETE"
    
            cookie = kwargs["headers"]["Cookie"]
            assert cookie is not None
            assert cookie == "SessionToken=%5Brequest%20token%5D;"
    
            return MockUpResponse(200, {}, "ok")
    
        c = configure_connection(password_method="auth_token",
                                 auth_token="[request token]",
                                 implementation=MockUpServerConnection)
    
        c._delegate_connection.resources.insert(1, logout_resource)
        c._logout()
        mock.method.assert_called_once()