def test_session_create(self):
     user = User.load_by_id(db.user.create("test"))
     token = Token.generate(user.api_key)
     token.approve(user.name)
     session = Session.create(token)
     self.assertIsInstance(session, Session)
     self.assertDictEqual(user.__dict__, session.user.__dict__)
 def test_session_create(self):
     user = User.load_by_id(db.user.create("test"))
     token = Token.generate(user.api_key)
     token.approve(user.name)
     session = Session.create(token)
     self.assertIsInstance(session, Session)
     self.assertDictEqual(user.__dict__, session.user.__dict__)
Ejemplo n.º 3
0
def get_session(request, data):
    """ Create new session after validating the API_key and token.
    """
    output_format = data.get('format', 'xml')
    try:
        api_key = data['api_key']
        token = Token.load(data['token'], api_key)
    except KeyError:
        raise InvalidAPIUsage(6, output_format=output_format)       # Missing Required Params

    if not token:
        if not Token.is_valid_api_key(api_key):
            raise InvalidAPIUsage(10, output_format=output_format)  # Invalid API_key
        raise InvalidAPIUsage(4, output_format=output_format)       # Invalid token
    if token.has_expired():
        raise InvalidAPIUsage(15, output_format=output_format)      # Token expired
    if not token.user:
        raise InvalidAPIUsage(14, output_format=output_format)      # Unauthorized token

    session = Session.create(token)

    doc, tag, text = Doc().tagtext()
    with tag('lfm', status='ok'):
        with tag('session'):
            with tag('name'):
                text(session.user.name)
            with tag('key'):
                text(session.sid)
            with tag('subscriber'):
                text('0')

    return format_response('<?xml version="1.0" encoding="utf-8"?>\n' + yattag.indent(doc.getvalue()),
                           data.get('format', "xml"))
    def test_session_load(self):
        user = User.load_by_id(db.user.create("test"))
        token = Token.generate(user.api_key)
        token.approve(user.name)
        session = Session.create(token)
        self.assertIsInstance(session, Session)
        self.assertDictEqual(user.__dict__, session.user.__dict__)
        session.user = None

        # Load with session_key + api_key
        session2 = Session.load(session.sid, session.api_key)
        self.assertDictEqual(user.__dict__, session2.__dict__['user'].__dict__)
        session2.user = None
        self.assertDictEqual(session.__dict__, session2.__dict__)
    def test_session_load(self):
        user = User.load_by_id(db.user.create("test"))
        token = Token.generate(user.api_key)
        token.approve(user.name)
        session = Session.create(token)
        self.assertIsInstance(session, Session)
        self.assertDictEqual(user.__dict__, session.user.__dict__)
        session.user = None

        # Load with session_key + api_key
        session2 = Session.load(session.sid, session.api_key)
        self.assertDictEqual(user.__dict__, session2.__dict__['user'].__dict__)
        session2.user = None
        self.assertDictEqual(session.__dict__, session2.__dict__)
Ejemplo n.º 6
0
def get_session(request, data):
    """ Create new session after validating the API_key and token.
    """
    output_format = data.get('format', 'xml')
    try:
        api_key = data['api_key']
        token = Token.load(data['token'], api_key)
    except KeyError:
        raise InvalidAPIUsage(
            CompatError.INVALID_PARAMETERS,
            output_format=output_format)  # Missing Required Params

    if not token:
        if not Token.is_valid_api_key(api_key):
            raise InvalidAPIUsage(
                CompatError.INVALID_API_KEY,
                output_format=output_format)  # Invalid API_key
        raise InvalidAPIUsage(CompatError.INVALID_TOKEN,
                              output_format=output_format)  # Invalid token
    if token.has_expired():
        raise InvalidAPIUsage(CompatError.TOKEN_EXPIRED,
                              output_format=output_format)  # Token expired
    if not token.user:
        raise InvalidAPIUsage(
            CompatError.UNAUTHORIZED_TOKEN,
            output_format=output_format)  # Unauthorized token

    session = Session.create(token)

    doc, tag, text = Doc().tagtext()
    with tag('lfm', status='ok'):
        with tag('session'):
            with tag('name'):
                text(session.user.name)
            with tag('key'):
                text(session.sid)
            with tag('subscriber'):
                text('0')

    return format_response(
        '<?xml version="1.0" encoding="utf-8"?>\n' +
        yattag.indent(doc.getvalue()), data.get('format', "xml"))