Exemplo n.º 1
0
    def handle(self, *args, **options):
        """Populate database with fake objects."""

        self.stdout.write(self.style.SUCCESS('Creating 15 organizations'))
        for _ in tqdm(range(15)):
            organization = OrganizationFactory.create()
            UserProfileFactory.create(organizations=(organization, ), )

        self.stdout.write(self.style.SUCCESS('Creating 50 offers'))
        for _ in tqdm(range(50)):
            OfferFactory.create(
                organization=random.choice(Organization.objects.all()),
                image__path=ImageField(
                    from_func=placeimg_com_download(1000, 400, 'any')))

        self.stdout.write(self.style.SUCCESS('Creating 150 volunteers'))
        for _ in tqdm(range(150)):
            userprofile = UserProfileFactory.create()
            no_of_offers = random.randrange(10)
            for offer in random.sample(list(Offer.objects.all()),
                                       no_of_offers):
                offer.volunteers.add(userprofile.user)

        self.stdout.write(
            self.style.SUCCESS('Database successfully populated'))
Exemplo n.º 2
0
def initialize_filled_volunteer_and_organization():
    """Initialize volunteer filled with data."""

    # create volunteer user
    volunteer_user2 = UserFactory(
        email='*****@*****.**',
        password='******'
    )

    # create organization user to create offers
    organization2 = OrganizationFactory()
    # this is required due to login to this user
    UserFactory(
        email='*****@*****.**',
        password='******',
        userprofile__organizations=[organization2]
    )

    # create organization offers and assign volunteer to them
    for _ in range(11, 15):
        OfferFactory(
            organization=organization2,
            offer_status='published',
            volunteers=[volunteer_user2]
        )

    # create additional organization offers for administrator use
    for _ in range(100, 110):
        OfferFactory(
            organization=organization2,
            offer_status='unpublished',
        )

    return volunteer_user2, organization2
Exemplo n.º 3
0
    def setUpTestData(cls):
        """Set up data for all tests."""
        cls.organization = OrganizationFactory()

        cls.inactive_offer = OfferFactory(
            organization=cls.organization,
            offer_status='unpublished',
        )
        cls.active_offer = OfferFactory(
            organization=cls.organization,
            offer_status='published',
            finished_at=None,
            recruitment_end_date=None,
        )

        cls.volunteer = UserProfileFactory.create(
            user__username='******',
            user__email='*****@*****.**',
            user__password='******',
        )
        cls.organization_profile = UserProfileFactory.create(
            user__username='******',
            user__email='*****@*****.**',
            user__password='******',
            organizations=(cls.organization, ),
        )
        cls.admin = UserProfileFactory.create(
            user__username='******',
            user__email='*****@*****.**',
            user__password='******',
            is_administrator=True,
        )
Exemplo n.º 4
0
    def test_offer_list_fields(self):
        """Test list's fields of offers REST API endpoint."""
        OfferFactory.create_batch(42)

        response = self.client.get('/api/offers/')

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        for offer in response.data:
            common.test_offer_list_fields(self, offer)
Exemplo n.º 5
0
    def test_offer_list_length(self):
        """Test offers list length for admin user.

        All existing offers are visible for admin user.
        """
        OfferFactory.create_batch(42)

        response = self.client.get('/api/offers/')

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response.data), 42)
Exemplo n.º 6
0
    def test_offers_list_for_admin(self):
        """Test offers' list for account of admin."""
        OfferFactory.create_batch(37)

        self.client.force_login(
            UserFactory(userprofile__is_administrator=True))

        response = self.client.get('/o/offers')

        self._test_offers_list(response)
        self.assertEqual(len(response.context['offers']), 37)
Exemplo n.º 7
0
    def test_user_joined_some_offers(self):
        """Tests if user joined more than one offer."""

        user = UserFactory()
        self.client.force_login(user=user)

        OfferFactory.create_batch(2, volunteers=[user])

        # offer that user did not join
        OfferFactory(volunteers=UserFactory.create_batch(10))

        res = self.client.get(ENDPOINT_URL)

        self.assertEqual(res.status_code, 200)
        self.assertEqual(len(res.data), 2)
Exemplo n.º 8
0
 def setUp(self):
     """Set up each test."""
     super(TestAuthenticatedUserJoinOffer, self).setUp()
     self.user = UserFactory.create()
     self.offer = OfferFactory.create()
     self.offer.publish()
     self.client.force_login(self.user)
Exemplo n.º 9
0
 def test_organization_offer_read_status(self):
     """Test organization offer's read status for user with organization."""
     for offer in OfferFactory.create_batch(94,
                                            organization=self.organization):
         response = self.client.get('/api/offers/{id}/'.format(id=offer.id))
         self.assertEqual(response.status_code, status.HTTP_200_OK)
         common.test_offer_list_fields(self, response.data)
