コード例 #1
0
    def test_record_listen(self):
        """ Tests if listen is recorded correctly if valid information is provided. """

        token = Token.generate(self.lfm_user.api_key)
        token.approve(self.lfm_user.name)
        session = Session.create(token)

        timestamp = int(time.time())
        data = {
            'method': 'track.scrobble',
            'api_key': self.lfm_user.api_key,
            'sk': session.sid,
            'artist[0]': 'Kishore Kumar',
            'track[0]': 'Saamne Ye Kaun Aya',
            'album[0]': 'Jawani Diwani',
            'duration[0]': 300,
            'timestamp[0]': timestamp,
        }

        r = self.client.post(url_for('api_compat.api_methods'), data=data)
        self.assert200(r)

        response = xmltodict.parse(r.data)
        self.assertEqual(response['lfm']['@status'], 'ok')
        self.assertEqual(response['lfm']['scrobbles']['@accepted'], '1')

        # Check if listen reached the influx listenstore
        time.sleep(1)
        listens = self.ls.fetch_listens(self.lb_user['musicbrainz_id'], from_ts=timestamp-1)
        self.assertEqual(len(listens), 1)
コード例 #2
0
    def test_load(self):
        token = Token.generate(self.user.api_key)
        self.assertIsInstance(token, Token)
        self.assertIsNone(token.user)
        """ Before approving """
        # Load with token
        token1 = Token.load(token.token)
        self.assertIsNone(token1.user)
        self.assertDictEqual(token1.__dict__, token.__dict__)

        # Load with token & api_key
        token2 = Token.load(token.token, token.api_key)
        self.assertIsNone(token2.user)
        self.assertDictEqual(token2.__dict__, token.__dict__)

        token.approve(self.user.name)
        """ After approving the token """
        # Load with token
        token1 = Token.load(token.token)
        self.assertIsInstance(token1.user, User)
        self.assertDictEqual(token1.user.__dict__, token.user.__dict__)
        token_user = token.user
        token.user, token1.user = None, None
        self.assertDictEqual(token1.__dict__, token.__dict__)
        token.user = token_user

        # Load with token & api_key
        token2 = Token.load(token.token, token.api_key)
        self.assertIsInstance(token2.user, User)
        self.assertDictEqual(token2.user.__dict__, token.user.__dict__)
        token.user, token1.user = None, None
        self.assertDictEqual(token1.__dict__, token.__dict__)
コード例 #3
0
    def test_load(self):
        token = Token.generate(self.user.api_key)
        self.assertIsInstance(token, Token)
        self.assertIsNone(token.user)

        """ Before approving """
        # Load with token
        token1 = Token.load(token.token)
        self.assertIsNone(token1.user)
        self.assertDictEqual(token1.__dict__, token.__dict__)

        # Load with token & api_key
        token2 = Token.load(token.token, token.api_key)
        self.assertIsNone(token2.user)
        self.assertDictEqual(token2.__dict__, token.__dict__)

        token.approve(self.user.name)

        """ After approving the token """
        # Load with token
        token1 = Token.load(token.token)
        self.assertIsInstance(token1.user, User)
        self.assertDictEqual(token1.user.__dict__, token.user.__dict__)
        token_user = token.user
        token.user, token1.user = None, None
        self.assertDictEqual(token1.__dict__, token.__dict__)
        token.user = token_user

        # Load with token & api_key
        token2 = Token.load(token.token, token.api_key)
        self.assertIsInstance(token2.user, User)
        self.assertDictEqual(token2.user.__dict__, token.user.__dict__)
        token.user, token1.user = None, None
        self.assertDictEqual(token1.__dict__, token.__dict__)
コード例 #4
0
    def test_record_listen(self):
        """ Tests if listen is recorded correctly if valid information is provided. """

        token = Token.generate(self.lfm_user.api_key)
        token.approve(self.lfm_user.name)
        session = Session.create(token)

        timestamp = int(time.time())
        data = {
            'method': 'track.scrobble',
            'api_key': self.lfm_user.api_key,
            'sk': session.sid,
            'artist[0]': 'Kishore Kumar',
            'track[0]': 'Saamne Ye Kaun Aya',
            'album[0]': 'Jawani Diwani',
            'duration[0]': 300,
            'timestamp[0]': timestamp,
        }

        r = self.client.post(url_for('api_compat.api_methods'), data=data)
        self.assert200(r)

        response = xmltodict.parse(r.data)
        self.assertEqual(response['lfm']['@status'], 'ok')
        self.assertEqual(response['lfm']['scrobbles']['@accepted'], '1')

        # Check if listen reached the timescale listenstore
        time.sleep(2)
        listens, _, _ = self.ls.fetch_listens(self.lb_user['musicbrainz_id'],
                                              from_ts=timestamp - 1)
        self.assertEqual(len(listens), 1)
