def test_pack_views_response_ok(self): user, packs, sounds = create_user_and_sounds(num_sounds=5, num_packs=1) for sound in sounds: sound.change_processing_state("OK") sound.change_moderation_state("OK") # Login so api returns session login based responses self.client.login(username=user.username, password='******') # 200 response on pack instance resp = self.client.get( reverse('apiv2-pack-instance', kwargs={'pk': packs[0].id})) self.assertEqual(resp.status_code, 200) # 200 response on pack instance sounds list resp = self.client.get( reverse('apiv2-pack-sound-list', kwargs={'pk': packs[0].id})) self.assertEqual(resp.status_code, 200) # 200 response on pack instance download # This test uses a https connection. resp = self.client.get(reverse('apiv2-pack-download', kwargs={'pk': packs[0].id}), secure=True) self.assertEqual(resp.status_code, 200)
def test_oauth2_response_ok(self): user, packs, sounds = create_user_and_sounds(num_sounds=5, num_packs=1) client = ApiV2Client.objects.create( user=user, description='', name='', url='', redirect_uri='https://freesound.org') # Login so api returns session login based responses self.client.login(username=user.username, password='******') # 200 response on Oauth2 authorize resp = self.client.post(reverse('oauth2_provider:authorize'), { 'client_id': client.id, 'response_type': 'code' }, secure=True) self.assertEqual(resp.status_code, 200) # 302 response on Oauth2 logout and authorize resp = self.client.post( reverse('oauth2_provider:logout_and_authorize'), {'client_id': client.id}, secure=True) self.assertEqual(resp.status_code, 302)
def test_cors_header(self): # Create App to login using token user, packs, sounds = create_user_and_sounds(num_sounds=5, num_packs=1) c = ApiV2Client(user=user, status='OK', redirect_uri="https://freesound.com", url="https://freesound.com", name="test") c.save() sound = sounds[0] sound.change_processing_state("OK") sound.change_moderation_state("OK") headers = { 'HTTP_AUTHORIZATION': 'Token %s' % c.key, 'HTTP_ORIGIN': 'https://www.google.com' } resp = self.client.options(reverse('apiv2-sound-instance', kwargs={'pk': sound.id}), secure=True, **headers) self.assertEqual(resp.status_code, 200) # Check if header is present self.assertEqual(resp['ACCESS-CONTROL-ALLOW-ORIGIN'], '*')
def test_oauth2_response_ok(self): user, packs, sounds = create_user_and_sounds(num_sounds=5, num_packs=1) client = ApiV2Client.objects.create(user=user, description='', name='', url='', redirect_uri='https://freesound.org') # Login so api returns session login based responses self.client.login(username=user.username, password='******') # 200 response on Oauth2 authorize resp = self.client.post(reverse('oauth2_provider:authorize'), {'client_id': client.id, 'response_type': 'code'}, secure=True) self.assertEqual(resp.status_code, 200) # 302 response on Oauth2 logout and authorize resp = self.client.post(reverse('oauth2_provider:logout_and_authorize'), {'client_id': client.id}, secure=True) self.assertEqual(resp.status_code, 302)
def test_pack_views_response_ok(self): user, packs, sounds = create_user_and_sounds(num_sounds=5, num_packs=1) for sound in sounds: sound.change_processing_state("OK") sound.change_moderation_state("OK") # Login so api returns session login based responses self.client.login(username=user.username, password='******') # 200 response on pack instance resp = self.client.get(reverse('apiv2-pack-instance', kwargs={'pk': packs[0].id})) self.assertEqual(resp.status_code, 200) # 200 response on pack instance sounds list resp = self.client.get(reverse('apiv2-pack-sound-list', kwargs={'pk': packs[0].id})) self.assertEqual(resp.status_code, 200)
def test_encoding(self): # Create App to login using token user, packs, sounds = create_user_and_sounds(num_sounds=5, num_packs=1) c = ApiV2Client(user=user, status='OK', redirect_uri="https://freesound.com", url="https://freesound.com", name="test") c.save() sound = sounds[0] sound.change_processing_state("OK") sound.change_moderation_state("OK") headers = { 'HTTP_AUTHORIZATION': 'Token %s' % c.key, } # make query that can't be decoded resp = self.client.options("/apiv2/search/text/?query=ambient&filter=tag:(rain%20OR%CAfe)", secure=True, **headers) self.assertEqual(resp.status_code, 200)
def test_basic_user_response_ok(self): user, packs, sounds = create_user_and_sounds(num_sounds=5, num_packs=1) # 200 response on register page resp = self.client.get(reverse('apiv2-registration'), secure=True) self.assertEqual(resp.status_code, 200) # 200 response on login page resp = self.client.get(reverse('api-login'), secure=True) self.assertEqual(resp.status_code, 200) self.client.login(username=user.username, password='******') # 200 response on keys page resp = self.client.get(reverse('apiv2-apply'), secure=True) self.assertEqual(resp.status_code, 200) # 302 response on logout page resp = self.client.get(reverse('api-logout'), secure=True) self.assertEqual(resp.status_code, 302)
def test_basic_user_response_ok(self): user, packs, sounds = create_user_and_sounds(num_sounds=5, num_packs=1) # 200 response on register page resp = self.client.get(reverse('apiv2-registration'), secure=True) self.assertEqual(resp.status_code, 200) # 200 response on login page resp = self.client.get(reverse('api-login'), secure=True) self.assertEqual(resp.status_code, 200) self.client.login(username=user.username, password='******') # 200 response on keys page resp = self.client.get(reverse('apiv2-apply'), secure=True) self.assertEqual(resp.status_code, 200) # 302 response on logout page resp = self.client.get(reverse('api-logout'), secure=True) self.assertEqual(resp.status_code, 302)
def test_cors_header(self): # Create App to login using token user, packs, sounds = create_user_and_sounds(num_sounds=5, num_packs=1) c = ApiV2Client(user=user, status='OK', redirect_uri="https://freesound.com", url="https://freesound.com", name="test") c.save() sound = sounds[0] sound.change_processing_state("OK") sound.change_moderation_state("OK") headers = { 'HTTP_AUTHORIZATION': 'Token %s' % c.key, 'HTTP_ORIGIN': 'https://www.google.com' } resp = self.client.options(reverse('apiv2-sound-instance', kwargs={'pk': sound.id}), secure=True, **headers) self.assertEqual(resp.status_code, 200) # Check if header is present self.assertEqual(resp['ACCESS-CONTROL-ALLOW-ORIGIN'], '*')
def test_encoding(self): # Create App to login using token user, packs, sounds = create_user_and_sounds(num_sounds=5, num_packs=1) c = ApiV2Client(user=user, status='OK', redirect_uri="https://freesound.com", url="https://freesound.com", name="test") c.save() sound = sounds[0] sound.change_processing_state("OK") sound.change_moderation_state("OK") headers = { 'HTTP_AUTHORIZATION': 'Token %s' % c.key, } # make query that can't be decoded resp = self.client.options( "/apiv2/search/text/?query=ambient&filter=tag:(rain%20OR%CAfe)", secure=True, **headers) self.assertEqual(resp.status_code, 200)
def test_report_count_statuses(self): # Create some initial data user, pp, ss = create_user_and_sounds(num_sounds=1, num_packs=1) pack = pp[0] sound = ss[0] sound.change_processing_state("OK") sound.change_moderation_state("OK") SoundRating.objects.create(sound=sound, user=user, rating=4) sound.refresh_from_db( ) # Refresh from db after methods that use F-expressions sound.add_comment(user=user, comment="testComment") sound.refresh_from_db( ) # Refresh from db after methods that use F-expressions forum = Forum.objects.create(name="testForum", name_slug="test_forum", description="test") thread = Thread.objects.create(forum=forum, title="testThread", author=user) Post.objects.create(author=user, body="testBody", thread=thread) Post.objects.create(author=user, body="testBody unnmoderated", thread=thread, moderation_state="NM") user.profile.refresh_from_db( ) # Refresh from db after methods that use F-expressions # Assert initial counts are ok self.assertEquals(user.profile.num_sounds, 1) self.assertEquals( user.profile.num_posts, 1 ) # Note that count is 1 because one of the posts is not moderated self.assertEquals(pack.num_sounds, 1) self.assertEquals(pack.num_downloads, 0) self.assertEquals(sound.num_ratings, 1) self.assertEquals(sound.avg_rating, 4) self.assertEquals(sound.num_comments, 1) self.assertEquals(sound.num_downloads, 0) # Run command and assert counts are still ok call_command('report_count_statuses') self.assertEquals(user.profile.num_sounds, 1) self.assertEquals(user.profile.num_posts, 1) self.assertEquals(pack.num_sounds, 1) self.assertEquals(pack.num_downloads, 0) self.assertEquals(sound.num_ratings, 1) self.assertEquals(sound.avg_rating, 4) self.assertEquals(sound.num_comments, 1) self.assertEquals(sound.num_downloads, 0) # Manually set the counts to something wrong user.profile.num_sounds = 21 user.profile.num_posts = 21 user.profile.save() pack.num_sounds = 21 pack.num_downloads = 21 pack.save() sound.num_ratings = 21 sound.avg_rating = 21 sound.num_comments = 21 sound.num_downloads = 21 sound.save() # Re-run command with -n and assert counts are still wrong call_command('report_count_statuses', '--no-changes') user.profile.refresh_from_db() sound.refresh_from_db() pack.refresh_from_db() self.assertNotEquals(user.profile.num_sounds, 1) self.assertNotEquals(user.profile.num_posts, 1) self.assertNotEquals(pack.num_sounds, 1) self.assertNotEquals(pack.num_downloads, 0) self.assertNotEquals(sound.num_ratings, 1) self.assertNotEquals(sound.avg_rating, 4) self.assertNotEquals(sound.num_comments, 1) self.assertNotEquals(sound.num_downloads, 0) # Re-run command with -d and assert that all counts are ok except for download counts call_command('report_count_statuses', '--skip-downloads') user.profile.refresh_from_db() sound.refresh_from_db() pack.refresh_from_db() self.assertEquals(user.profile.num_sounds, 1) self.assertEquals( user.profile.num_posts, 1) # Note this is still 1 as unmoderated posts do not count self.assertEquals(pack.num_sounds, 1) self.assertNotEquals(pack.num_downloads, 0) self.assertEquals(sound.num_ratings, 1) self.assertEquals(sound.avg_rating, 4) self.assertEquals(sound.num_comments, 1) self.assertNotEquals(sound.num_downloads, 0) # Re-run command with no options set and check that all counts are ok now call_command('report_count_statuses') user.profile.refresh_from_db() sound.refresh_from_db() pack.refresh_from_db() self.assertEquals(user.profile.num_sounds, 1) self.assertEquals(user.profile.num_posts, 1) self.assertEquals(pack.num_sounds, 1) self.assertEquals(pack.num_downloads, 0) self.assertEquals(sound.num_ratings, 1) self.assertEquals(sound.avg_rating, 4) self.assertEquals(sound.num_comments, 1) self.assertEquals(sound.num_downloads, 0)
def test_report_count_statuses(self): # Create some initial data user, pp, ss = create_user_and_sounds(num_sounds=1, num_packs=1) pack = pp[0] sound = ss[0] sound.change_processing_state("OK") sound.change_moderation_state("OK") SoundRating.objects.create(sound=sound, user=user, rating=4) sound.refresh_from_db() # Refresh from db after methods that use F-expressions sound.add_comment(user=user, comment="testComment") sound.refresh_from_db() # Refresh from db after methods that use F-expressions forum = Forum.objects.create(name="testForum", name_slug="test_forum", description="test") thread = Thread.objects.create(forum=forum, title="testThread", author=user) Post.objects.create(author=user, body="testBody", thread=thread) Post.objects.create(author=user, body="testBody unnmoderated", thread=thread, moderation_state="NM") user.profile.refresh_from_db() # Refresh from db after methods that use F-expressions # Assert initial counts are ok self.assertEquals(user.profile.num_sounds, 1) self.assertEquals(user.profile.num_posts, 1) # Note that count is 1 because one of the posts is not moderated self.assertEquals(pack.num_sounds, 1) self.assertEquals(pack.num_downloads, 0) self.assertEquals(sound.num_ratings, 1) self.assertEquals(sound.avg_rating, 4) self.assertEquals(sound.num_comments, 1) self.assertEquals(sound.num_downloads, 0) # Run command and assert counts are still ok call_command('report_count_statuses') self.assertEquals(user.profile.num_sounds, 1) self.assertEquals(user.profile.num_posts, 1) self.assertEquals(pack.num_sounds, 1) self.assertEquals(pack.num_downloads, 0) self.assertEquals(sound.num_ratings, 1) self.assertEquals(sound.avg_rating, 4) self.assertEquals(sound.num_comments, 1) self.assertEquals(sound.num_downloads, 0) # Manually set the counts to something wrong user.profile.num_sounds = 21 user.profile.num_posts = 21 user.profile.save() pack.num_sounds = 21 pack.num_downloads = 21 pack.save() sound.num_ratings = 21 sound.avg_rating = 21 sound.num_comments = 21 sound.num_downloads = 21 sound.save() # Re-run command with -n and assert counts are still wrong call_command('report_count_statuses', '--no-changes') user.profile.refresh_from_db() sound.refresh_from_db() pack.refresh_from_db() self.assertNotEquals(user.profile.num_sounds, 1) self.assertNotEquals(user.profile.num_posts, 1) self.assertNotEquals(pack.num_sounds, 1) self.assertNotEquals(pack.num_downloads, 0) self.assertNotEquals(sound.num_ratings, 1) self.assertNotEquals(sound.avg_rating, 4) self.assertNotEquals(sound.num_comments, 1) self.assertNotEquals(sound.num_downloads, 0) # Re-run command with -d and assert that all counts are ok except for download counts call_command('report_count_statuses', '--skip-downloads') user.profile.refresh_from_db() sound.refresh_from_db() pack.refresh_from_db() self.assertEquals(user.profile.num_sounds, 1) self.assertEquals(user.profile.num_posts, 1) # Note this is still 1 as unmoderated posts do not count self.assertEquals(pack.num_sounds, 1) self.assertNotEquals(pack.num_downloads, 0) self.assertEquals(sound.num_ratings, 1) self.assertEquals(sound.avg_rating, 4) self.assertEquals(sound.num_comments, 1) self.assertNotEquals(sound.num_downloads, 0) # Re-run command with no options set and check that all counts are ok now call_command('report_count_statuses') user.profile.refresh_from_db() sound.refresh_from_db() pack.refresh_from_db() self.assertEquals(user.profile.num_sounds, 1) self.assertEquals(user.profile.num_posts, 1) self.assertEquals(pack.num_sounds, 1) self.assertEquals(pack.num_downloads, 0) self.assertEquals(sound.num_ratings, 1) self.assertEquals(sound.avg_rating, 4) self.assertEquals(sound.num_comments, 1) self.assertEquals(sound.num_downloads, 0)