| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2014 The Swarming Authors. All rights reserved. | 2 # Copyright 2014 The Swarming Authors. All rights reserved. |
| 3 # Use of this source code is governed by the Apache v2.0 license that can be | 3 # Use of this source code is governed by the Apache v2.0 license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 import logging | 6 import logging |
| 7 import sys | 7 import sys |
| 8 import unittest | 8 import unittest |
| 9 | 9 |
| 10 import test_env | 10 import test_env |
| 11 test_env.setup_test_env() | 11 test_env.setup_test_env() |
| 12 | 12 |
| 13 import webtest | 13 import webtest |
| 14 | 14 |
| 15 from components import auth_testing | 15 from components import auth_testing |
| 16 from components import template | 16 from components import template |
| 17 from test_support import test_case | 17 from test_support import test_case |
| 18 | 18 |
| 19 import handlers_frontend | 19 import handlers_frontend |
| 20 import importer | 20 import importer |
| 21 import replication | 21 import replication |
| 22 | 22 |
| 23 | 23 |
| 24 GOOD_IMPORTER_CONFIG = [ | 24 GOOD_IMPORTER_CONFIG = """ |
| 25 { | 25 # Comment. |
| 26 'domain': 'example.com', | 26 tarball { |
| 27 'systems': ['ldap'], | 27 domain: "example.com" |
| 28 'url': 'http://example.com/stuff.tar.gz', | 28 systems: "ldap" |
| 29 }, | 29 url: "http://example.com/stuff.tar.gz" |
| 30 { | 30 } |
| 31 'format': 'plainlist', | 31 plainlist { |
| 32 'group': 'chromium-committers', | 32 group: "chromium-committers" |
| 33 'url': 'http://chromium-committers.appspot.com/chromium', | 33 url: "http://chromium-committers.appspot.com/chromium" |
| 34 }, | 34 } |
| 35 ] | 35 """ |
| 36 | 36 |
| 37 BAD_IMPORTER_CONFIG = [ | 37 BAD_IMPORTER_CONFIG = """ |
| 38 # Missing 'url'. | 38 # Missing 'url'. |
| 39 { | 39 tarball { |
| 40 'domain': 'example.com', | 40 domain: "example.com" |
| 41 'systems': ['ldap'], | 41 systems: "ldap" |
| 42 }, | 42 } |
| 43 ] | 43 """ |
| 44 | 44 |
| 45 | 45 |
| 46 class FrontendHandlersTest(test_case.TestCase): | 46 class FrontendHandlersTest(test_case.TestCase): |
| 47 """Tests the frontend handlers.""" | 47 """Tests the frontend handlers.""" |
| 48 | 48 |
| 49 def setUp(self): | 49 def setUp(self): |
| 50 super(FrontendHandlersTest, self).setUp() | 50 super(FrontendHandlersTest, self).setUp() |
| 51 self.mock(replication, 'trigger_replication', lambda *_args, **_kws: None) | 51 self.mock(replication, 'trigger_replication', lambda *_args, **_kws: None) |
| 52 self.app = webtest.TestApp( | 52 self.app = webtest.TestApp( |
| 53 handlers_frontend.create_application(debug=True), | 53 handlers_frontend.create_application(debug=True), |
| 54 extra_environ={'REMOTE_ADDR': '127.0.0.1'}) | 54 extra_environ={'REMOTE_ADDR': '127.0.0.1'}) |
| 55 auth_testing.mock_is_admin(self, True) | 55 auth_testing.mock_is_admin(self, True) |
| 56 auth_testing.mock_get_current_identity(self) | 56 auth_testing.mock_get_current_identity(self) |
| 57 | 57 |
| 58 def tearDown(self): | 58 def tearDown(self): |
| 59 try: | 59 try: |
| 60 template.reset() | 60 template.reset() |
| 61 finally: | 61 finally: |
| 62 super(FrontendHandlersTest, self).tearDown() | 62 super(FrontendHandlersTest, self).tearDown() |
| 63 | 63 |
| 64 def test_warmup(self): | 64 def test_warmup(self): |
| 65 response = self.app.get('/_ah/warmup') | 65 response = self.app.get('/_ah/warmup') |
| 66 self.assertEqual(200, response.status_code) | 66 self.assertEqual(200, response.status_code) |
| 67 self.assertEqual('ok', response.body) | 67 self.assertEqual('ok', response.body) |
| 68 | 68 |
| 69 def test_importer_config_get_default(self): | 69 def test_importer_config_get_default(self): |
| 70 response = self.app.get('/auth_service/api/v1/importer/config', status=200) | 70 response = self.app.get('/auth_service/api/v1/importer/config', status=200) |
| 71 self.assertEqual({'config': []}, response.json) | 71 self.assertEqual({'config': ''}, response.json) |
| 72 | 72 |
| 73 def test_importer_config_get(self): | 73 def test_importer_config_get(self): |
| 74 importer.write_config(GOOD_IMPORTER_CONFIG) | 74 importer.write_config_text(GOOD_IMPORTER_CONFIG) |
| 75 response = self.app.get('/auth_service/api/v1/importer/config', status=200) | 75 response = self.app.get('/auth_service/api/v1/importer/config', status=200) |
| 76 self.assertEqual({'config': GOOD_IMPORTER_CONFIG}, response.json) | 76 self.assertEqual({'config': GOOD_IMPORTER_CONFIG}, response.json) |
| 77 | 77 |
| 78 def test_importer_config_post_ok(self): | 78 def test_importer_config_post_ok(self): |
| 79 response = self.app.post_json( | 79 response = self.app.post_json( |
| 80 '/auth_service/api/v1/importer/config', | 80 '/auth_service/api/v1/importer/config', |
| 81 {'config': GOOD_IMPORTER_CONFIG}, | 81 {'config': GOOD_IMPORTER_CONFIG}, |
| 82 headers={'X-XSRF-Token': auth_testing.generate_xsrf_token_for_test()}, | 82 headers={'X-XSRF-Token': auth_testing.generate_xsrf_token_for_test()}, |
| 83 status=200) | 83 status=200) |
| 84 self.assertEqual({'ok': True}, response.json) | 84 self.assertEqual({'ok': True}, response.json) |
| 85 self.assertEqual(GOOD_IMPORTER_CONFIG, importer.read_config()) | 85 self.assertEqual(GOOD_IMPORTER_CONFIG, importer.read_config_text()) |
| 86 | 86 |
| 87 def test_importer_config_post_bad(self): | 87 def test_importer_config_post_bad(self): |
| 88 response = self.app.post_json( | 88 response = self.app.post_json( |
| 89 '/auth_service/api/v1/importer/config', | 89 '/auth_service/api/v1/importer/config', |
| 90 {'config': BAD_IMPORTER_CONFIG}, | 90 {'config': BAD_IMPORTER_CONFIG}, |
| 91 headers={'X-XSRF-Token': auth_testing.generate_xsrf_token_for_test()}, | 91 headers={'X-XSRF-Token': auth_testing.generate_xsrf_token_for_test()}, |
| 92 status=400) | 92 status=400) |
| 93 self.assertEqual({'text': 'Invalid config format.'}, response.json) | 93 self.assertEqual( |
| 94 self.assertEqual([], importer.read_config()) | 94 {'text': '\'url\' field is required in TarballEntry'}, response.json) |
| 95 self.assertEqual('', importer.read_config_text()) |
| 95 | 96 |
| 96 def test_importer_config_post_locked(self): | 97 def test_importer_config_post_locked(self): |
| 97 self.mock(handlers_frontend.config, 'is_remote_configured', lambda: True) | 98 self.mock(handlers_frontend.config, 'is_remote_configured', lambda: True) |
| 98 response = self.app.post_json( | 99 response = self.app.post_json( |
| 99 '/auth_service/api/v1/importer/config', | 100 '/auth_service/api/v1/importer/config', |
| 100 {'config': GOOD_IMPORTER_CONFIG}, | 101 {'config': GOOD_IMPORTER_CONFIG}, |
| 101 headers={'X-XSRF-Token': auth_testing.generate_xsrf_token_for_test()}, | 102 headers={'X-XSRF-Token': auth_testing.generate_xsrf_token_for_test()}, |
| 102 status=409) | 103 status=409) |
| 103 self.assertEqual( | 104 self.assertEqual( |
| 104 {'text': 'The configuration is managed elsewhere'}, response.json) | 105 {'text': 'The configuration is managed elsewhere'}, response.json) |
| 105 | 106 |
| 106 | 107 |
| 107 if __name__ == '__main__': | 108 if __name__ == '__main__': |
| 108 if '-v' in sys.argv: | 109 if '-v' in sys.argv: |
| 109 unittest.TestCase.maxDiff = None | 110 unittest.TestCase.maxDiff = None |
| 110 logging.basicConfig(level=logging.DEBUG) | 111 logging.basicConfig(level=logging.DEBUG) |
| 111 else: | 112 else: |
| 112 logging.basicConfig(level=logging.FATAL) | 113 logging.basicConfig(level=logging.FATAL) |
| 113 unittest.main() | 114 unittest.main() |
| OLD | NEW |