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_happy_path(self, mock_send_mail, mock_token_generator): mock_token_generator.return_value = 'foo-bar' expected_redirect = reverse('accounts:login') data = {'email': self.user.email} resp = self.client.post(self.url, data=data) self.assertRedirects(resp, expected_redirect) messages = get_messages_from_response(resp) self.assertIn( 'We have sent a password reset email to the address provided.', messages) email_context = { 'email': '*****@*****.**', 'domain': 'testserver', 'site_name': 'testserver', 'uid': urlsafe_base64_encode(force_bytes(self.user.pk)), 'user': self.user, 'token': mock_token_generator(), 'protocol': 'http' } mock_send_mail.assert_called_once_with( 'registration/password_reset_subject.txt', 'password_reset_email.html', email_context, None, # From email, defaults to DEFAULT_FROM_EMAIL self.user.email, html_email_template_name=None)
def test_unauthenticated_request_redirects_to_login_page(self): expected_redirect = reverse('accounts:login') resp = self.client.get(self.url) self.assertRedirects(resp, expected_redirect) messages = get_messages_from_response(resp) self.assertIn('Please login with your new password.', messages)
def test_authenticated_request_redirects_to_profile_page(self): user = MoodyUtil.create_user() self.client.login(username=user.username, password=MoodyUtil.DEFAULT_USER_PASSWORD) expected_redirect = reverse('accounts:profile') resp = self.client.get(self.url) self.assertRedirects(resp, expected_redirect) messages = get_messages_from_response(resp) self.assertIn('Your password has been updated!', messages)
def test_get_request_for_user_without_auth_is_redirected_to_profile_page( self): self.client.login(username=self.user_without_auth.username, password=MoodyUtil.DEFAULT_USER_PASSWORD) resp = self.client.get(self.url) messages = get_messages_from_response(resp) last_message = messages[-1] self.assertRedirects(resp, self.redirect_url) self.assertEqual(last_message, 'You have not authorized MoodyTunes with Spotify')
def test_requests_are_rate_limited_after_max_requests_processed( self, mock_is_ratelimited): mock_is_ratelimited.return_value = True data = {'code': MoodyUtil._generate_song_code()} resp = self.client.post(self.url, data) messages = get_messages_from_response(resp) last_message = messages[-1] self.assertEqual( last_message, 'You have submitted too many suggestions! Try again in a minute')
def test_post_request_with_invalid_emotion_displays_error(self): data = { 'playlist_name': self.test_playlist_name, 'emotion': 'bad-value' } 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)
def test_post_request_for_empty_emotion_playlist_displays_error(self): data = { 'playlist_name': self.test_playlist_name, 'emotion': self.emotion.name } resp = self.client.post(self.url, data) messages = get_messages_from_response(resp) last_message = messages[-1] msg = 'Your {} playlist is empty! Try adding some songs to export the playlist'.format( self.emotion.full_name.lower()) self.assertEqual(last_message, msg)
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)
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 test_spotify_error_fetching_tokens_redirects_to_error_page( self, mock_spotify): spotify_client = mock.Mock() spotify_client.get_access_and_refresh_tokens.side_effect = SpotifyException 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, 'We were unable to retrieve your Spotify profile. Please try again.' )
def test_post_with_invalid_image_upload_displays_error(self): song = MoodyUtil.create_song() MoodyUtil.create_user_song_vote(self.user, song, self.emotion, True) with open( '{}/apps/spotify/tests/fixtures/hack.php'.format( settings.BASE_DIR), 'rb') as hack_file: hack = BytesIO(hack_file.read()) hack.name = 'hack.php' data = { 'playlist_name': self.test_playlist_name, 'emotion': self.emotion.name, 'cover_image': hack } 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)
def test_post_request_with_invalid_playlist_name_and_cover_image_displays_error( self): song = MoodyUtil.create_song() MoodyUtil.create_user_song_vote(self.user, song, self.emotion, True) with open( '{}/apps/spotify/tests/fixtures/cat.jpg'.format( settings.BASE_DIR), 'rb') as img_file: img = BytesIO(img_file.read()) img.name = 'my_cover.jpg' data = { 'playlist_name': 'test|timeout /T 15', 'emotion': self.emotion.name, 'cover_image': img } 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)