Skip to content

Commit

Permalink
Merge 3fb42e5 into fd29f97
Browse files Browse the repository at this point in the history
  • Loading branch information
kbecker42 committed Sep 15, 2022
2 parents fd29f97 + 3fb42e5 commit 5d25d6d
Show file tree
Hide file tree
Showing 2 changed files with 226 additions and 22 deletions.
29 changes: 29 additions & 0 deletions pybossa/view/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -3170,6 +3170,7 @@ def coowners(short_name):

ensure_authorized_to('read', project)
ensure_authorized_to('update', project)

response = dict(
template='/projects/coowners.html',
project=sanitize_project,
Expand All @@ -3188,9 +3189,21 @@ def coowners(short_name):
if form.user.data:
# search users
query = form.user.data
params = json.loads(request.data)

filters = {'enabled': True}

# Search all users.
users = user_repo.search_by_name(query, **filters)

# If searching contacts, filter results to only coowners.
if params.get('contact'):
filtered_list = []
for user in users:
if user.id in project.owners_ids:
filtered_list.append(user)
users = filtered_list

if not users:
markup = Markup('<strong>{}</strong> {} <strong>{}</strong>')
flash(markup.format(gettext("Ooops!"),
Expand All @@ -3215,19 +3228,35 @@ def coowners(short_name):
# delete ids that don't exist anymore
delete = [value for value in old_list if value not in overlap_list]
for _id in delete:
# Remove the coowner.
project.owners_ids.remove(_id)
# add ids that weren't there
add = [value for value in new_list if value not in overlap_list]
for _id in add:
project.owners_ids.append(_id)

# Update coowners_dict in response.
coowners_dict = [{"id": user.id, "fullname": user.fullname} for user in user_repo.get_users(project.owners_ids)]
response['coowners_dict'] = coowners_dict

# save contacts
new_list = [int(x) for x in json.loads(request.data).get('contacts', [])]

# remove any contacts that were just removed from coowners.
for _id in delete:
if _id in project.info.get('contacts', []):
new_list.remove(_id)

auditlogger.log_event(project, current_user, 'update', 'project.contacts',
project.info.get('contacts'), new_list)
project.info['contacts'] = new_list

project_repo.save(project)

# Update contacts_dict in response.
contacts_dict = [{"id": user.id, "fullname": user.fullname} for user in user_repo.get_users(project.info['contacts'])]
response['contacts_dict'] = contacts_dict

flash(gettext('Configuration updated successfully'), 'success')

return handle_content_type(response)
Expand Down
219 changes: 197 additions & 22 deletions test/test_coowners.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import json
from test.helper import web
from test import with_context
from test import db, Fixtures
from test.factories import ProjectFactory, UserFactory
from pybossa.core import user_repo
from pybossa.repositories import ProjectRepository


Expand Down Expand Up @@ -173,28 +176,200 @@ def test_coowner_can(self):

@with_context
def test_user_search(self):
self.register()
self.signin()
self.register(name="John2", email="john2@john.com",
password="passwd")
self.app.get("/admin/users/addsubadmin/2", follow_redirects=True)
self.register(name="John3", email="john3@john.com",
password="passwd")
self.app.get("/admin/users/addsubadmin/3", follow_redirects=True)
self.signin(email="john2@john.com", password="passwd")
self.new_project()
from pybossa.core import project_repo

data = {'user': 'johnny9'}
res = self.app.post('/project/sampleapp/coowners',
data=data,
follow_redirects=True)
assert "We didn&#39;t find any enabled user matching your query" in str(res.data), res.data
admin, user2, user3, user4 = UserFactory.create_batch(4)

data = {'user': 'John3'}
res = self.app.post('/project/sampleapp/coowners',
data=data,
follow_redirects=True)
assert "/project/sampleapp/add_coowner/John3" in str(res.data), res.data
project = ProjectFactory.create(owner=user2, published=True, short_name='sampleapp')
project.owners_ids.append(user3.id)
project_repo.save(project)

csrf = self.get_csrf('/account/signin')
self.signin(email=admin.email_addr, csrf=csrf)

data = {'user': user2.name}
res = self.app.post('/project/%s/coowners?api_key=%s' % (project.short_name, admin.api_key),
content_type='application/json',
data=json.dumps(data),
follow_redirects=True,
headers={'X-CSRFToken': csrf})
res_data = json.loads(res.data)

# Verify project owner is only user included in contacts.
assert len(res_data['contacts_dict']) == 1
assert res_data['contacts_dict'][0]['id'] == user2.id

# Verify project coowner is included in coowners.
assert len(res_data['coowners_dict']) == 2
assert res_data['coowners_dict'][0]['id'] == user2.id
assert res_data['coowners_dict'][1]['id'] == user3.id

@with_context
def test_coowner_add_contact(self):
from pybossa.core import project_repo

admin, user2, user3, user4 = UserFactory.create_batch(4)

project = ProjectFactory.create(owner=user2, published=True, short_name='sampleapp')
project.owners_ids.append(user3.id)
project_repo.save(project)

csrf = self.get_csrf('/account/signin')
self.signin(email=admin.email_addr, csrf=csrf)

data = {'user': user3.name}
res = self.app.post('/project/%s/coowners?api_key=%s' % (project.short_name, admin.api_key),
content_type='application/json',
data=json.dumps(data),
follow_redirects=True,
headers={'X-CSRFToken': csrf})
res_data = json.loads(res.data)

# Verify project owner is only user included in contacts.
assert len(res_data['contacts_dict']) == 1
assert res_data['contacts_dict'][0]['id'] == user2.id

# Verify project coowner is included in coowners.
assert len(res_data['coowners_dict']) == 2
assert res_data['coowners_dict'][0]['id'] == user2.id
assert res_data['coowners_dict'][1]['id'] == user3.id

# Add user3 (coowner) as a contact.
data = {'contacts': [user2.id, user3.id]}
res = self.app.post('/project/%s/coowners?api_key=%s' % (project.short_name, admin.api_key),
content_type='application/json',
data=json.dumps(data),
follow_redirects=True,
headers={'X-CSRFToken': csrf})
res_data = json.loads(res.data)

# Verify project owner and coowner are included in contacts.
assert len(res_data['contacts_dict']) == 2
assert res_data['contacts_dict'][0]['id'] == user2.id
assert res_data['contacts_dict'][1]['id'] == user3.id

@with_context
def test_user_search_found(self):
from pybossa.core import project_repo

admin, user2, user3, user4 = UserFactory.create_batch(4)

project = ProjectFactory.create(owner=user2, published=True, short_name='sampleapp')
project.owners_ids.append(user3.id)
project_repo.save(project)

csrf = self.get_csrf('/account/signin')
self.signin(email=admin.email_addr, csrf=csrf)

data = {'user': user3.name}
res = self.app.post('/project/%s/coowners?api_key=%s' % (project.short_name, admin.api_key),
content_type='application/json',
data=json.dumps(data),
follow_redirects=True,
headers={'X-CSRFToken': csrf})
res_data = json.loads(res.data)

# Verify user3 is found in result.
assert len(res_data['found']) == 1
assert (res_data['found'][0]['id'] == user3.id)

@with_context
def test_user_search_not_found(self):
from pybossa.core import project_repo

admin, user2, user3, user4 = UserFactory.create_batch(4)

project = ProjectFactory.create(owner=user2, published=True, short_name='sampleapp')
project.owners_ids.append(user3.id)
project_repo.save(project)

csrf = self.get_csrf('/account/signin')
self.signin(email=admin.email_addr, csrf=csrf)

data = {'user': 'not exist'}
res = self.app.post('/project/%s/coowners?api_key=%s' % (project.short_name, admin.api_key),
content_type='application/json',
data=json.dumps(data),
follow_redirects=True,
headers={'X-CSRFToken': csrf})
res_data = json.loads(res.data)

# Verify no results
assert len(res_data['found']) == 0

@with_context
def test_user_search_partial_found(self):
from pybossa.core import project_repo

admin, user2, user3, user4 = UserFactory.create_batch(4)

project = ProjectFactory.create(owner=user2, published=True, short_name='sampleapp')
project.owners_ids.append(user3.id)
project_repo.save(project)

csrf = self.get_csrf('/account/signin')
self.signin(email=admin.email_addr, csrf=csrf)

data = {'user': 'User'}
res = self.app.post('/project/%s/coowners?api_key=%s' % (project.short_name, admin.api_key),
content_type='application/json',
data=json.dumps(data),
follow_redirects=True,
headers={'X-CSRFToken': csrf})
res_data = json.loads(res.data)

# Verify results (admin, user2, user3, user4)
assert len(res_data['found']) == 4

@with_context
def test_user_search_contact_found(self):
from pybossa.core import project_repo

admin, user2, user3, user4 = UserFactory.create_batch(4)

project = ProjectFactory.create(owner=user2, published=True, short_name='sampleapp')
project.owners_ids.append(user3.id)
project_repo.save(project)

csrf = self.get_csrf('/account/signin')
self.signin(email=admin.email_addr, csrf=csrf)

data = {'user': 'User', 'contact': True}
res = self.app.post('/project/%s/coowners?api_key=%s' % (project.short_name, admin.api_key),
content_type='application/json',
data=json.dumps(data),
follow_redirects=True,
headers={'X-CSRFToken': csrf})
res_data = json.loads(res.data)

# Verify results (user2, user3)
assert len(res_data['found']) == 2
assert res_data['found'][0]['id'] == user2.id
assert res_data['found'][1]['id'] == user3.id

@with_context
def test_user_search_contact_not_found(self):
from pybossa.core import project_repo

admin, user2, user3, user4 = UserFactory.create_batch(4)

project = ProjectFactory.create(owner=user2, published=True, short_name='sampleapp')
project.owners_ids.append(user3.id)
project_repo.save(project)

csrf = self.get_csrf('/account/signin')
self.signin(email=admin.email_addr, csrf=csrf)

data = {'user': user4.name, 'contact': True}
res = self.app.post('/project/%s/coowners?api_key=%s' % (project.short_name, admin.api_key),
content_type='application/json',
data=json.dumps(data),
follow_redirects=True,
headers={'X-CSRFToken': csrf})
res_data = json.loads(res.data)

# Verify no results.
assert len(res_data['found']) == 0

@with_context
def test_coowner_invalid(self):
Expand Down Expand Up @@ -223,8 +398,8 @@ def test_creator_is_added_as_owner(self):
from pybossa.core import project_repo
self.register()
project = Fixtures.create_project({
'passwd_hash': 'hello',
'data_classification': dict(input_data="L4 - public", output_data="L4 - public"),
'passwd_hash': 'hello',
'data_classification': dict(input_data="L4 - public", output_data="L4 - public"),
'kpi': 0.5,
'product': 'abc',
'subproduct': 'def'
Expand Down

0 comments on commit 5d25d6d

Please sign in to comment.