def test_base_functionality(self):
        cisco_hello_api = CiscoHelloApi()
        assert cisco_hello_api.is_ready_for_use() is False

        assert cisco_hello_api.client_id is None
        assert cisco_hello_api.client_secret is None

        with pytest.raises(CredentialsNotFoundException):
            cisco_hello_api.create_temporary_access_token()

        cisco_hello_api.load_client_credentials()
        assert cisco_hello_api.client_id != "dummy_id", "Should contain valid test credentials"
        assert cisco_hello_api.client_id != "dummy_secret", "Should contain valid test credentials"

        with open(BaseCiscoApiConsoleSettings.CREDENTIALS_FILE) as f:
            jdata = json.loads(f.read())
        assert jdata == cisco_hello_api.get_client_credentials()

        # create a temporary access token
        cisco_hello_api.create_temporary_access_token()
        assert cisco_hello_api.current_access_token is not None
        assert cache.get(CiscoHelloApi.AUTH_TOKEN_CACHE_KEY,
                         None) is not None, "Cached value should be created"

        # test that the class is now ready to use
        assert cisco_hello_api.is_ready_for_use() is True

        # test automatic recreation of the http_auth_header if no cached temp token is available
        cisco_hello_api.http_auth_header = None
        cache.delete(CiscoHelloApi.AUTH_TOKEN_CACHE_KEY)
        assert cisco_hello_api.is_ready_for_use() is True
        assert cisco_hello_api.http_auth_header is not None

        # try to recreate it
        token_before = cisco_hello_api.current_access_token
        cisco_hello_api.create_temporary_access_token()
        assert cisco_hello_api.current_access_token == token_before

        # force the recreation of the token
        cisco_hello_api.current_access_token = {
            "token_type": "my dummy value",
            "access_token": "my dummy value"
        }  # manually overwrite it to see that something happens
        cisco_hello_api.create_temporary_access_token(force_new_token=True)
        assert cisco_hello_api.current_access_token != "my dummy value"

        # cleanup
        cisco_hello_api.drop_cached_token()
        assert cisco_hello_api.current_access_token is None
        assert cache.get(CiscoHelloApi.AUTH_TOKEN_CACHE_KEY,
                         None) is None, "Cached value should be removed"

        # try to drop it again (nothing should happen)
        cisco_hello_api.drop_cached_token()
    def test_base_functionality(self):
        cisco_hello_api = CiscoHelloApi()
        assert cisco_hello_api.is_ready_for_use() is False

        assert cisco_hello_api.client_id is None
        assert cisco_hello_api.client_secret is None

        with pytest.raises(CredentialsNotFoundException):
            cisco_hello_api.create_temporary_access_token()

        cisco_hello_api.load_client_credentials()
        assert cisco_hello_api.client_id != "dummy_id", "Should contain valid test credentials"
        assert cisco_hello_api.client_id != "dummy_secret", "Should contain valid test credentials"

        with open(BaseCiscoApiConsoleSettings.CREDENTIALS_FILE) as f:
            jdata = json.loads(f.read())
        assert jdata == cisco_hello_api.get_client_credentials()

        # create a temporary access token
        cisco_hello_api.create_temporary_access_token()
        assert cisco_hello_api.current_access_token is not None
        assert cache.get(CiscoHelloApi.AUTH_TOKEN_CACHE_KEY, None) is not None, "Cached value should be created"

        # test that the class is now ready to use
        assert cisco_hello_api.is_ready_for_use() is True

        # test automatic recreation of the http_auth_header if no cached temp token is available
        cisco_hello_api.http_auth_header = None
        cache.delete(CiscoHelloApi.AUTH_TOKEN_CACHE_KEY)
        assert cisco_hello_api.is_ready_for_use() is True
        assert cisco_hello_api.http_auth_header is not None

        # try to recreate it
        token_before = cisco_hello_api.current_access_token
        cisco_hello_api.create_temporary_access_token()
        assert cisco_hello_api.current_access_token == token_before

        # force the recreation of the token
        cisco_hello_api.current_access_token = {
            "token_type": "my dummy value",
            "access_token": "my dummy value"
        }  # manually overwrite it to see that something happens
        cisco_hello_api.create_temporary_access_token(force_new_token=True)
        assert cisco_hello_api.current_access_token != "my dummy value"

        # cleanup
        cisco_hello_api.drop_cached_token()
        assert cisco_hello_api.current_access_token is None
        assert cache.get(CiscoHelloApi.AUTH_TOKEN_CACHE_KEY, None) is None, "Cached value should be removed"

        # try to drop it again (nothing should happen)
        cisco_hello_api.drop_cached_token()
Exemplo n.º 3
0
def test_cisco_hello_api_access(client_id, client_secret, drop_credentials=True):
    """
    test the Cisco Hello API access
    """
    try:
        base_api = CiscoHelloApi()
        base_api.load_client_credentials()

        if drop_credentials:
            base_api.drop_cached_token()

        base_api.client_id = client_id
        base_api.client_secret = client_secret

        base_api.hello_api_call()

        return True

    except:
        return False
Exemplo n.º 4
0
def check_cisco_hello_api_access(client_id,
                                 client_secret,
                                 drop_credentials=True):
    """
    test the Cisco Hello API access
    """
    try:
        base_api = CiscoHelloApi()
        base_api.load_client_credentials()

        if drop_credentials:
            base_api.drop_cached_token()

        base_api.client_id = client_id
        base_api.client_secret = client_secret

        base_api.hello_api_call()

        return True

    except Exception:
        return False
Exemplo n.º 5
0
def check_cisco_hello_api_access(client_id,
                                 client_secret,
                                 drop_credentials=True):
    """
    test the Cisco Hello API access
    """
    try:
        base_api = CiscoHelloApi()
        base_api.load_client_credentials()

        if drop_credentials:
            base_api.drop_cached_token()

        base_api.client_id = client_id
        base_api.client_secret = client_secret

        base_api.hello_api_call()

        return True

    except Exception as ex:
        logging.error("Cisco Hello API test access failed (%s)" % ex,
                      exc_info=True)
        return False