Пример #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'))
Пример #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
Пример #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,
        )
Пример #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)
Пример #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)
Пример #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)
Пример #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)
Пример #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)
Пример #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)
Пример #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())
Пример #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)
Пример #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]
     )
Пример #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)
Пример #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)
Пример #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",
     )
Пример #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)
Пример #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)
Пример #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)
Пример #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)
Пример #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'))
Пример #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)
Пример #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)
Пример #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'))
Пример #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'])
Пример #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)
Пример #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)
Пример #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)
Пример #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)
Пример #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)