QChris has uploaded a new change for review.
https://gerrit.wikimedia.org/r/179133
Change subject: Validate that SCID's schema name matches the schema data's title
......................................................................
Validate that SCID's schema name matches the schema data's title
RT: 9016
Change-Id: I43e108672ae654fcd16c5b223212ae6180fea61d
---
M server/eventlogging/schema.py
M server/tests/fixtures.py
M server/tests/test_schema.py
3 files changed, 37 insertions(+), 4 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/EventLogging
refs/changes/33/179133/1
diff --git a/server/eventlogging/schema.py b/server/eventlogging/schema.py
index 6010211..b2519b7 100644
--- a/server/eventlogging/schema.py
+++ b/server/eventlogging/schema.py
@@ -57,6 +57,20 @@
schema = json.loads(http_get(url))
except (ValueError, EnvironmentError) as ex:
raise jsonschema.SchemaError('Schema fetch failure: %s' % ex)
+
+ # As the scid's name has not been verified, we assure that it matches the
+ # parsed schema data's title.
+ try:
+ if schema_name != schema['title']:
+ raise jsonschema.SchemaError(
+ 'SCID name (%s) and fetched schema title (%s) do not match'
+ % (schema_name, schema['title']))
+ except KeyError:
+ raise jsonschema.SchemaError(
+ 'Fetched schema data does not have a "title" field')
+ except TypeError:
+ raise jsonschema.SchemaError(
+ 'Fetched schema data does not seem to be a json object')
jsonschema.Draft3Validator.check_schema(schema)
return schema
diff --git a/server/tests/fixtures.py b/server/tests/fixtures.py
index e060692..ab50090 100644
--- a/server/tests/fixtures.py
+++ b/server/tests/fixtures.py
@@ -16,7 +16,8 @@
import sqlalchemy
-TEST_SCHEMA_SCID = ('TestSchema', 123)
+TEST_SCHEMA_NAME = 'TestSchema'
+TEST_SCHEMA_SCID = (TEST_SCHEMA_NAME, 123)
_schemas = {
eventlogging.schema.CAPSULE_SCID: {
diff --git a/server/tests/test_schema.py b/server/tests/test_schema.py
index 2e17707..6ce994d 100644
--- a/server/tests/test_schema.py
+++ b/server/tests/test_schema.py
@@ -22,6 +22,7 @@
HttpRequestAttempted,
HttpSchemaTestMixin,
SchemaTestMixin,
+ TEST_SCHEMA_NAME,
TEST_SCHEMA_SCID
)
@@ -31,9 +32,12 @@
def test_valid_resp(self):
"""Test handling of HTTP response containing valid schema."""
- self.http_resp = '{"properties":{"value":{"type":"number"}}}'
+ self.http_resp = '{"title": "%s", "properties":{"value":{' \
+ '"type":"number"}}}' % (TEST_SCHEMA_NAME)
schema = eventlogging.schema.http_get_schema(TEST_SCHEMA_SCID)
- self.assertEqual(schema, {'properties': {'value': {'type': 'number'}}})
+ self.assertEqual(schema, {
+ 'title': TEST_SCHEMA_NAME,
+ 'properties': {'value': {'type': 'number'}}})
def test_invalid_resp(self):
"""Test handling of HTTP response not containing valid schema."""
@@ -43,10 +47,24 @@
def test_caching(self):
"""Valid HTTP responses containing JSON Schema are cached."""
- self.http_resp = '{"properties":{"value":{"type":"number"}}}'
+ self.http_resp = '{"title": "%s", "properties":{"value":{' \
+ '"type":"number"}}}' % (TEST_SCHEMA_NAME)
eventlogging.get_schema(TEST_SCHEMA_SCID)
self.assertIn(TEST_SCHEMA_SCID, eventlogging.schema.schema_cache)
+ def test_titleless_schema_response(self):
+ """Event's schema data has to hold a 'title' field."""
+ self.http_resp = '{"properties":{"value":{"type":"number"}}}'
+ with self.assertRaises(eventlogging.SchemaError):
+ eventlogging.get_schema(TEST_SCHEMA_SCID)
+
+ def test_mismatching_schema_name(self):
+ """Event's schema name has to match revision's page name."""
+ self.http_resp = '{"title": "%s%s", "properties":{"value":{' \
+ '"type":"number"}}}' % (TEST_SCHEMA_NAME, 'foo')
+ with self.assertRaises(eventlogging.SchemaError):
+ eventlogging.get_schema(TEST_SCHEMA_SCID)
+
class SchemaTestCase(SchemaTestMixin, unittest.TestCase):
"""Tests for :module:`eventlogging.schema`."""
--
To view, visit https://gerrit.wikimedia.org/r/179133
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I43e108672ae654fcd16c5b223212ae6180fea61d
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/EventLogging
Gerrit-Branch: master
Gerrit-Owner: QChris <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits