def test_submit_listen(self): """ Sends a valid listen after handshaking and checks if it is present in the listenstore """ timestamp = int(time.time()) audioscrobbler_auth_token = _get_audioscrobbler_auth_token(self.user['auth_token'], timestamp) r = self.handshake(self.user['musicbrainz_id'], audioscrobbler_auth_token, timestamp) self.assert200(r) response = r.data.decode('utf-8').split('\n') self.assertEqual(response[0], 'OK') sid = response[1] data = { 's': sid, 'a[0]': 'Kishore Kumar', 't[0]': 'Saamne Ye Kaun Aya', 'o[0]': 'P', 'l[0]': 300, 'b[0]': 'Jawani Diwani', 'i[0]': int(time.time()), } r = self.client.post(url_for('api_compat_old.submit_listens'), data=data) self.assert200(r) self.assertEqual(r.data.decode('utf-8'), 'OK\n') time.sleep(1) recalculate_all_user_data() to_ts = int(time.time()) listens, _, _ = self.ls.fetch_listens(self.user, to_ts=to_ts) self.assertEqual(len(listens), 1)
def test_playing_now(self): """ Tests playing now notifications """ timestamp = int(time.time()) audioscrobbler_auth_token = _get_audioscrobbler_auth_token( self.user['auth_token'], timestamp) r = self.handshake(self.user['musicbrainz_id'], audioscrobbler_auth_token, timestamp) self.assert200(r) response = r.data.decode('utf-8').split('\n') self.assertEqual(response[0], 'OK') sid = response[1] data = { 's': sid, 'a': 'Kishore Kumar', 't': 'Saamne Ye Kaun Aya', 'b': 'Jawani Diwani', } r = self.client.post(url_for('api_compat_old.submit_now_playing'), data=data) self.assert200(r) self.assertEqual(r.data.decode('utf-8'), 'OK\n')
def test_submit_listen(self): """ Sends a valid listen after handshaking and checks if it is present in the listenstore """ timestamp = int(time.time()) audioscrobbler_auth_token = _get_audioscrobbler_auth_token(self.user['auth_token'], timestamp) r = self.handshake(self.user['musicbrainz_id'], audioscrobbler_auth_token, timestamp) self.assert200(r) response = r.data.decode('utf-8').split('\n') self.assertEqual(response[0], 'OK') sid = response[1] data = { 's': sid, 'a[0]': 'Kishore Kumar', 't[0]': 'Saamne Ye Kaun Aya', 'o[0]': 'P', 'l[0]': 300, 'b[0]': 'Jawani Diwani', 'i[0]': int(time.time()), } r = self.client.post(url_for('api_compat_old.submit_listens'), data=data) self.assert200(r) self.assertEqual(r.data.decode('utf-8'), 'OK\n') time.sleep(1) to_ts = int(time.time()) listens = self.ls.fetch_listens(self.user['musicbrainz_id'], to_ts=to_ts) self.assertEqual(len(listens), 1)
def test_submit_listen_invalid_data(self): """ Tests endpoint for 400 Bad Request if invalid data is sent """ timestamp = int(time.time()) audioscrobbler_auth_token = _get_audioscrobbler_auth_token( self.user['auth_token'], timestamp) r = self.handshake(self.user['musicbrainz_id'], audioscrobbler_auth_token, timestamp) self.assert200(r) response = r.data.decode('utf-8').split('\n') self.assertEqual(response[0], 'OK') sid = response[1] # no artist in data data = { 's': sid, 't[0]': 'Saamne Ye Kaun Aya', 'o[0]': 'P', 'l[0]': 300, 'b[0]': 'Jawani Diwani', 'i[0]': int(time.time()), } r = self.client.post(url_for('api_compat_old.submit_listens'), data=data) self.assert400(r) self.assertEqual(r.data.decode('utf-8').split()[0], 'FAILED') # add artist and remove track name data['a[0]'] = 'Kishore Kumar' del data['t[0]'] r = self.client.post(url_for('api_compat_old.submit_listens'), data=data) self.assert400(r) self.assertEqual(r.data.decode('utf-8').split()[0], 'FAILED') # add track name and remove timestamp data['t[0]'] = 'Saamne Ye Kaun Aya' del data['i[0]'] r = self.client.post(url_for('api_compat_old.submit_listens'), data=data) self.assert400(r) self.assertEqual(r.data.decode('utf-8').split()[0], 'FAILED') # re-add a timestamp in ns data['i[0]'] = int(time.time()) * 10**9 r = self.client.post(url_for('api_compat_old.submit_listens'), data=data) self.assert400(r) self.assertEqual(r.data.decode('utf-8').split()[0], 'FAILED')
def test_handshake(self): """ Tests handshake for a user that exists """ timestamp = int(time.time()) audioscrobbler_auth_token = _get_audioscrobbler_auth_token(self.user['auth_token'], timestamp) r = self.handshake(self.user['musicbrainz_id'], audioscrobbler_auth_token, timestamp) self.assert200(r) response = r.data.decode('utf-8').split('\n') self.assertEqual(len(response), 5) self.assertEqual(response[0], 'OK') self.assertEqual(len(response[1]), 32)
def test_handshake(self): """ Tests handshake for a user that exists """ timestamp = int(time.time()) audioscrobbler_auth_token = _get_audioscrobbler_auth_token(self.user['auth_token'], timestamp) r = self.handshake(self.user['musicbrainz_id'], audioscrobbler_auth_token, timestamp) self.assert200(r) response = r.data.decode('utf-8').split('\n') self.assertEqual(len(response), 5) self.assertEqual(response[0], 'OK') self.assertEqual(len(response[1]), 32)
def test_submit_listen_invalid_data(self): """ Tests endpoint for 400 Bad Request if invalid data is sent """ timestamp = int(time.time()) audioscrobbler_auth_token = _get_audioscrobbler_auth_token(self.user['auth_token'], timestamp) r = self.handshake(self.user['musicbrainz_id'], audioscrobbler_auth_token, timestamp) self.assert200(r) response = r.data.decode('utf-8').split('\n') self.assertEqual(response[0], 'OK') sid = response[1] # no artist in data data = { 's': sid, 't[0]': 'Saamne Ye Kaun Aya', 'o[0]': 'P', 'l[0]': 300, 'b[0]': 'Jawani Diwani', 'i[0]': int(time.time()), } r = self.client.post(url_for('api_compat_old.submit_listens'), data=data) self.assert400(r) self.assertEqual(r.data.decode('utf-8').split()[0], 'FAILED') # add artist and remove track name data['a[0]'] = 'Kishore Kumar' del data['t[0]'] r = self.client.post(url_for('api_compat_old.submit_listens'), data=data) self.assert400(r) self.assertEqual(r.data.decode('utf-8').split()[0], 'FAILED') # add track name and remove timestamp data['t[0]'] = 'Saamne Ye Kaun Aya' del data['i[0]'] r = self.client.post(url_for('api_compat_old.submit_listens'), data=data) self.assert400(r) self.assertEqual(r.data.decode('utf-8').split()[0], 'FAILED') # re-add a timestamp in ns data['i[0]'] = int(time.time()) * 10**9 r = self.client.post(url_for('api_compat_old.submit_listens'), data=data) self.assert400(r) self.assertEqual(r.data.decode('utf-8').split()[0], 'FAILED')
def test_handshake_post(self): """ Tests POST requests to handshake endpoint """ ts = int(time.time()) args = { 'hs': 'true', 'p': '1.2', 'c': 'tst', 'v': '0.1', 'u': self.user['musicbrainz_id'], 't': ts, 'a': _get_audioscrobbler_auth_token(self.user['auth_token'], ts) } r = self.client.post('/', query_string=args) self.assert200(r) response = r.data.decode('utf-8').split('\n') self.assertEqual(len(response), 5) self.assertEqual(response[0], 'OK') self.assertEqual(len(response[1]), 32)
def test_handshake_post(self): """ Tests POST requests to handshake endpoint """ ts = int(time.time()) args = { 'hs': 'true', 'p': '1.2', 'c': 'tst', 'v': '0.1', 'u': self.user['musicbrainz_id'], 't': ts, 'a': _get_audioscrobbler_auth_token(self.user['auth_token'], ts) } r = self.client.post('/', query_string=args) self.assert200(r) response = r.data.decode('utf-8').split('\n') self.assertEqual(len(response), 5) self.assertEqual(response[0], 'OK') self.assertEqual(len(response[1]), 32)
def test_playing_now(self): """ Tests playing now notifications """ timestamp = int(time.time()) audioscrobbler_auth_token = _get_audioscrobbler_auth_token(self.user['auth_token'], timestamp) r = self.handshake(self.user['musicbrainz_id'], audioscrobbler_auth_token, timestamp) self.assert200(r) response = r.data.decode('utf-8').split('\n') self.assertEqual(response[0], 'OK') sid = response[1] data = { 's': sid, 'a': 'Kishore Kumar', 't': 'Saamne Ye Kaun Aya', 'b': 'Jawani Diwani', } r = self.client.post(url_for('api_compat_old.submit_now_playing'), data=data) self.assert200(r) self.assertEqual(r.data.decode('utf-8'), 'OK\n')