Skip to content

Commit

Permalink
Fix up search and image tests and add new oauth web client tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kingosticks committed Jun 1, 2017
1 parent 23c7d05 commit b7d95be
Show file tree
Hide file tree
Showing 5 changed files with 363 additions and 223 deletions.
2 changes: 1 addition & 1 deletion mopidy_spotify/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def _refresh_token(self):
raise Error('OAuth response returned error: %s %s' % (
data['error'], data.get('error_description', '')))
elif not data.get('access_token'):
raise Error('OAuth response missing access_token.' %
raise Error('OAuth response from %s missing access_token.' %
self._refresh_url)
elif data.get('token_type') != 'Bearer':
raise Error('OAuth response from %s has wrong token_type: %s' % (
Expand Down
23 changes: 21 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import spotify

from mopidy_spotify import backend, library
from mopidy_spotify import backend, library, web


@pytest.fixture
Expand All @@ -34,6 +34,8 @@ def config(tmpdir):
'search_artist_count': 10,
'search_track_count': 50,
'toplist_countries': ['GB', 'US'],
'client_id': 'abcd1234',
'client_secret': 'YWJjZDEyMzQ='
}
}

Expand Down Expand Up @@ -333,6 +335,16 @@ def web_track_mock(web_artist_mock, web_album_mock):
}


@pytest.fixture
def web_oauth_mock():
return {
'access_token': 'NgCXRK...MzYjw',
'token_type': 'Bearer',
'scope': 'user-read-private user-read-email',
'expires_in': 3600,
}


