test_api_v1.py 11.1 KB
Newer Older
1
# Copyright (C) 2011 Lukas Lalinsky
2
# Distributed under the MIT license, see the LICENSE file for details.
3

Lukáš Lalinský's avatar
Lukáš Lalinský committed
4
import unittest
5
from nose.tools import assert_equals, assert_raises
Lukáš Lalinský's avatar
Lukáš Lalinský committed
6
import tests
7 8
from tests import (
    prepare_database, with_database,
9 10 11 12 13 14 15 16 17 18
    TEST_1_LENGTH,
    TEST_1_FP,
    TEST_1_FP_RAW,
    TEST_2_LENGTH,
    TEST_2_FP,
    TEST_2_FP_RAW,
)
from werkzeug.wrappers import Request
from werkzeug.test import EnvironBuilder
from werkzeug.datastructures import MultiDict
19
from acoustid import tables
20 21
from acoustid.api import errors
from acoustid.api.v1 import (
22
    LookupHandler,
23
    LookupHandlerParams,
24
    SubmitHandler,
25 26 27 28 29 30 31 32
    SubmitHandlerParams,
    APIHandler,
)


def test_ok():
    handler = APIHandler()
    resp = handler._ok({'tracks': [{'id': 1, 'name': 'Track 1'}]})
33
    assert_equals('text/xml; charset=UTF-8', resp.content_type)
34 35 36 37 38 39 40 41
    expected = '<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n<response status="ok"><tracks><track><id>1</id><name>Track 1</name></track></tracks></response>'
    assert_equals(expected, resp.data)
    assert_equals('200 OK', resp.status)


def test_error():
    handler = APIHandler()
    resp = handler._error(123, 'something is wrong')
42
    assert_equals('text/xml; charset=UTF-8', resp.content_type)
43 44 45 46
    expected = '<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n<response status="error"><error>something is wrong</error></response>'
    assert_equals(expected, resp.data)
    assert_equals('400 BAD REQUEST', resp.status)
    resp = handler._error(234, 'oops', status=500)
47
    assert_equals('text/xml; charset=UTF-8', resp.content_type)
48 49 50 51 52 53 54 55 56
    expected = '<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n<response status="error"><error>oops</error></response>'
    assert_equals(expected, resp.data)
    assert_equals('500 INTERNAL SERVER ERROR', resp.status)


@with_database
def test_lookup_handler_params(conn):
    # missing client
    values = MultiDict({})
Lukáš Lalinský's avatar
Lukáš Lalinský committed
57
    params = LookupHandlerParams(tests.script.config)
58 59 60
    assert_raises(errors.MissingParameterError, params.parse, values, conn)
    # invalid client
    values = MultiDict({'client': 'N/A'})
Lukáš Lalinský's avatar
Lukáš Lalinský committed
61
    params = LookupHandlerParams(tests.script.config)
62 63 64
    assert_raises(errors.InvalidAPIKeyError, params.parse, values, conn)
    # missing length
    values = MultiDict({'client': 'app1key'})
Lukáš Lalinský's avatar
Lukáš Lalinský committed
65
    params = LookupHandlerParams(tests.script.config)
66 67 68
    assert_raises(errors.MissingParameterError, params.parse, values, conn)
    # missing fingerprint
    values = MultiDict({'client': 'app1key', 'length': str(TEST_1_LENGTH)})
Lukáš Lalinský's avatar
Lukáš Lalinský committed
69
    params = LookupHandlerParams(tests.script.config)
70 71 72
    assert_raises(errors.MissingParameterError, params.parse, values, conn)
    # invalid fingerprint
    values = MultiDict({'client': 'app1key', 'length': str(TEST_1_LENGTH), 'fingerprint': '...'})
Lukáš Lalinský's avatar
Lukáš Lalinský committed
73
    params = LookupHandlerParams(tests.script.config)
74 75 76
    assert_raises(errors.InvalidFingerprintError, params.parse, values, conn)
    # all ok
    values = MultiDict({'client': 'app1key', 'length': str(TEST_1_LENGTH), 'fingerprint': TEST_1_FP})
