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
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)
def test_second_registration(self): """Test register if user is registered already""" UserFactory.create( email="*****@*****.**", password="******", ) response = self.client.post( reverse('register'), json.dumps({ "email": "*****@*****.**", "password": "******" }), content_type='application/json' ) self.assertEqual(response.status_code, 201)
def test_get(self): for _ in range(5): UserFactory.create() admin_emails = sorted( UserFactory.create(userprofile__is_administrator=True).email for _ in range(2)) res = self.client.get(ENDPOINT_URL) self.assertDictEqual( res.data, { 'administrator_emails': admin_emails, 'applicant_types': ContactSerializer.APPLICANT_CHOICES, })
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())
def initialize_empty_organization(): """Initialize empty organization.""" organization1 = OrganizationFactory() UserFactory(email='*****@*****.**', password='******', userprofile__organizations=[organization1]) return organization1
def test_does_not_raise_error_if_user_is_admin(self): user = UserFactory.create(userprofile__is_administrator=True) try: validate_admin_email(user.email) except ValidationError: self.fail('raised error when user is admin')
def setUp(self): """Set up each test.""" super().setUp() self.organization = OrganizationFactory() self.client.force_login(UserFactory( userprofile__organizations=[self.organization] ))
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)
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] )
def test_raises_error_if_user_is_not_admin(self): user = UserFactory.create(userprofile__is_administrator=False) with self.assertRaises( ValidationError, msg='Administrator o adresie e-mail {} nie istnieje.'.format( user.email, ), ): validate_admin_email(user.email)
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)
def test_user_joined_no_offers(self): """Tests if user did not join any offer.""" user = UserFactory() self.client.force_login(user=user) res = self.client.get(ENDPOINT_URL) self.assertEqual(res.status_code, 200) self.assertEqual(res.data, [])
def initialize_empty_volunteer(): """Initialize empty volunteer.""" volunteer_user1 = UserFactory( email='*****@*****.**', password='******', first_name='Grzegorz', last_name='Brzęczyszczykiewicz', userprofile__phone_no='333666999', ) return volunteer_user1
def test_login(self): user = UserFactory(password='******', userprofile__organizations=[OrganizationFactory()]) res = self.client.post(ENDPOINT_URL, { 'username': user.username, 'password': '******' }, format='json') self.assertEqual(res.status_code, 200)
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)
def test_login(self): user = UserFactory.create(password='******') organization = OrganizationFactory.create() organization.userprofiles = [user.userprofile] organization.save() res = self.client.post(ENDPOINT_URL, { 'username': user.username, 'password': '******' }, format='json') self.assertEqual(res.status_code, 200)
def test_organization_update_status(self): """Test organization's update status for regular user. API for now is read-only. """ self.client.force_login(UserFactory()) response = self.client.put( '/api/organizations/{}/'.format(OrganizationFactory().id), self.organization_payload, content_type='application/json', ) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
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)
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)
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'])
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)
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)
def setUp(self): """setting up each test.""" UserFactory.create(first_name="nie-Jan", last_name="nie-Kowalski") self.totally_fake_user = UserFactory.create()
def setUp(self): self.offer = OfferFactory(volunteers=UserFactory.create_batch(10))
def setUp(self): """ Set up for each test """ self.user = UserFactory.create() self.uid = str(urlsafe_base64_encode(force_bytes(self.user.pk)), 'utf-8') self.token = default_token_generator.make_token(self.user)
def setUpClass(cls): super().setUpClass() cls.admin = UserFactory.create(userprofile__is_administrator=True)
def setUp(self): """Set up each test.""" super().setUp() self.client.force_login(UserFactory())
def setUp(self): """Set up each test.""" super().setUp() self.client.force_login( UserFactory(userprofile__is_administrator=True))
def setUpClass(cls): cls.user = UserFactory.create()