Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into 2541-group-search-t…
Browse files Browse the repository at this point in the history
…ests

Conflicts:
	ckan/tests/controllers/test_group.py
  • Loading branch information
David Read committed Sep 9, 2015
2 parents d80321c + 35ca145 commit a16261d
Show file tree
Hide file tree
Showing 5 changed files with 434 additions and 5 deletions.
2 changes: 1 addition & 1 deletion ckan/templates/group/member_new.html
Expand Up @@ -10,7 +10,7 @@ <h1 class="page-heading">
{% block page_heading %}{{ _('Edit Member') if user else _('Add Member') }}{% endblock %}
</h1>
{% block form %}
<form class="dataset-form form-horizontal add-member-form" method='post'>
<form class="dataset-form form-horizontal add-member-form" method='post' id="add-member-form">
<div class="row-fluid">
<div class="control-group control-medium">
{% if not user %}
Expand Down
2 changes: 1 addition & 1 deletion ckan/templates/group/members.html
Expand Up @@ -8,7 +8,7 @@

{% block primary_content_inner %}
<h3 class="page-heading">{{ _('{0} members'.format(c.members|length)) }}</h3>
<table class="table table-header table-hover table-bordered">
<table class="table table-header table-hover table-bordered" id="member-table">
<col width="70" />
<col width="40" />
<col width="20" />
Expand Down
143 changes: 143 additions & 0 deletions ckan/tests/controllers/test_group.py
Expand Up @@ -232,6 +232,149 @@ def test_anon_user_trying_to_delete_fails(self):
assert_equal(group['state'], 'active')


class TestGroupMembership(helpers.FunctionalTestBase):

def _create_group(self, owner_username, users=None):
'''Create a group with the owner defined by owner_username and
optionally with a list of other users.'''
if users is None:
users = []
context = {'user': owner_username, 'ignore_auth': True, }
group = helpers.call_action('group_create', context=context,
name='test-group', users=users)
return group

def _get_group_add_member_page(self, app, user, group_name):
env = {'REMOTE_USER': user['name'].encode('ascii')}
url = url_for(controller='group',
action='member_new',
id=group_name)
response = app.get(url=url, extra_environ=env)
return env, response

def test_membership_list(self):
'''List group admins and members'''
app = self._get_test_app()
user_one = factories.User(fullname='User One', name='user-one')
user_two = factories.User(fullname='User Two')

other_users = [
{'name': user_two['id'], 'capacity': 'member'}
]

group = self._create_group(user_one['name'], other_users)

member_list_url = url_for(controller='group', action='members',
id=group['id'])
member_list_response = app.get(member_list_url)

assert_true('2 members' in member_list_response)

member_response_html = BeautifulSoup(member_list_response.body)
user_names = [u.string for u in
member_response_html.select('#member-table td.media a')]
roles = [r.next_sibling.next_sibling.string
for r
in member_response_html.select('#member-table td.media')]

user_roles = dict(zip(user_names, roles))

assert_equal(user_roles['User One'], 'Admin')
assert_equal(user_roles['User Two'], 'Member')

def test_membership_add(self):
'''Member can be added via add member page'''
app = self._get_test_app()
owner = factories.User(fullname='My Owner')
factories.User(fullname="My Fullname", name='my-user')
group = self._create_group(owner['name'])

env, response = self._get_group_add_member_page(app,
owner,
group['name'])

add_form = response.forms['add-member-form']
add_form['username'] = 'my-user'
add_response = submit_and_follow(app, add_form, env, 'save')

assert_true('2 members' in add_response)

add_response_html = BeautifulSoup(add_response.body)
user_names = [u.string for u in
add_response_html.select('#member-table td.media a')]
roles = [r.next_sibling.next_sibling.string
for r in add_response_html.select('#member-table td.media')]

user_roles = dict(zip(user_names, roles))

assert_equal(user_roles['My Owner'], 'Admin')
assert_equal(user_roles['My Fullname'], 'Member')

