def test_two_slots_one_session(self): # If there are two slots, we return data for each of them, in order of the slots # start time user1 = UserFactory(first_name="Melanie", last_name="Pickle", email='*****@*****.**') user2 = UserFactory(first_name="Ichabod", last_name="Crane", email='*****@*****.**') session = SessionFactory() slot1 = SlotFactory(start=datetime.time(13)) slot2 = SlotFactory(start=datetime.time(8)) session.slots.add(slot1) session.slots.add(slot2) SessionRoleFactory(session=session, user=user1, role=SessionRole.SESSION_ROLE_CHAIR) SessionRoleFactory(session=session, user=user2, role=SessionRole.SESSION_ROLE_CHAIR, status=False) data = self.get_api_result() self.assertEqual([{ 'conf_key': slot2.pk, 'chair_email': '*****@*****.**', 'chair_name': 'Melanie Pickle', 'runner_email': '', 'runner_name': '' }, { 'conf_key': slot1.pk, 'chair_email': '*****@*****.**', 'chair_name': 'Melanie Pickle', 'runner_email': '', 'runner_name': '' }], data)
def setUp(self): super(TestSponsorApplicationForm, self).setUp() self.sponsor_level = SponsorLevelFactory() self.user = UserFactory() self.data = { 'name': 'Sponsor', 'contact_name': self.user.get_full_name(), 'contact_email': self.user.email, 'contact_phone': '336-867-5309', 'contact_address': '123 Main Street, Anytown, NC 90210', 'level': self.sponsor_level.pk, 'wants_table': True, 'wants_booth': True, }
def test_two_slots_two_sessions(self): # If there are two slots, we return data for each of them, in order of the slots # start time user1 = UserFactory(first_name="Melanie", last_name="Pickle", email='*****@*****.**') user2 = UserFactory(first_name="Ichabod", last_name="Crane", email='*****@*****.**') session1 = SessionFactory() session2 = SessionFactory() slot1 = SlotFactory(start=datetime.time(13)) slot2 = SlotFactory(start=datetime.time(8)) session1.slots.add(slot1) session2.slots.add(slot2) SessionRoleFactory(session=session1, user=user1, role=SessionRole.SESSION_ROLE_CHAIR) SessionRoleFactory(session=session1, user=user2, role=SessionRole.SESSION_ROLE_RUNNER) user3 = UserFactory(first_name="Mike", last_name="Hammer", email='*****@*****.**') user4 = UserFactory(first_name="Sam", last_name="Spade", email='*****@*****.**') SessionRoleFactory(session=session2, user=user3, role=SessionRole.SESSION_ROLE_CHAIR) SessionRoleFactory(session=session2, user=user4, role=SessionRole.SESSION_ROLE_RUNNER) data = self.get_api_result() self.assertEqual([{ 'conf_key': slot2.pk, 'chair_email': '*****@*****.**', 'chair_name': 'Mike Hammer', 'runner_email': '*****@*****.**', 'runner_name': 'Sam Spade' }, { 'conf_key': slot1.pk, 'chair_email': '*****@*****.**', 'chair_name': 'Melanie Pickle', 'runner_email': '*****@*****.**', 'runner_name': 'Ichabod Crane' }], data)
def setUp(self): super(TestSponsorApply, self).setUp() Conference.objects.get_or_create(pk=settings.CONFERENCE_ID) self.user = UserFactory() self.login_user(self.user) self.sponsor_level = SponsorLevelFactory() self.data = { 'name': 'Sponsor', 'contact_name': self.user.get_full_name(), 'contact_emails': [self.user.email], 'contact_phone': '336-867-5309', 'contact_address': '123 Main Street, Anytown, NC 90210', 'level': self.sponsor_level.pk, 'wants_table': True, 'wants_booth': True, 'web_logo': open(TEST_IMAGE_FILENAME, "rb"), 'external_url': 'http://example.com', 'web_description': 'Fools paradise', }
def setUp(self): super(TestSponsorApplicationForm, self).setUp() self.sponsor_level = SponsorLevelFactory() self.user = UserFactory() self.second_email = '*****@*****.**' self.data = { 'name': 'Sponsor', 'contact_name': self.user.get_full_name(), 'contact_emails': self.user.email + "\n" + self.second_email, 'contact_phone': '336-867-5309', 'contact_address': '123 Main Street, Anytown, NC 90210', 'level': self.sponsor_level.pk, 'wants_table': True, 'wants_booth': True, 'external_url': 'https://example.com', 'web_description': 'Funky sponsor', } self.files = { 'web_logo': SimpleUploadedFile('file.png', TEST_IMAGE), }
def test_two_chairs_two_runners_one_declined(self): user1 = UserFactory(first_name="Melanie", last_name="Pickle", email='*****@*****.**') user2 = UserFactory(first_name="Ichabod", last_name="Crane", email='*****@*****.**') session = SessionFactory() slot = SlotFactory() session.slots.add(slot) SessionRoleFactory(session=session, user=user1, role=SessionRole.SESSION_ROLE_CHAIR) SessionRoleFactory(session=session, user=user2, role=SessionRole.SESSION_ROLE_CHAIR, status=False) user3 = UserFactory(first_name="Mike", last_name="Hammer", email='*****@*****.**') user4 = UserFactory(first_name="Sam", last_name="Spade", email='*****@*****.**') SessionRoleFactory(session=session, user=user3, role=SessionRole.SESSION_ROLE_RUNNER) SessionRoleFactory(session=session, user=user4, role=SessionRole.SESSION_ROLE_RUNNER, status=False) data = self.get_api_result() self.assertEqual([{ 'conf_key': slot.pk, 'chair_email': '*****@*****.**', 'chair_name': 'Melanie Pickle', 'runner_email': '*****@*****.**', 'runner_name': 'Mike Hammer' }], data)
def test_slot_chair_and_runner(self): user1 = UserFactory(first_name="Melanie", last_name="Pickle", email='*****@*****.**') user2 = UserFactory(first_name="Ichabod", last_name="Crane", email='*****@*****.**') session = SessionFactory() slot = SlotFactory() session.slots.add(slot) SessionRoleFactory(session=session, user=user1, role=SessionRole.SESSION_ROLE_CHAIR) SessionRoleFactory(session=session, user=user2, role=SessionRole.SESSION_ROLE_RUNNER) data = self.get_api_result() self.assertEqual([{ 'conf_key': slot.pk, 'chair_email': '*****@*****.**', 'chair_name': 'Melanie Pickle', 'runner_email': '*****@*****.**', 'runner_name': 'Ichabod Crane', }], data)
def test_slot_runner_no_chair(self): user = UserFactory(first_name="Melanie", last_name="Pickle", email='*****@*****.**') session = SessionFactory() slot = SlotFactory() session.slots.add(slot) SessionRoleFactory(session=session, user=user, role=SessionRole.SESSION_ROLE_RUNNER) data = self.get_api_result() self.assertEqual([{ 'conf_key': slot.pk, 'chair_email': '', 'chair_name': '', 'runner_email': '*****@*****.**', 'runner_name': 'Melanie Pickle', }], data)
class TestSponsorApplicationForm(TestCase): form_class = forms.SponsorApplicationForm def setUp(self): super(TestSponsorApplicationForm, self).setUp() self.sponsor_level = SponsorLevelFactory() self.user = UserFactory() self.data = { 'name': 'Sponsor', 'contact_name': self.user.get_full_name(), 'contact_email': self.user.email, 'contact_phone': '336-867-5309', 'contact_address': '123 Main Street, Anytown, NC 90210', 'level': self.sponsor_level.pk, 'wants_table': True, 'wants_booth': True, } def test_initial(self): """User data should be passed in as initial data.""" form = self.form_class(user=self.user) self.assertEqual(form.initial['contact_name'], self.user.get_full_name()) self.assertEqual(form.initial['contact_email'], self.user.email) def test_user_saved_as_applicant(self): """User should be saved as the sponsor's applicant.""" form = self.form_class(user=self.user, data=self.data) sponsor = form.save() self.assertEqual(sponsor.applicant, self.user) def test_phone_required(self): """Contact phone number is a required field.""" self.data.pop('contact_phone') form = self.form_class(user=self.user, data=self.data) self.assertFalse(form.is_valid()) self.assertTrue('contact_phone' in form.errors) def test_address_required(self): """Contact address is a required field.""" self.data.pop('contact_address') form = self.form_class(user=self.user, data=self.data) self.assertFalse(form.is_valid()) self.assertTrue('contact_address' in form.errors) def test_wants_booth_optional(self): """ wants_booth is optional and should be stored as False if not given. """ self.data.pop('wants_booth') form = self.form_class(user=self.user, data=self.data) self.assertTrue(form.is_valid()) sponsor = form.save() self.assertFalse(sponsor.wants_booth) def test_wants_booth_true(self): form = self.form_class(user=self.user, data=self.data) self.assertTrue(form.is_valid()) sponsor = form.save() self.assertTrue(sponsor.wants_booth) def test_wants_table_optional(self): """ wants_table is optional and should be stored as False if not given. """ self.data.pop('wants_table') form = self.form_class(user=self.user, data=self.data) self.assertTrue(form.is_valid()) sponsor = form.save() self.assertFalse(sponsor.wants_table) def test_wants_table_true(self): form = self.form_class(user=self.user, data=self.data) self.assertTrue(form.is_valid()) sponsor = form.save() self.assertTrue(sponsor.wants_table)
class TestSponsorApply(ViewTestMixin, TestCase): fixtures = [ os.path.join(FIXTURE_DIR, 'sponsorship_levels.json'), os.path.join(FIXTURE_DIR, 'sponsorship_benefits.json'), ] url_name = 'sponsor_apply' def setUp(self): super(TestSponsorApply, self).setUp() Conference.objects.get_or_create(pk=settings.CONFERENCE_ID) self.user = UserFactory() self.login_user(self.user) self.sponsor_level = SponsorLevelFactory() self.data = { 'name': 'Sponsor', 'contact_name': self.user.get_full_name(), 'contact_emails': [self.user.email], 'contact_phone': '336-867-5309', 'contact_address': '123 Main Street, Anytown, NC 90210', 'level': self.sponsor_level.pk, 'wants_table': True, 'wants_booth': True, 'web_logo': open(TEST_IMAGE_FILENAME, "rb"), 'external_url': 'http://example.com', 'web_description': 'Fools paradise', } def test_get_unauthenticated(self): self.client.logout() response = self.client.get(reverse(self.url_name)) self.assertEqual(response.status_code, 302) def test_post_unauthenticated(self): self.client.logout() response = self.client.post(reverse(self.url_name), data=self.data) self.assertRedirectsToLogin(response) def test_get(self): response = self.client.get(reverse(self.url_name)) self.assertEqual(response.status_code, 200) self.assertTrue('form' in response.context) self.assertFalse(response.context['form'].is_bound) def test_post_valid(self): response = self.client.post(reverse(self.url_name), data=self.data) self.assertRedirectsNoFollow(response, reverse('dashboard')) self.assertEqual(Sponsor.objects.count(), 1) sponsor = Sponsor.objects.first() self.assertEqual(self.data['web_description'], sponsor.web_description) self.assertEqual(TEST_IMAGE, sponsor.web_logo.read()) def test_post_invalid(self): response = self.client.post(reverse(self.url_name), data={}) self.assertEqual(response.status_code, 200) self.assertTrue('form' in response.context) self.assertTrue(response.context['form'].is_bound) self.assertFalse(response.context['form'].is_valid()) def test_company_link_required(self): self.data.pop('external_url') response = self.client.post(reverse(self.url_name), data=self.data) self.assertIn('external_url', response.context['form'].errors) def test_web_description_required(self): self.data.pop('web_description') response = self.client.post(reverse(self.url_name), data=self.data) self.assertIn('web_description', response.context['form'].errors) def test_web_logo_required(self): self.data.pop('web_logo') response = self.client.post(reverse(self.url_name), data=self.data) self.assertIn('web_logo', response.context['form'].errors)
class TestSponsorApplicationForm(TestCase): form_class = forms.SponsorApplicationForm def setUp(self): super(TestSponsorApplicationForm, self).setUp() self.sponsor_level = SponsorLevelFactory() self.user = UserFactory() self.second_email = '*****@*****.**' self.data = { 'name': 'Sponsor', 'contact_name': self.user.get_full_name(), 'contact_emails': self.user.email + "\n" + self.second_email, 'contact_phone': '336-867-5309', 'contact_address': '123 Main Street, Anytown, NC 90210', 'level': self.sponsor_level.pk, 'wants_table': True, 'wants_booth': True, 'external_url': 'https://example.com', 'web_description': 'Funky sponsor', } self.files = { 'web_logo': SimpleUploadedFile('file.png', TEST_IMAGE), } def test_initial(self): """User data should be passed in as initial data.""" form = self.form_class(user=self.user) self.assertEqual(form.initial['contact_name'], self.user.get_full_name()) self.assertEqual(form.initial['contact_emails'], [self.user.email]) def test_user_saved_as_applicant(self): """User should be saved as the sponsor's applicant.""" form = self.form_class(user=self.user, data=self.data, files=self.files) sponsor = form.save() self.assertEqual(sponsor.applicant, self.user) self.assertEqual(sponsor.contact_emails, [self.user.email, self.second_email]) def validate_field_required(self, field_name, value=None, error=None): # Test that a field is required, or some value is not valid. # If value is None, don't supply that field; otherwise, use that value # Expect the field name to be in the errors. # If 'error' given, expect that to be form.errors[field_name] if value is None: self.data.pop(field_name) else: self.data[field_name] = value form = self.form_class(user=self.user, data=self.data, files=self.files) self.assertFalse(form.is_valid()) self.assertIn(field_name, form.errors) if error: self.assertEqual(error, form.errors[field_name]) def test_contact_email_required(self): """Must be at least one contact email""" self.validate_field_required('contact_emails', '', [u'This field is required.']) def test_contact_emails_validated(self): self.validate_field_required('contact_emails', 'not_an_email\[email protected]', [u'Enter valid email addresses.']) def test_phone_required(self): """Contact phone number is a required field.""" self.validate_field_required('contact_phone') def test_address_required(self): """Contact address is a required field.""" self.validate_field_required('contact_address') def test_wants_booth_optional(self): """ wants_booth is optional and should be stored as False if not given. """ self.data.pop('wants_booth') form = self.form_class(user=self.user, data=self.data, files=self.files) self.assertTrue(form.is_valid(), msg=form.errors) sponsor = form.save() self.assertFalse(sponsor.wants_booth) def test_wants_booth_true(self): form = self.form_class(user=self.user, data=self.data, files=self.files) self.assertTrue(form.is_valid(), msg=form.errors) sponsor = form.save() self.assertTrue(sponsor.wants_booth) def test_wants_table_optional(self): """ wants_table is optional and should be stored as False if not given. """ self.data.pop('wants_table') form = self.form_class(user=self.user, data=self.data, files=self.files) self.assertTrue(form.is_valid(), msg=form.errors) sponsor = form.save() self.assertFalse(sponsor.wants_table) def test_wants_table_true(self): form = self.form_class(user=self.user, data=self.data, files=self.files) self.assertTrue(form.is_valid(), msg=form.errors) sponsor = form.save() self.assertTrue(sponsor.wants_table) def test_company_link_required(self): self.validate_field_required('external_url') def test_company_description_required(self): self.validate_field_required('web_description') def test_web_logo_required(self): self.files.pop('web_logo') form = self.form_class(user=self.user, data=self.data, files=self.files) self.assertFalse(form.is_valid()) self.assertIn('web_logo', form.errors)