Esempio n. 1
0
    def test_auth(self):

        currentDateTime = int(time.time())
        print("CURRENT DATETIME: ", currentDateTime)

        currentDateTimeString = datetime.datetime.fromtimestamp(
            currentDateTime).strftime("%FT%T.000Z")
        print("CURRENT DATETIME STR: ", currentDateTimeString)

        BCAPI = api.EntityContext(BASCLOUD_TEST_URL)

        BCAPI.authenticateWithUserLogin(BASCLOUD_TEST_EMAIL,
                                        BASCLOUD_TEST_PASS)

        print("TOKEN: ", BCAPI.getToken())
        print(
            "EXPIRATION DATE: ",
            datetime.datetime.fromtimestamp(
                BCAPI.getTokenExpirationDate()).strftime("%FT%T.000Z"))

        self.assertEqual(BCAPI.isAuthenticated(), True,
                         "APi should be authenticated.")
        self.assertTrue(BCAPI.getTokenExpirationDate() > currentDateTime,
                        "Expiration date should be in the future.")
        self.assertTrue(
            len(BCAPI.getToken()) > 0, "Token should be not empty.")
Esempio n. 2
0
    def test_auth(self):
        # Invalid authentication should return unauthorized by the api and throw exception accordingly
        BCAPI = api.EntityContext("https://test.local")

        try:
            BCAPI.authenticateWithUserLogin("*****@*****.**", "test")
        except api.ConnectionError:
            self.assertEqual(BCAPI.isAuthenticated(), False,
                             "API should not be authenticated.")
            self.assertTrue(BCAPI.getTokenExpirationDate() == -1,
                            "Expiration date should be invalid.")
            self.assertTrue(
                len(BCAPI.getToken()) == 0, "Token should be empty.")
        except:
            raise
Esempio n. 3
0
import datetime
import sys
import traceback

# import the BAS Cloud API library using the name api
import pyBAScloudAPI as api

# Error handler function which is given as a error callback to request functions
def printErrorHandler(exception, json):
    print("\t\tException occured in request: ", exception, json)


print("Initialising library...")

# For accessing the BAScloud API, a API endpoint URL needs to be defined (Note: no trailing symbols, must end with TLD identifier)
BCAPI = api.EntityContext("server_URL")
print("\tOK." )


# Before accessing any API methods, a user must be authenticated
print("1. - Authentication with user login" )

# /**
# * All methods in the BAScloud API library potential throw different exceptions to signal a failure in the API request.
# * 
# * The following exceptions may be thrown:
# * 
# * ServerError: If the server returns a (unexpected) server error 500.
# * ConnectionError: If the connection to the server could not be establised.
# * BadRequest: If parameters given through the request were invalid e.g. invalid UUID.
# * UnauthorizedRequest: If the currently authenticated user has no authority to access the API endpoint.
Esempio n. 4
0
import unittest
import time
import datetime

from definitions import *

import pyBAScloudAPI as api

BCAPI = api.EntityContext(BASCLOUD_TEST_URL)


def errorHandler(e, j):
    raise e


def authenticate():
    currentDateTime = int(time.time())
    print("CURRENT DATETIME: ", currentDateTime)

    currentDateTimeString = datetime.datetime.fromtimestamp(
        currentDateTime).strftime("%FT%T.000Z")
    print("CURRENT DATETIME STR: ", currentDateTimeString)

    BCAPI.authenticateWithUserLogin(BASCLOUD_TEST_ADMIN_EMAIL,
                                    BASCLOUD_TEST_PASS)

    print("TOKEN: ", BCAPI.getToken())
    print(
        "EXPIRATION DATE: ",
        datetime.datetime.fromtimestamp(
            BCAPI.getTokenExpirationDate()).strftime("%FT%T.000Z"))
Esempio n. 5
0
      api.Util.parseDateTimeString(dateTime="2021-04-26T10:56:58.000Z"))
assert api.Util.parseDateTimeString(
    dateTime="2021-04-26T10:56:58.000Z") == 1619427418

print(
    "\tParameters:",
    api.Util.parseURLParameter(
        url=
        "test.local/tenants/XXX/connectors/XXX/devices?page[size]=1&page[before]=Mzk5ZTM1MWYtNTI3OS00YzFhLTk0MmUtYTZiODBmMjFiYzVh"
    ))

print("Demo of library methods for BAScloud API endpoints.")

print("Initialising library...")

BCAPI = api.EntityContext("https://basc-prd-apm-euw.azure-api.net/v2")

print("\tOK.")

print("1. - Authentication with user login")

BCAPI.authenticateWithUserLogin(email="*****@*****.**",
                                password="******")
print("\tOK.")
print("\tAuthenticated: ", BCAPI.isAuthenticated())
print("\tToken valid until: ",
      datetime.datetime.fromtimestamp(BCAPI.getTokenExpirationDate()))
print("\tToken: ", BCAPI.getToken())

print("2. - Access user information")