Skip to content

Commit

Permalink
Fix group activity stream to show deleted datasets. Add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
David Read committed Jan 18, 2019
1 parent 619d54f commit 2d45fd8
Show file tree
Hide file tree
Showing 3 changed files with 231 additions and 8 deletions.
15 changes: 12 additions & 3 deletions ckan/model/activity.py
Expand Up @@ -204,7 +204,6 @@ def _group_activity_query(group_id):
model.Member,
and_(
model.Activity.object_id == model.Member.table_id,
model.Member.state == 'active'
)
).outerjoin(
model.Package,
Expand All @@ -219,8 +218,18 @@ def _group_activity_query(group_id):
# to a group but was then removed will not show up. This may not be
# desired but is consistent with legacy behaviour.
or_(
model.Member.group_id == group_id,
model.Activity.object_id == group_id
# active dataset in the group
and_(model.Member.group_id == group_id,
model.Member.state == 'active',
model.Package.state == 'active'),
# deleted dataset in the group
and_(model.Member.group_id == group_id,
model.Member.state == 'deleted',
model.Package.state == 'deleted'),
# (we want to avoid showing changes to an active dataset that
# was once in this group)
# activity the the group itself
model.Activity.object_id == group_id,
)
)

Expand Down
154 changes: 154 additions & 0 deletions ckan/tests/controllers/test_group.py
Expand Up @@ -758,3 +758,157 @@ def test_group_index(self):
assert_in('Test Group {0}'.format(i), response)

assert 'Test Group 20' not in response


class TestActivity(helpers.FunctionalTestBase):
def test_simple(self):
'''Checking the template shows the activity stream.'''
app = self._get_test_app()
user = factories.User()
group = factories.Group(user=user)

url = url_for('group.activity',
id=group['id'])
response = app.get(url)
assert_in('Mr. Test User', response)
assert_in('created the group', response)

def test_create_group(self):
app = self._get_test_app()
user = factories.User()
group = factories.Group(user=user)

url = url_for('group.activity',
id=group['id'])
response = app.get(url)
assert_in('<a href="/user/{}">Mr. Test User'.format(user['name']),
response)
assert_in('created the group', response)
assert_in('<a href="/group/{}">Test Group'.format(
group['name']), response)

def _clear_activities(self):
model.Session.query(model.ActivityDetail).delete()
model.Session.query(model.Activity).delete()
model.Session.flush()

def test_change_group(self):
app = self._get_test_app()
user = factories.User()
group = factories.Group(user=user)
self._clear_activities()
group['title'] = 'Group with changed title'
helpers.call_action(
'group_update', context={'user': user['name']}, **group)

url = url_for('group.activity',
id=group['id'])
response = app.get(url)
assert_in('<a href="/user/{}">Mr. Test User'.format(user['name']),
response)
assert_in('updated the group', response)
assert_in('<a href="/group/{}">Group with changed title'
.format(group['name']), response)

def test_create_dataset(self):
app = self._get_test_app()
user = factories.User()
group = factories.Group(user=user)
dataset = factories.Dataset(groups=[{'id': group['id']}], user=user)

url = url_for('group.activity',
id=group['id'])
response = app.get(url)
assert_in('<a href="/user/{}">Mr. Test User'.format(user['name']),
response)
assert_in('created the dataset', response)
assert_in('<a href="/dataset/{}">Test Dataset'.format(dataset['name']),
response)

def test_change_dataset(self):
app = self._get_test_app()
user = factories.User()
group = factories.Group(user=user)
dataset = factories.Dataset(groups=[{'id': group['id']}], user=user)
self._clear_activities()
dataset['title'] = 'Dataset with changed title'
helpers.call_action(
'package_update', context={'user': user['name']}, **dataset)

url = url_for('group.activity',
id=group['id'])
response = app.get(url)
assert_in('<a href="/user/{}">Mr. Test User'.format(user['name']),
response)
assert_in('updated the dataset', response)
assert_in('<a href="/dataset/{}">Dataset with changed title'
.format(dataset['name']),
response)

def test_delete_dataset(self):
app = self._get_test_app()
user = factories.User()
group = factories.Group(user=user)
dataset = factories.Dataset(groups=[{'id': group['id']}], user=user)
self._clear_activities()
helpers.call_action(
'package_delete', context={'user': user['name']}, **dataset)

url = url_for('group.activity',
id=group['id'])
response = app.get(url)
assert_in('<a href="/user/{}">Mr. Test User'.format(user['name']),
response)
assert_in('deleted the dataset', response)
assert_in('<a href="/dataset/{}">Test Dataset'
.format(dataset['name']),
response)

