Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAleksey Lim <alsroot@sugarlabs.org>2012-10-07 07:08:28 (GMT)
committer Aleksey Lim <alsroot@sugarlabs.org>2012-10-07 07:08:28 (GMT)
commitcba1ea75012259b5f62e8f06f32b3fa8123eddeb (patch)
tree7e1ea3d5098d07ac0f30c397de8d9febb516f056
parenta00696ec1bfb9f387c15a5bc3b587b02fa73e351 (diff)
Test GUID value only on creation
-rw-r--r--active_document/commands.py8
-rw-r--r--active_document/directory.py4
-rwxr-xr-xtests/units/commands.py14
-rwxr-xr-xtests/units/document.py12
-rwxr-xr-xtests/units/index.py29
5 files changed, 35 insertions, 32 deletions
diff --git a/active_document/commands.py b/active_document/commands.py
index 1e50fe3..4ab8587 100644
--- a/active_document/commands.py
+++ b/active_document/commands.py
@@ -13,7 +13,6 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
-import re
import logging
from active_document import env
@@ -22,8 +21,6 @@ from active_toolkit import enforce
_logger = logging.getLogger('active_document.commands')
-_GUID_RE = re.compile('[a-zA-Z0-9_+-.]+$')
-
def command(scope, **kwargs):
@@ -202,11 +199,6 @@ class CommandsProcessor(object):
cmd = self.resolve(request)
enforce(cmd is not None, CommandNotFound, 'Unsupported command')
- guid = request.get('guid')
- if guid is not None:
- enforce(_GUID_RE.match(guid) is not None,
- 'Specified malformed GUID')
-
enforce(request.access_level & cmd.access_level, env.Forbidden,
'Operation is permitted on requester\'s level')
diff --git a/active_document/directory.py b/active_document/directory.py
index f6f6c3a..9bf60cb 100644
--- a/active_document/directory.py
+++ b/active_document/directory.py
@@ -14,6 +14,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
+import re
import shutil
import logging
from os.path import exists, join
@@ -28,6 +29,8 @@ from active_toolkit import util, enforce
# To invalidate existed index on stcuture changes
_LAYOUT_VERSION = 1
+_GUID_RE = re.compile('[a-zA-Z0-9_+-.]+$')
+
_logger = logging.getLogger('active_document.document')
@@ -97,6 +100,7 @@ class Directory(object):
if 'guid' in props:
guid = props['guid']
+ enforce(_GUID_RE.match(guid) is not None, 'Malformed GUID')
else:
guid = props['guid'] = env.uuid()
diff --git a/tests/units/commands.py b/tests/units/commands.py
index f5f753b..fb3a66f 100755
--- a/tests/units/commands.py
+++ b/tests/units/commands.py
@@ -236,20 +236,6 @@ class CommandsTest(tests.Test):
],
calls)
- def test_MalformedGUIDs(self):
- calls = []
-
- class TestCommandsProcessor(CommandsProcessor):
-
- @document_command(method='PROBE')
- def command(self, **kwargs):
- pass
-
- cp = TestCommandsProcessor()
-
- self.call(cp, 'PROBE', document='testdocument', guid='guid')
- self.assertRaises(RuntimeError, self.call, cp, 'PROBE', document='testdocument', guid='foo/bar')
-
def test_AccessLevel(self):
calls = []
diff --git a/tests/units/document.py b/tests/units/document.py
index a8a8ca2..9c99d5b 100755
--- a/tests/units/document.py
+++ b/tests/units/document.py
@@ -729,6 +729,18 @@ class DocumentTest(tests.Test):
self.assertEqual(5, doc.meta('blob')['mtime'])
self.assertEqual('blob-2', file('document/1/1/blob.blob').read())
+ def test_MalformedGUIDs(self):
+
+ class Document(document.Document):
+ pass
+
+ directory = Directory(tests.tmpdir, Document, IndexWriter)
+
+ self.assertRaises(RuntimeError, directory.create, {'guid': 'foo/bar'})
+ self.assertRaises(RuntimeError, directory.create, {'guid': 'foo bar'})
+ self.assertRaises(RuntimeError, directory.create, {'guid': 'foo#bar'})
+ assert directory.create({'guid': 'foo-bar.1-2'})
+
def __test_Integers(self):
db = Index({
'prop': ActiveProperty('prop', 1, 'A', typecast=int, full_text=True),
diff --git a/tests/units/index.py b/tests/units/index.py
index 8113734..bfd0b4f 100755
--- a/tests/units/index.py
+++ b/tests/units/index.py
@@ -265,22 +265,31 @@ class IndexTest(tests.Test):
db = Index({
'var_1': ActiveProperty('var_1', 1, 'A'),
'var_2': ActiveProperty('var_2', 2, 'B'),
- 'var_3': ActiveProperty('var_3', 3, 'C'),
})
- db.store('1', {'var_1': '1', 'var_2': '1', 'var_3': '5'}, True)
- db.store('2', {'var_1': '2', 'var_2': '2', 'var_3': '5'}, True)
- db.store('3', {'var_1': '3', 'var_2': '3', 'var_3': '4'}, True)
+ db.store('1', {'var_1': '1', 'var_2': '3'}, True)
+ db.store('2', {'var_1': '2', 'var_2': '2'}, True)
+ db.store('3', {'var_1': '3', 'var_2': '1'}, True)
+
+ self.assertEqual(
+ ([{'guid': '1'}, {'guid': '2'}, {'guid': '3'}], 3),
+ db._find(order_by='var_1'))
+ self.assertEqual(
+ ([{'guid': '1'}, {'guid': '2'}, {'guid': '3'}], 3),
+ db._find(order_by='+var_1'))
+ self.assertEqual(
+ ([{'guid': '3'}, {'guid': '2'}, {'guid': '1'}], 3),
+ db._find(order_by='-var_1'))
self.assertEqual(
- ([{'guid': '1', 'var_1': '1'}, {'guid': '2', 'var_1': '2'}, {'guid': '3', 'var_1': '3'}], 3),
- db._find(reply=['var_1'], order_by='var_2'))
+ ([{'guid': '3'}, {'guid': '2'}, {'guid': '1'}], 3),
+ db._find(order_by='var_2'))
self.assertEqual(
- ([{'guid': '1', 'var_1': '1'}, {'guid': '2', 'var_1': '2'}, {'guid': '3', 'var_1': '3'}], 3),
- db._find(reply=['var_1'], order_by='+var_2'))
+ ([{'guid': '3'}, {'guid': '2'}, {'guid': '1'}], 3),
+ db._find(order_by='+var_2'))
self.assertEqual(
- ([{'guid': '3', 'var_1': '3'}, {'guid': '2', 'var_1': '2'}, {'guid': '1', 'var_1': '1'}], 3),
- db._find(reply=['var_1'], order_by='-var_2'))
+ ([{'guid': '1'}, {'guid': '2'}, {'guid': '3'}], 3),
+ db._find(order_by='-var_2'))
def test_find_GroupBy(self):
db = Index({