# Path: fnapy/connection.py
# class FnapyConnection(object):
#     """The connection class of fnapy
# 
#     Usage::
#         connection = FnapyConnection(credentials=credentials, sandbox=sandbox)
# 
#     Example:
# 
#     * Create a connection with a credentials dictionary::
# 
#         credentials = {'partner_id': 'my_partner_id', 'shop_id': 'my_shop_id',
#                        'key': 'my_key', 'sandbox': False}
# 
#     * Create a connection using the environment variables for a given account
#       type (the sandbox account is used if sandbox True. If sandbox is False, the
#       real account is used)::
# 
#         connection = FnapyConnection(sandbox=True)
# 
#     .. note:: You must have previously defined the following environment
#         variables:
# 
#     * for the sandbox: FNAC_SANDBOX_PARTNER_ID, FNAC_SANDBOX_SHOP_ID,
#         FNAC_SANDBOX_KEY
# 
#     * for the real account: FNAC_PARTNER_ID, FNAC_SHOP_ID, FNAC_KEY
# 
#     """
# 
#     def __init__(self, credentials={}, sandbox=None):
#         # credentials
#         if len(credentials) > 0:
#             expecteds = ("partner_id", "shop_id", "key", "sandbox")
#             for expected in expecteds:
#                 if credentials.get(expected) is None:
#                     msg = "You didn't provide the {}."
#                     msg += "You must provide the following keys in credentials: "
#                     msg += ", ".join(expecteds)
#                     raise FnapyConnectionError(msg.format(expected))
# 
#         # sandbox
#         else:
#             if sandbox is not None:
#                 credentials = get_credentials(sandbox)
#                 credentials.update({"sandbox": sandbox})
#             else:
#                 msg = "You must either specify credentials or sandbox as arguments."
#                 raise FnapyConnectionError(msg)
# 
#         check_credentials_validity(credentials)
#         self.partner_id = credentials["partner_id"]
#         self.shop_id = credentials["shop_id"]
#         self.key = credentials["key"]
#         self.sandbox = credentials["sandbox"]
# 
# Path: fnapy/exceptions.py
# class FnapyConnectionError(FnapyException):
#     """Raised when the connection is incorrect"""


import os
import pytest
from fnapy.connection import FnapyConnection
from fnapy.exceptions import FnapyConnectionError
    """FnapyConnection should be able to accept the keyword sandbox=False when env vars exist"""
    monkeypatch.setattr('fnapy.connection.get_credentials', lambda x: mock_get_env_vars(True))
    connection = FnapyConnection(sandbox=False)


def test_connection_with_unavailable_env_vars_for_sandbox(monkeypatch):
    """FnapyConnection should raise a FnapyConnectionError when sandbox=True and env vars don't exist"""
    monkeypatch.setattr('fnapy.connection.get_credentials', lambda x: mock_get_env_vars(False))
    with pytest.raises(FnapyConnectionError):
        connection = FnapyConnection(sandbox=True)


def test_connection_with_unavailable_env_vars_for_real_account(monkeypatch):
    """FnapyConnection should raise a FnapyConnectionError when sandbox=False and env vars don't exist"""
    monkeypatch.setattr('fnapy.connection.get_credentials', lambda x: mock_get_env_vars(False))
    with pytest.raises(FnapyConnectionError):
        connection = FnapyConnection(sandbox=False)


def test_connection_with_valid_keys_for_credentials():
    """FnapyConnection should be able to accept a dict with valid keys for the credentials"""
    credentials = {'partner_id': 'abc', 'shop_id': 'thebestshop',
                   'key': 'goldenkey', 'sandbox': False}
    connection = FnapyConnection(credentials=credentials)


def test_connection_with_invalid_keys_for_credentials():
    """FnapyConnection should raise a FnapyConnectionError if an invalid key is provided"""
    credentials = {'login': 'abc', 'shop_id': 'thebestshop',
                   'key': 'goldenkey', 'sandbox': True}

