|  | @@ -0,0 +1,124 @@
 | 
	
		
			
			|  | 1 | +from collections import defaultdict
 | 
	
		
			
			|  | 2 | +import mock
 | 
	
		
			
			|  | 3 | +from searx.engines import spotify
 | 
	
		
			
			|  | 4 | +from searx.testing import SearxTestCase
 | 
	
		
			
			|  | 5 | +
 | 
	
		
			
			|  | 6 | +
 | 
	
		
			
			|  | 7 | +class TestSpotifyEngine(SearxTestCase):
 | 
	
		
			
			|  | 8 | +
 | 
	
		
			
			|  | 9 | +    def test_request(self):
 | 
	
		
			
			|  | 10 | +        query = 'test_query'
 | 
	
		
			
			|  | 11 | +        dicto = defaultdict(dict)
 | 
	
		
			
			|  | 12 | +        dicto['pageno'] = 0
 | 
	
		
			
			|  | 13 | +        params = spotify.request(query, dicto)
 | 
	
		
			
			|  | 14 | +        self.assertIn('url', params)
 | 
	
		
			
			|  | 15 | +        self.assertIn(query, params['url'])
 | 
	
		
			
			|  | 16 | +        self.assertIn('spotify.com', params['url'])
 | 
	
		
			
			|  | 17 | +
 | 
	
		
			
			|  | 18 | +    def test_response(self):
 | 
	
		
			
			|  | 19 | +        self.assertRaises(AttributeError, spotify.response, None)
 | 
	
		
			
			|  | 20 | +        self.assertRaises(AttributeError, spotify.response, [])
 | 
	
		
			
			|  | 21 | +        self.assertRaises(AttributeError, spotify.response, '')
 | 
	
		
			
			|  | 22 | +        self.assertRaises(AttributeError, spotify.response, '[]')
 | 
	
		
			
			|  | 23 | +
 | 
	
		
			
			|  | 24 | +        response = mock.Mock(text='{}')
 | 
	
		
			
			|  | 25 | +        self.assertEqual(spotify.response(response), [])
 | 
	
		
			
			|  | 26 | +
 | 
	
		
			
			|  | 27 | +        response = mock.Mock(text='{"data": []}')
 | 
	
		
			
			|  | 28 | +        self.assertEqual(spotify.response(response), [])
 | 
	
		
			
			|  | 29 | +
 | 
	
		
			
			|  | 30 | +        json = """
 | 
	
		
			
			|  | 31 | +        {
 | 
	
		
			
			|  | 32 | +          "tracks": {
 | 
	
		
			
			|  | 33 | +            "href": "https://api.spotify.com/v1/search?query=nosfell&offset=0&limit=20&type=track",
 | 
	
		
			
			|  | 34 | +            "items": [
 | 
	
		
			
			|  | 35 | +              {
 | 
	
		
			
			|  | 36 | +                "album": {
 | 
	
		
			
			|  | 37 | +                  "album_type": "album",
 | 
	
		
			
			|  | 38 | +                  "external_urls": {
 | 
	
		
			
			|  | 39 | +                    "spotify": "https://open.spotify.com/album/5c9ap1PBkSGLxT3J73toxA"
 | 
	
		
			
			|  | 40 | +                  },
 | 
	
		
			
			|  | 41 | +                  "href": "https://api.spotify.com/v1/albums/5c9ap1PBkSGLxT3J73toxA",
 | 
	
		
			
			|  | 42 | +                  "id": "5c9ap1PBkSGLxT3J73toxA",
 | 
	
		
			
			|  | 43 | +                  "name": "Album Title",
 | 
	
		
			
			|  | 44 | +                  "type": "album",
 | 
	
		
			
			|  | 45 | +                  "uri": "spotify:album:5c9ap1PBkSGLxT3J73toxA"
 | 
	
		
			
			|  | 46 | +                },
 | 
	
		
			
			|  | 47 | +                "artists": [
 | 
	
		
			
			|  | 48 | +                  {
 | 
	
		
			
			|  | 49 | +                    "external_urls": {
 | 
	
		
			
			|  | 50 | +                      "spotify": "https://open.spotify.com/artist/0bMc6b75FfZEpQHG1jifKu"
 | 
	
		
			
			|  | 51 | +                    },
 | 
	
		
			
			|  | 52 | +                    "href": "https://api.spotify.com/v1/artists/0bMc6b75FfZEpQHG1jifKu",
 | 
	
		
			
			|  | 53 | +                    "id": "0bMc6b75FfZEpQHG1jifKu",
 | 
	
		
			
			|  | 54 | +                    "name": "Artist Name",
 | 
	
		
			
			|  | 55 | +                    "type": "artist",
 | 
	
		
			
			|  | 56 | +                    "uri": "spotify:artist:0bMc6b75FfZEpQHG1jifKu"
 | 
	
		
			
			|  | 57 | +                  }
 | 
	
		
			
			|  | 58 | +                ],
 | 
	
		
			
			|  | 59 | +                "disc_number": 1,
 | 
	
		
			
			|  | 60 | +                "duration_ms": 202386,
 | 
	
		
			
			|  | 61 | +                "explicit": false,
 | 
	
		
			
			|  | 62 | +                "external_ids": {
 | 
	
		
			
			|  | 63 | +                  "isrc": "FRV640600067"
 | 
	
		
			
			|  | 64 | +                },
 | 
	
		
			
			|  | 65 | +                "external_urls": {
 | 
	
		
			
			|  | 66 | +                  "spotify": "https://open.spotify.com/track/2GzvFiedqW8hgqUpWcASZa"
 | 
	
		
			
			|  | 67 | +                },
 | 
	
		
			
			|  | 68 | +                "href": "https://api.spotify.com/v1/tracks/2GzvFiedqW8hgqUpWcASZa",
 | 
	
		
			
			|  | 69 | +                "id": "1000",
 | 
	
		
			
			|  | 70 | +                "is_playable": true,
 | 
	
		
			
			|  | 71 | +                "name": "Title of track",
 | 
	
		
			
			|  | 72 | +                "popularity": 6,
 | 
	
		
			
			|  | 73 | +                "preview_url": "https://p.scdn.co/mp3-preview/7b8ecda580965a066b768c2647f877e43f7b1a0a",
 | 
	
		
			
			|  | 74 | +                "track_number": 3,
 | 
	
		
			
			|  | 75 | +                "type": "track",
 | 
	
		
			
			|  | 76 | +                "uri": "spotify:track:2GzvFiedqW8hgqUpWcASZa"
 | 
	
		
			
			|  | 77 | +              }
 | 
	
		
			
			|  | 78 | +            ],
 | 
	
		
			
			|  | 79 | +            "limit": 20,
 | 
	
		
			
			|  | 80 | +            "next": "https://api.spotify.com/v1/search?query=nosfell&offset=20&limit=20&type=track",
 | 
	
		
			
			|  | 81 | +            "offset": 0,
 | 
	
		
			
			|  | 82 | +            "previous": null,
 | 
	
		
			
			|  | 83 | +            "total": 107
 | 
	
		
			
			|  | 84 | +          }
 | 
	
		
			
			|  | 85 | +        }
 | 
	
		
			
			|  | 86 | +        """
 | 
	
		
			
			|  | 87 | +        response = mock.Mock(text=json)
 | 
	
		
			
			|  | 88 | +        results = spotify.response(response)
 | 
	
		
			
			|  | 89 | +        self.assertEqual(type(results), list)
 | 
	
		
			
			|  | 90 | +        self.assertEqual(len(results), 1)
 | 
	
		
			
			|  | 91 | +        self.assertEqual(results[0]['title'], 'Title of track')
 | 
	
		
			
			|  | 92 | +        self.assertEqual(results[0]['url'], 'https://open.spotify.com/track/2GzvFiedqW8hgqUpWcASZa')
 | 
	
		
			
			|  | 93 | +        self.assertEqual(results[0]['content'], 'Artist Name • Album Title • Title of track')
 | 
	
		
			
			|  | 94 | +        self.assertIn('1000', results[0]['embedded'])
 | 
	
		
			
			|  | 95 | +
 | 
	
		
			
			|  | 96 | +        json = """
 | 
	
		
			
			|  | 97 | +        {
 | 
	
		
			
			|  | 98 | +          "tracks": {
 | 
	
		
			
			|  | 99 | +            "href": "https://api.spotify.com/v1/search?query=nosfell&offset=0&limit=20&type=track",
 | 
	
		
			
			|  | 100 | +            "items": [
 | 
	
		
			
			|  | 101 | +              {
 | 
	
		
			
			|  | 102 | +                "href": "https://api.spotify.com/v1/tracks/2GzvFiedqW8hgqUpWcASZa",
 | 
	
		
			
			|  | 103 | +                "id": "1000",
 | 
	
		
			
			|  | 104 | +                "is_playable": true,
 | 
	
		
			
			|  | 105 | +                "name": "Title of track",
 | 
	
		
			
			|  | 106 | +                "popularity": 6,
 | 
	
		
			
			|  | 107 | +                "preview_url": "https://p.scdn.co/mp3-preview/7b8ecda580965a066b768c2647f877e43f7b1a0a",
 | 
	
		
			
			|  | 108 | +                "track_number": 3,
 | 
	
		
			
			|  | 109 | +                "type": "album",
 | 
	
		
			
			|  | 110 | +                "uri": "spotify:track:2GzvFiedqW8hgqUpWcASZa"
 | 
	
		
			
			|  | 111 | +              }
 | 
	
		
			
			|  | 112 | +            ],
 | 
	
		
			
			|  | 113 | +            "limit": 20,
 | 
	
		
			
			|  | 114 | +            "next": "https://api.spotify.com/v1/search?query=nosfell&offset=20&limit=20&type=track",
 | 
	
		
			
			|  | 115 | +            "offset": 0,
 | 
	
		
			
			|  | 116 | +            "previous": null,
 | 
	
		
			
			|  | 117 | +            "total": 107
 | 
	
		
			
			|  | 118 | +          }
 | 
	
		
			
			|  | 119 | +        }
 | 
	
		
			
			|  | 120 | +        """
 | 
	
		
			
			|  | 121 | +        response = mock.Mock(text=json)
 | 
	
		
			
			|  | 122 | +        results = spotify.response(response)
 | 
	
		
			
			|  | 123 | +        self.assertEqual(type(results), list)
 | 
	
		
			
			|  | 124 | +        self.assertEqual(len(results), 0)
 |