404 nicely if you try to interact with a missing current version

This commit is contained in:
Matthew Hodgson 2018-01-07 23:45:55 +00:00 committed by Hubert Chathi
parent 72788cf9c1
commit 66a4ca1d28
2 changed files with 57 additions and 16 deletions

View File

@ -184,6 +184,17 @@ class EndToEndRoomKeyStore(SQLBaseStore):
desc="delete_e2e_room_keys", desc="delete_e2e_room_keys",
) )
@staticmethod
def _get_current_version(txn, user_id):
txn.execute(
"SELECT MAX(version) FROM e2e_room_keys_versions WHERE user_id=?",
(user_id,)
)
row = txn.fetchone()
if not row:
raise StoreError(404, 'No current backup version')
return row[0]
def get_e2e_room_keys_version_info(self, user_id, version=None): def get_e2e_room_keys_version_info(self, user_id, version=None):
"""Get info metadata about a version of our room_keys backup. """Get info metadata about a version of our room_keys backup.
@ -199,11 +210,7 @@ class EndToEndRoomKeyStore(SQLBaseStore):
def _get_e2e_room_keys_version_info_txn(txn): def _get_e2e_room_keys_version_info_txn(txn):
if version is None: if version is None:
txn.execute( this_version = self._get_current_version(txn, user_id)
"SELECT MAX(version) FROM e2e_room_keys_versions WHERE user_id=?",
(user_id,)
)
this_version = txn.fetchone()[0]
else: else:
this_version = version this_version = version
@ -266,23 +273,35 @@ class EndToEndRoomKeyStore(SQLBaseStore):
"create_e2e_room_keys_version_txn", _create_e2e_room_keys_version_txn "create_e2e_room_keys_version_txn", _create_e2e_room_keys_version_txn
) )
@defer.inlineCallbacks def delete_e2e_room_keys_version(self, user_id, version=None):
def delete_e2e_room_keys_version(self, user_id, version):
"""Delete a given backup version of the user's room keys. """Delete a given backup version of the user's room keys.
Doesn't delete their actual key data. Doesn't delete their actual key data.
Args: Args:
user_id(str): the user whose backup version we're deleting user_id(str): the user whose backup version we're deleting
version(str): the ID of the backup version we're deleting version(str): Optional. the version ID of the backup version we're deleting
If missing, we delete the current backup version info.
Raises:
StoreError: with code 404 if there are no e2e_room_keys_versions present,
or if the version requested doesn't exist.
""" """
keyvalues = { def _delete_e2e_room_keys_version_txn(txn):
"user_id": user_id, if version is None:
"version": version, this_version = self._get_current_version(txn, user_id)
} else:
this_version = version
yield self._simple_delete( return self._simple_delete_one_txn(
table="e2e_room_keys_versions", txn,
keyvalues=keyvalues, table="e2e_room_keys_versions",
desc="delete_e2e_room_keys_version", keyvalues={
"user_id": user_id,
"version": this_version,
},
)
return self.runInteraction(
"delete_e2e_room_keys_version",
_delete_e2e_room_keys_version_txn
) )

View File

@ -123,6 +123,28 @@ class E2eRoomKeysHandlerTestCase(unittest.TestCase):
"auth_data": "second_version_auth_data", "auth_data": "second_version_auth_data",
}) })
@defer.inlineCallbacks
def test_delete_missing_version(self):
"""Check that we get a 404 on deleting nonexistent versions
"""
res = None
try:
yield self.handler.delete_version(self.local_user, "1")
except errors.SynapseError as e:
res = e.code
self.assertEqual(res, 404)
@defer.inlineCallbacks
def test_delete_missing_current_version(self):
"""Check that we get a 404 on deleting nonexistent current version
"""
res = None
try:
yield self.handler.delete_version(self.local_user)
except errors.SynapseError as e:
res = e.code
self.assertEqual(res, 404)
@defer.inlineCallbacks @defer.inlineCallbacks
def test_delete_version(self): def test_delete_version(self):
"""Check that we can create and then delete versions. """Check that we can create and then delete versions.