Exemplo n.º 1
0
def test_merging_deletion_to_modification_or_creation(fake_datetime,
                                                      tag_factory,
                                                      user_factory,
                                                      initial_operation):
    tag = tag_factory(names=['dummy'], category_name='dummy')
    user = user_factory()
    db.session.add_all([tag, user])
    db.session.flush()
    with fake_datetime('13:00:00'):
        initial_operation(tag, user)
    tag.names = [db.TagName('changed')]
    with fake_datetime('14:00:00'):
        snapshots.save_entity_modification(tag, user)
    tag.names = [db.TagName('changed again')]
    with fake_datetime('14:00:01'):
        snapshots.save_entity_deletion(tag, user)
    assert db.session.query(db.Snapshot).count() == 2
    results = db.session \
        .query(db.Snapshot) \
        .order_by(db.Snapshot.snapshot_id.asc()) \
        .all()
    assert results[1].operation == db.Snapshot.OPERATION_DELETED
    assert results[1].data == {
        'names': ['changed again'],
        'category': 'dummy',
        'suggestions': [],
        'implications': [],
    }
Exemplo n.º 2
0
def test_merging_deletion_to_modification_or_creation(
        fake_datetime, tag_factory, user_factory, initial_operation):
    tag = tag_factory(names=['dummy'], category_name='dummy')
    user = user_factory()
    db.session.add_all([tag, user])
    db.session.flush()
    with fake_datetime('13:00:00'):
        initial_operation(tag, user)
    tag.names = [db.TagName('changed')]
    with fake_datetime('14:00:00'):
        snapshots.save_entity_modification(tag, user)
    tag.names = [db.TagName('changed again')]
    with fake_datetime('14:00:01'):
        snapshots.save_entity_deletion(tag, user)
    assert db.session.query(db.Snapshot).count() == 2
    results = db.session \
        .query(db.Snapshot) \
        .order_by(db.Snapshot.snapshot_id.asc()) \
        .all()
    assert results[1].operation == db.Snapshot.OPERATION_DELETED
    assert results[1].data == {
        'names': ['changed again'],
        'category': 'dummy',
        'suggestions': [],
        'implications': [],
    }
Exemplo n.º 3
0
 def put(self, ctx, post_id):
     post = posts.get_post_by_id(post_id)
     if ctx.has_file('content'):
         auth.verify_privilege(ctx.user, 'posts:edit:content')
         posts.update_post_content(post, ctx.get_file('content'))
     if ctx.has_param('tags'):
         auth.verify_privilege(ctx.user, 'posts:edit:tags')
         posts.update_post_tags(post, ctx.get_param_as_list('tags'))
     if ctx.has_param('safety'):
         auth.verify_privilege(ctx.user, 'posts:edit:safety')
         posts.update_post_safety(post, ctx.get_param_as_string('safety'))
     if ctx.has_param('source'):
         auth.verify_privilege(ctx.user, 'posts:edit:source')
         posts.update_post_source(post, ctx.get_param_as_string('source'))
     elif ctx.has_param('contentUrl'):
         posts.update_post_source(post, ctx.get_param_as_string('contentUrl'))
     if ctx.has_param('relations'):
         auth.verify_privilege(ctx.user, 'posts:edit:relations')
         posts.update_post_relations(post, ctx.get_param_as_list('relations'))
     if ctx.has_param('notes'):
         auth.verify_privilege(ctx.user, 'posts:edit:notes')
         posts.update_post_notes(post, ctx.get_param_as_list('notes'))
     if ctx.has_param('flags'):
         auth.verify_privilege(ctx.user, 'posts:edit:flags')
         posts.update_post_flags(post, ctx.get_param_as_list('flags'))
     if ctx.has_file('thumbnail'):
         auth.verify_privilege(ctx.user, 'posts:edit:thumbnail')
         posts.update_post_thumbnail(post, ctx.get_file('thumbnail'))
     post.last_edit_time = datetime.datetime.now()
     ctx.session.flush()
     snapshots.save_entity_modification(post, ctx.user)
     ctx.session.commit()
     tags.export_to_json()
     return posts.serialize_post_with_details(post, ctx.user)
Exemplo n.º 4
0
def test_not_merging_operations_by_different_users(
        fake_datetime, tag_factory, user_factory):
    tag = tag_factory(names=['dummy'])
    user1, user2 = [user_factory(), user_factory()]
    db.session.add_all([tag, user1, user2])
    db.session.flush()
    with fake_datetime('13:00:00'):
        snapshots.save_entity_creation(tag, user1)
        tag.names = [db.TagName('changed')]
        snapshots.save_entity_modification(tag, user2)
    assert db.session.query(db.Snapshot).count() == 2
