def test_exclude_downvoted_songs(self): MoodyUtil.create_user_song_vote(self.user, self.song, self.emotion, False) songs = ExportPlaylistHelper.get_export_playlist_for_user( self.user, self.emotion.name) self.assertNotIn(self.song.code, songs)
def test_happy_path(self): MoodyUtil.create_user_song_vote(self.user, self.song, self.emotion, True) songs = ExportPlaylistHelper.get_export_playlist_for_user( self.user, self.emotion.name) self.assertIn(self.song.code, songs)
def test_filter_playlist_by_genre(self): new_song = MoodyUtil.create_song(genre='super-dope') MoodyUtil.create_user_song_vote(user=self.user, song=self.song, emotion=self.emotion, vote=True) MoodyUtil.create_user_song_vote(user=self.user, song=new_song, emotion=self.emotion, vote=True) data = {'emotion': self.emotion.name, 'genre': new_song.genre} resp = self.client.get(self.url, data=data) resp_data = resp.json() queryset = UserSongVote.objects.filter(user=self.user, emotion=self.emotion, vote=True, song__genre=new_song.genre) votes_for_emotion_data = average(queryset, 'song__valence', 'song__energy', 'song__danceability') valence = votes_for_emotion_data['song__valence__avg'] * 100 energy = votes_for_emotion_data['song__energy__avg'] * 100 danceability = votes_for_emotion_data['song__danceability__avg'] * 100 self.assertEqual(resp.status_code, status.HTTP_200_OK) self.assertEqual(len(resp_data['results']), 1) self.assertEqual(resp_data['results'][0]['song']['code'], new_song.code) self.assertEqual(resp_data['valence'], valence) self.assertEqual(resp_data['energy'], energy) self.assertEqual(resp_data['danceability'], danceability)
def setUpTestData(cls): cls.song_1 = MoodyUtil.create_song(energy=.5, valence=.75, danceability=.5) cls.song_2 = MoodyUtil.create_song(energy=.65, valence=.45, danceability=.75)
def test_duplicate_attempts_with_different_moody_users_results_in_failure( self, mock_spotify): MoodyUtil.create_spotify_auth(user=self.other_user, access_token='test-access-token', refresh_token='test-refresh-token', spotify_user_id='test-user-id') spotify_client = mock.Mock() spotify_client.get_access_and_refresh_tokens.return_value = { 'access_token': 'test-access-token', 'refresh_token': 'test-refresh-token' } spotify_client.get_user_profile.return_value = {'id': 'test-user-id'} mock_spotify.return_value = spotify_client query_params = {'code': 'test-spotify-code', 'state': self.state} resp = self.client.get(self.url, data=query_params, follow=True) messages = get_messages_from_response(resp) last_message = messages[-1] self.assertRedirects(resp, self.failure_url) self.assertEqual( last_message, 'Spotify user test-user-id has already authorized MoodyTunes.')
def test_playlist_includes_songs_from_other_artists_if_top_artist_playlist_is_less_than_limit( self): top_artists = ['Madlib', 'MF DOOM', 'Surf Curse'] song_params = { 'energy': .5, 'valence': .75, 'danceability': .65, } top_artist_song = MoodyUtil.create_song(artist=top_artists[0], **song_params) # Create songs from other artists for _ in range(10): artist = ''.join( [random.choice(string.ascii_letters) for _ in range(10)]) MoodyUtil.create_song(artist=artist, **song_params) limit = 5 playlist = generate_browse_playlist(song_params['energy'], song_params['valence'], song_params['danceability'], jitter=0, top_artists=top_artists, limit=limit) self.assertEqual(len(playlist), limit) self.assertIn(top_artist_song, playlist)
def setUpTestData(cls): cls.user = MoodyUtil.create_user() cls.other_user = MoodyUtil.create_user(username='******') cls.url = reverse('spotify:spotify-auth-callback') cls.success_url = reverse('spotify:export') cls.failure_url = reverse('spotify:spotify-auth-failure') cls.state = 'state-key'
def test_top_artists_passed_returns_songs_by_top_artists(self): top_artists = ['Madlib', 'MF DOOM', 'Surf Curse'] song_params = { 'energy': .5, 'valence': .75, 'danceability': .65, } for artist in top_artists: MoodyUtil.create_song(artist=artist, **song_params) MoodyUtil.create_song(artist='Bum', **song_params) expected_playlist = list(Song.objects.filter(artist__in=top_artists)) playlist = generate_browse_playlist(song_params['energy'], song_params['valence'], song_params['danceability'], jitter=0, top_artists=top_artists) # Sort playlists for comparison as the playlist is shuffled playlist = list(playlist) expected_playlist.sort(key=lambda song: song.code) playlist.sort(key=lambda song: song.code) self.assertListEqual(expected_playlist, playlist)
def setUpTestData(cls): cls.user_with_auth = MoodyUtil.create_user(username='******') cls.user_without_auth = MoodyUtil.create_user( username='******') cls.spotify_auth = MoodyUtil.create_spotify_auth(cls.user_with_auth) cls.factory = RequestFactory()
def setUpTestData(cls): cls.emotion = Emotion.objects.get(name=Emotion.HAPPY) cls.test_playlist_name = 'test-playlist' cls.user = MoodyUtil.create_user() cls.user_with_no_auth = MoodyUtil.create_user(username='******') cls.url = reverse('spotify:export') cls.spotify_auth = MoodyUtil.create_spotify_auth(cls.user)
def setUpTestData(cls): cls.user_with_profile = MoodyUtil.create_user() cls.user_without_profile = MoodyUtil.create_user( username='******') cls.url = reverse('accounts:user-profile') cls.user_profile = MoodyUtil.create_user_profile( cls.user_with_profile, has_rejected_spotify_auth=False)
def test_retrieve_cached_playlist_returns_cached_playlist( self, mock_cache): MoodyUtil.create_song() playlist = Song.objects.all() mock_cache.get.return_value = playlist returned_playlist = self.manager.retrieve_cached_browse_playlist() self.assertEqual(playlist, returned_playlist)
def test_get_request_for_user_with_auth_displays_revoke_page(self): MoodyUtil.create_spotify_auth(self.user_with_auth) self.client.login(username=self.user_with_auth.username, password=MoodyUtil.DEFAULT_USER_PASSWORD) resp = self.client.get(self.url) self.assertEqual(resp.status_code, status.HTTP_200_OK) self.assertTemplateUsed(resp, 'revoke_spotify_auth.html')
def test_happy_path(self): song = MoodyUtil.create_song(valence=.5, energy=.75) outlier_song = MoodyUtil.create_song(valence=.25, energy=.30) playlist = generate_browse_playlist(song.energy, song.valence, song.danceability) self.assertIn(song, playlist) self.assertNotIn(outlier_song, playlist)
def setUpTestData(cls): cls.user = MoodyUtil.create_user() cls.auth = MoodyUtil.create_spotify_auth(cls.user) cls.playlist_name = 'new_playlist' cls.playlist_id = 'spotify:playlist:id' cls.songs = [] for i in range(30): cls.songs.append(MoodyUtil.create_song().code)
def test_playlist_respects_limit(self): for _ in range(10): MoodyUtil.create_song() params = {'emotion': Emotion.HAPPY, 'jitter': 0, 'limit': 5} resp = self.client.get(self.url, data=params) resp_data = resp.json()['results'] self.assertEqual(resp.status_code, status.HTTP_200_OK) self.assertEqual(len(resp_data), params['limit'])
def test_method_omits_empty_genre(self): song_with_genre = MoodyUtil.create_song(genre='hiphop') MoodyUtil.create_song(genre='') expected_choices = [ default_option[0], (song_with_genre.genre, song_with_genre.genre.capitalize()), ] choices = get_genre_choices() self.assertEqual(choices, expected_choices)
def test_downvoted_songs_are_not_returned(self): MoodyUtil.create_user_song_vote(user=self.user, song=self.song, emotion=self.emotion, vote=False) data = {'emotion': self.emotion.name} resp = self.client.get(self.url, data=data) resp_data = resp.json() self.assertEqual(resp.status_code, status.HTTP_200_OK) self.assertEqual(len(resp_data['results']), 0)
def test_duplicate_votes_for_song_returns_one_record_for_song(self): MoodyUtil.create_user_song_vote(self.user, self.song, self.emotion, True) MoodyUtil.create_user_song_vote(self.user, self.song, self.emotion, True, context='WORK') songs = ExportPlaylistHelper.get_export_playlist_for_user( self.user, self.emotion.name) self.assertEqual(len(songs), 1)
def test_method_returns_all_song_genres(self): hiphop_song = MoodyUtil.create_song(genre='hiphop') rock_song = MoodyUtil.create_song(genre='rock') expected_choices = [ default_option[0], (hiphop_song.genre, hiphop_song.genre.capitalize()), (rock_song.genre, rock_song.genre.capitalize()) ] choices = get_genre_choices() self.assertEqual(choices, expected_choices)
def test_filter_does_not_remove_different_songs(self): other_song = MoodyUtil.create_song() MoodyUtil.create_user_song_vote(self.user, self.song, self.emotion, True) MoodyUtil.create_user_song_vote(self.user, other_song, self.emotion, True) user_votes = UserSongVote.objects.filter(user=self.user, emotion=self.emotion) filtered_votes = filter_duplicate_votes_on_song_from_playlist( user_votes) self.assertEqual(filtered_votes.count(), 2)
def test_filter_by_genre(self): genre = 'hiphop' other_song = MoodyUtil.create_song(genre=genre) MoodyUtil.create_user_song_vote(self.user, self.song, self.emotion, True) MoodyUtil.create_user_song_vote(self.user, other_song, self.emotion, True) songs = ExportPlaylistHelper.get_export_playlist_for_user( self.user, self.emotion.name, genre=other_song.genre) self.assertIn(other_song.code, songs) self.assertNotIn(self.song.code, songs)
def test_cache_browse_playlist_calls_cache_with_expected_arguments( self, mock_cache): MoodyUtil.create_song() data = { 'emotion': 'HPY', 'context': 'WORK', 'description': '', 'playlist': Song.objects.all() } cache_key = 'browse:cached-playlist:{}'.format(self.user.username) self.manager.cache_browse_playlist(**data) mock_cache.set.assert_called_once_with( cache_key, data, settings.BROWSE_PLAYLIST_CACHE_TIMEOUT)
def test_filter_removes_duplicate_votes_on_song(self): MoodyUtil.create_user_song_vote(self.user, self.song, self.emotion, True) MoodyUtil.create_user_song_vote(self.user, self.song, self.emotion, True, context='WORK') user_votes = UserSongVote.objects.filter(user=self.user, emotion=self.emotion) filtered_votes = filter_duplicate_votes_on_song_from_playlist( user_votes) self.assertEqual(filtered_votes.count(), 1)
def test_limit_on_playlist(self): energy = .5 valence = .75 danceability = .65 for _ in range(10): MoodyUtil.create_song(energy=energy, valence=valence, danceability=danceability) playlist = generate_browse_playlist(energy, valence, danceability, limit=5) self.assertEqual(len(playlist), 5)
def test_happy_path(self): MoodyUtil.create_user_song_vote(self.user, self.song, self.emotion, True) MoodyUtil.create_user_song_vote(self.user, self.song, self.emotion, True, 'WORK') data = {'emotion': self.emotion.name, 'song_code': self.song.code} resp = self.client.get(self.url, data=data) resp_data = resp.json() expected_contexts = ['', 'WORK'] self.assertEqual(resp.status_code, status.HTTP_200_OK) self.assertEqual(resp_data['contexts'], expected_contexts)
def test_endpoint_returns_contexts_for_downvotes(self): MoodyUtil.create_user_song_vote(self.user, self.song, self.emotion, False, 'PARTY') MoodyUtil.create_user_song_vote(self.user, self.song, self.emotion, False, 'WORK') data = {'emotion': self.emotion.name, 'song_code': self.song.code} resp = self.client.get(self.url, data=data) resp_data = resp.json() expected_contexts = ['PARTY', 'WORK'] self.assertEqual(resp.status_code, status.HTTP_200_OK) self.assertEqual(resp_data['contexts'], expected_contexts)
def test_filter_on_genre(self): MoodyUtil.create_song() expected_song = MoodyUtil.create_song(genre='super-dope') params = { 'emotion': Emotion.HAPPY, 'jitter': 0, 'genre': expected_song.genre } resp = self.client.get(self.url, data=params) resp_data = resp.json()['results'] resp_song = resp_data[0] self.assertEqual(resp.status_code, status.HTTP_200_OK) self.assertEqual(resp_song['code'], expected_song.code)
def test_happy_path(self): user = MoodyUtil.create_user(email='*****@*****.**') uid = urlsafe_base64_encode(force_bytes(user.pk)) token = PasswordResetTokenGenerator().make_token(user) url = reverse('accounts:password-reset-confirm', kwargs={ 'uidb64': uid, 'token': token }) initial_resp = self.client.get(url) reset_url = initial_resp['Location'] expected_redirect = reverse('accounts:password-reset-complete') data = {'new_password1': 'password', 'new_password2': 'password'} resp = self.client.post(reset_url, data=data) self.assertRedirects(resp, expected_redirect, fetch_redirect_response=False) # Test password updated OK user.refresh_from_db() updated_user = authenticate(username=user.username, password=data['new_password1']) self.assertEqual(user.pk, updated_user.pk)
def test_post_request_with_invalid_playlist_name_displays_error(self): song = MoodyUtil.create_song() MoodyUtil.create_user_song_vote(self.user, song, self.emotion, True) data = { 'playlist_name': 'test|timeout /T 15', 'emotion': self.emotion.name, } resp = self.client.post(self.url, data) messages = get_messages_from_response(resp) last_message = messages[-1] msg = 'Please submit a valid request' self.assertEqual(last_message, msg)