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_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, mock_update_top_artist_task): user_1 = MoodyUtil.create_user(username='******') user_2 = MoodyUtil.create_user(username='******') MoodyUtil.create_spotify_auth(user_1, spotify_user_id='test_user_1') MoodyUtil.create_spotify_auth(user_2, spotify_user_id='test_user_2') RefreshTopArtistsFromSpotifyTask().run() self.assertEqual(mock_update_top_artist_task.call_count, 2)
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 test_context_sets_show_spotify_auth_to_false_for_existing_spotify_auth_record( self): MoodyUtil.create_spotify_auth(self.user) data = { 'username': self.user.username, 'password': MoodyUtil.DEFAULT_USER_PASSWORD } resp = self.client.post(self.url, data=data) self.assertRedirects( resp, f'{settings.LOGIN_REDIRECT_URL}?show_spotify_auth=False') # Ensure UserProfile record is updated to indicate user has already authenticated with Spotify self.user.userprofile.refresh_from_db() self.assertTrue(self.user.userprofile.has_rejected_spotify_auth)
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 test_post_request_for_user_with_auth_deletes_spotify_data(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.post(self.url) self.assertFalse( SpotifyAuth.objects.filter(user=self.user_with_auth).exists()) self.assertFalse( SpotifyUserData.objects.filter( spotify_auth__user=self.user_with_auth).exists()) messages = get_messages_from_response(resp) last_message = messages[-1] self.assertRedirects(resp, self.redirect_url) self.assertEqual(last_message, 'We have deleted your Spotify data from Moodytunes')
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_duplicate_attempts_for_same_moody_user_results_in_success( self, mock_spotify): MoodyUtil.create_spotify_auth(user=self.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) self.assertRedirects(resp, self.success_url) self.assertEqual(SpotifyAuth.objects.filter(user=self.user).count(), 1)
def test_browse_request_uses_user_top_artists_when_provided( self, mock_generate_playlist): top_artists = ['Madlib', 'MF DOOM', 'Surf Curse'] auth = MoodyUtil.create_spotify_auth(self.user) SpotifyUserData.objects.create(spotify_auth=auth, top_artists=top_artists) params = {'emotion': Emotion.HAPPY} self.client.get(self.url, data=params) call_kwargs = mock_generate_playlist.mock_calls[0][2] called_top_artists = call_kwargs['top_artists'] self.assertListEqual(called_top_artists, top_artists)
def setUpTestData(cls): cls.user = MoodyUtil.create_user() cls.auth = MoodyUtil.create_spotify_auth(cls.user)