Lukáš Lalinský's avatar
Lukáš Lalinský committed
77
    params = LookupHandlerParams(tests.script.config)
78 79
    params.parse(values, conn)
    assert_equals(1, params.application_id)
Lukáš Lalinský's avatar
Lukáš Lalinský committed
80 81
    assert_equals(TEST_1_LENGTH, params.fingerprints[0]['duration'])
    assert_equals(TEST_1_FP_RAW, params.fingerprints[0]['fingerprint'])
82 83


84
@with_database
Lukáš Lalinský's avatar
Lukáš Lalinský committed
85
@unittest.skip("disabled")
Lukáš Lalinský's avatar
Lukáš Lalinský committed
86
def lookup_handler(conn):
87 88 89
    values = {'client': 'app1key', 'length': str(TEST_1_LENGTH), 'fingerprint': TEST_1_FP}
    builder = EnvironBuilder(method='POST', data=values)
    # no matches
Lukáš Lalinský's avatar
Lukáš Lalinský committed
90
    handler = LookupHandler.create_from_server(tests.script, conn=conn)
91
    resp = handler.handle(Request(builder.get_environ()))
92
    assert_equals('text/xml; charset=UTF-8', resp.content_type)
93 94 95 96 97
    expected = "<?xml version='1.0' encoding='UTF-8'?>\n<response><status>ok</status><results /></response>"
    assert_equals(expected, resp.data)
    assert_equals('200 OK', resp.status)
    # one exact match
    prepare_database(conn, """
Lukáš Lalinský's avatar
Lukáš Lalinský committed
98 99
INSERT INTO fingerprint (length, fingerprint, track_id, submission_count)
    VALUES (%s, %s, 1, 1);
100
""", (TEST_1_LENGTH, TEST_1_FP_RAW))
Lukáš Lalinský's avatar
Lukáš Lalinský committed
101
    handler = LookupHandler.create_from_server(tests.script, conn=conn)
102
    resp = handler.handle(Request(builder.get_environ()))
103
    assert_equals('text/xml; charset=UTF-8', resp.content_type)
104
    expected = "<?xml version='1.0' encoding='UTF-8'?>\n<response><status>ok</status><results><result><score>1.0</score><id>eb31d1c3-950e-468b-9e36-e46fa75b1291</id></result></results></response>"
105 106 107 108 109
    assert_equals(expected, resp.data)
    assert_equals('200 OK', resp.status)
    # one exact match with MBIDs
    values = {'client': 'app1key', 'length': str(TEST_1_LENGTH), 'fingerprint': TEST_1_FP, 'meta': '1'}
    builder = EnvironBuilder(method='POST', data=values)
Lukáš Lalinský's avatar
Lukáš Lalinský committed
110
    handler = LookupHandler.create_from_server(tests.script, conn=conn)
111
    resp = handler.handle(Request(builder.get_environ()))
112
    assert_equals('text/xml; charset=UTF-8', resp.content_type)
113
    expected = "<?xml version='1.0' encoding='UTF-8'?>\n<response><status>ok</status><results><result><tracks><track><id>b81f83ee-4da4-11e0-9ed8-0025225356f3</id></track></tracks><score>1.0</score><id>eb31d1c3-950e-468b-9e36-e46fa75b1291</id></result></results></response>"
114 115
    assert_equals(expected, resp.data)
    assert_equals('200 OK', resp.status)
116
    # one exact match with MBIDs (no exta metadata in v1)
117 118
    values = {'client': 'app1key', 'length': str(TEST_1_LENGTH), 'fingerprint': TEST_1_FP, 'meta': '2'}
    builder = EnvironBuilder(method='POST', data=values)
Lukáš Lalinský's avatar
Lukáš Lalinský committed
119
    handler = LookupHandler.create_from_server(tests.script, conn=conn)
120
    resp = handler.handle(Request(builder.get_environ()))
121
    assert_equals('text/xml; charset=UTF-8', resp.content_type)