@pytest.fixture
def mopidy_artist_mock():
return models.Artist(
Expand All @@ -358,11 +370,18 @@ def session_mock():


@pytest.fixture
def backend_mock(session_mock, config):
def web_client_mock():
web_client_mock = mock.Mock(spec=web.OAuthClient)
return web_client_mock


@pytest.fixture
def backend_mock(session_mock, config, web_client_mock):
backend_mock = mock.Mock(spec=backend.SpotifyBackend)
backend_mock._config = config
backend_mock._session = session_mock
backend_mock._bitrate = 160
backend_mock._web_client = web_client_mock
return backend_mock


Expand Down
224 changes: 90 additions & 134 deletions tests/test_images.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
from __future__ import unicode_literals

import json

from mopidy import models

import pytest

import responses

import mopidy_spotify
from mopidy_spotify import images


Expand All @@ -18,55 +13,47 @@ def img_provider(provider):
return provider


@responses.activate
def test_get_artist_images(img_provider):
def test_get_artist_images(web_client_mock, img_provider):
uris = [
'spotify:artist:4FCGgZrVQtcbDFEap3OAb2',
'http://open.spotify.com/artist/0Nsz79ZcE8E4i3XZhCzZ1l',
]

responses.add(
responses.GET, 'https://api.spotify.com/v1/artists/',
body=json.dumps({
'artists': [
{
'id': '4FCGgZrVQtcbDFEap3OAb2',
'images': [
{
'height': 640,
'url': 'img://1/a',
'width': 640,
},
{
'height': 300,
'url': 'img://1/b',
'width': 300,
},
]
},
{
'id': '0Nsz79ZcE8E4i3XZhCzZ1l',
'images': [
{
'height': 64,
'url': 'img://2/a',
'width': 64
}
]
}
]
})
)
web_client_mock.get.return_value = {
'artists': [
{
'id': '4FCGgZrVQtcbDFEap3OAb2',
'images': [
{
'height': 640,
'url': 'img://1/a',
'width': 640,
},
{
'height': 300,
'url': 'img://1/b',
'width': 300,
},
]
},
{
'id': '0Nsz79ZcE8E4i3XZhCzZ1l',
'images': [
{
'height': 64,
'url': 'img://2/a',
'width': 64
}
]
}
]
}

result = img_provider.get_images(uris)

assert len(responses.calls) == 1
assert (
responses.calls[0].request.url ==
web_client_mock.get.assert_called_once_with(
'https://api.spotify.com/v1/artists/?ids='
'4FCGgZrVQtcbDFEap3OAb2,0Nsz79ZcE8E4i3XZhCzZ1l')
assert responses.calls[0].request.headers['User-Agent'].startswith(
'Mopidy-Spotify/%s' % mopidy_spotify.__version__)

assert len(result) == 2
assert sorted(result.keys()) == sorted(uris)
Expand All @@ -93,35 +80,29 @@ def test_get_artist_images(img_provider):
assert image2a.width == 64


@responses.activate
def test_get_album_images(img_provider):
def test_get_album_images(web_client_mock, img_provider):
uris = [
'http://play.spotify.com/album/1utFPuvgBHXzLJdqhCDOkg',
]

responses.add(
responses.GET, 'https://api.spotify.com/v1/albums/',
body=json.dumps({
'albums': [
{
'id': '1utFPuvgBHXzLJdqhCDOkg',
'images': [
{
'height': 640,
'url': 'img://1/a',
'width': 640,
}
]
}
]
})
)
web_client_mock.get.return_value = {
'albums': [
{
'id': '1utFPuvgBHXzLJdqhCDOkg',
'images': [
{
'height': 640,
'url': 'img://1/a',
'width': 640,
}
]
}
]
}

result = img_provider.get_images(uris)

assert len(responses.calls) == 1
assert (
responses.calls[0].request.url ==
web_client_mock.get.assert_called_once_with(
'https://api.spotify.com/v1/albums/?ids=1utFPuvgBHXzLJdqhCDOkg')

assert len(result) == 1
Expand All @@ -135,38 +116,32 @@ def test_get_album_images(img_provider):
assert image.width == 640


@responses.activate
def test_get_track_images(img_provider):
def test_get_track_images(web_client_mock, img_provider):
uris = [
'spotify:track:41shEpOKyyadtG6lDclooa',
]

responses.add(
responses.GET, 'https://api.spotify.com/v1/tracks/',
body=json.dumps({
'tracks': [
{
'id': '41shEpOKyyadtG6lDclooa',
'album': {
'uri': 'spotify:album:1utFPuvgBHXzLJdqhCDOkg',
'images': [
{
'height': 640,
'url': 'img://1/a',
'width': 640,
}
]
}
web_client_mock.get.return_value = {
'tracks': [
{
'id': '41shEpOKyyadtG6lDclooa',
'album': {
'uri': 'spotify:album:1utFPuvgBHXzLJdqhCDOkg',
'images': [
{
'height': 640,
'url': 'img://1/a',
'width': 640,
}
]
}
]
})
)
}
]
}

result = img_provider.get_images(uris)

assert len(responses.calls) == 1
assert (
responses.calls[0].request.url ==
web_client_mock.get.assert_called_once_with(
'https://api.spotify.com/v1/tracks/?ids=41shEpOKyyadtG6lDclooa')

assert len(result) == 1
Expand All @@ -180,54 +155,49 @@ def test_get_track_images(img_provider):
assert image.width == 640


@responses.activate
def test_results_are_cached(img_provider):
def test_results_are_cached(web_client_mock, img_provider):
uris = [
'spotify:track:41shEpOKyyadtG6lDclooa',
]

responses.add(
responses.GET, 'https://api.spotify.com/v1/tracks/',
body=json.dumps({
'tracks': [
{
'id': '41shEpOKyyadtG6lDclooa',
'album': {
'uri': 'spotify:album:1utFPuvgBHXzLJdqhCDOkg',
'images': [
{
'height': 640,
'url': 'img://1/a',
'width': 640,
}
]
}
web_client_mock.get.return_value = {
'tracks': [
{
'id': '41shEpOKyyadtG6lDclooa',
'album': {
'uri': 'spotify:album:1utFPuvgBHXzLJdqhCDOkg',
'images': [
{
'height': 640,
'url': 'img://1/a',
'width': 640,
}
]
}
]
})
)
}
]
}

result1 = img_provider.get_images(uris)
result2 = img_provider.get_images(uris)

assert len(responses.calls) == 1
web_client_mock.get.assert_called_once()
assert result1 == result2


@responses.activate
def test_max_50_ids_per_request(img_provider):
def test_max_50_ids_per_request(web_client_mock, img_provider):
uris = ['spotify:track:%d' % i for i in range(51)]

responses.add(responses.GET, '', body=json.dumps({}))
web_client_mock.get.return_value = {}

img_provider.get_images(uris)

assert len(responses.calls) == 2
assert web_client_mock.get.call_count == 2

request_url_1 = responses.calls[0].request.url
request_url_1 = web_client_mock.get.call_args_list[0][0][0]
assert request_url_1.endswith(','.join(str(i) for i in range(50)))

request_url_2 = responses.calls[1].request.url
request_url_2 = web_client_mock.get.call_args_list[1][0][0]
assert request_url_2.endswith('ids=50')


Expand All @@ -244,22 +214,8 @@ def test_no_uris_gives_no_results(img_provider):
assert result == {}


@responses.activate
def test_service_returns_invalid_json(img_provider, caplog):
responses.add(
responses.GET, 'https://api.spotify.com/v1/tracks/', body='abc')

result = img_provider.get_images(['spotify:track:41shEpOKyyadtG6lDclooa'])

assert result == {}
assert "JSON decoding failed for" in caplog.text()


@responses.activate
def test_service_returns_empty_result(img_provider):
responses.add(
responses.GET, 'https://api.spotify.com/v1/tracks/',
body=json.dumps({'tracks': [{}]}))
def test_service_returns_empty_result(web_client_mock, img_provider):
web_client_mock.get.return_value = {'tracks': [{}]}

result = img_provider.get_images(['spotify:track:41shEpOKyyadtG6lDclooa'])

Expand Down
Loading

0 comments on commit b7d95be

Please sign in to comment.