Exemplo n.º 10
0
    def setUp(self):
        """Set up test for OfferFactory"""

        self.fake_user1 = UserFactory.create(
            first_name="Fake user first_name1",
            last_name="Fake user last_name1")
        self.fake_user2 = UserFactory.create(
            first_name="Fake user first_name2",
            last_name="Fake user last_name2")
        self.fake_offer1 = OfferFactory.create(volunteers=User.objects.all())
        self.fake_offer2 = OfferFactory.create(
            title="Jakiś tytuł",
            description="Zwięzły opis",
            organization__name="Nazwa odnośnej organizacji")
        self.fake_offer3 = OfferFactory.create(
            organization=Organization.objects.last())
Exemplo n.º 11
0
    def test_offer_delete_status(self):
        """Test offer's delete status for regular user.

        API for now is read-only.
        """
        response = self.client.delete('/api/offers/{}/'.format(
            OfferFactory().id))
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
Exemplo n.º 12
0
    def test_offer_delete_status(self):
        """Test offer's delete status for user with organization.

        API for now is read-only.
        """
        offer = OfferFactory(organization=self.organization)
        response = self.client.delete('/api/offers/{}/'.format(offer.id))
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
 def setUp(self):
     super().setUp()
     user = UserFactory()
     self.client.force_login(user)
     OfferFactory.create_batch(
         67,
         offer_status='published',
         finished_at=None,
         recruitment_end_date=None,
         volunteers=UserFactory.create_batch(20)
     )
     OfferFactory.create_batch(
         73,
         offer_status='published',
         finished_at=None,
         recruitment_end_date=None,
         volunteers=UserFactory.create_batch(20) + [user]
     )
Exemplo n.º 14
0
 def test_image(self):
     """Test image field."""
     offer = OfferFactory()
     request = RequestFactory().get('/')
     request.user = AnonymousUser()
     self.assertEqual(urlparse(OfferSerializer(
         offer,
         context={'request': request},
     ).data['image']).path, offer.image.url)
Exemplo n.º 15
0
    def test_offer_read_status(self):
        """Test offer's read status for admin user.

        All existing offers are visible for admin user.
        """
        for offer in OfferFactory.create_batch(63):
            response = self.client.get('/api/offers/{id}/'.format(id=offer.id))
            self.assertEqual(response.status_code, status.HTTP_200_OK)
            common.test_offer_list_fields(self, response.data)
Exemplo n.º 16
0
 def setUp(self):
     self.offer = OfferFactory(
         organization__name="Halperin Organix",
         description="Dokładny opis oferty",
         requirements="Dokładny opis wymagań",
         time_commitment="333 dni w roku",
         benefits="Wszelkie korzyści z uczestnictwa w wolontariacie",
         location="Polska, Poznań",
         title="Zwięzły tytuł oferty",
         time_period="Od 23.09.2015 do 25.12.2016",
     )
Exemplo n.º 17
0
    def test_offer_update_status(self):
        """Test offer's update status for admin user.

        Admin user without connected organization is allowed to edit any offer.
        """
        response = self.client.put(
            '/api/offers/{}/'.format(OfferFactory().id),
            self.offer_payload,
            content_type='application/json',
        )

        self.assertEqual(response.status_code, status.HTTP_200_OK)
Exemplo n.º 18
0
 def test_image(self):
     """Test image field."""
     offer = OfferFactory()
     self.assertEqual(
         urlparse(
             OfferSerializer(
                 offer,
                 context={
                     'request': RequestFactory().get('/')
                 },
             ).data['image']).path,
         offer.images.get(is_main=True).path.url)
Exemplo n.º 19
0
    def test_offer_update_status(self):
        """Test offer's update status for anonymous user.

        Anonymous user is not allowed to edit offer.
        """
        response = self.client.put(
            '/api/offers/{}/'.format(
                OfferFactory(offer_status='published').id),
            self.offer_payload,
            content_type='application/json',
        )

        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
Exemplo n.º 20
0
    def test_offer_list_length(self):
        """Test offers list length for user with organization.

        Because we set up 74 unpublished offer create for user's organization
        and 21 published, user with organization will see 95 offers.
        """
        OfferFactory.create_batch(21, offer_status='published')
        OfferFactory.create_batch(37, offer_status='unpublished')
        OfferFactory.create_batch(42, offer_status='rejected')
        OfferFactory.create_batch(74, organization=self.organization)

        response = self.client.get('/api/offers/')

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response.data), 95)
Exemplo n.º 21
0
    def handle(self, *args, **options):
        """Populate database with fake objects."""

        # create 5 organizations:
        for _ in range(5):
            organization = OrganizationFactory.create()
            UserProfileFactory.create(organizations=(organization, ), )

        # create 50 offers:
        for _ in range(50):
            OfferFactory.create(
                organization=random.choice(Organization.objects.all()))

        # create 150 volunteers:
        for _ in range(150):
            userprofile = UserProfileFactory.create()
            no_of_offers = random.randrange(10)
            for offer in random.sample(list(Offer.objects.all()),
                                       no_of_offers):
                offer.volunteers.add(userprofile.user)

        self.stdout.write(
            self.style.SUCCESS('Database successfully populated'))