def test_change_dataset_that_used_to_be_in_the_group(self):
app = self._get_test_app()
user = factories.User()
group = factories.Group(user=user)
dataset = factories.Dataset(groups=[{'id': group['id']}], user=user)
# remove the dataset from the group
dataset['groups'] = []
helpers.call_action(
'package_update', context={'user': user['name']}, **dataset)
self._clear_activities()
# edit the dataset
dataset['title'] = 'Dataset with changed title'
helpers.call_action(
'package_update', context={'user': user['name']}, **dataset)

# dataset change should not show up in its former group
url = url_for('group.activity',
id=group['id'])
response = app.get(url)
assert_in('No activities are within this activity stream', response)

def test_delete_dataset_that_used_to_be_in_the_group(self):
app = self._get_test_app()
user = factories.User()
group = factories.Group(user=user)
dataset = factories.Dataset(groups=[{'id': group['id']}], user=user)
# remove the dataset from the group
dataset['groups'] = []
helpers.call_action(
'package_update', context={'user': user['name']}, **dataset)
self._clear_activities()
dataset['title'] = 'Dataset with changed title'
helpers.call_action(
'package_delete', context={'user': user['name']}, **dataset)

# NOTE:
# ideally the dataset's deletion would not show up in its old group
# but it can't be helped without _group_activity_query getting v
# complicated
url = url_for('group.activity',
id=group['id'])
response = app.get(url)
assert_in('<a href="/user/{}">Mr. Test User'.format(user['name']),
response)
assert_in('deleted the dataset', response)
assert_in('<a href="/dataset/{}">Test Dataset'
.format(dataset['name']),
response)
70 changes: 65 additions & 5 deletions ckan/tests/controllers/test_organization.py
Expand Up @@ -600,6 +600,29 @@ def test_create_organization(self):
assert_in('<a href="/organization/{}">Test Organization'.format(
org['name']), response)

def _clear_activities(self):
model.Session.query(model.ActivityDetail).delete()
model.Session.query(model.Activity).delete()
model.Session.flush()

def test_change_organization(self):
app = self._get_test_app()
user = factories.User()
org = factories.Organization(user=user)
self._clear_activities()
org['title'] = 'Organization with changed title'
helpers.call_action(
'organization_update', context={'user': user['name']}, **org)

url = url_for('organization.activity',
id=org['id'])
response = app.get(url)
assert_in('<a href="/user/{}">Mr. Test User'.format(user['name']),
response)
assert_in('updated the organization', response)
assert_in('<a href="/organization/{}">Organization with changed title'
.format(org['name']), response)

def test_create_dataset(self):
app = self._get_test_app()
user = factories.User()
Expand All @@ -615,11 +638,6 @@ def test_create_dataset(self):
assert_in('<a href="/dataset/{}">Test Dataset'.format(dataset['name']),
response)

def _clear_activities(self):
model.Session.query(model.ActivityDetail).delete()
model.Session.query(model.Activity).delete()
model.Session.flush()

def test_change_dataset(self):
app = self._get_test_app()
user = factories.User()
Expand Down Expand Up @@ -658,3 +676,45 @@ def test_delete_dataset(self):
assert_in('<a href="/dataset/{}">Test Dataset'
.format(dataset['name']),
response)

def test_change_dataset_that_used_to_be_in_the_org(self):
app = self._get_test_app()
user = factories.User()
org = factories.Organization(user=user)
org2 = factories.Organization(user=user)
dataset = factories.Dataset(owner_org=org['id'], user=user)
# remove the dataset from the org
dataset['owner_org'] = org2['id']
helpers.call_action(
'package_update', context={'user': user['name']}, **dataset)
self._clear_activities()
# edit the dataset
dataset['title'] = 'Dataset with changed title'
helpers.call_action(
'package_update', context={'user': user['name']}, **dataset)

# dataset change should not show up in its former org
url = url_for('organization.activity',
id=org['id'])
response = app.get(url)
assert_in('No activities are within this activity stream', response)

def test_delete_dataset_that_used_to_be_in_the_org(self):
app = self._get_test_app()
user = factories.User()
org = factories.Organization(user=user)
org2 = factories.Organization(user=user)
dataset = factories.Dataset(owner_org=org['id'], user=user)
# remove the dataset from the org
dataset['owner_org'] = org2['id']
helpers.call_action(
'package_update', context={'user': user['name']}, **dataset)
self._clear_activities()
dataset['title'] = 'Dataset with changed title'
helpers.call_action(
'package_delete', context={'user': user['name']}, **dataset)

url = url_for('organization.activity',
id=org['id'])
response = app.get(url)
assert_in('No activities are within this activity stream', response)

0 comments on commit 2d45fd8

Please sign in to comment.