Exemplo n.º 5
0
def test_not_merging_operations_by_different_users(fake_datetime, tag_factory,
                                                   user_factory):
    tag = tag_factory(names=['dummy'])
    user1, user2 = [user_factory(), user_factory()]
    db.session.add_all([tag, user1, user2])
    db.session.flush()
    with fake_datetime('13:00:00'):
        snapshots.save_entity_creation(tag, user1)
        tag.names = [db.TagName('changed')]
        snapshots.save_entity_modification(tag, user2)
    assert db.session.query(db.Snapshot).count() == 2
Exemplo n.º 6
0
def test_merging_modification_to_creation(tag_factory, user_factory):
    tag = tag_factory(names=['dummy'])
    user = user_factory()
    db.session.add_all([tag, user])
    db.session.flush()
    snapshots.save_entity_creation(tag, user)
    tag.names = [db.TagName('changed')]
    snapshots.save_entity_modification(tag, user)
    results = db.session.query(db.Snapshot).all()
    assert len(results) == 1
    assert results[0].operation == db.Snapshot.OPERATION_CREATED
    assert results[0].data['names'] == ['changed']
Exemplo n.º 7
0
def test_not_merging_due_to_time_difference(fake_datetime, tag_factory,
                                            user_factory):
    tag = tag_factory(names=['dummy'])
    user = user_factory()
    db.session.add_all([tag, user])
    db.session.flush()
    with fake_datetime('13:00:00'):
        snapshots.save_entity_creation(tag, user)
    tag.names = [db.TagName('changed')]
    with fake_datetime('13:10:01'):
        snapshots.save_entity_modification(tag, user)
    assert db.session.query(db.Snapshot).count() == 2
Exemplo n.º 8
0
def test_not_merging_due_to_time_difference(
        fake_datetime, tag_factory, user_factory):
    tag = tag_factory(names=['dummy'])
    user = user_factory()
    db.session.add_all([tag, user])
    db.session.flush()
    with fake_datetime('13:00:00'):
        snapshots.save_entity_creation(tag, user)
    tag.names = [db.TagName('changed')]
    with fake_datetime('13:10:01'):
        snapshots.save_entity_modification(tag, user)
    assert db.session.query(db.Snapshot).count() == 2
Exemplo n.º 9
0
def test_merging_modification_to_creation(tag_factory, user_factory):
    tag = tag_factory(names=['dummy'])
    user = user_factory()
    db.session.add_all([tag, user])
    db.session.flush()
    snapshots.save_entity_creation(tag, user)
    tag.names = [db.TagName('changed')]
    snapshots.save_entity_modification(tag, user)
    results = db.session.query(db.Snapshot).all()
    assert len(results) == 1
    assert results[0].operation == db.Snapshot.OPERATION_CREATED
    assert results[0].data['names'] == ['changed']
Exemplo n.º 10
0
def test_not_adding_snapshot_if_data_doesnt_change(
        fake_datetime, tag_factory, user_factory):
    tag = tag_factory(names=['dummy'])
    user = user_factory()
    db.session.add_all([tag, user])
    db.session.flush()
    with fake_datetime('13:00:00'):
        snapshots.save_entity_creation(tag, user)
    with fake_datetime('14:00:00'):
        snapshots.save_entity_modification(tag, user)
    results = db.session.query(db.Snapshot).all()
    assert len(results) == 1
    assert results[0].operation == db.Snapshot.OPERATION_CREATED
    assert results[0].data['names'] == ['dummy']
Exemplo n.º 11
0
 def post(self, ctx):
     auth.verify_privilege(ctx.user, 'posts:feature')
     post_id = ctx.get_param_as_int('id', required=True)
     post = posts.get_post_by_id(post_id)
     featured_post = posts.try_get_featured_post()
     if featured_post and featured_post.post_id == post.post_id:
         raise posts.PostAlreadyFeaturedError(
             'Post %r is already featured.' % post_id)
     posts.feature_post(post, ctx.user)
     if featured_post:
         snapshots.save_entity_modification(featured_post, ctx.user)
     snapshots.save_entity_modification(post, ctx.user)
     ctx.session.commit()
     return posts.serialize_post_with_details(post, ctx.user)