Exemplo n.º 22
0
    def test_offer_update_status(self):
        """Test offer's update status for user with organization.

        Regular user with connected organization is allowed to edit its offer.
        """
        self.client.force_login(
            UserFactory(userprofile__organizations=[self.organization]))

        response = self.client.put('/api/offers/{}/'.format(
            OfferFactory(organization=self.organization).id),
                                   self.offer_payload,
                                   content_type='application/json')

        self.assertEqual(response.status_code, status.HTTP_200_OK)
Exemplo n.º 23
0
    def test_offer_update_status(self):
        """Test offer's update status for regular user.

        Regular user without connected organization is not allowed to edit
        offer.
        """
        self.client.force_login(UserFactory())

        response = self.client.put('/api/offers/{}/'.format(
            OfferFactory(offer_status='published').id),
                                   self.offer_payload,
                                   content_type='application/json')

        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
Exemplo n.º 24
0
    def handle(self, *args, **options):
        """Populate database with fake objects."""

        # create 5 organizations
        for organization in range(0, 5):
            OrganizationFactory.create()

        # create admin-user
        UserProfileFactory.create(is_administrator=True)

        # create 15 volunteers
        for volunteer in range(0, 14):
            UserProfileFactory.create(
                organizations=self.create_list_of_objects('Organization', 3),
                is_administrator=False)

        # create 50 offers
        for offer in range(0, 50):
            OfferFactory.create(
                organization=self.create_list_of_objects('Organization', 1)[0],
                volunteers=self.create_list_of_objects('User', 3))

        self.stdout.write(
            self.style.SUCCESS('Database successfully populated'))
Exemplo n.º 25
0
    def test_user_joined_one_offer(self):
        """Tests if user joined one offer."""

        user = UserFactory()
        self.client.force_login(user=user)

        offer = OfferFactory()
        offer.volunteers.add(user)

        res = self.client.get(ENDPOINT_URL)

        self.assertEqual(res.status_code, 200)
        self.assertEqual(len(res.data), 1)
        self.assertEqual(offer.id, res.data[0]['id'])
        self.assertEqual(offer.title, res.data[0]['title'])
Exemplo n.º 26
0
 def setUpTestData(cls):
     """Fixtures for Offer model unittests."""
     volunteers = [
         UserFactory(email='*****@*****.**'),
         UserFactory(email='*****@*****.**'),
         UserFactory(email='*****@*****.**'),
     ]
     offer = OfferFactory(
         organization__name='Some great organization',
         description='A lot of unnecessary work.',
         requirements='Patience, lot of free time',
         time_commitment='12.12.2015',
         benefits='Friends with benefits',
         location='Poland, Poznań',
         title='Example Offer Title',
         time_period='2-5 times a week',
     )
     for volunteer in volunteers:
         offer.volunteers.add(volunteer)
Exemplo n.º 27
0
    def test_offer_list_length(self):
        """Test offers list length for anonymous user.

        Because we set up 13 published offers, only them will be visible for
        anonymous user.
        """
        OfferFactory.create_batch(13, offer_status='published')
        OfferFactory.create_batch(54, offer_status='unpublished')
        OfferFactory.create_batch(47, offer_status='rejected')

        response = self.client.get('/api/offers/')

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response.data), 13)
Exemplo n.º 28
0
    def test_offer_list_for_anonymous_user(self):
        """Test offers' list for anonymus user."""
        OfferFactory.create_batch(
            31,
            offer_status='published',
            finished_at=None,
            recruitment_end_date=None,
        )
        OfferFactory.create_batch(48, offer_status='unpublished')
        OfferFactory.create_batch(52, offer_status='rejected')

        response = self.client.get('/o/offers')

        self._test_offers_list(response)
        self.assertEqual(len(response.context['offers']), 31)
Exemplo n.º 29
0
    def test_offers_list_for_volunteer(self):
        """Test offers' list for account of volunteer."""
        OfferFactory.create_batch(
            67,
            offer_status='published',
            finished_at=None,
            recruitment_end_date=None,
        )
        OfferFactory.create_batch(73, offer_status='unpublished')
        OfferFactory.create_batch(89, offer_status='rejected')

        self.client.force_login(UserFactory())

        response = self.client.get('/o/offers')

        self._test_offers_list(response)
        self.assertEqual(len(response.context['offers']), 67)
Exemplo n.º 30
0
    def test_offers_list_for_organization(self):
        """Test offers' list for account of organization."""
        OfferFactory.create_batch(
            96,
            offer_status='published',
            finished_at=None,
            recruitment_end_date=None,
        )
        OfferFactory.create_batch(17, offer_status='unpublished')
        OfferFactory.create_batch(22, offer_status='rejected')

        self.client.force_login(
            UserFactory(userprofile__organizations=[OrganizationFactory()]))

        response = self.client.get('/o/offers')

        self._test_offers_list(response)
        self.assertEqual(len(response.context['offers']), 96)