122
    expected = "<?xml version='1.0' encoding='UTF-8'?>\n<response><status>ok</status><results><result><tracks><track><id>b81f83ee-4da4-11e0-9ed8-0025225356f3</id></track></tracks><score>1.0</score><id>eb31d1c3-950e-468b-9e36-e46fa75b1291</id></result></results></response>"
123 124 125 126
    assert_equals(expected, resp.data)
    assert_equals('200 OK', resp.status)


127 128 129 130
@with_database
def test_submit_handler_params(conn):
    # missing client
    values = MultiDict({})
Lukáš Lalinský's avatar
Lukáš Lalinský committed
131
    params = SubmitHandlerParams(tests.script.config)
132 133 134
    assert_raises(errors.MissingParameterError, params.parse, values, conn)
    # invalid client
    values = MultiDict({'client': 'N/A'})
Lukáš Lalinský's avatar
Lukáš Lalinský committed
135
    params = SubmitHandlerParams(tests.script.config)
136 137 138
    assert_raises(errors.InvalidAPIKeyError, params.parse, values, conn)
    # missing user
    values = MultiDict({'client': 'app1key'})
Lukáš Lalinský's avatar
Lukáš Lalinský committed
139
    params = SubmitHandlerParams(tests.script.config)
140 141 142
    assert_raises(errors.MissingParameterError, params.parse, values, conn)
    # invalid user
    values = MultiDict({'client': 'app1key', 'user': 'N/A'})
Lukáš Lalinský's avatar
Lukáš Lalinský committed
143
    params = SubmitHandlerParams(tests.script.config)
144 145 146
    assert_raises(errors.InvalidUserAPIKeyError, params.parse, values, conn)
    # missing fingerprint
    values = MultiDict({'client': 'app1key', 'user': 'user1key'})
Lukáš Lalinský's avatar
Lukáš Lalinský committed
147
    params = SubmitHandlerParams(tests.script.config)
148
    assert_raises(errors.MissingParameterError, params.parse, values, conn)
149 150 151 152 153 154 155 156
    # missing duration
    values = MultiDict({'client': 'app1key', 'user': 'user1key',
        'mbid': ['4d814cb1-20ec-494f-996f-f31ca8a49784', '66c0f5cc-67b6-4f51-80cd-ab26b5aaa6ea'],
        'puid': '4e823498-c77d-4bfb-b6cc-85b05c2783cf',
        'fingerprint': TEST_1_FP,
        'bitrate': '192',
        'format': 'MP3'
    })
Lukáš Lalinský's avatar
Lukáš Lalinský committed
157
    params = SubmitHandlerParams(tests.script.config)
158
    assert_raises(errors.MissingParameterError, params.parse, values, conn)
159 160 161 162 163 164 165 166 167
    # all ok (single submission)
    values = MultiDict({'client': 'app1key', 'user': 'user1key',
        'mbid': ['4d814cb1-20ec-494f-996f-f31ca8a49784', '66c0f5cc-67b6-4f51-80cd-ab26b5aaa6ea'],
        'puid': '4e823498-c77d-4bfb-b6cc-85b05c2783cf',
        'length': str(TEST_1_LENGTH),
        'fingerprint': TEST_1_FP,
        'bitrate': '192',
        'format': 'MP3'
    })
Lukáš Lalinský's avatar
Lukáš Lalinský committed
168
    params = SubmitHandlerParams(tests.script.config)
169 170 171 172 173 174 175 176 177
    params.parse(values, conn)
    assert_equals(1, len(params.submissions))
    assert_equals(['4d814cb1-20ec-494f-996f-f31ca8a49784', '66c0f5cc-67b6-4f51-80cd-ab26b5aaa6ea'], params.submissions[0]['mbids'])
    assert_equals('4e823498-c77d-4bfb-b6cc-85b05c2783cf', params.submissions[0]['puid'])
    assert_equals(TEST_1_LENGTH, params.submissions[0]['duration'])
    assert_equals(TEST_1_FP_RAW, params.submissions[0]['fingerprint'])
    assert_equals(192, params.submissions[0]['bitrate'])
    assert_equals('MP3', params.submissions[0]['format'])
    # all ok (single submission)