Exemplo n.º 12
0
def test_not_adding_snapshot_if_data_doesnt_change(fake_datetime, tag_factory,
                                                   user_factory):
    tag = tag_factory(names=['dummy'])
    user = user_factory()
    db.session.add_all([tag, user])
    db.session.flush()
    with fake_datetime('13:00:00'):
        snapshots.save_entity_creation(tag, user)
    with fake_datetime('14:00:00'):
        snapshots.save_entity_modification(tag, user)
    results = db.session.query(db.Snapshot).all()
    assert len(results) == 1
    assert results[0].operation == db.Snapshot.OPERATION_CREATED
    assert results[0].data['names'] == ['dummy']
Exemplo n.º 13
0
 def post(self, ctx):
     auth.verify_privilege(ctx.user, 'posts:feature')
     post_id = ctx.get_param_as_int('id', required=True)
     post = posts.get_post_by_id(post_id)
     featured_post = posts.try_get_featured_post()
     if featured_post and featured_post.post_id == post.post_id:
         raise posts.PostAlreadyFeaturedError(
             'Post %r is already featured.' % post_id)
     posts.feature_post(post, ctx.user)
     if featured_post:
         snapshots.save_entity_modification(featured_post, ctx.user)
     snapshots.save_entity_modification(post, ctx.user)
     ctx.session.commit()
     return posts.serialize_post_with_details(post, ctx.user)
Exemplo n.º 14
0
def test_merging_deletion_all_the_way_deletes_all_snapshots(
        fake_datetime, tag_factory, user_factory, expected_operation):
    tag = tag_factory(names=['dummy'])
    user = user_factory()
    db.session.add_all([tag, user])
    db.session.flush()
    with fake_datetime('13:00:00'):
        snapshots.save_entity_creation(tag, user)
    tag.names = [db.TagName('changed')]
    with fake_datetime('13:00:01'):
        snapshots.save_entity_modification(tag, user)
    tag.names = [db.TagName('changed again')]
    with fake_datetime('13:00:02'):
        snapshots.save_entity_deletion(tag, user)
    assert db.session.query(db.Snapshot).count() == 0
Exemplo n.º 15
0
def test_merging_deletion_all_the_way_deletes_all_snapshots(
        fake_datetime, tag_factory, user_factory, expected_operation):
    tag = tag_factory(names=['dummy'])
    user = user_factory()
    db.session.add_all([tag, user])
    db.session.flush()
    with fake_datetime('13:00:00'):
        snapshots.save_entity_creation(tag, user)
    tag.names = [db.TagName('changed')]
    with fake_datetime('13:00:01'):
        snapshots.save_entity_modification(tag, user)
    tag.names = [db.TagName('changed again')]
    with fake_datetime('13:00:02'):
        snapshots.save_entity_deletion(tag, user)
    assert db.session.query(db.Snapshot).count() == 0
Exemplo n.º 16
0
 def put(self, ctx, category_name):
     category = tag_categories.get_category_by_name(category_name)
     if ctx.has_param('name'):
         auth.verify_privilege(ctx.user, 'tag_categories:edit:name')
         tag_categories.update_category_name(
             category, ctx.get_param_as_string('name'))
     if ctx.has_param('color'):
         auth.verify_privilege(ctx.user, 'tag_categories:edit:color')
         tag_categories.update_category_color(
             category, ctx.get_param_as_string('color'))
     ctx.session.flush()
     snapshots.save_entity_modification(category, ctx.user)
     ctx.session.commit()
     tags.export_to_json()
     return tag_categories.serialize_category_with_details(category)
Exemplo n.º 17
0
def test_get_serialized_history(fake_datetime, tag_factory, user_factory):
    tag = tag_factory(names=['dummy'])
    user = user_factory(name='the-user')
    db.session.add_all([tag, user])
    db.session.flush()
    with fake_datetime('2016-04-19 13:00:00'):
        snapshots.save_entity_creation(tag, user)
    tag.names = [db.TagName('changed')]
    db.session.flush()
    with fake_datetime('2016-04-19 13:10:01'):
        snapshots.save_entity_modification(tag, user)
    assert snapshots.get_serialized_history(tag) == [
        {
            'operation': 'modified',
            'time': datetime.datetime(2016, 4, 19, 13, 10, 1),
            'type': 'tag',
            'id': 'changed',
            'user': '******',
            'data': {
                'names': ['changed'],
                'category': 'dummy',
                'suggestions': [],
                'implications': [],
            },
            'earlier-data': {
                'names': ['dummy'],
                'category': 'dummy',
                'suggestions': [],
                'implications': [],
            },
        },
        {
            'operation': 'created',
            'time': datetime.datetime(2016, 4, 19, 13, 0, 0),
            'type': 'tag',
            'id': 'dummy',
            'user': '******',
            'data': {
                'names': ['dummy'],
                'category': 'dummy',
                'suggestions': [],
                'implications': [],
            },
            'earlier-data': None,
        },
    ]
