| OLD | NEW |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 from google.appengine.ext import ndb | 5 from google.appengine.ext import ndb |
| 6 | 6 from google.appengine.api import users |
| 7 from testing_utils import testing | 7 from testing_utils import testing |
| 8 | 8 |
| 9 from model.versioned_config import VersionedConfig | 9 from model.versioned_config import VersionedConfig |
| 10 | 10 |
| 11 | 11 |
| 12 class _Config(VersionedConfig): | 12 class _Config(VersionedConfig): |
| 13 a = ndb.IntegerProperty(indexed=False, default=0) | 13 a = ndb.IntegerProperty(indexed=False, default=0) |
| 14 | 14 |
| 15 | 15 |
| 16 class VersionedConfigTest(testing.AppengineTestCase): | 16 class VersionedConfigTest(testing.AppengineTestCase): |
| 17 |
| 17 def _CreateFirstVersion(self): | 18 def _CreateFirstVersion(self): |
| 18 config = _Config.Get() | 19 config = _Config.Get() |
| 19 config.Update(a=1) | 20 config.Update(users.User(email='admin@chromium.org'), True, a=1) |
| 20 | 21 |
| 21 def testGetWhenNoConfigCreatedYet(self): | 22 def testGetWhenNoConfigCreatedYet(self): |
| 22 config = _Config.Get() | 23 config = _Config.Get() |
| 23 self.assertIsNotNone(config) | 24 self.assertIsNotNone(config) |
| 24 self.assertEqual(0, config.a) | 25 self.assertEqual(0, config.a) |
| 25 | 26 |
| 26 def testNonAdminCanNotUpdate(self): | 27 def testNonAdminCanNotUpdate(self): |
| 27 config = _Config.Get() | 28 config = _Config.Get() |
| 28 with self.assertRaises(Exception): | 29 with self.assertRaises(Exception): |
| 29 config.Update() | 30 config.Update(users.User(email='admin@chromium.org'), False, a=1) |
| 30 | 31 |
| 31 def testUpdateWhenChanged(self): | 32 def testUpdateWhenChanged(self): |
| 32 self.mock_current_user(user_email='test@chromium.org', is_admin=True) | |
| 33 self._CreateFirstVersion() | 33 self._CreateFirstVersion() |
| 34 config = _Config.Get() | 34 config = _Config.Get() |
| 35 self.assertIsNotNone(config) | 35 self.assertIsNotNone(config) |
| 36 self.assertTrue(config.Update(a=2)) | 36 self.assertTrue(config.Update(users.User(email='admin@chromium.org'), True, |
| 37 a=2)) |
| 37 | 38 |
| 38 config = _Config.Get() | 39 config = _Config.Get() |
| 39 self.assertIsNotNone(config) | 40 self.assertIsNotNone(config) |
| 40 self.assertEqual(2, config.version) | 41 self.assertEqual(2, config.version) |
| 41 self.assertEqual(2, config.a) | 42 self.assertEqual(2, config.a) |
| 42 | 43 |
| 43 def testNotUpdateWhenNotChanged(self): | 44 def testNotUpdateWhenNotChanged(self): |
| 44 self.mock_current_user(user_email='test@chromium.org', is_admin=True) | |
| 45 self._CreateFirstVersion() | 45 self._CreateFirstVersion() |
| 46 config = _Config.Get() | 46 config = _Config.Get() |
| 47 self.assertIsNotNone(config) | 47 self.assertIsNotNone(config) |
| 48 self.assertFalse(config.Update(a=1)) | 48 self.assertFalse(config.Update(users.User(email='admin@chromium.org'), True, |
| 49 a=1)) |
| 49 | 50 |
| 50 config = _Config.Get() | 51 config = _Config.Get() |
| 51 self.assertIsNotNone(config) | 52 self.assertIsNotNone(config) |
| 52 self.assertEqual(1, config.version) | 53 self.assertEqual(1, config.version) |
| 53 self.assertEqual(1, config.a) | 54 self.assertEqual(1, config.a) |
| OLD | NEW |