import time
import vcr

import yaml

from mendeley import Mendeley
from mendeley.session import MendeleySession


class DummyStateGenerator(object):
    @staticmethod
    def generate_state():
        return 'state1234'


def load_config_from_file(filename):
    with open(filename) as f:
        return yaml.load(f)


def load_config():
    try:
        return load_config_from_file('test_config.yml')
    except IOError:
        return load_config_from_file('test_config.yml.default')


def configure_mendeley():
    config = load_config()
    return Mendeley(config['clientId'],
                    config['clientSecret'],
                    config['redirectUri'],
                    state_generator=DummyStateGenerator())


def get_user_session():
    config = load_config()
    mendeley = configure_mendeley()

    return MendeleySession(mendeley, config['accessToken'])


def get_client_credentials_session():
    config = load_config()
    mendeley = configure_mendeley()

    if config['recordMode'] == 'none':
        return MendeleySession(mendeley, config['accessToken'])
    else:
        return mendeley.start_client_credentials_flow().authenticate()


def cassette(path):
    config = load_config()
    return vcr.use_cassette(path, filter_headers=['authorization'], record_mode=config['recordMode'])


def sleep(seconds):
    config = load_config()

    if config['recordMode'] != 'none':
        time.sleep(seconds)

@@ -805,24 +805,76 @@
 e_mendeley()
+%0A    token = %7B'access_token': config%5B'accessToken'%5D%7D
 %0A%0A    return
@@ -900,37 +900,21 @@
 ndeley, 
-config%5B'accessT
+t
 oken
-'%5D
 )%0A%0A%0Adef 
@@ -1042,32 +1042,88 @@
 de'%5D == 'none':%0A
+        token = %7B'access_token': config%5B'accessToken'%5D%7D%0A
         return M
@@ -1147,37 +1147,21 @@
 ndeley, 
-config%5B'accessT
+t
 oken
-'%5D
 )%0A    el

Fix tests.