Skip to content

Commit

Permalink
Use mock requests in Soundcloud tests
Browse files Browse the repository at this point in the history
Soundcloud sometimes answers 403 for requests
  • Loading branch information
aleksihakli committed Jul 1, 2023
1 parent 4194b7c commit ef0ea0b
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 11 deletions.
2 changes: 1 addition & 1 deletion embed_video/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django import forms
from django.core.validators import URLValidator
from django.core.exceptions import ValidationError
from django.core.validators import URLValidator
from django.utils.safestring import mark_safe

from embed_video.backends import (
Expand Down
92 changes: 86 additions & 6 deletions embed_video/tests/backends/tests_soundcloud.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from json import dumps
from unittest import TestCase
from unittest.mock import patch

import requests
import requests_mock

from embed_video.backends import (
SoundCloudBackend,
Expand All @@ -12,15 +14,89 @@

class SoundCloudBackendTestCase(TestCase):
urls = (
("https://soundcloud.com/community/soundcloud-case-study-wildlife", "82244706"),
(
"https://soundcloud.com/community/soundcloud-case-study-wildlife",
"82244706",
"https://soundcloud.com/oembed?format=json&url=https%3A%2F%2Fsoundcloud.com%2Fcommunity%2Fsoundcloud-case-study-wildlife",
dumps(
{
"version": 1.0,
"type": "rich",
"provider_name": "SoundCloud",
"provider_url": "https://soundcloud.com",
"height": 400,
"width": "100%",
"title": "SoundCloud Case Study: Wildlife Control by SoundCloud Community",
"description": "Listen to how Wildlife Control makes the most of the SoundCloud platform, and it's API.",
"thumbnail_url": "https://i1.sndcdn.com/artworks-000042390403-ouou1g-t500x500.jpg",
"html": '<iframe width="100%" height="400" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?visual=true&url=https%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F82244706&show_artwork=true"></iframe>',
"author_name": "SoundCloud Community",
"author_url": "https://soundcloud.com/community",
}
),
),
(
"https://soundcloud.com/matej-roman/jaromir-nohavica-karel-plihal-mikymauz",
"7834701",
"https://soundcloud.com/oembed?format=json&url=https%3A%2F%2Fsoundcloud.com%2Fmatej-roman%2Fjaromir-nohavica-karel-plihal-mikymauz",
dumps(
{
"version": 1.0,
"type": "rich",
"provider_name": "SoundCloud",
"provider_url": "https://soundcloud.com",
"height": 400,
"width": "100%",
"title": "Jaromír Nohavica, Karel Plíhal - Mikymauz by Matěj Roman",
"description": "",
"thumbnail_url": "https://soundcloud.com/images/fb_placeholder.png",
"html": '<iframe width="100%" height="400" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?visual=true&url=https%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F7834701&show_artwork=true"></iframe>',
"author_name": "Matěj Roman",
"author_url": "https://soundcloud.com/matej-roman",
}
),
),
(
"https://soundcloud.com/beny97/sets/jaromir-nohavica-prazska",
"960591",
"https://soundcloud.com/oembed?format=json&url=https%3A%2F%2Fsoundcloud.com%2Fbeny97%2Fsets%2Fjaromir-nohavica-prazska",
dumps(
{
"version": 1.0,
"type": "rich",
"provider_name": "SoundCloud",
"provider_url": "https://soundcloud.com",
"height": 450,
"width": "100%",
"title": "Jaromir Nohavica - Prazska palena by beny97",
"description": "",
"thumbnail_url": "https://soundcloud.com/images/fb_placeholder.png",
"html": '<iframe width="100%" height="450" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?visual=true&url=https%3A%2F%2Fapi.soundcloud.com%2Fplaylists%2F960591&show_artwork=true"></iframe>',
"author_name": "beny97",
"author_url": "https://soundcloud.com/beny97",
}
),
),
("https://soundcloud.com/beny97/sets/jaromir-nohavica-prazska", "960591"),
(
"https://soundcloud.com/jet-silver/couleur-3-downtown-boogie-show-prove",
"257485194",
"https://soundcloud.com/oembed?format=json&url=https%3A%2F%2Fsoundcloud.com%2Fjet-silver%2Fcouleur-3-downtown-boogie-show-prove",
dumps(
{
"version": 1.0,
"type": "rich",
"provider_name": "SoundCloud",
"provider_url": "https://soundcloud.com",
"height": 400,
"width": "100%",
"title": "Couleur 3 - Downtown Boogie : Show & Prove by Jet Silver",
"description": "Show & Prove specially recorded for Downtown Boogie, the best hip-hop radio show in Switzerland, on Couleur 3 radio. Shout out to Vincz Lee, Jiggy Jones, Green Giant, Dynamike and Geos for having us one the ones and twos !",
"thumbnail_url": "https://i1.sndcdn.com/artworks-000156604478-47oo6y-t500x500.jpg",
"html": '<iframe width="100%" height="400" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?visual=true&url=https%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F257485194&show_artwork=true"></iframe>',
"author_name": "Jet Silver",
"author_url": "https://soundcloud.com/jet-silver",
}
),
),
)

Expand All @@ -44,13 +120,17 @@ def get_info(self):

def test_detect(self):
for url in self.urls:
backend = detect_backend(url[0])
self.assertIsInstance(backend, self.instance)
with requests_mock.Mocker() as m:
m.get(url[2], text=url[3])
backend = detect_backend(url[0])
self.assertIsInstance(backend, self.instance)

def test_code(self):
for url in self.urls:
backend = self.instance(url[0])
self.assertEqual(backend.code, url[1])
with requests_mock.Mocker() as m:
m.get(url[2], text=url[3])
backend = self.instance(url[0])
self.assertEqual(backend.code, url[1])

def test_width(self):
self.assertEqual(self.foo.width, 123)
Expand Down
36 changes: 32 additions & 4 deletions embed_video/tests/templatetags/tests_embed_video_tags.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import re
import urllib.parse as urlparse
from json import dumps
from unittest import TestCase
from unittest.mock import Mock, patch

import requests_mock
from django.http import HttpRequest
from django.template import TemplateSyntaxError
from django.template.base import Template
Expand Down Expand Up @@ -171,12 +173,38 @@ def test_tag_soundcloud(self):
{{ soundcloud.url }} {{ soundcloud.backend }}
{% endvideo %}
"""
self.assertRenderedTemplate(
template,
"https://w.soundcloud.com/player/?visual=true&amp;url=https%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F82244706&amp;show_artwork=true "
"SoundCloudBackend",

# Soundcloud backend generates the following embed URL and fetches the payload from it
# you can check the URL contents and update it as needed
url = "https://soundcloud.com/oembed?format=json&url=https%3A%2F%2Fsoundcloud.com%2Fcommunity%2Fsoundcloud-case-study-wildlife"
response = dumps(
{
"version": 1.0,
"type": "rich",
"provider_name": "SoundCloud",
"provider_url": "https://soundcloud.com",
"height": 400,
"width": "100%",
"title": "SoundCloud Case Study: Wildlife Control by SoundCloud Community",
"description": "Listen to how Wildlife Control makes the most of the SoundCloud platform, and it's API.",
"thumbnail_url": "https://i1.sndcdn.com/artworks-000042390403-ouou1g-t500x500.jpg",
"html": "<iframe "
'width="100%" height="400" scrolling="no" frameborder="no" '
'src="https://w.soundcloud.com/player/?visual=true&url=https%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F82244706&show_artwork=true"'
"></iframe>",
"author_name": "SoundCloud Community",
"author_url": "https://soundcloud.com/community",
}
)

with requests_mock.Mocker() as m:
m.get(url, text=response)
self.assertRenderedTemplate(
template,
"https://w.soundcloud.com/player/?visual=true&amp;url=https%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F82244706&amp;show_artwork=true "
"SoundCloudBackend",
)

@patch("embed_video.backends.EMBED_VIDEO_TIMEOUT", 0.000001)
@patch("urllib3.connectionpool.log")
@patch("embed_video.templatetags.embed_video_tags.logger")
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ deps =
dj42: django>=4.2,<4.3
djmain: https://github.com/django/django/archive/main.tar.gz
coverage
requests-mock
usedevelop = True
commands =
coverage run -m django test --settings=embed_video.tests.django_settings
Expand Down

0 comments on commit ef0ea0b

Please sign in to comment.