# Path: alignak_app/backend/ws_client.py
# class WSClient(object):
#     """
#         Class who manage Web Service requests
#     """
# 
#     def __init__(self):
#         self.ws_backend = ''
#         self.token = ''
#         self.auth = None
# 
#     def login(self, token):
#         """
#         Login to Web Service. Used only to ensure that the user token is identical
# 
#         :param token: user's token of App
#         :type token: str
#         """
# 
#         self.ws_backend = settings.get_config('Alignak', 'webservice')
# 
#         try:
#             # WS login
#             logger.info("Requesting Web Service authentication with token: %s", token)
#             headers = {'Content-Type': 'application/json'}
#             params = {'username': token, 'password': ''}
#             response = requests.post(
#                 '/'.join([self.ws_backend, 'login']), json=params, headers=headers
#             )
# 
#             resp = response.json()
# 
#             if '_result' in resp:
#                 assert token == resp['_result'][0]
#                 self.token = token
#                 self.auth = requests.auth.HTTPBasicAuth(self.token, '')
#         except (RequestsConnectionError, AssertionError) as exp:
#             msg = 'Connection to Web Service on [%s] has failed' % \
#                   '/'.join([self.ws_backend, 'login'])
#             logger.warning(msg)
#             logger.debug(exp)
# 
#     def get(self, endpoint, params):  # pragma: no cover
#         """
#         GET on alignak Backend Web Service
# 
#         :param endpoint: WS endpoint
#         :type endpoint: str
#         :param params: dict of parameters for the WS
#         :type params: dict|None
#         :return: request result
#         :rtype: dict
#         """
# 
#         request = None
# 
#         try:
#             request = requests.get(
#                 '/'.join([self.ws_backend, endpoint]), params=params, auth=self.auth
#             )
#             logger.info('GET on [%s] web service > %s', endpoint, str(request))
# 
#             request.raise_for_status()
#             logger.debug('\tparams: [%s]', str(params))
# 
#         except RequestsConnectionError as exp:
#             logger.error("Backend connection error, error: %s", exp)
# 
#         return request.json()
# 
#     def post(self, endpoint, params=None):  # pragma: no cover
#         """
#         Post on alignak Backend Web Service
# 
#         :param endpoint: WS endpoint
#         :type endpoint: str
#         :param params: dict of parameters for the WS
#         :type params: dict
#         :return: list of properties when query item | list of items when get many items
#         :rtype: list
#         """
# 
#         request = None
# 
#         try:
#             headers = {'Content-Type': 'application/json'}
#             params = json.dumps(params)
# 
#             request = requests.post(
#                 '/'.join([self.ws_backend, endpoint]), data=params, headers=headers, auth=self.auth
#             )
# 
#             request.raise_for_status()
#             logger.info('POST on [%s] backend > %s', endpoint, str(request))
#             logger.debug('\tdata: [%s]', str(params))
#             logger.debug('\theaders: [%s]', str(headers))
#         except (RequestsConnectionError, HTTPError) as exp:
#             logger.warning("WS connection error, error: %s", str(exp))
# 
#         except Exception as exp:
#             logger.exception("WS exception, error: %s", exp)
# 
#         return request.json()
# 
# Path: alignak_app/utils/config.py
# class Settings(object):
#     def __init__(self):
#     def init_config(self):
#     def get_config(self, section, option, boolean=False):
#     def set_config(self, section, option, new_value):
#     def get_image(self, name):
#     def init_css(self):
# def open_url(endpoint='login'):  # pragma: no cover
# def get_url_endpoint_from_icon_name(icon_name):


import unittest2
from alignak_app.backend.ws_client import WSClient
from alignak_app.utils.config import settings
# You should have received a copy of the GNU Affero General Public License
# along with (AlignakApp).  If not, see <http://www.gnu.org/licenses/>.




class TestWSClient(unittest2.TestCase):
    """
        This file test methods of WSClient class
    """

    # Init config for all methods.
    settings.init_config()

    def test_initialize_ws_client(self):
        """Initialize Web Service"""

        under_test = WSClient()

        self.assertFalse(under_test.token)
        self.assertFalse(under_test.ws_backend)
        self.assertIsNone(under_test.auth)

    def test_login_ws_with_wrong_token(self):
        """Login to WS with Wrong Token"""

        under_test = WSClient()
        under_test.login('')
        self.assertFalse(under_test.token)


