def test_get_active_users_to_process(self): db_user.create(2, 'newspotifyuser') db_spotify.create_spotify( user_id=2, user_token='token', refresh_token='refresh_token', token_expires_ts=int(time.time()), ) users = db_spotify.get_active_users_to_process() self.assertEqual(len(users), 2) self.assertEqual(users[0]['user_id'], 1) self.assertEqual(users[1]['user_id'], 2) # check order, the users should be sorted by latest_listened_at timestamp db_user.create(3, 'newnewspotifyuser') db_spotify.create_spotify( user_id=3, user_token='tokentoken', refresh_token='newrefresh_token', token_expires_ts=int(time.time()), ) t = int(time.time()) db_spotify.update_latest_listened_at(2, t + 20) db_spotify.update_latest_listened_at(1, t + 10) users = db_spotify.get_active_users_to_process() self.assertEqual(len(users), 3) self.assertEqual(users[0]['user_id'], 2) self.assertEqual(users[1]['user_id'], 1) self.assertEqual(users[2]['user_id'], 3) db_spotify.add_update_error(2, 'something broke') db_spotify.add_update_error(3, 'oops.') users = db_spotify.get_active_users_to_process() self.assertEqual(len(users), 1) self.assertEqual(users[0]['user_id'], 1)
def test_create_spotify(self): db_user.create(2, 'spotify') db_spotify.create_spotify( user_id=2, user_token='token', refresh_token='refresh_token', token_expires_ts=int(time.time()), ) token = db_spotify.get_token_for_user(2) self.assertEqual(token, 'token')
def setUp(self): super(SpotifyDatabaseTestCase, self).setUp() db_user.create(1, 'testspotifyuser') self.user = db_user.get(1) db_spotify.create_spotify( user_id=self.user['id'], user_token='token', refresh_token='refresh_token', token_expires_ts=int(time.time()), )
def test_delete_when_spotify_import_activated(self): user_id = db_user.create(11, 'kishore') user = db_user.get(user_id) self.assertIsNotNone(user) db_spotify.create_spotify(user_id, 'user token', 'refresh token', 0) db_user.delete(user_id) user = db_user.get(user_id) self.assertIsNone(user) token = db_spotify.get_token_for_user(user_id) self.assertIsNone(token)
def add_new_user(user_id, spot_access_token): """Create a spotify row for a user based on OAuth access tokens Args: user_id: A flask auth `current_user.id` spot_access_token: A spotipy access token from SpotifyOAuth.get_access_token """ access_token = spot_access_token['access_token'] refresh_token = spot_access_token['refresh_token'] expires_at = spot_access_token['expires_at'] db_spotify.create_spotify(user_id, access_token, refresh_token, expires_at)
def add_new_user(user_id, spot_access_token): """Create a spotify row for a user based on OAuth access tokens Args: user_id: A flask auth `current_user.id` spot_access_token: A spotipy access token from SpotifyOAuth.get_access_token """ access_token = spot_access_token['access_token'] refresh_token = spot_access_token['refresh_token'] expires_at = int(time.time()) + spot_access_token['expires_in'] permissions = spot_access_token['scope'] active = SPOTIFY_IMPORT_PERMISSIONS[0] in permissions and SPOTIFY_IMPORT_PERMISSIONS[1] in permissions db_spotify.create_spotify(user_id, access_token, refresh_token, expires_at, active, permissions)