# Path: lib/credentials_adapters.py
# class CredentialsFactory(AbstractCredentialsFactory):
#     def __init__(self):
#         pass
# 
#     @staticmethod
#     def instantiate(credential_type="user"):
#         return {"user": St2UserCredentials, "token": St2UserToken, "apikey": St2ApiKey}.get(
#             credential_type, St2UserCredentials
#         )
# 
# class St2ApiKey(AbstractCredentials):
#     def __init__(self, apikey=None):
#         self.apikey = None
#         if apikey:
#             self.apikey = apikey
# 
#     def __repr__(self):
#         return self.apikey
# 
#     def requests(self):
#         return {"St2-Api-Key": self.apikey}
# 
#     def st2client(self):
#         return {"api_key": self.apikey}
# 
# class St2UserCredentials(AbstractCredentials):
#     def __init__(self, username=None, password=None):
#         self.username = username
#         self.password = password
# 
#     def __repr__(self):
#         return "".join([self.username, " : ", "".join(["*" for c in self.password])])
# 
#     def requests(self, st2_x_auth=False):
#         # TODO: FIX: Find a cleaner way for requests to produce the header.
#         headers = (
#             HTTPBasicAuth(self.username, self.password)
#             .__call__(SimpleNamespace(**{"headers": {}}))
#             .headers
#         )
#         if st2_x_auth:
#             headers["X-Authenticate"] = headers["Authorization"]
#             del headers["Authorization"]
#         return headers
# 
#     def st2client(self):
#         raise NotImplementedError
# 
# class St2UserToken(AbstractCredentials):
#     def __init__(self, token=None):
#         self.token = None
#         if token:
#             self.token = token
# 
#     def __repr__(self):
#         return self.token
# 
#     def requests(self):
#         return {"X-Auth-Token": self.token}
# 
#     def st2client(self):
#         return {"token": self.token}


from lib.credentials_adapters import CredentialsFactory, St2ApiKey, St2UserCredentials, St2UserToken
# coding:utf-8

pytest_plugins = ["errbot.backends.test"]
extra_plugin_dir = "."


def test_credentials_manager():
    """
    Credentials manager
    """
    username = "peter.parker"
    password = "stickyweb"
    token = "abcdef1234"
    apikey = "1234abcdef"

    # request user
    user_creds = CredentialsFactory.instantiate("user")(username, password)
    assert isinstance(user_creds, St2UserCredentials)
    assert user_creds.requests() == {"Authorization": "Basic cGV0ZXIucGFya2VyOnN0aWNreXdlYg=="}

    # requests token

# Path: lib/credentials_adapters.py
# class CredentialsFactory(AbstractCredentialsFactory):
#     def __init__(self):
#         pass
# 
#     @staticmethod
#     def instantiate(credential_type="user"):
#         return {"user": St2UserCredentials, "token": St2UserToken, "apikey": St2ApiKey}.get(
#             credential_type, St2UserCredentials
#         )
# 
# class St2ApiKey(AbstractCredentials):
#     def __init__(self, apikey=None):
#         self.apikey = None
#         if apikey:
#             self.apikey = apikey
# 
#     def __repr__(self):
#         return self.apikey
# 
#     def requests(self):
#         return {"St2-Api-Key": self.apikey}
# 
#     def st2client(self):
#         return {"api_key": self.apikey}
# 
# class St2UserCredentials(AbstractCredentials):
#     def __init__(self, username=None, password=None):
#         self.username = username
#         self.password = password
# 
#     def __repr__(self):
#         return "".join([self.username, " : ", "".join(["*" for c in self.password])])
# 
#     def requests(self, st2_x_auth=False):
#         # TODO: FIX: Find a cleaner way for requests to produce the header.
#         headers = (
#             HTTPBasicAuth(self.username, self.password)
#             .__call__(SimpleNamespace(**{"headers": {}}))
#             .headers
#         )
#         if st2_x_auth:
#             headers["X-Authenticate"] = headers["Authorization"]
#             del headers["Authorization"]
#         return headers
# 
#     def st2client(self):
#         raise NotImplementedError
# 
# class St2UserToken(AbstractCredentials):
#     def __init__(self, token=None):
#         self.token = None
#         if token:
#             self.token = token
# 
#     def __repr__(self):
#         return self.token
# 
#     def requests(self):
#         return {"X-Auth-Token": self.token}
# 
#     def st2client(self):
#         return {"token": self.token}


# Path: tests/test_credentials_manager.py
from lib.credentials_adapters import CredentialsFactory, St2ApiKey, St2UserCredentials, St2UserToken

# coding:utf-8

pytest_plugins = ["errbot.backends.test"]
extra_plugin_dir = "."


def test_credentials_manager():
    """
    Credentials manager
    """
    username = "peter.parker"
    password = "stickyweb"
    token = "abcdef1234"
    apikey = "1234abcdef"

    # request user
    user_creds = CredentialsFactory.instantiate("user")(username, password)
    assert isinstance(user_creds, St2UserCredentials)
    assert user_creds.requests() == {"Authorization": "Basic cGV0ZXIucGFya2VyOnN0aWNreXdlYg=="}

    # requests token

    token_creds = CredentialsFactory.instantiate("token")(token)