# Path: alignak_app/backend/ws_client.py
# class WSClient(object):
#     """
#         Class who manage Web Service requests
#     """
# 
#     def __init__(self):
#         self.ws_backend = ''
#         self.token = ''
#         self.auth = None
# 
#     def login(self, token):
#         """
#         Login to Web Service. Used only to ensure that the user token is identical
# 
#         :param token: user's token of App
#         :type token: str
#         """
# 
#         self.ws_backend = settings.get_config('Alignak', 'webservice')
# 
#         try:
#             # WS login
#             logger.info("Requesting Web Service authentication with token: %s", token)
#             headers = {'Content-Type': 'application/json'}
#             params = {'username': token, 'password': ''}
#             response = requests.post(
#                 '/'.join([self.ws_backend, 'login']), json=params, headers=headers
#             )
# 
#             resp = response.json()
# 
#             if '_result' in resp:
#                 assert token == resp['_result'][0]
#                 self.token = token
#                 self.auth = requests.auth.HTTPBasicAuth(self.token, '')
#         except (RequestsConnectionError, AssertionError) as exp:
#             msg = 'Connection to Web Service on [%s] has failed' % \
#                   '/'.join([self.ws_backend, 'login'])
#             logger.warning(msg)
#             logger.debug(exp)
# 
#     def get(self, endpoint, params):  # pragma: no cover
#         """
#         GET on alignak Backend Web Service
# 
#         :param endpoint: WS endpoint
#         :type endpoint: str
#         :param params: dict of parameters for the WS
#         :type params: dict|None
#         :return: request result
#         :rtype: dict
#         """
# 
#         request = None
# 
#         try:
#             request = requests.get(
#                 '/'.join([self.ws_backend, endpoint]), params=params, auth=self.auth
#             )
#             logger.info('GET on [%s] web service > %s', endpoint, str(request))
# 
#             request.raise_for_status()
#             logger.debug('\tparams: [%s]', str(params))
# 
#         except RequestsConnectionError as exp:
#             logger.error("Backend connection error, error: %s", exp)
# 
#         return request.json()
# 
#     def post(self, endpoint, params=None):  # pragma: no cover
#         """
#         Post on alignak Backend Web Service
# 
#         :param endpoint: WS endpoint
#         :type endpoint: str
#         :param params: dict of parameters for the WS
#         :type params: dict
#         :return: list of properties when query item | list of items when get many items
#         :rtype: list
#         """
# 
#         request = None
# 
#         try:
#             headers = {'Content-Type': 'application/json'}
#             params = json.dumps(params)
# 
#             request = requests.post(
#                 '/'.join([self.ws_backend, endpoint]), data=params, headers=headers, auth=self.auth
#             )
# 
#             request.raise_for_status()
#             logger.info('POST on [%s] backend > %s', endpoint, str(request))
#             logger.debug('\tdata: [%s]', str(params))
#             logger.debug('\theaders: [%s]', str(headers))
#         except (RequestsConnectionError, HTTPError) as exp:
#             logger.warning("WS connection error, error: %s", str(exp))
# 
#         except Exception as exp:
#             logger.exception("WS exception, error: %s", exp)
# 
#         return request.json()
# 
# Path: alignak_app/utils/config.py
# class Settings(object):
#     def __init__(self):
#     def init_config(self):
#     def get_config(self, section, option, boolean=False):
#     def set_config(self, section, option, new_value):
#     def get_image(self, name):
#     def init_css(self):
# def open_url(endpoint='login'):  # pragma: no cover
# def get_url_endpoint_from_icon_name(icon_name):


# Path: test/test_ws_client.py
import unittest2
from alignak_app.backend.ws_client import WSClient
from alignak_app.utils.config import settings

# You should have received a copy of the GNU Affero General Public License
# along with (AlignakApp).  If not, see <http://www.gnu.org/licenses/>.




class TestWSClient(unittest2.TestCase):
    """
        This file test methods of WSClient class
    """

    # Init config for all methods.
    settings.init_config()

    def test_initialize_ws_client(self):
        """Initialize Web Service"""

        under_test = WSClient()

        self.assertFalse(under_test.token)
        self.assertFalse(under_test.ws_backend)
        self.assertIsNone(under_test.auth)

    def test_login_ws_with_wrong_token(self):
        """Login to WS with Wrong Token"""

        under_test = WSClient()
        under_test.login('')
        self.assertFalse(under_test.token)


        config_url = settings.get_config('Alignak', 'webservice')