def test_admin_add(self):
'''Admin can be added via add member page'''
app = self._get_test_app()
owner = factories.User(fullname='My Owner')
factories.User(fullname="My Fullname", name='my-user')
group = self._create_group(owner['name'])

env, response = self._get_group_add_member_page(app,
owner,
group['name'])

add_form = response.forms['add-member-form']
add_form['username'] = 'my-user'
add_form['role'] = 'admin'
add_response = submit_and_follow(app, add_form, env, 'save')

assert_true('2 members' in add_response)

add_response_html = BeautifulSoup(add_response.body)
user_names = [u.string for u in
add_response_html.select('#member-table td.media a')]
roles = [r.next_sibling.next_sibling.string
for r in add_response_html.select('#member-table td.media')]

user_roles = dict(zip(user_names, roles))

assert_equal(user_roles['My Owner'], 'Admin')
assert_equal(user_roles['My Fullname'], 'Admin')

def test_remove_member(self):
'''Member can be removed from group'''
app = self._get_test_app()
user_one = factories.User(fullname='User One', name='user-one')
user_two = factories.User(fullname='User Two')

other_users = [
{'name': user_two['id'], 'capacity': 'member'}
]

group = self._create_group(user_one['name'], other_users)

remove_url = url_for(controller='group', action='member_delete',
user=user_two['id'], id=group['id'])

env = {'REMOTE_USER': user_one['name'].encode('ascii')}
remove_response = app.post(remove_url, extra_environ=env, status=302)
# redirected to member list after removal
remove_response = remove_response.follow()

assert_true('Group member has been deleted.' in remove_response)
assert_true('1 members' in remove_response)

remove_response_html = BeautifulSoup(remove_response.body)
user_names = [u.string for u in
remove_response_html.select('#member-table td.media a')]
roles = [r.next_sibling.next_sibling.string
for r in
remove_response_html.select('#member-table td.media')]

user_roles = dict(zip(user_names, roles))

assert_equal(len(user_roles.keys()), 1)
assert_equal(user_roles['User One'], 'Admin')


class TestGroupFollow(helpers.FunctionalTestBase):

def test_group_follow(self):
Expand Down
166 changes: 166 additions & 0 deletions ckan/tests/controllers/test_organization.py
@@ -1,3 +1,4 @@
from bs4 import BeautifulSoup
from nose.tools import assert_equal, assert_true
from routes import url_for

Expand Down Expand Up @@ -259,3 +260,168 @@ def test_delete(self):
for dataset in datasets:
d = helpers.call_action('package_show', id=dataset['id'])
assert_equal(d['state'], 'deleted')


class TestOrganizationSearch(helpers.FunctionalTestBase):

'''Test searching for organizations.'''

def setup(self):
super(TestOrganizationSearch, self).setup()
self.app = self._get_test_app()
factories.Organization(name='org-one', title='AOrg One')
factories.Organization(name='org-two', title='AOrg Two')
factories.Organization(name='org-three', title='Org Three')
self.search_url = url_for(controller='organization', action='index')

def test_organization_search(self):
'''Requesting organization search (index) returns list of
organizations and search form.'''

index_response = self.app.get(self.search_url)
index_response_html = BeautifulSoup(index_response.body)
org_names = index_response_html.select('ul.media-grid '
'li.media-item '
'h3.media-heading')
org_names = [n.string for n in org_names]

assert_equal(len(org_names), 3)
assert_true('AOrg One' in org_names)
assert_true('AOrg Two' in org_names)
assert_true('Org Three' in org_names)

def test_organization_search_results(self):
'''Searching via organization search form returns list of expected
organizations.'''

index_response = self.app.get(self.search_url)
search_form = index_response.forms['organization-search-form']
search_form['q'] = 'AOrg'
search_response = webtest_submit(search_form)

search_response_html = BeautifulSoup(search_response.body)
org_names = search_response_html.select('ul.media-grid '
'li.media-item '
'h3.media-heading')
org_names = [n.string for n in org_names]

