def test_updated_details(): user, organization, client = setup() organization.add_manage(user) assert login(client, user) # Valid case response = client.post( path=reverse('organizations:details', args=[organization.pk]), data={ 'name': 'Test Organization', }, follow=True ) assert response.redirect_chain[0][0] == reverse('organizations:details', args=[organization.pk]) assert response.redirect_chain[0][1] == HTTP_302_FOUND organization.refresh_from_db() assert organization.name == 'Test Organization' # Invalid form response = client.post( path=reverse('organizations:details', args=[organization.pk]), data={ 'foo': 'bar', } ) assert response.status_code == HTTP_200_OK
def test_new_project_no_permissions(): user, organization, client = setup() assert login(client, user) assert len(user.get_projects()) == 0 response = client.post( path=reverse('projects:new_project'), data={ }, ) assert response.status_code == HTTP_403_FORBIDDEN project = ProjectFactory(organization=organization) project.add_manage(user) assert len(user.get_projects()) == 1 response = client.post( path=reverse('projects:new_project'), data={ 'name': 'Test Project', 'description': 'Test Description', 'organization': organization.pk }, ) assert response.status_code == HTTP_403_FORBIDDEN assert len(user.get_projects()) == 1
def test_updated_details(): user, organization, client = setup() organization.add_manage(user) assert login(client, user) # Valid case response = client.post(path=reverse('organizations:details', args=[organization.pk]), data={ 'name': 'Test Organization', }, follow=True) assert response.redirect_chain[0][0] == reverse('organizations:details', args=[organization.pk]) assert response.redirect_chain[0][1] == HTTP_302_FOUND organization.refresh_from_db() assert organization.name == 'Test Organization' # Invalid form response = client.post(path=reverse('organizations:details', args=[organization.pk]), data={ 'foo': 'bar', }) assert response.status_code == HTTP_200_OK
def test_organization_invite_user_no_data(): user, organization, client = setup() organization.add_invite(user) assert login(client, user) response = client.post(path=reverse('organizations:invite_user', args=[organization.pk]), data={}, follow=True) assert response.status_code == HTTP_200_OK
def test_organizations(): user, organization, client = setup() response = client.get(path=reverse('organizations:organizations')) assert response.status_code == HTTP_302_FOUND assert login(client, user) response = client.get(path=reverse('organizations:organizations')) assert response.status_code == HTTP_200_OK
def test_project_manage_access_no_data(): user, organization, client = setup() organization.add_manage(user) assert login(client, user) # Send an invalid payload response = client.post(path=reverse('organizations:manage_access', args=[organization.pk]), data={}, follow=True) assert response.status_code == HTTP_200_OK
def test_organization_invite_user_no_data(): user, organization, client = setup() organization.add_invite(user) assert login(client, user) response = client.post( path=reverse('organizations:invite_user', args=[organization.pk]), data={ }, follow=True ) assert response.status_code == HTTP_200_OK
def test_new_project_fail_validation(): user, organization, client = setup() organization.add_create(user) assert login(client, user) assert len(user.get_projects()) == 0 response = client.post( path=reverse('projects:new_project'), data={}, ) assert len(user.get_projects()) == 0 assert response.status_code == HTTP_200_OK
def test_project_manage_access_no_data(): user, organization, client = setup() organization.add_manage(user) assert login(client, user) # Send an invalid payload response = client.post( path=reverse('organizations:manage_access', args=[organization.pk]), data={ }, follow=True ) assert response.status_code == HTTP_200_OK
def test_new_project_fail_validation(): user, organization, client = setup() organization.add_create(user) assert login(client, user) assert len(user.get_projects()) == 0 response = client.post( path=reverse('projects:new_project'), data={ }, ) assert len(user.get_projects()) == 0 assert response.status_code == HTTP_200_OK
def test_new_organization_fail_validation(): user, organization, client = setup() user.is_superuser = True user.save() assert login(client, user) assert user.get_organizations().count() == 2 response = client.post( path=reverse('organizations:new_organization'), data={ }, ) assert user.get_organizations().count() == 2 assert response.status_code == HTTP_200_OK
def test_organizations(): user, organization, client = setup() response = client.get( path=reverse('organizations:organizations') ) assert response.status_code == HTTP_302_FOUND assert login(client, user) response = client.get( path=reverse('organizations:organizations') ) assert response.status_code == HTTP_200_OK
def test_new_project(): user, organization, client = setup() organization.add_create(user) assert login(client, user) assert len(user.get_projects()) == 0 response = client.post(path=reverse('projects:new_project'), data={ 'name': 'Test Project', 'description': 'Test Description', 'organization': organization.pk }, follow=True) projects = user.get_projects() assert response.redirect_chain[0][0] == reverse('projects:project', args=[projects[0].pk]) assert response.redirect_chain[0][1] == HTTP_302_FOUND assert len(projects) == 1 assert projects[0].organization.pk == organization.pk
def test_new_organization(): user, organization, client = setup() user.is_superuser = True user.save() assert login(client, user) assert len(user.get_organizations()) == 2 response = client.post( path=reverse('organizations:new_organization'), data={ 'name': 'Test Organization', 'website': 'http://example.com', }, follow=True ) organizations = user.get_organizations() assert response.redirect_chain[0][0] == reverse('organizations:organization', args=[organizations.get(name='Test Organization').pk]) assert response.redirect_chain[0][1] == HTTP_302_FOUND assert len(organizations) == 3
def test_organization_invite_user_sends_email(): user, organization, client = setup() organization.add_manage(user) assert login(client, user) # Send an invalid payload response = client.post(path=reverse('organizations:invite_user', args=[organization.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_new_project(): user, organization, client = setup() organization.add_create(user) assert login(client, user) assert len(user.get_projects()) == 0 response = client.post( path=reverse('projects:new_project'), data={ 'name': 'Test Project', 'description': 'Test Description', 'organization': organization.pk }, follow=True ) projects = user.get_projects() assert response.redirect_chain[0][0] == reverse('projects:project', args=[projects[0].pk]) assert response.redirect_chain[0][1] == HTTP_302_FOUND assert len(projects) == 1 assert projects[0].organization.pk == organization.pk
def test_organization_invite_user_sends_email(): user, organization, client = setup() organization.add_manage(user) assert login(client, user) # Send an invalid payload response = client.post( path=reverse('organizations:invite_user', args=[organization.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_organization_invite_user_as_collaborator(): user, organization, client = setup() organization.add_invite(user) assert login(client, user) response = client.post(path=reverse('organizations:invite_user', args=[organization.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 organization.can_create(new_user) assert organization.can_invite(new_user) assert not organization.can_manage(new_user)
def test_organization_invite_user_as_collaborator(): user, organization, client = setup() organization.add_invite(user) assert login(client, user) response = client.post( path=reverse('organizations:invite_user', args=[organization.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 organization.can_create(new_user) assert organization.can_invite(new_user) assert not organization.can_manage(new_user)
def test_organization_invite_user_as_manager(): user, organization, client = setup() organization.add_invite(user) assert login(client, user) # Not a manager so this should fail response = client.post( path=reverse('organizations:invite_user', args=[organization.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='*****@*****.**') organization.add_invite(user) # Now we can do this organization.add_manage(user) response = client.post( path=reverse('organizations:invite_user', args=[organization.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 organization.can_create(new_user) assert organization.can_invite(new_user) assert organization.can_manage(new_user)
def test_user_permissions_for_new_organization(): user, organization, client = setup() # Not logged in should redirect to the login page response = client.get(reverse('organizations:new_organization'), follow=True) assert response.redirect_chain[0][0] == '{0}?next={1}'.format( reverse('users:login'), reverse('organizations:new_organization') ) assert response.redirect_chain[0][1] == HTTP_302_FOUND assert login(client, user) response = client.get(reverse('organizations:new_organization')) assert response.status_code == HTTP_403_FORBIDDEN # Super user does what they want user.is_superuser = True user.save() response = client.get(reverse('organizations:new_organization')) assert response.status_code == HTTP_200_OK user.is_superuser = False user.save()
def test_organization_invite_user_as_manager(): user, organization, client = setup() organization.add_invite(user) assert login(client, user) # Not a manager so this should fail response = client.post(path=reverse('organizations:invite_user', args=[organization.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='*****@*****.**') organization.add_invite(user) # Now we can do this organization.add_manage(user) response = client.post(path=reverse('organizations:invite_user', args=[organization.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 organization.can_create(new_user) assert organization.can_invite(new_user) assert organization.can_manage(new_user)
def test_new_organization_no_permissions(): user, organization, client = setup() assert login(client, user) assert len(user.get_organizations()) == 0 response = client.post( path=reverse('organizations:new_organization'), data={ }, ) assert response.status_code == HTTP_403_FORBIDDEN organization = OrganizationFactory() organization.add_manage(user) assert len(user.get_organizations()) == 1 response = client.post( path=reverse('organizations:new_organization'), data={ 'name': 'Test organization', }, ) assert response.status_code == HTTP_403_FORBIDDEN assert len(user.get_organizations()) == 1
def test_new_project_no_permissions(): user, organization, client = setup() assert login(client, user) assert len(user.get_projects()) == 0 response = client.post( path=reverse('projects:new_project'), data={}, ) assert response.status_code == HTTP_403_FORBIDDEN project = ProjectFactory(organization=organization) project.add_manage(user) assert len(user.get_projects()) == 1 response = client.post( path=reverse('projects:new_project'), data={ 'name': 'Test Project', 'description': 'Test Description', 'organization': organization.pk }, ) assert response.status_code == HTTP_403_FORBIDDEN assert len(user.get_projects()) == 1
def test_project_manage_access(): user, organization, client = setup() # Add superuser so the other user can be seen. user.is_superuser = True user.save() organization.add_manage(user) assert login(client, user) other_user = UserFactory() # Valid create case assert organization.can_create(other_user) == False response = client.post( path=reverse('organizations:manage_access', args=[organization.pk]), data={ 'can_create_{0}'.format(other_user.pk): True, }, follow=True ) assert response.redirect_chain[0][0] == reverse('organizations:manage_access', args=[organization.pk]) assert response.redirect_chain[0][1] == HTTP_302_FOUND assert organization.can_create(other_user) == True # Remove Create response = client.post( path=reverse('organizations:manage_access', args=[organization.pk]), data={ 'none_{0}'.format(other_user.pk): True, }, follow=True ) assert response.redirect_chain[0][0] == reverse('organizations:manage_access', args=[organization.pk]) assert response.redirect_chain[0][1] == HTTP_302_FOUND assert organization.can_create(other_user) == False # Valid invite case assert organization.can_invite(other_user) == False response = client.post( path=reverse('organizations:manage_access', args=[organization.pk]), data={ 'can_invite_{0}'.format(other_user.pk): True, }, follow=True ) assert response.redirect_chain[0][0] == reverse('organizations:manage_access', args=[organization.pk]) assert response.redirect_chain[0][1] == HTTP_302_FOUND assert organization.can_invite(other_user) == True # Remove invite response = client.post( path=reverse('organizations:manage_access', args=[organization.pk]), data={ 'none_{0}'.format(other_user.pk): True, }, follow=True ) assert response.redirect_chain[0][0] == reverse('organizations:manage_access', args=[organization.pk]) assert response.redirect_chain[0][1] == HTTP_302_FOUND assert organization.can_invite(other_user) == False # Valid manage case assert organization.can_manage(other_user) == False response = client.post( path=reverse('organizations:manage_access', args=[organization.pk]), data={ 'can_manage_{0}'.format(other_user.pk): True, }, follow=True ) assert response.redirect_chain[0][0] == reverse('organizations:manage_access', args=[organization.pk]) assert response.redirect_chain[0][1] == HTTP_302_FOUND assert organization.can_manage(other_user) == True # Remove manage response = client.post( path=reverse('organizations:manage_access', args=[organization.pk]), data={ 'none_{0}'.format(other_user.pk): True, }, follow=True ) assert response.redirect_chain[0][0] == reverse('organizations:manage_access', args=[organization.pk]) assert response.redirect_chain[0][1] == HTTP_302_FOUND assert organization.can_manage(other_user) == False
def test_project_manage_access(): user, organization, client = setup() # Add superuser so the other user can be seen. user.is_superuser = True user.save() organization.add_manage(user) assert login(client, user) other_user = UserFactory() # Valid create case assert organization.can_create(other_user) == False response = client.post(path=reverse('organizations:manage_access', args=[organization.pk]), data={ 'can_create_{0}'.format(other_user.pk): True, }, follow=True) assert response.redirect_chain[0][0] == reverse( 'organizations:manage_access', args=[organization.pk]) assert response.redirect_chain[0][1] == HTTP_302_FOUND assert organization.can_create(other_user) == True # Remove Create response = client.post(path=reverse('organizations:manage_access', args=[organization.pk]), data={ 'none_{0}'.format(other_user.pk): True, }, follow=True) assert response.redirect_chain[0][0] == reverse( 'organizations:manage_access', args=[organization.pk]) assert response.redirect_chain[0][1] == HTTP_302_FOUND assert organization.can_create(other_user) == False # Valid invite case assert organization.can_invite(other_user) == False response = client.post(path=reverse('organizations:manage_access', args=[organization.pk]), data={ 'can_invite_{0}'.format(other_user.pk): True, }, follow=True) assert response.redirect_chain[0][0] == reverse( 'organizations:manage_access', args=[organization.pk]) assert response.redirect_chain[0][1] == HTTP_302_FOUND assert organization.can_invite(other_user) == True # Remove invite response = client.post(path=reverse('organizations:manage_access', args=[organization.pk]), data={ 'none_{0}'.format(other_user.pk): True, }, follow=True) assert response.redirect_chain[0][0] == reverse( 'organizations:manage_access', args=[organization.pk]) assert response.redirect_chain[0][1] == HTTP_302_FOUND assert organization.can_invite(other_user) == False # Valid manage case assert organization.can_manage(other_user) == False response = client.post(path=reverse('organizations:manage_access', args=[organization.pk]), data={ 'can_manage_{0}'.format(other_user.pk): True, }, follow=True) assert response.redirect_chain[0][0] == reverse( 'organizations:manage_access', args=[organization.pk]) assert response.redirect_chain[0][1] == HTTP_302_FOUND assert organization.can_manage(other_user) == True # Remove manage response = client.post(path=reverse('organizations:manage_access', args=[organization.pk]), data={ 'none_{0}'.format(other_user.pk): True, }, follow=True) assert response.redirect_chain[0][0] == reverse( 'organizations:manage_access', args=[organization.pk]) assert response.redirect_chain[0][1] == HTTP_302_FOUND assert organization.can_manage(other_user) == False