def setUp(self): self.user = User.objects.create_user(email="*****@*****.**", password="******", object_channel="default") self.second_user = User.objects.create_user( email="*****@*****.**", password="******", object_channel="default") self.third_user = User.objects.create_user( email="*****@*****.**", password="******", object_channel="default") organization = Organization(name="test", type=0, owner=self.user) organization.save(object_channel="default") organization.members.add(self.second_user) address = GoogleAddress(typed_address="Rua. Teçaindá, 81") address.save(object_channel="default") self.project = Project.objects.create(name="test project", slug="test slug", details="abc", description="abc", owner=self.user, hidden_address=True, address=address, organization=organization, object_channel="default") self.request = RequestFactory().get('/') self.request.channel = "default"
def test_can_create_project_with_another_owner(self): """Assert that it's possible to create a project with another owner""" ChannelSetting.objects.create( key="CAN_CREATE_PROJECTS_WITHOUT_ORGANIZATION", value="1", object_channel="default") cache.clear() client = APIClient() client.force_authenticate(user=self.user) data = copy.deepcopy(base_project) data["owner"] = self.second_user.pk # Without organization response = client.post(reverse("project-list"), data, format="json") self.assertTrue(response.status_code == 400) self.assertTrue(response.data["owner"][0] == "Organization field must be set to set owner.") # Not part of the organization organization = Organization(name="test", type=0, owner=self.user) organization.save(object_channel="default") data["organization_id"] = organization.pk response = client.post(reverse("project-list"), data, format="json") self.assertTrue(response.status_code == 400) self.assertTrue(response.data["owner"][0] == "User is a not a member of the organization.") # Part of the organization organization.members.add(self.second_user) response = client.post(reverse("project-list"), data, format="json") self.assertTrue(response.status_code == 201) project = Project.objects.get(pk=response.data["id"]) self.assertTrue(project.owner.id == self.second_user.id)
def setUp(self): user = User.objects.create_user(email="*****@*****.**", password="******", object_channel="default") self.user = user organization = Organization(name="test organization", slug="overriden", owner=user, type=0, published=True) organization.save(object_channel="default") self.organization = organization
def test_user_is_owner_or_member(self): """Test user is owner or member of organization""" wrong_org = Organization(name="test", type=0, owner=self.second_user) wrong_org.save(object_channel="default") self.data['organization_id'] = wrong_org.pk response = self.client.post(reverse("project-list"), self.data, format="json") self.assertTrue(response.status_code == 403)
def test_index_on_causes_update(self): """ Test organization index gets updated when a cause is modified """ cause = Cause.objects.all().order_by('pk').first() organization = Organization(name="test organization", details="abc", owner=self.user, address=self.address1, published=True, type=0) organization.save(object_channel="default") self.assertTrue(SearchQuerySet().models(Organization).filter(causes=cause.pk).count() == 0) organization.causes.add(cause) self.assertTrue(SearchQuerySet().models(Organization).filter(causes=cause.pk).count() == 1)
def setUp(self): self.user = User.objects.create_user(email="*****@*****.**", password="******", object_channel="default") self.user2 = User.objects.create_user(email="*****@*****.**", password="******", object_channel="default") self.user3 = User.objects.create_user(email="*****@*****.**", password="******", object_channel="default") organization = Organization(name="test organization", slug="test-organization", owner=self.user, type=0, published=True) organization.save(object_channel="default") organization.members.add(self.user) # Owner is usually added to members by view organization.members.add(self.user2) self.organization = organization self.client = APIClient()
def setUp(self): user = User.objects.create_user(email="*****@*****.**", password="******", object_channel="default") mail.outbox = [] # Mails sent before creating don't matter organization = Organization(name="test organization", type=0, owner=user) organization.save(object_channel="default") self.organization = organization
def test_update_fields(self): """Test patch request update fields""" updated_project = { "name": "test update", "details": "update", "description": "update", "causes": [{ "id": 3 }], "skills": [{ "id": 1 }, { "id": 2 }, { "id": 3 }], "galleries": [], "documents": [], "categories": [] } response = self.client.patch(reverse("project-detail", ["test-project"]), updated_project, format="json") self.assertTrue(response.status_code == 200) self.assertTrue(response.data["name"] == "test update") self.assertTrue(response.data["details"] == "update") self.assertTrue(response.data["description"] == "update") # Don't erase associations with other channel objects self.assertTrue(len(response.data["causes"]) == 2) self.assertTrue(len(response.data["skills"]) == 4) self.assertEqual(response.data["causes"][-1]["name"], "other-channel") self.assertEqual(response.data["skills"][-1]["name"], "other-channel") self.assertTrue(len(response.data["galleries"]) == 1) self.assertTrue(len(response.data["documents"]) == 1) self.assertTrue(len(response.data["categories"]) == 1) user = User.objects.create_user(email="*****@*****.**", password="******", object_channel="default") organization = Organization(name="test", type=0, owner=self.user) organization.save(object_channel="default") organization.members.add(user) project = Project.objects.get(pk=response.data['id']) project.organization = organization project.save() self.client.force_authenticate(user) response = self.client.patch(reverse("project-detail", ["test-project"]), updated_project, format="json") self.assertTrue(response.status_code == 200)
def setUp(self): user = User.objects.create_user(email="*****@*****.**", password="******", object_channel="default") self.user = user user2 = User.objects.create_user(email="*****@*****.**", password="******", object_channel="default") self.user2 = user2 organization = Organization(name="test organization", slug="test-organization", owner=user, type=0, published=True) organization.save(object_channel="default") self.organization = organization self.client = APIClient() self.client.force_authenticate(user)
class ManageableProjectsRouteTestCase(TestCase): def setUp(self): self.user = User.objects.create_user( email="*****@*****.**", password="******", object_channel="default") self.user2 = User.objects.create_user( email="*****@*****.**", password="******", object_channel="default") self.organization = Organization(name="test", type=0, owner=self.user) self.organization.save(object_channel="default") self.organization2 = Organization(name="test2", type=0, owner=self.user2) self.organization2.save(object_channel="default") self.organization2.members.add(self.user) p = Project.objects.create(name="test project 1", owner=self.user, object_channel="default") p = Project.objects.create(name="test project 2", owner=self.user, organization=self.organization, object_channel="default") p = Project.objects.create(name="test project 3", owner=self.user2, organization=self.organization2, object_channel="default") self.client = APIClient() self.client.force_authenticate(user=self.user) def test_requires_authentication(self): """Test hitting route unauthenticated returns 401""" client = APIClient() response = client.get(reverse("project-manageable"), {}, format="json") self.assertTrue(response.status_code == 401) def test_returns_projects(self): """Test hitting route authenticated returns projects""" response = self.client.get(reverse("project-manageable"), {}, format="json") self.assertTrue(response.status_code == 200) self.assertTrue(len(response.data["results"]) == 3) def test_number_of_queries(self): """Test project manageable does only 30 queries""" with self.assertNumQueries(30): response = self.client.get(reverse("project-manageable"), {}, format="json")
def test_organization_publish(self): """ Assert that setting Organization.published=True updates published_date """ organization = Organization(name="test organization", owner=self.user, type=0) organization.save(object_channel="default") self.assertTrue(organization.published == False) self.assertTrue(organization.published_date == None) organization.published = True organization.save() self.assertTrue(organization.published == True) self.assertTrue(organization.published_date)
def test_index_on_create_and_update(self): """ Test organization index gets updated when a organization is created or updated """ self.assertTrue(SearchQuerySet().models(Project).all().count() == 0) organization = Organization(name="test organization", details="abc", owner=self.user, address=self.address1, published=True, type=0) organization.save(object_channel="default") self.assertTrue(SearchQuerySet().models(Organization).all().count() == 1) self.assertTrue(SearchQuerySet().models(Organization).filter(address_components__exact=whoosh_raw("São Paulo-administrative_area_level_2")).count() == 1) organization.address = self.address2 organization.save() self.assertTrue(SearchQuerySet().models(Organization).all().count() == 1) self.assertTrue(SearchQuerySet().models(Organization).filter(address_components__exact=whoosh_raw("Campinas-administrative_area_level_2")).count() == 1)
def _get_or_create_organization(self, data: dict) -> Organization: name = data['name'] try: logging.debug(f'Getting organization {name}') organization = Organization.objects.get(name=name, channel__slug="gdd") except Organization.DoesNotExist: logging.debug(f'Failed. Creating organization {name}') organization = Organization(**data) if not self.dry_run: organization.save(object_channel="gdd") return organization
def test_can_create_in_any_organization_if_settings_allow(self): """Test user can create project inside any organization if properly configured""" ChannelSetting.objects.create( key="CAN_CREATE_PROJECTS_IN_ANY_ORGANIZATION", value="1", object_channel="default") cache.clear() wrong_org = Organization(name="test", type=0, owner=self.second_user) wrong_org.save(object_channel="default") self.data['organization_id'] = wrong_org.pk response = self.client.post(reverse("project-list"), self.data, format="json") self.assertTrue(response.status_code == 201)
def test_excerpt_from_details(self): """ Assert that if an organization has no .description, it will use 100 chars from .details as an excerpt """ small_details = ("a" * 100) details = ("a" * 100) + "b" expected_description = "a" * 100 organization = Organization(name="test organization", owner=self.user, type=0, published=True, details=details) organization.save(object_channel="default") organization = Organization.objects.get(pk=organization.id) self.assertTrue(organization.description == expected_description) organization = Organization(name="test organization", owner=self.user, type=0, published=True, details=small_details) organization.save(object_channel="default") organization = Organization.objects.get(pk=organization.id) self.assertTrue(organization.description == small_details)
def test_can_create(self): """Test user can create project with valid organization""" organization = Organization(name="test", type=0, owner=self.user) organization.save(object_channel="default") organization.members.add(self.second_user) self.data['organization_id'] = organization.pk response = self.client.post(reverse("project-list"), self.data, format="json") self.assertTrue(response.status_code == 201) self.client.force_authenticate(self.second_user) response = self.client.post(reverse("project-list"), self.data, format="json") self.assertTrue(response.status_code == 201)
def test_organization_index_on_address_update(self): """ Test organization index gets reindexed if address changes """ self.assertTrue(SearchQuerySet().models(Organization).all().count() == 0) organization = Organization(name="test organization", details="abc", owner=self.user, address=self.address1, published=True, type=0) organization.save(object_channel="default") self.assertTrue(SearchQuerySet().models(Organization).all().count() == 1) self.assertTrue(SearchQuerySet().models(Organization).filter(address_components__exact=whoosh_raw("São Paulo-administrative_area_level_2")).count() == 1) organization.address.typed_address = "Campinas, SP - Brazil" organization.address.save() self.assertTrue(SearchQuerySet().models(Organization).all().count() == 1) self.assertTrue(SearchQuerySet().models(Organization).filter(address_components__exact=whoosh_raw("Campinas-administrative_area_level_2")).count() == 1) organization.address.delete() self.assertTrue(SearchQuerySet().models(Organization).all().count() == 0) self.assertTrue(SearchQuerySet().models(Organization).filter(address_components__exact=whoosh_raw("Campinas-administrative_area_level_2")).count() == 0)
def test_can_hide_address(self): """Test user can create project with valid organization""" org = Organization(name="test", type=0, owner=self.user) org.save(object_channel="default") org.members.add(self.second_user) self.data['organization_id'] = org.pk self.data['hidden_address'] = True response = self.client.post(reverse("project-list"), self.data, format="json") # Owner retrieving response = self.client.get(reverse("project-detail", ["test-project"]), format="json") self.assertTrue(response.data["address"]["typed_address"] == self.data["address"]["typed_address"]) self.assertTrue(response.data["hidden_address"] == True) # Organization member retrieving self.client.force_authenticate(self.second_user) response = self.client.get(reverse("project-detail", ["test-project"]), format="json") self.assertTrue(response.data["address"]["typed_address"] == self.data["address"]["typed_address"]) self.assertTrue(response.data["hidden_address"] == True) # Non member retrieving self.client.force_authenticate(self.third_user) response = self.client.get(reverse("project-detail", ["test-project"]), format="json") self.assertTrue(response.data["address"] == None) self.assertTrue(response.data["hidden_address"] == True) # Project without organization self.client.force_authenticate(self.second_user) Project.objects.filter(slug="test-project").update(organization=None) response = self.client.get(reverse("project-detail", ["test-project"]), format="json") self.assertTrue(response.data["address"] == None) self.assertTrue(response.data["hidden_address"] == True)
def create_sample_organizations(): user1 = User.objects.create_user(name="z", email="*****@*****.**", password="******", object_channel="default") user2 = User.objects.create_user(name="a", email="*****@*****.**", password="******", object_channel="test-channel") address1 = GoogleAddress(typed_address="São paulo, SP - Brazil") address2 = GoogleAddress(typed_address="Santo André, SP - Brazil") address3 = GoogleAddress( typed_address="New york, New york - United States") address4 = GoogleAddress( typed_address="New york, New york - United States") address5 = GoogleAddress( typed_address="New york, New york - United States") address1.save(object_channel="default") address2.save(object_channel="default") address3.save(object_channel="default") address4.save(object_channel="default") address5.save(object_channel="test-channel") organization = Organization(name="test organization", details="abc", owner=user1, address=address1, published=True, type=0) organization.save(object_channel="default") organization.causes.add(Cause.objects.all().order_by('pk')[0]) organization = Organization(name="test organization2", details="abc", owner=user1, address=address2, published=True, highlighted=True, type=0) organization.save(object_channel="default") organization.causes.add(Cause.objects.all().order_by('pk')[1]) organization = Organization(name="test organization3", details="abc", owner=user1, address=address3, published=True, type=0) organization.save(object_channel="default") organization = Organization(name="test organization4", details="abc", owner=user1, address=address4, published=False, type=0) organization.save(object_channel="default") organization = Organization(name="test organization5", details="abc", owner=user2, address=address5, published=True, type=0) organization.save(object_channel="test-channel")
def test_slug_doesnt_repeat(self): """ Assert that slug does not repeat """ organization = Organization(name="test organization", details="abc", owner=self.user, type=0) organization.save(object_channel="default") self.assertTrue(organization.slug == "test-organization-1")
class ProjectApplyStatusUpdateTestCase(TestCase): def setUp(self): # Create organization self.organization_owner = User.objects.create_user( email="*****@*****.**", password="******", object_channel="default") self.organization_member = User.objects.create_user( email="*****@*****.**", password="******", object_channel="default") self.organization = Organization(name="test", type=0, owner=self.organization_owner) self.organization.save(object_channel="default") self.organization.members.add(self.organization_member) # Create project self.project_owner = User.objects.create_user( email="*****@*****.**", password="******", object_channel="default") self.project = Project.objects.create(name="test project", details="abc", description="abc", owner=self.project_owner, organization=self.organization, object_channel="default") # Apply self.applier = User.objects.create_user(email="*****@*****.**", password="******", object_channel="default") self.client = APIClient() self.client.force_authenticate(user=self.applier) response = self.client.post(reverse("project-applies-apply", ["test-project"]), format="json") self.assertTrue(response.data["detail"] == "Successfully applied.") self.assertTrue(response.status_code == 200) # Get apply self.apply_id = Apply.objects.last().pk def _assert_can_update_apply(self): # Update apply data = {"status": "unapplied"} response = self.client.patch(reverse("project-applies-detail", ["test-project", self.apply_id]), data=data, format="json") self.assertTrue(response.status_code == 200) self.assertTrue(response.data["status"] == "unapplied") # Get apply response = self.client.get(reverse("project-applies-list", ["test-project"]), format="json") self.assertTrue(response.data[0]["status"] == "unapplied") def test_project_owner_can_update_apply_status(self): """Assert that project owner can update apply status""" self.client.force_authenticate(user=self.project_owner) self._assert_can_update_apply() def test_organization_owner_can_update_apply_status(self): """Assert that organization owner can update apply status""" self.client.force_authenticate(user=self.organization_owner) self._assert_can_update_apply() def test_organization_member_can_update_apply_status(self): """Assert that organization member can update apply status""" self.client.force_authenticate(user=self.organization_member) self._assert_can_update_apply() def test_unauthorized_user_cant_update_status(self): """Assert that organization member can update apply status""" client = APIClient() response = self.client.patch(reverse("project-applies-detail", ["test-project", self.apply_id]), data={"status": "unapplied"}, format="json") self.assertTrue(response.status_code == 403) client.force_authenticate(user=self.applier) response = self.client.patch(reverse("project-applies-detail", ["test-project", self.apply_id]), data={"status": "unapplied"}, format="json") self.assertTrue(response.status_code == 403) def test_update_to_invalid_status(self): """Assert that it's not possible to update to invalid apply status""" self.client.force_authenticate(user=self.project_owner) response = self.client.patch(reverse("project-applies-detail", ["test-project", self.apply_id]), data={"status": "invalid-status"}, format="json") self.assertTrue(response.status_code == 400) self.assertTrue(response.data["status"] == ["\"invalid-status\" is not a valid choice."])