Merge remote-tracking branch 'refs/remotes/origin/3504-clients-compatible-with-3036...
[arvados.git] / sdk / python / tests / test_api.py
1 #!/usr/bin/env python
2
3 import apiclient.errors
4 import arvados
5 import httplib2
6 import json
7 import mimetypes
8 import os
9 import run_test_server
10 import unittest
11
12 from apiclient.http import RequestMockBuilder
13 from httplib import responses as HTTP_RESPONSES
14
15 if not mimetypes.inited:
16     mimetypes.init()
17
18 class ArvadosApiClientTest(unittest.TestCase):
19     @classmethod
20     def response_from_code(cls, code):
21         return httplib2.Response(
22             {'status': code,
23              'reason': HTTP_RESPONSES.get(code, "Unknown Response"),
24              'Content-Type': mimetypes.types_map['.json']})
25
26     @classmethod
27     def api_error_response(cls, code, *errors):
28         return (cls.response_from_code(code),
29                 json.dumps({'errors': errors,
30                             'error_token': '1234567890+12345678'}))
31
32     @classmethod
33     def setUpClass(cls):
34         # The apiclient library has support for mocking requests for
35         # testing, but it doesn't extend to the discovery document
36         # itself. For now, bring up an API server that will serve
37         # a discovery document.
38         # FIXME: Figure out a better way to stub this out.
39         run_test_server.run()
40         mock_responses = {
41             'arvados.humans.delete': (cls.response_from_code(500), ""),
42             'arvados.humans.get': cls.api_error_response(
43                 422, "Bad UUID format", "Bad output format"),
44             'arvados.humans.list': (None, json.dumps(
45                     {'items_available': 0, 'items': []})),
46             }
47         req_builder = RequestMockBuilder(mock_responses)
48         cls.api = arvados.api('v1', cache=False,
49                               host=os.environ['ARVADOS_API_HOST'],
50                               token='discovery-doc-only-no-token-needed',
51                               insecure=True,
52                               requestBuilder=req_builder)
53
54     @classmethod
55     def tearDownClass(cls):
56         run_test_server.stop()
57
58     def test_basic_list(self):
59         answer = self.api.humans().list(
60             filters=[['uuid', 'is', None]]).execute()
61         self.assertEqual(answer['items_available'], len(answer['items']))
62
63     def test_exceptions_include_errors(self):
64         with self.assertRaises(apiclient.errors.HttpError) as err_ctx:
65             self.api.humans().get(uuid='xyz-xyz-abcdef').execute()
66         err_s = str(err_ctx.exception)
67         for msg in ["Bad UUID format", "Bad output format"]:
68             self.assertIn(msg, err_s)
69
70     def test_exceptions_without_errors_have_basic_info(self):
71         with self.assertRaises(apiclient.errors.HttpError) as err_ctx:
72             self.api.humans().delete(uuid='xyz-xyz-abcdef').execute()
73         self.assertIn("500", str(err_ctx.exception))
74
75
76 if __name__ == '__main__':
77     unittest.main()