def test_editing_project_category(): user, organization, project, client = setup() project.add_manage(user) assert login(client, user) category = Category.objects.create(name='Test Category', project=project) response = client.post( path=reverse('projects:edit_category', args=[project.pk, category.pk]), data={ 'name': 'Another Name', }, follow=True ) assert response.redirect_chain[0][0] == reverse('projects:edit_category', args=[project.pk, category.pk]) assert response.redirect_chain[0][1] == HTTP_302_FOUND category.refresh_from_db() assert category.name == 'Another Name' # Bad data response = client.post( path=reverse('projects:edit_category', args=[project.pk, category.pk]), data={ 'foo': 'bar', }, follow=True ) assert response.status_code == HTTP_200_OK category.refresh_from_db() assert category.name == 'Another Name'
def test_creating_project_category(): user, organization, project, client = setup() project.add_manage(user) assert login(client, user) assert len(project.category_set.all()) == 0 response = client.post( path=reverse('projects:new_category', args=[project.pk]), data={ 'name': 'Test Category', }, follow=True ) assert response.redirect_chain[0][0] == reverse('projects:categories', args=[project.pk]) assert response.redirect_chain[0][1] == HTTP_302_FOUND assert len(project.category_set.all()) == 1 assert project.category_set.first().name == 'Test Category' # Bad data response = client.post( path=reverse('projects:new_category', args=[project.pk]), data={ 'foo': 'bar', }, follow=True ) assert response.status_code == HTTP_200_OK assert len(project.category_set.all()) == 1 assert project.category_set.first().name == 'Test Category'
def test_updated_details(): user, organization, project, client = setup() project.add_manage(user) assert login(client, user) # Valid case category = Category.objects.create(name='Test Category', project=project) response = client.post( path=reverse('projects:details', args=[project.pk]), data={ 'name': 'Test Project', 'description': 'Test Description', 'tags': ['test, tags'] }, follow=True ) assert response.redirect_chain[0][0] == reverse('projects:details', args=[project.pk]) assert response.redirect_chain[0][1] == HTTP_302_FOUND project.refresh_from_db() assert project.name == 'Test Project' assert project.description == 'Test Description' assert len(project.tags.all()) == 2 # Invalid form response = client.post( path=reverse('projects:details', args=[project.pk]), data={ 'foo': 'bar', } ) assert response.status_code == HTTP_200_OK
def test_editing_project_category(): user, organization, project, client = setup() project.add_manage(user) assert login(client, user) category = Category.objects.create(name='Test Category', project=project) response = client.post(path=reverse('projects:edit_category', args=[project.pk, category.pk]), data={ 'name': 'Another Name', }, follow=True) assert response.redirect_chain[0][0] == reverse( 'projects:edit_category', args=[project.pk, category.pk]) assert response.redirect_chain[0][1] == HTTP_302_FOUND category.refresh_from_db() assert category.name == 'Another Name' # Bad data response = client.post(path=reverse('projects:edit_category', args=[project.pk, category.pk]), data={ 'foo': 'bar', }, follow=True) assert response.status_code == HTTP_200_OK category.refresh_from_db() assert category.name == 'Another Name'
def test_updated_details(): user, organization, project, client = setup() project.add_manage(user) assert login(client, user) # Valid case category = Category.objects.create(name='Test Category', project=project) response = client.post(path=reverse('projects:details', args=[project.pk]), data={ 'name': 'Test Project', 'description': 'Test Description', 'tags': ['test, tags'] }, follow=True) assert response.redirect_chain[0][0] == reverse('projects:details', args=[project.pk]) assert response.redirect_chain[0][1] == HTTP_302_FOUND project.refresh_from_db() assert project.name == 'Test Project' assert project.description == 'Test Description' assert len(project.tags.all()) == 2 # Invalid form response = client.post(path=reverse('projects:details', args=[project.pk]), data={ 'foo': 'bar', }) assert response.status_code == HTTP_200_OK
def test_creating_project_category(): user, organization, project, client = setup() project.add_manage(user) assert login(client, user) assert len(project.category_set.all()) == 0 response = client.post(path=reverse('projects:new_category', args=[project.pk]), data={ 'name': 'Test Category', }, follow=True) assert response.redirect_chain[0][0] == reverse('projects:categories', args=[project.pk]) assert response.redirect_chain[0][1] == HTTP_302_FOUND assert len(project.category_set.all()) == 1 assert project.category_set.first().name == 'Test Category' # Bad data response = client.post(path=reverse('projects:new_category', args=[project.pk]), data={ 'foo': 'bar', }, follow=True) assert response.status_code == HTTP_200_OK assert len(project.category_set.all()) == 1 assert project.category_set.first().name == 'Test Category'
def test_project_invite_user_no_data(): user, organization, project, client = setup() project.add_invite(user) assert login(client, user) response = client.post(path=reverse('projects:project_invite_user', args=[project.pk]), data={}, follow=True) assert response.status_code == HTTP_200_OK
def test_project_invite_user_no_data(): user, organization, project, client = setup() project.add_invite(user) assert login(client, user) response = client.post( path=reverse('projects:project_invite_user', args=[project.pk]), data={ }, follow=True ) assert response.status_code == HTTP_200_OK
def test_project_manage_access_no_data(): user, organization, project, client = setup() project.add_manage(user) assert login(client, user) # Send an invalid payload response = client.post( path=reverse('projects:manage_access', args=[project.pk]), data={ }, follow=True ) assert response.status_code == HTTP_200_OK
def test_new_document_fail_validation(): user, organization, project, client = setup() project.add_create(user) assert login(client, user) with open('projects/views/documents/tests/test_file.txt') as test_file: assert len(user.get_documents()) == 0 response = client.post( path=reverse('projects:new_document', args=[project.pk]), data={ }, ) assert len(user.get_documents()) == 0 assert response.status_code == HTTP_200_OK
def test_deleting_project_category(): user, organization, project, client = setup() project.add_manage(user) assert login(client, user) category = Category.objects.create(name='Test Category', project=project) assert len(project.category_set.all()) == 1 response = client.delete(path=reverse('projects:delete_category', args=[project.pk, category.pk]), follow=True) assert response.redirect_chain[0][0] == reverse('projects:categories', args=[project.pk]) assert response.redirect_chain[0][1] == HTTP_302_FOUND assert len(project.category_set.all()) == 0
def test_deleting_project_category(): user, organization, project, client = setup() project.add_manage(user) assert login(client, user) category = Category.objects.create(name='Test Category', project=project) assert len(project.category_set.all()) == 1 response = client.delete( path=reverse('projects:delete_category', args=[project.pk, category.pk]), follow=True ) assert response.redirect_chain[0][0] == reverse('projects:categories', args=[project.pk]) assert response.redirect_chain[0][1] == HTTP_302_FOUND assert len(project.category_set.all()) == 0
def test_project_invite_user_sends_email(): user, organization, project, client = setup() project.add_manage(user) assert login(client, user) # Send an invalid payload response = client.post(path=reverse('projects:project_invite_user', args=[project.pk]), data={ 'email': '*****@*****.**', 'first_name': 'Test', 'last_name': 'User', 'user_type': 'manager' }, follow=True) assert response.status_code == HTTP_200_OK assert len(mail.outbox) == 1 assert mail.outbox[0].subject == 'Invitation to collaborate'
def test_project_invite_user_sends_email(): user, organization, project, client = setup() project.add_manage(user) assert login(client, user) # Send an invalid payload response = client.post( path=reverse('projects:project_invite_user', args=[project.pk]), data={ 'email': '*****@*****.**', 'first_name': 'Test', 'last_name': 'User', 'user_type': 'manager' }, follow=True ) assert response.status_code == HTTP_200_OK assert len(mail.outbox) == 1 assert mail.outbox[0].subject == 'Invitation to collaborate'
def test_project_invite_user_as_collaborator(): user, organization, project, client = setup() project.add_invite(user) assert login(client, user) response = client.post(path=reverse('projects:project_invite_user', args=[project.pk]), data={ 'email': '*****@*****.**', 'first_name': 'Test', 'last_name': 'User', 'user_type': 'collaborator' }, follow=True) assert response.status_code == HTTP_200_OK new_user = User.objects.get(email='*****@*****.**') assert project.can_create(new_user) assert project.can_invite(new_user) assert not project.can_manage(new_user)
def test_new_document(): user, organization, project, client = setup() project.add_create(user) assert login(client, user) with open('projects/views/documents/tests/test_file.txt') as test_file: assert len(user.get_documents()) == 0 response = client.post( path=reverse('projects:new_document', args=[project.pk]), data={ 'description': 'Test Description', 'file': test_file, }, follow=True ) documents = user.get_documents() assert response.redirect_chain[0][0] == reverse('projects:document', args=[project.pk, documents[0].pk]) assert response.redirect_chain[0][1] == HTTP_302_FOUND assert len(documents) == 1 assert documents[0].project.pk == project.pk
def test_project_invite_user_as_collaborator(): user, organization, project, client = setup() project.add_invite(user) assert login(client, user) response = client.post( path=reverse('projects:project_invite_user', args=[project.pk]), data={ 'email': '*****@*****.**', 'first_name': 'Test', 'last_name': 'User', 'user_type': 'collaborator' }, follow=True ) assert response.status_code == HTTP_200_OK new_user = User.objects.get(email='*****@*****.**') assert project.can_create(new_user) assert project.can_invite(new_user) assert not project.can_manage(new_user)
def test_project_invite_user_as_manager(): user, organization, project, client = setup() project.add_invite(user) assert login(client, user) # Not a manager so this should fail response = client.post( path=reverse('projects:project_invite_user', args=[project.pk]), data={ 'email': '*****@*****.**', 'first_name': 'Test', 'last_name': 'User', 'user_type': 'manager' }, follow=True ) assert response.status_code == HTTP_200_OK with pytest.raises(User.DoesNotExist): User.objects.get(email='*****@*****.**') project.add_invite(user) # Now we can do this project.add_manage(user) response = client.post( path=reverse('projects:project_invite_user', args=[project.pk]), data={ 'email': '*****@*****.**', 'first_name': 'Test', 'last_name': 'User', 'user_type': 'manager' }, follow=True ) assert response.status_code == HTTP_200_OK new_user = User.objects.get(email='*****@*****.**') assert project.can_create(new_user) assert project.can_invite(new_user) assert project.can_manage(new_user)
def test_project_invite_user_as_manager(): user, organization, project, client = setup() project.add_invite(user) assert login(client, user) # Not a manager so this should fail response = client.post(path=reverse('projects:project_invite_user', args=[project.pk]), data={ 'email': '*****@*****.**', 'first_name': 'Test', 'last_name': 'User', 'user_type': 'manager' }, follow=True) assert response.status_code == HTTP_200_OK with pytest.raises(User.DoesNotExist): User.objects.get(email='*****@*****.**') project.add_invite(user) # Now we can do this project.add_manage(user) response = client.post(path=reverse('projects:project_invite_user', args=[project.pk]), data={ 'email': '*****@*****.**', 'first_name': 'Test', 'last_name': 'User', 'user_type': 'manager' }, follow=True) assert response.status_code == HTTP_200_OK new_user = User.objects.get(email='*****@*****.**') assert project.can_create(new_user) assert project.can_invite(new_user) assert project.can_manage(new_user)
def test_transfer_ownership(): user, organization, project, client = setup() project.add_manage(user) assert login(client, user) # Test that the user does not have permissions yet. new_organization = OrganizationFactory() # Valid case assert project.organization.pk == organization.pk response = client.post( path=reverse('projects:transfer_ownership', args=[project.pk]), data={ 'organization': new_organization.pk, }, follow=True ) assert response.status_code == HTTP_200_OK project.refresh_from_db() assert project.organization.pk != new_organization.pk # Test that if the user does not have enough permissions, that things do not work. new_organization.add_create(user) # Valid case assert project.organization.pk == organization.pk response = client.post( path=reverse('projects:transfer_ownership', args=[project.pk]), data={ 'organization': new_organization.pk, }, follow=True ) assert response.status_code == HTTP_200_OK project.refresh_from_db() assert project.organization.pk == organization.pk new_organization.delete_create(user) # Test that if the user does not have enough permissions, that things do not work. new_organization.add_invite(user) # Valid case assert project.organization.pk == organization.pk response = client.post( path=reverse('projects:transfer_ownership', args=[project.pk]), data={ 'organization': new_organization.pk, }, follow=True ) assert response.status_code == HTTP_200_OK project.refresh_from_db() assert project.organization.pk == organization.pk new_organization.delete_invite(user) # Now test that they do. new_organization.add_manage(user) # Valid case assert project.organization.pk == organization.pk response = client.post( path=reverse('projects:transfer_ownership', args=[project.pk]), data={ 'organization': new_organization.pk, }, follow=True ) assert response.redirect_chain[0][0] == reverse('projects:transfer_ownership', args=[project.pk]) assert response.redirect_chain[0][1] == HTTP_302_FOUND project.refresh_from_db() assert project.organization.pk == new_organization.pk
def test_transfer_ownership(): user, organization, project, client = setup() project.add_manage(user) assert login(client, user) # Test that the user does not have permissions yet. new_organization = OrganizationFactory() # Valid case assert project.organization.pk == organization.pk response = client.post(path=reverse('projects:transfer_ownership', args=[project.pk]), data={ 'organization': new_organization.pk, }, follow=True) assert response.status_code == HTTP_200_OK project.refresh_from_db() assert project.organization.pk != new_organization.pk # Test that if the user does not have enough permissions, that things do not work. new_organization.add_create(user) # Valid case assert project.organization.pk == organization.pk response = client.post(path=reverse('projects:transfer_ownership', args=[project.pk]), data={ 'organization': new_organization.pk, }, follow=True) assert response.status_code == HTTP_200_OK project.refresh_from_db() assert project.organization.pk == organization.pk new_organization.delete_create(user) # Test that if the user does not have enough permissions, that things do not work. new_organization.add_invite(user) # Valid case assert project.organization.pk == organization.pk response = client.post(path=reverse('projects:transfer_ownership', args=[project.pk]), data={ 'organization': new_organization.pk, }, follow=True) assert response.status_code == HTTP_200_OK project.refresh_from_db() assert project.organization.pk == organization.pk new_organization.delete_invite(user) # Now test that they do. new_organization.add_manage(user) # Valid case assert project.organization.pk == organization.pk response = client.post(path=reverse('projects:transfer_ownership', args=[project.pk]), data={ 'organization': new_organization.pk, }, follow=True) assert response.redirect_chain[0][0] == reverse( 'projects:transfer_ownership', args=[project.pk]) assert response.redirect_chain[0][1] == HTTP_302_FOUND project.refresh_from_db() assert project.organization.pk == new_organization.pk
def test_project_manage_access(): user, organization, project, client = setup() # Add superuser so the other user can be seen. user.is_superuser = True user.save() project.add_manage(user) assert login(client, user) other_user = UserFactory() # Valid create case assert project.can_create(other_user) == False response = client.post( path=reverse('projects:manage_access', args=[project.pk]), data={ 'can_create_{0}'.format(other_user.pk): True, }, follow=True ) assert response.redirect_chain[0][0] == reverse('projects:manage_access', args=[project.pk]) assert response.redirect_chain[0][1] == HTTP_302_FOUND assert project.can_create(other_user) == True # Remove Create response = client.post( path=reverse('projects:manage_access', args=[project.pk]), data={ 'none_{0}'.format(other_user.pk): True, }, follow=True ) assert response.redirect_chain[0][0] == reverse('projects:manage_access', args=[project.pk]) assert response.redirect_chain[0][1] == HTTP_302_FOUND assert project.can_create(other_user) == False # Valid invite case assert project.can_invite(other_user) == False response = client.post( path=reverse('projects:manage_access', args=[project.pk]), data={ 'can_invite_{0}'.format(other_user.pk): True, }, follow=True ) assert response.redirect_chain[0][0] == reverse('projects:manage_access', args=[project.pk]) assert response.redirect_chain[0][1] == HTTP_302_FOUND assert project.can_invite(other_user) == True # Remove invite response = client.post( path=reverse('projects:manage_access', args=[project.pk]), data={ 'none_{0}'.format(other_user.pk): True, }, follow=True ) assert response.redirect_chain[0][0] == reverse('projects:manage_access', args=[project.pk]) assert response.redirect_chain[0][1] == HTTP_302_FOUND assert project.can_invite(other_user) == False # Valid manage case assert project.can_manage(other_user) == False response = client.post( path=reverse('projects:manage_access', args=[project.pk]), data={ 'can_manage_{0}'.format(other_user.pk): True, }, follow=True ) assert response.redirect_chain[0][0] == reverse('projects:manage_access', args=[project.pk]) assert response.redirect_chain[0][1] == HTTP_302_FOUND assert project.can_manage(other_user) == True # Remove manage response = client.post( path=reverse('projects:manage_access', args=[project.pk]), data={ 'none_{0}'.format(other_user.pk): True, }, follow=True ) assert response.redirect_chain[0][0] == reverse('projects:manage_access', args=[project.pk]) assert response.redirect_chain[0][1] == HTTP_302_FOUND assert project.can_manage(other_user) == False
def test_user_permissions_for_document_export(): user, organization, project, client = setup() # Not logged in should redirect to the login page response = client.get(reverse('projects:export_project', args=[project.pk]), follow=True) assert response.redirect_chain[0][0] == '{0}?next={1}'.format( reverse('users:login'), reverse('projects:export_project', args=[project.pk])) assert response.redirect_chain[0][1] == HTTP_302_FOUND assert login(client, user) response = client.get(reverse('projects:export_project', args=[project.pk])) assert response.status_code == HTTP_403_FORBIDDEN # Test correct permissions organization.add_manage(user) response = client.get(reverse('projects:export_project', args=[project.pk]), follow=True) assert response.redirect_chain[0][0] == reverse('projects:project', args=[project.pk]) assert response.redirect_chain[0][1] == HTTP_302_FOUND organization.delete_manage(user) project.add_manage(user) response = client.get(reverse('projects:export_project', args=[project.pk]), follow=True) assert response.redirect_chain[0][0] == reverse('projects:project', args=[project.pk]) assert response.redirect_chain[0][1] == HTTP_302_FOUND project.delete_manage(user) # Super user does what they want user.is_superuser = True user.save() response = client.get(reverse('projects:export_project', args=[project.pk]), follow=True) assert response.redirect_chain[0][0] == reverse('projects:project', args=[project.pk]) assert response.redirect_chain[0][1] == HTTP_302_FOUND user.is_superuser = False user.save() organization.add_create(user) response = client.get(reverse('projects:export_project', args=[project.pk]), follow=True) assert response.redirect_chain[0][0] == reverse('projects:project', args=[project.pk]) assert response.redirect_chain[0][1] == HTTP_302_FOUND organization.delete_create(user) organization.add_invite(user) response = client.get(reverse('projects:export_project', args=[project.pk]), follow=True) assert response.redirect_chain[0][0] == reverse('projects:project', args=[project.pk]) assert response.redirect_chain[0][1] == HTTP_302_FOUND organization.delete_invite(user) project.add_create(user) response = client.get(reverse('projects:export_project', args=[project.pk]), follow=True) assert response.redirect_chain[0][0] == reverse('projects:project', args=[project.pk]) assert response.redirect_chain[0][1] == HTTP_302_FOUND project.delete_create(user) project.add_invite(user) response = client.get(reverse('projects:export_project', args=[project.pk]), follow=True) assert response.redirect_chain[0][0] == reverse('projects:project', args=[project.pk]) assert response.redirect_chain[0][1] == HTTP_302_FOUND project.delete_invite(user)