# Path: fnapy/connection.py
# class FnapyConnection(object):
#     """The connection class of fnapy
# 
#     Usage::
#         connection = FnapyConnection(credentials=credentials, sandbox=sandbox)
# 
#     Example:
# 
#     * Create a connection with a credentials dictionary::
# 
#         credentials = {'partner_id': 'my_partner_id', 'shop_id': 'my_shop_id',
#                        'key': 'my_key', 'sandbox': False}
# 
#     * Create a connection using the environment variables for a given account
#       type (the sandbox account is used if sandbox True. If sandbox is False, the
#       real account is used)::
# 
#         connection = FnapyConnection(sandbox=True)
# 
#     .. note:: You must have previously defined the following environment
#         variables:
# 
#     * for the sandbox: FNAC_SANDBOX_PARTNER_ID, FNAC_SANDBOX_SHOP_ID,
#         FNAC_SANDBOX_KEY
# 
#     * for the real account: FNAC_PARTNER_ID, FNAC_SHOP_ID, FNAC_KEY
# 
#     """
# 
#     def __init__(self, credentials={}, sandbox=None):
#         # credentials
#         if len(credentials) > 0:
#             expecteds = ("partner_id", "shop_id", "key", "sandbox")
#             for expected in expecteds:
#                 if credentials.get(expected) is None:
#                     msg = "You didn't provide the {}."
#                     msg += "You must provide the following keys in credentials: "
#                     msg += ", ".join(expecteds)
#                     raise FnapyConnectionError(msg.format(expected))
# 
#         # sandbox
#         else:
#             if sandbox is not None:
#                 credentials = get_credentials(sandbox)
#                 credentials.update({"sandbox": sandbox})
#             else:
#                 msg = "You must either specify credentials or sandbox as arguments."
#                 raise FnapyConnectionError(msg)
# 
#         check_credentials_validity(credentials)
#         self.partner_id = credentials["partner_id"]
#         self.shop_id = credentials["shop_id"]
#         self.key = credentials["key"]
#         self.sandbox = credentials["sandbox"]
# 
# Path: fnapy/exceptions.py
# class FnapyConnectionError(FnapyException):
#     """Raised when the connection is incorrect"""


# Path: tests/test_connection.py
import os
import pytest
from fnapy.connection import FnapyConnection
from fnapy.exceptions import FnapyConnectionError

    """FnapyConnection should be able to accept the keyword sandbox=False when env vars exist"""
    monkeypatch.setattr('fnapy.connection.get_credentials', lambda x: mock_get_env_vars(True))
    connection = FnapyConnection(sandbox=False)


def test_connection_with_unavailable_env_vars_for_sandbox(monkeypatch):
    """FnapyConnection should raise a FnapyConnectionError when sandbox=True and env vars don't exist"""
    monkeypatch.setattr('fnapy.connection.get_credentials', lambda x: mock_get_env_vars(False))
    with pytest.raises(FnapyConnectionError):
        connection = FnapyConnection(sandbox=True)


def test_connection_with_unavailable_env_vars_for_real_account(monkeypatch):
    """FnapyConnection should raise a FnapyConnectionError when sandbox=False and env vars don't exist"""
    monkeypatch.setattr('fnapy.connection.get_credentials', lambda x: mock_get_env_vars(False))
    with pytest.raises(FnapyConnectionError):
        connection = FnapyConnection(sandbox=False)


def test_connection_with_valid_keys_for_credentials():
    """FnapyConnection should be able to accept a dict with valid keys for the credentials"""
    credentials = {'partner_id': 'abc', 'shop_id': 'thebestshop',
                   'key': 'goldenkey', 'sandbox': False}
    connection = FnapyConnection(credentials=credentials)


def test_connection_with_invalid_keys_for_credentials():
    """FnapyConnection should raise a FnapyConnectionError if an invalid key is provided"""
    credentials = {'login': 'abc', 'shop_id': 'thebestshop',
                   'key': 'goldenkey', 'sandbox': True}

    with pytest.raises(FnapyConnectionError):