assert_equal(len(org_names), 2)
assert_true('AOrg One' in org_names)
assert_true('AOrg Two' in org_names)
assert_true('Org Three' not in org_names)

def test_organization_search_no_results(self):
'''Searching with a term that doesn't apply returns no results.'''

index_response = self.app.get(self.search_url)
search_form = index_response.forms['organization-search-form']
search_form['q'] = 'No Results Here'
search_response = webtest_submit(search_form)

search_response_html = BeautifulSoup(search_response.body)
org_names = search_response_html.select('ul.media-grid '
'li.media-item '
'h3.media-heading')
org_names = [n.string for n in org_names]

assert_equal(len(org_names), 0)
assert_true("No organizations found for &#34;No Results Here&#34;"
in search_response)


class TestOrganizationInnerSearch(helpers.FunctionalTestBase):

'''Test searching within an organization.'''

def test_organization_search_within_org(self):
'''Organization read page request returns list of datasets owned by
organization.'''
app = self._get_test_app()

org = factories.Organization()
factories.Dataset(name="ds-one", title="Dataset One",
owner_org=org['id'])
factories.Dataset(name="ds-two", title="Dataset Two",
owner_org=org['id'])
factories.Dataset(name="ds-three", title="Dataset Three",
owner_org=org['id'])

org_url = url_for(controller='organization', action='read',
id=org['id'])
org_response = app.get(org_url)
org_response_html = BeautifulSoup(org_response.body)

ds_titles = org_response_html.select('.dataset-list '
'.dataset-item '
'.dataset-heading a')
ds_titles = [t.string for t in ds_titles]

assert_true('3 datasets found' in org_response)
assert_equal(len(ds_titles), 3)
assert_true('Dataset One' in ds_titles)
assert_true('Dataset Two' in ds_titles)
assert_true('Dataset Three' in ds_titles)

def test_organization_search_within_org_results(self):
'''Searching within an organization returns expected dataset
results.'''
app = self._get_test_app()

org = factories.Organization()
factories.Dataset(name="ds-one", title="Dataset One",
owner_org=org['id'])
factories.Dataset(name="ds-two", title="Dataset Two",
owner_org=org['id'])
factories.Dataset(name="ds-three", title="Dataset Three",
owner_org=org['id'])

org_url = url_for(controller='organization', action='read',
id=org['id'])
org_response = app.get(org_url)
search_form = org_response.forms['organization-datasets-search-form']
search_form['q'] = 'One'
search_response = webtest_submit(search_form)
assert_true('1 dataset found for &#34;One&#34;' in search_response)

search_response_html = BeautifulSoup(search_response.body)

ds_titles = search_response_html.select('.dataset-list '
'.dataset-item '
'.dataset-heading a')
ds_titles = [t.string for t in ds_titles]

assert_equal(len(ds_titles), 1)
assert_true('Dataset One' in ds_titles)
assert_true('Dataset Two' not in ds_titles)
assert_true('Dataset Three' not in ds_titles)

def test_organization_search_within_org_no_results(self):
'''Searching for non-returning phrase within an organization returns
no results.'''
app = self._get_test_app()

org = factories.Organization()
factories.Dataset(name="ds-one", title="Dataset One",
owner_org=org['id'])
factories.Dataset(name="ds-two", title="Dataset Two",
owner_org=org['id'])
factories.Dataset(name="ds-three", title="Dataset Three",
owner_org=org['id'])

org_url = url_for(controller='organization', action='read',
id=org['id'])
org_response = app.get(org_url)
search_form = org_response.forms['organization-datasets-search-form']
search_form['q'] = 'Nout'
search_response = webtest_submit(search_form)

assert_true('No datasets found for &#34;Nout&#34;' in search_response)

search_response_html = BeautifulSoup(search_response.body)

ds_titles = search_response_html.select('.dataset-list '
'.dataset-item '
'.dataset-heading a')
ds_titles = [t.string for t in ds_titles]

assert_equal(len(ds_titles), 0)

0 comments on commit a16261d

Please sign in to comment.