diff --git a/pybossa/view/projects.py b/pybossa/view/projects.py index 61649a6499..606e3b0486 100644 --- a/pybossa/view/projects.py +++ b/pybossa/view/projects.py @@ -3244,7 +3244,7 @@ def coowners(short_name): # remove any contacts that were just removed from coowners. for _id in delete: - if _id in project.info.get('contacts', []): + if _id in new_list: new_list.remove(_id) auditlogger.log_event(project, current_user, 'update', 'project.contacts', diff --git a/test/test_coowners.py b/test/test_coowners.py index 6cc8e5f546..eb0290444d 100644 --- a/test/test_coowners.py +++ b/test/test_coowners.py @@ -235,7 +235,7 @@ def test_coowner_add_contact(self): assert res_data['coowners_dict'][1]['id'] == user3.id # Add user3 (coowner) as a contact. - data = {'contacts': [user2.id, user3.id]} + data = {'coowners': project.owners_ids, '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), @@ -248,6 +248,71 @@ def test_coowner_add_contact(self): assert res_data['contacts_dict'][0]['id'] == user2.id assert res_data['contacts_dict'][1]['id'] == user3.id + @with_context + def test_coowner_remove_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 = {'coowners': project.owners_ids, '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 + + # Remove user3 as a coowner. + data = {'coowners': [user2.id], '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 is only user included in contacts. + assert len(res_data['contacts_dict']) == 1 + assert res_data['contacts_dict'][0]['id'] == user2.id + + # Verify coowner has been removed. + assert len(res_data['coowners_dict']) == 1 + assert res_data['coowners_dict'][0]['id'] == user2.id + + # Verify contact has been removed. + assert len(res_data['contacts_dict']) == 1 + assert res_data['contacts_dict'][0]['id'] == user2.id + @with_context def test_user_search_found(self): from pybossa.core import project_repo