# Path: cliche/credentials.py
# class TwitterCredential(Credential):
#     """Information about Twitter User"""
# 
#     #: (:class:`int`) The primary key from :class:`Credential.id`.
#     id = Column(Integer, ForeignKey('credential.id'), primary_key=True)
# 
#     #: (:class:`int`) Twitter user id
#     identifier = Column(BigInteger, nullable=False, unique=True)
# 
#     #: (:class:`str`) The oauth token.
#     token = Column(String)
# 
#     #: (:class:`str`) The oauth secret token.
#     token_secret = Column(String)
# 
#     __tablename__ = 'twitter_credential'
#     __mapper_args__ = {'polymorphic_identity': 'twitter'}
#     __repr_columns__ = id, identifier
# 
# Path: cliche/web/social/oauth.py
# class Version(enum.Enum):
# class OAuthVendorConverter(BaseConverter):
# def login(vendor):
# def oauth_authorized(vendor):
# def make_account(credential_table, name, user_id):
#     def __init__(self, url_map):
#     def to_python(self, value):
#     def to_url(self, value):
# 
# Path: tests/web_utils.py
# def assert_contain_text(text, expr, data):
#     def traverse(elements):
#         for element in elements:
#             if text in element.text_content():
#                 return True
#         else:
#             return False
# 
#     tree = document_fromstring(str(data)).cssselect(expr)
#     assert tree
#     assert traverse(tree)
# 
# def get_url(endpoint, **kwargs):
#     url = None
#     with app.test_request_context():
#         url = url_for(endpoint, **kwargs)
#     return url


import datetime
from cliche.credentials import TwitterCredential
from cliche.user import User
from cliche.web.social.oauth import Vendor, Version
from ...web_utils import assert_contain_text, get_url


def test_authorize_who_logged(fx_session, fx_flask_client, fx_twitter_config):
    user = User(name='cliche.io')
    with fx_session.begin():
        fx_session.add(user)

    with fx_flask_client.session_transaction() as sess:
        sess['logged_id'] = user.id
        sess['logged_time'] = datetime.datetime.utcnow()

    rv = fx_flask_client.get(get_url('oauth.oauth_authorized',
                                     vendor='twitter'))
    assert rv.status_code == 302


def test_twitter_authorize_failed(fx_flask_client,
                                  fx_twitter_config, monkeypatch):
    fake_vendors = [
        Vendor('twitter', TwitterCredential, Version.oauth1,
               FakeProvider(None), ('screen_name', 'user_id'))
    ]

    monkeypatch.setattr('cliche.web.social.oauth.vendors', fake_vendors)

    rv = fx_flask_client.get(get_url('oauth.oauth_authorized',
                                     vendor='twitter'))
    assert rv.status_code == 302

    rv = fx_flask_client.get(get_url('index'))

# Path: cliche/credentials.py
# class TwitterCredential(Credential):
#     """Information about Twitter User"""
# 
#     #: (:class:`int`) The primary key from :class:`Credential.id`.
#     id = Column(Integer, ForeignKey('credential.id'), primary_key=True)
# 
#     #: (:class:`int`) Twitter user id
#     identifier = Column(BigInteger, nullable=False, unique=True)
# 
#     #: (:class:`str`) The oauth token.
#     token = Column(String)
# 
#     #: (:class:`str`) The oauth secret token.
#     token_secret = Column(String)
# 
#     __tablename__ = 'twitter_credential'
#     __mapper_args__ = {'polymorphic_identity': 'twitter'}
#     __repr_columns__ = id, identifier
# 
# Path: cliche/web/social/oauth.py
# class Version(enum.Enum):
# class OAuthVendorConverter(BaseConverter):
# def login(vendor):
# def oauth_authorized(vendor):
# def make_account(credential_table, name, user_id):
#     def __init__(self, url_map):
#     def to_python(self, value):
#     def to_url(self, value):
# 
# Path: tests/web_utils.py
# def assert_contain_text(text, expr, data):
#     def traverse(elements):
#         for element in elements:
#             if text in element.text_content():
#                 return True
#         else:
#             return False
# 
#     tree = document_fromstring(str(data)).cssselect(expr)
#     assert tree
#     assert traverse(tree)
# 
# def get_url(endpoint, **kwargs):
#     url = None
#     with app.test_request_context():
#         url = url_for(endpoint, **kwargs)
#     return url


# Path: tests/web/social/oauth_test.py
import datetime
from cliche.credentials import TwitterCredential
from cliche.user import User
from cliche.web.social.oauth import Vendor, Version
from ...web_utils import assert_contain_text, get_url



def test_authorize_who_logged(fx_session, fx_flask_client, fx_twitter_config):
    user = User(name='cliche.io')
    with fx_session.begin():
        fx_session.add(user)

    with fx_flask_client.session_transaction() as sess:
        sess['logged_id'] = user.id
        sess['logged_time'] = datetime.datetime.utcnow()

    rv = fx_flask_client.get(get_url('oauth.oauth_authorized',
                                     vendor='twitter'))
    assert rv.status_code == 302


def test_twitter_authorize_failed(fx_flask_client,
                                  fx_twitter_config, monkeypatch):
    fake_vendors = [
        Vendor('twitter', TwitterCredential, Version.oauth1,
               FakeProvider(None), ('screen_name', 'user_id'))
    ]

    monkeypatch.setattr('cliche.web.social.oauth.vendors', fake_vendors)

    rv = fx_flask_client.get(get_url('oauth.oauth_authorized',
                                     vendor='twitter'))
    assert rv.status_code == 302

    rv = fx_flask_client.get(get_url('index'))

    assert_contain_text('You denied the request to sign in.', 'ul.flash>li',