Exemplo n.º 18
0
def test_merging_resets_merging_time_window(fake_datetime, tag_factory,
                                            user_factory):
    tag = tag_factory(names=['dummy'])
    user = user_factory()
    db.session.add_all([tag, user])
    db.session.flush()
    with fake_datetime('13:00:00'):
        snapshots.save_entity_creation(tag, user)
    tag.names = [db.TagName('changed')]
    with fake_datetime('13:09:59'):
        snapshots.save_entity_modification(tag, user)
    tag.names = [db.TagName('changed again')]
    with fake_datetime('13:19:59'):
        snapshots.save_entity_modification(tag, user)
    results = db.session.query(db.Snapshot).all()
    assert len(results) == 1
    assert results[0].data['names'] == ['changed again']
Exemplo n.º 19
0
def test_merging_resets_merging_time_window(
        fake_datetime, tag_factory, user_factory):
    tag = tag_factory(names=['dummy'])
    user = user_factory()
    db.session.add_all([tag, user])
    db.session.flush()
    with fake_datetime('13:00:00'):
        snapshots.save_entity_creation(tag, user)
    tag.names = [db.TagName('changed')]
    with fake_datetime('13:09:59'):
        snapshots.save_entity_modification(tag, user)
    tag.names = [db.TagName('changed again')]
    with fake_datetime('13:19:59'):
        snapshots.save_entity_modification(tag, user)
    results = db.session.query(db.Snapshot).all()
    assert len(results) == 1
    assert results[0].data['names'] == ['changed again']
Exemplo n.º 20
0
def test_get_serialized_history(fake_datetime, tag_factory, user_factory):
    tag = tag_factory(names=['dummy'])
    user = user_factory(name='the-user')
    db.session.add_all([tag, user])
    db.session.flush()
    with fake_datetime('2016-04-19 13:00:00'):
        snapshots.save_entity_creation(tag, user)
    tag.names = [db.TagName('changed')]
    db.session.flush()
    with fake_datetime('2016-04-19 13:10:01'):
        snapshots.save_entity_modification(tag, user)
    assert snapshots.get_serialized_history(tag) == [
        {
            'operation': 'modified',
            'time': datetime.datetime(2016, 4, 19, 13, 10, 1),
            'type': 'tag',
            'id': 'changed',
            'user': '******',
            'data': {
                'names': ['changed'],
                'category': 'dummy',
                'suggestions': [],
                'implications': [],
            },
            'earlier-data': {
                'names': ['dummy'],
                'category': 'dummy',
                'suggestions': [],
                'implications': [],
            },
        },
        {
            'operation': 'created',
            'time': datetime.datetime(2016, 4, 19, 13, 0, 0),
            'type': 'tag',
            'id': 'dummy',
            'user': '******',
            'data': {
                'names': ['dummy'],
                'category': 'dummy',
                'suggestions': [],
                'implications': [],
            },
            'earlier-data': None,
        },
    ]
Exemplo n.º 21
0
def test_merging_modifications(fake_datetime, tag_factory, user_factory):
    tag = tag_factory(names=['dummy'])
    user = user_factory()
    db.session.add_all([tag, user])
    db.session.flush()
    with fake_datetime('13:00:00'):
        snapshots.save_entity_creation(tag, user)
    tag.names = [db.TagName('changed')]
    with fake_datetime('14:00:00'):
        snapshots.save_entity_modification(tag, user)
    tag.names = [db.TagName('changed again')]
    with fake_datetime('14:00:01'):
        snapshots.save_entity_modification(tag, user)
    results = db.session.query(db.Snapshot).all()
    assert len(results) == 2
    assert results[0].operation == db.Snapshot.OPERATION_CREATED
    assert results[1].operation == db.Snapshot.OPERATION_MODIFIED
    assert results[0].data['names'] == ['dummy']
    assert results[1].data['names'] == ['changed again']
Exemplo n.º 22
0
def test_merging_modifications(fake_datetime, tag_factory, user_factory):
    tag = tag_factory(names=['dummy'])
    user = user_factory()
    db.session.add_all([tag, user])
    db.session.flush()
    with fake_datetime('13:00:00'):
        snapshots.save_entity_creation(tag, user)
    tag.names = [db.TagName('changed')]
    with fake_datetime('14:00:00'):
        snapshots.save_entity_modification(tag, user)
    tag.names = [db.TagName('changed again')]
    with fake_datetime('14:00:01'):
        snapshots.save_entity_modification(tag, user)
    results = db.session.query(db.Snapshot).all()
    assert len(results) == 2
    assert results[0].operation == db.Snapshot.OPERATION_CREATED
    assert results[1].operation == db.Snapshot.OPERATION_MODIFIED
    assert results[0].data['names'] == ['dummy']
    assert results[1].data['names'] == ['changed again']
