Allow resuscitation of deleted home projects.

This commit is contained in:
Sybren A. Stüvel 2016-06-15 15:07:18 +02:00
parent ba28f2ac3b
commit 1a48c37bd6
2 changed files with 55 additions and 5 deletions

View File

@ -21,9 +21,26 @@ def create_home_project(user_id):
"""Creates a home project for the given user.""" """Creates a home project for the given user."""
log.info('Creating home project for user %s', user_id) log.info('Creating home project for user %s', user_id)
project = projects.create_new_project(project_name='Home', overrides = {
user_id=ObjectId(user_id), 'category': 'home',
overrides={'category': 'home'}) 'summary': 'This is your home project. Pastebin and Blender settings sync in one!',
'description': '# Your home project\n\n'
'This is your home project. It has functionality to act '
'as a pastebin for text, images and other assets, and '
'allows synchronisation of your Blender settings.'
}
# Maybe the user has a deleted home project.
proj_coll = current_app.data.driver.db['projects']
deleted_proj = proj_coll.find_one({'user': user_id, 'category': 'home', '_deleted': True})
if deleted_proj:
log.info('User %s has a deleted project %s, restoring', user_id, deleted_proj['_id'])
project = deleted_proj
else:
log.debug('User %s does not have a deleted project', user_id)
project = projects.create_new_project(project_name='Home',
user_id=ObjectId(user_id),
overrides=overrides)
# Re-validate the authentication token, so that the put_internal call sees the # Re-validate the authentication token, so that the put_internal call sees the
# new group created for the project. # new group created for the project.
@ -99,7 +116,7 @@ def has_home_project(user_id):
"""Returns True iff the user has a home project.""" """Returns True iff the user has a home project."""
proj_coll = current_app.data.driver.db['projects'] proj_coll = current_app.data.driver.db['projects']
return proj_coll.count({'user': user_id, 'category': 'home'}) > 0 return proj_coll.count({'user': user_id, 'category': 'home', '_deleted': False}) > 0
def setup_app(app, url_prefix): def setup_app(app, url_prefix):

View File

@ -115,9 +115,16 @@ class HomeProjectTest(AbstractPillarTest):
validate_token() validate_token()
self.assertFalse(home_project.has_home_project(user_id)) self.assertFalse(home_project.has_home_project(user_id))
home_project.create_home_project(user_id) proj = home_project.create_home_project(user_id)
self.assertTrue(home_project.has_home_project(user_id)) self.assertTrue(home_project.has_home_project(user_id))
# Delete the project.
resp = self.client.delete('/projects/%s' % proj['_id'],
headers={'Authorization': self.make_header('token'),
'If-Match': proj['_etag']})
self.assertEqual(204, resp.status_code, resp.data)
self.assertFalse(home_project.has_home_project(user_id))
@responses.activate @responses.activate
def test_home_project_projections(self): def test_home_project_projections(self):
"""Getting the home project should support projections.""" """Getting the home project should support projections."""
@ -196,3 +203,29 @@ class HomeProjectTest(AbstractPillarTest):
self.assertEqual(json_proj2['_etag'], db_proj2['_etag']) self.assertEqual(json_proj2['_etag'], db_proj2['_etag'])
self.assertNotEqual(db_proj1['_etag'], db_proj2['_etag']) self.assertNotEqual(db_proj1['_etag'], db_proj2['_etag'])
self.assertNotEqual(db_proj1['_id'], db_proj2['_id']) self.assertNotEqual(db_proj1['_id'], db_proj2['_id'])
def test_delete_restore(self):
"""Deleting and then recreating a home project should restore the deleted project."""
self._create_user_with_token(roles={u'subscriber'}, token='token')
# Create home project by getting it.
resp = self.client.get('/bcloud/home-project',
headers={'Authorization': self.make_header('token')})
self.assertEqual(200, resp.status_code, resp.data)
before_delete_json_proj = json.loads(resp.data)
# Delete the project.
resp = self.client.delete('/projects/%s' % before_delete_json_proj['_id'],
headers={'Authorization': self.make_header('token'),
'If-Match': before_delete_json_proj['_etag']})
self.assertEqual(204, resp.status_code, resp.data)
# Recreate home project by getting it.
resp = self.client.get('/bcloud/home-project',
headers={'Authorization': self.make_header('token')})
self.assertEqual(200, resp.status_code, resp.data)
after_delete_json_proj = json.loads(resp.data)
self.assertEqual(before_delete_json_proj['_id'],
after_delete_json_proj['_id'])