コード例 #5
0
    def test_record_listen_now_playing(self):
        """ Tests if listen of type 'nowplaying' is recorded correctly
            if valid information is provided.
        """

        token = Token.generate(self.lfm_user.api_key)
        token.approve(self.lfm_user.name)
        session = Session.create(token)

        data = {
            'method': 'track.updateNowPlaying',
            'api_key': self.lfm_user.api_key,
            'sk': session.sid,
            'artist[0]': 'Kishore Kumar',
            'track[0]': 'Saamne Ye Kaun Aya',
            'album[0]': 'Jawani Diwani',
            'duration[0]': 300,
            'timestamp[0]': int(time.time()),
        }

        r = self.client.post(url_for('api_compat.api_methods'), data=data)
        self.assert200(r)

        response = xmltodict.parse(r.data)
        self.assertEqual(response['lfm']['@status'], 'ok')
        self.assertIsNotNone(response['lfm']['nowplaying'])
コード例 #6
0
 def test_session_create(self):
     user = User.load_by_id(db_user.create(1, "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__)
コード例 #7
0
 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__)
コード例 #8
0
    def test_record_listen_now_playing(self):
        """ Tests if listen of type 'nowplaying' is recorded correctly
            if valid information is provided.
        """

        token = Token.generate(self.lfm_user.api_key)
        token.approve(self.lfm_user.name)
        session = Session.create(token)

        data = {
            'method': 'track.updateNowPlaying',
            'api_key': self.lfm_user.api_key,
            'sk': session.sid,
            'artist[0]': 'Kishore Kumar',
            'track[0]': 'Saamne Ye Kaun Aya',
            'album[0]': 'Jawani Diwani',
            'duration[0]': 300,
            'timestamp[0]': int(time.time()),
        }

        r = self.client.post(url_for('api_compat.api_methods'), data=data)
        self.assert200(r)

        response = xmltodict.parse(r.data)
        self.assertEqual(response['lfm']['@status'], 'ok')
        self.assertIsNotNone(response['lfm']['nowplaying'])
コード例 #9
0
    def test_user_getinfo_no_listenstore(self):
        """If this listenstore is unavailable, performing a query that gets user information
           (touches the listenstore for user count) should return an error message in the
           requested format"""
        timescale_connection._ts = None

        token = Token.generate(self.lfm_user.api_key)
        token.approve(self.lfm_user.name)
        session = Session.create(token)

        data = {
            'method': 'user.getInfo',
            'api_key': self.lfm_user.api_key,
            'sk': session.sid
        }

        r = self.client.post(url_for('api_compat.api_methods'), data=data)
        self.assert200(r)
        self.assertEqual(r.headers["Content-type"],
                         "application/xml; charset=utf-8")

        expected_message = b"""<?xml version="1.0" encoding="utf-8"?>
<lfm status="failed">
  <error code="16">The service is temporarily unavailable, please try again.</error>
</lfm>"""
        assert r.data == expected_message
コード例 #10
0
 def test_has_expired(self):
     token = Token.generate(str(uuid.uuid4()))
     self.assertFalse(token.has_expired())
     token.timestamp = token.timestamp - timedelta(minutes=TOKEN_EXPIRATION_TIME - 1)
     # This is asssertFalse because in the next 1 minute the next statement will get executed
     self.assertFalse(token.has_expired())
     token.timestamp = token.timestamp - timedelta(minutes=1)
     self.assertTrue(token.has_expired())
コード例 #11
0
 def test_has_expired(self):
     token = Token.generate(str(uuid.uuid4()))
     self.assertFalse(token.has_expired())
     token.timestamp = token.timestamp - timedelta(
         minutes=TOKEN_EXPIRATION_TIME - 1)
     # This is asssertFalse because in the next 1 minute the next statement will get executed
     self.assertFalse(token.has_expired())
     token.timestamp = token.timestamp - timedelta(minutes=1)
     self.assertTrue(token.has_expired())
コード例 #12
0
    def test_session_load(self):
        user = User.load_by_id(db_user.create(1, "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)
        self.assertDictEqual(user.__dict__, session2.__dict__['user'].__dict__)
        session2.user = None
        self.assertDictEqual(session.__dict__, session2.__dict__)
コード例 #13
0
    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__)
コード例 #14
0
    def test_approve(self):
        token = Token.generate(str(uuid.uuid4()))
        self.assertIsInstance(token, Token)
        self.assertIsNone(token.user)
        before_token = token.__dict__
        before_token.pop('user')

        token.approve(self.user.name)

        after_token = token.__dict__
        self.assertIsInstance(token.user, User)
        self.assertDictEqual(token.user.__dict__, self.user.__dict__)
        after_token.pop('user')

        self.assertDictEqual(after_token, before_token)
コード例 #15
0
    def test_approve(self):
        token = Token.generate(str(uuid.uuid4()))
        self.assertIsInstance(token, Token)
        self.assertIsNone(token.user)
        before_token = token.__dict__
        before_token.pop('user')

        token.approve(self.user.name)

        after_token = token.__dict__
        self.assertIsInstance(token.user, User)
        self.assertDictEqual(token.user.__dict__, self.user.__dict__)
        after_token.pop('user')

        self.assertDictEqual(after_token, before_token)
コード例 #16
0
def get_token(request, data):
    """ Issue a token to user after verying his API_KEY
    """
    output_format = data.get('format', 'xml')
    api_key = data.get('api_key')

    if not api_key:
        raise InvalidAPIUsage(CompatError.INVALID_PARAMETERS, output_format=output_format)   # Missing required params
    if not Token.is_valid_api_key(api_key):
        raise InvalidAPIUsage(CompatError.INVALID_API_KEY, output_format=output_format)      # Invalid API_KEY

    token = Token.generate(api_key)

    doc, tag, text = Doc().tagtext()
    with tag('lfm', status='ok'):
        with tag('token'):
            text(token.token)
    return format_response('<?xml version="1.0" encoding="utf-8"?>\n' + yattag.indent(doc.getvalue()),
                           output_format)
コード例 #17
0
    def test_get_session(self):
        """ Tests if the session key is valid and session is established correctly. """

        token = Token.generate(self.lfm_user.api_key)
        token.approve(self.lfm_user.name)

        data = {
            'method': 'auth.getsession',
            'api_key': self.lfm_user.api_key,
            'token': token.token,
        }
        r = self.client.post(url_for('api_compat.api_methods'), data=data)
        self.assert200(r)

        response = xmltodict.parse(r.data)
        self.assertEqual(response['lfm']['@status'], 'ok')
        self.assertEqual(response['lfm']['session']['name'], self.lfm_user.name)

        session_key = Session.load(response['lfm']['session']['key'])
        self.assertIsNotNone(session_key)
コード例 #18
0
    def test_get_session(self):
        """ Tests if the session key is valid and session is established correctly. """

        token = Token.generate(self.lfm_user.api_key)
        token.approve(self.lfm_user.name)

        data = {
            'method': 'auth.getsession',
            'api_key': self.lfm_user.api_key,
            'token': token.token,
        }
        r = self.client.post(url_for('api_compat.api_methods'), data=data)
        self.assert200(r)

        response = xmltodict.parse(r.data)
        self.assertEqual(response['lfm']['@status'], 'ok')
        self.assertEqual(response['lfm']['session']['name'],
                         self.lfm_user.name)

        session_key = Session.load(response['lfm']['session']['key'])
        self.assertIsNotNone(session_key)
コード例 #19
0
    def test_record_invalid_listen(self):
        """ Tests that error is raised if submited data contains unicode null """
        token = Token.generate(self.lfm_user.api_key)
        token.approve(self.lfm_user.name)
        session = Session.create(token)

        timestamp = int(time.time())
        data = {
            'method': 'track.scrobble',
            'api_key': self.lfm_user.api_key,
            'sk': session.sid,
            'artist[0]': '\u0000Kishore Kumar',
            'track[0]': 'Saamne Ye Kaun Aya',
            'album[0]': 'Jawani Diwani',
            'duration[0]': 300,
            'timestamp[0]': timestamp,
        }

        r = self.client.post(url_for('api_compat.api_methods'), data=data)
        self.assert400(r)
        self.assertEqual(r.json["error"],
                         "\u0000Kishore Kumar contains a unicode null")
コード例 #20
0
def get_token(request, data):
    """ Issue a token to user after verying his API_KEY
    """
    output_format = data.get('format', 'xml')
    api_key = data.get('api_key')

    if not api_key:
        raise InvalidAPIUsage(
            CompatError.INVALID_PARAMETERS,
            output_format=output_format)  # Missing required params
    if not Token.is_valid_api_key(api_key):
        raise InvalidAPIUsage(CompatError.INVALID_API_KEY,
                              output_format=output_format)  # Invalid API_KEY

    token = Token.generate(api_key)

    doc, tag, text = Doc().tagtext()
    with tag('lfm', status='ok'):
        with tag('token'):
            text(token.token)
    return format_response(
        '<?xml version="1.0" encoding="utf-8"?>\n' +
        yattag.indent(doc.getvalue()), output_format)
コード例 #21
0
 def test_generate(self):
     token = Token.generate(str(uuid.uuid4()))
     self.assertIsInstance(token, Token)
コード例 #22
0
 def test_generate(self):
     token = Token.generate(str(uuid.uuid4()))
     self.assertIsInstance(token, Token)