Exemplo n.º 23
0
 def put(self, ctx, tag_name):
     tag = tags.get_tag_by_name(tag_name)
     if ctx.has_param('names'):
         auth.verify_privilege(ctx.user, 'tags:edit:names')
         tags.update_tag_names(tag, ctx.get_param_as_list('names'))
     if ctx.has_param('category'):
         auth.verify_privilege(ctx.user, 'tags:edit:category')
         tags.update_tag_category_name(tag,
                                       ctx.get_param_as_string('category'))
     if ctx.has_param('suggestions'):
         auth.verify_privilege(ctx.user, 'tags:edit:suggestions')
         tags.update_tag_suggestions(tag,
                                     ctx.get_param_as_list('suggestions'))
     if ctx.has_param('implications'):
         auth.verify_privilege(ctx.user, 'tags:edit:implications')
         tags.update_tag_implications(tag,
                                      ctx.get_param_as_list('implications'))
     tag.last_edit_time = datetime.datetime.now()
     ctx.session.flush()
     snapshots.save_entity_modification(tag, ctx.user)
     ctx.session.commit()
     tags.export_to_json()
     return tags.serialize_tag_with_details(tag)
Exemplo n.º 24
0
 def put(self, ctx, tag_name):
     tag = tags.get_tag_by_name(tag_name)
     if ctx.has_param('names'):
         auth.verify_privilege(ctx.user, 'tags:edit:names')
         tags.update_tag_names(tag, ctx.get_param_as_list('names'))
     if ctx.has_param('category'):
         auth.verify_privilege(ctx.user, 'tags:edit:category')
         tags.update_tag_category_name(
             tag, ctx.get_param_as_string('category'))
     if ctx.has_param('suggestions'):
         auth.verify_privilege(ctx.user, 'tags:edit:suggestions')
         tags.update_tag_suggestions(
             tag, ctx.get_param_as_list('suggestions'))
     if ctx.has_param('implications'):
         auth.verify_privilege(ctx.user, 'tags:edit:implications')
         tags.update_tag_implications(
             tag, ctx.get_param_as_list('implications'))
     tag.last_edit_time = datetime.datetime.now()
     ctx.session.flush()
     snapshots.save_entity_modification(tag, ctx.user)
     ctx.session.commit()
     tags.export_to_json()
     return tags.serialize_tag_with_details(tag)
Exemplo n.º 25
0
 def put(self, ctx, post_id):
     post = posts.get_post_by_id(post_id)
     if ctx.has_file('content'):
         auth.verify_privilege(ctx.user, 'posts:edit:content')
         posts.update_post_content(post, ctx.get_file('content'))
     if ctx.has_param('tags'):
         auth.verify_privilege(ctx.user, 'posts:edit:tags')
         posts.update_post_tags(post, ctx.get_param_as_list('tags'))
     if ctx.has_param('safety'):
         auth.verify_privilege(ctx.user, 'posts:edit:safety')
         posts.update_post_safety(post, ctx.get_param_as_string('safety'))
     if ctx.has_param('source'):
         auth.verify_privilege(ctx.user, 'posts:edit:source')
         posts.update_post_source(post, ctx.get_param_as_string('source'))
     elif ctx.has_param('contentUrl'):
         posts.update_post_source(post,
                                  ctx.get_param_as_string('contentUrl'))
     if ctx.has_param('relations'):
         auth.verify_privilege(ctx.user, 'posts:edit:relations')
         posts.update_post_relations(post,
                                     ctx.get_param_as_list('relations'))
     if ctx.has_param('notes'):
         auth.verify_privilege(ctx.user, 'posts:edit:notes')
         posts.update_post_notes(post, ctx.get_param_as_list('notes'))
     if ctx.has_param('flags'):
         auth.verify_privilege(ctx.user, 'posts:edit:flags')
         posts.update_post_flags(post, ctx.get_param_as_list('flags'))
     if ctx.has_file('thumbnail'):
         auth.verify_privilege(ctx.user, 'posts:edit:thumbnail')
         posts.update_post_thumbnail(post, ctx.get_file('thumbnail'))
     post.last_edit_time = datetime.datetime.now()
     ctx.session.flush()
     snapshots.save_entity_modification(post, ctx.user)
     ctx.session.commit()
     tags.export_to_json()
     return posts.serialize_post_with_details(post, ctx.user)