178 179
    values = MultiDict({
        'client': 'app1key', 'user': 'user1key',
180 181 182 183 184 185 186 187 188 189 190 191 192
        'mbid.0': '4d814cb1-20ec-494f-996f-f31ca8a49784',
        'puid.0': '4e823498-c77d-4bfb-b6cc-85b05c2783cf',
        'length.0': str(TEST_1_LENGTH),
        'fingerprint.0': TEST_1_FP,
        'bitrate.0': '192',
        'format.0': 'MP3',
        'mbid.1': '66c0f5cc-67b6-4f51-80cd-ab26b5aaa6ea',
        'puid.1': '57b202a3-242b-4896-a79c-cac34bbca0b6',
        'length.1': str(TEST_2_LENGTH),
        'fingerprint.1': TEST_2_FP,
        'bitrate.1': '500',
        'format.1': 'FLAC',
    })
Lukáš Lalinský's avatar
Lukáš Lalinský committed
193
    params = SubmitHandlerParams(tests.script.config)
194 195 196 197 198 199 200 201 202 203 204 205 206 207
    params.parse(values, conn)
    assert_equals(2, len(params.submissions))
    assert_equals(['4d814cb1-20ec-494f-996f-f31ca8a49784'], params.submissions[0]['mbids'])
    assert_equals('4e823498-c77d-4bfb-b6cc-85b05c2783cf', params.submissions[0]['puid'])
    assert_equals(TEST_1_LENGTH, params.submissions[0]['duration'])
    assert_equals(TEST_1_FP_RAW, params.submissions[0]['fingerprint'])
    assert_equals(192, params.submissions[0]['bitrate'])
    assert_equals('MP3', params.submissions[0]['format'])
    assert_equals(['66c0f5cc-67b6-4f51-80cd-ab26b5aaa6ea'], params.submissions[1]['mbids'])
    assert_equals('57b202a3-242b-4896-a79c-cac34bbca0b6', params.submissions[1]['puid'])
    assert_equals(TEST_2_LENGTH, params.submissions[1]['duration'])
    assert_equals(TEST_2_FP_RAW, params.submissions[1]['fingerprint'])
    assert_equals(500, params.submissions[1]['bitrate'])
    assert_equals('FLAC', params.submissions[1]['format'])
208 209 210 211 212 213 214 215


@with_database
def test_submit_handler(conn):
    values = {'client': 'app1key', 'user': 'user1key',
        'length': str(TEST_1_LENGTH), 'fingerprint': TEST_1_FP, 'bitrate': 192,
        'mbid': 'b9c05616-1874-4d5d-b30e-6b959c922d28', 'format': 'FLAC'}
    builder = EnvironBuilder(method='POST', data=values)
Lukáš Lalinský's avatar
Lukáš Lalinský committed
216
    handler = SubmitHandler.create_from_server(tests.script, conn=conn)
217
    resp = handler.handle(Request(builder.get_environ()))
218
    assert_equals('text/xml; charset=UTF-8', resp.content_type)
Lukáš Lalinský's avatar
Lukáš Lalinský committed
219
    expected = "<?xml version='1.0' encoding='UTF-8'?>\n<response><status>ok</status><submissions><submission><status>pending</status><id>1</id></submission></submissions></response>"
220 221 222 223 224 225 226 227 228
    assert_equals(expected, resp.data)
    assert_equals('200 OK', resp.status)
    query = tables.submission.select().order_by(tables.submission.c.id.desc()).limit(1)
    submission = conn.execute(query).fetchone()
    assert_equals('b9c05616-1874-4d5d-b30e-6b959c922d28', submission['mbid'])
    assert_equals(1, submission['format_id'])
    assert_equals(192, submission['bitrate'])
    assert_equals(TEST_1_FP_RAW, submission['fingerprint'])
    assert_equals(TEST_1_LENGTH, submission['length'])