def test_negative_manual_attendance(self): """Ensure we disallow negative manual attendance. This is a regression test for https://github.com/swcarpentry/amy/issues/435. Warning: this used to test `admin_fee` for negative value, but since #1411 (https://github.com/swcarpentry/amy/pull/1411) we don't have `admin_fee` in the form nor on Event Details page.""" error_str = "Ensure this value is greater than or equal to 0." data = { 'host': self.test_host.id, 'tags': [self.test_tag.id], 'slug': '2016-06-30-test-event', 'manual_attendance': -36, 'invoice_status': 'unknown', } data['manual_attendance'] = -36 f = EventForm(data) self.assertIn('manual_attendance', f.errors) self.assertIn(error_str, f.errors['manual_attendance']) data['manual_attendance'] = 0 f = EventForm(data) self.assertNotIn('manual_attendance', f.errors) data['slug'] = '2016-06-30-test-event2' data['manual_attendance'] = 36 f = EventForm(data) self.assertTrue(f.is_valid())
def test_curricula_circuits_tag(self): """Ensure validation of `curricula` and `tags` fields.""" # missing tags data = { 'slug': '2018-10-28-curriculum', 'host': self.org_alpha.pk, # there has to be some tag 'tags': [Tag.objects.get(name='DC').pk], 'curricula': [ Curriculum.objects.get(slug='swc-python').pk, Curriculum.objects.get(mix_match=True).pk, ], } form = EventForm(data) # we're missing SWC and Circuits self.assertIn('curricula', form.errors) # try adding SWC tag data['tags'].append(Tag.objects.get(name='SWC').pk) form = EventForm(data) self.assertIn('curricula', form.errors) # now we're missing only circuits # try adding Circuits tag data['tags'].append(Tag.objects.get(name='Circuits').pk) form = EventForm(data) self.assertNotIn('curricula', form.errors)
def test_open_TTT_applications_form_validation(self): """Ensure validation of `open_TTT_applications` field.""" data = { 'slug': '2018-09-02-open-applications', 'host': self.org_alpha.pk, 'tags': [Tag.objects.get(name='SWC').pk], 'invoice_status': 'unknown', 'open_TTT_applications': True, } form = EventForm(data) self.assertFalse(form.is_valid()) self.assertIn('open_TTT_applications', form.errors.keys()) data['tags'] = [Tag.objects.get(name='TTT').pk] form = EventForm(data) self.assertTrue(form.is_valid())
def test_slug_illegal_formats(self): """Disallow slugs with wrong formats. Slug format should follow: YYYY-MM-DD-location, where YYYY, MM, DD can be unspecified (== 'xx').""" data = { 'slug': '', 'host': Organization.objects.all()[0].pk, 'tags': [Tag.objects.first().pk], 'invoice_status': 'unknown', } # disallow invalid formats formats = [ '20166-06-30-Krakow', '2016-006-30-Krakow', '2016-06-300-Krakow', '201-06-30-Krakow', '2016-6-30-Krakow', '2016-06-3-Krakow', 'SWC-2016-06-300-Krakow', '', 'xxxxx-xx-xx-Krakow', 'xxxx-xxx-xx-Krakow', 'xxxx-xx-xxx-Krakow', 'xxx-xx-xx-Krakow', 'xxxx-x-xx-Krakow', 'xxxx-xx-x-Krakow', ] for slug in formats: with self.subTest(slug=slug): data['slug'] = slug f = EventForm(data) self.assertEqual(f.is_valid(), False) self.assertIn('slug', f.errors)
def test_creating_event_with_no_comment(self): """Ensure that no comment is added when the form without comment content is saved.""" self.assertEqual(Comment.objects.count(), 0) data = { 'slug': '2018-12-28-test-event', 'host': self.org_alpha.id, 'tags': [self.test_tag.id], 'comment': '', } form = EventForm(data) form.save() self.assertEqual(Comment.objects.count(), 0)
def test_curricula_and_tags_validation(self): """Ensure validation of `curricula` and `tags` fields.""" # missing tags data = { 'slug': '2018-10-28-curriculum', 'host': self.org_alpha.pk, 'tags': [ Tag.objects.get(name='TTT').pk, Tag.objects.get(name='online').pk, ], 'curricula': [ Curriculum.objects.get(slug='swc-python').pk, Curriculum.objects.get(slug='dc-geospatial').pk, Curriculum.objects.get(slug='lc').pk, # below isn't a valid choice # Curriculum.objects.get(unknown=True).pk, ], } form = EventForm(data) self.assertIn('curricula', form.errors) # try adding SWC tag data['tags'].append(Tag.objects.get(name='SWC').pk) form = EventForm(data) self.assertIn('curricula', form.errors) # try adding DC tag data['tags'].append(Tag.objects.get(name='DC').pk) form = EventForm(data) self.assertIn('curricula', form.errors) # try adding LC tag data['tags'].append(Tag.objects.get(name='LC').pk) form = EventForm(data) self.assertNotIn('curricula', form.errors)
def test_creating_event_with_comment(self): """Ensure that a comment is added when the form with comment content is saved.""" self.assertEqual(Comment.objects.count(), 0) data = { 'slug': '2018-12-28-test-event', 'host': self.org_alpha.id, 'tags': [self.test_tag.id], 'comment': 'This is a test comment.', } form = EventForm(data) obj = form.save() self.assertEqual(Comment.objects.count(), 1) comment = Comment.objects.first() self.assertEqual(comment.comment, 'This is a test comment.') self.assertIn(comment, Comment.objects.for_model(obj))
def test_slug_illegal_characters(self): """Disallow slugs with wrong characters. Slug allows only: latin characters, numbers, dashes and underscores. Slug format should follow: YYYY-MM-DD-location, where YYYY, MM, DD can be unspecified (== 'xx').""" data = { 'slug': '', 'host': Organization.objects.all()[0].pk, 'tags': Tag.objects.all(), 'invoice_status': 'unknown', } # disallow illegal characters for slug_suffix in ['a/b', 'a b', 'a!b', 'a.b', 'a\\b', 'a?b', 'aób']: with self.subTest(slug_suffix=slug_suffix): data['slug'] = '2016-06-30-{}'.format(slug_suffix) f = EventForm(data) self.assertEqual(f.is_valid(), False) self.assertIn('slug', f.errors)
def test_slug_valid_formats(self): """Allow slugs with wrong formats. Slug format should follow: YYYY-MM-DD-location, where YYYY, MM, DD can be unspecified (== 'xx').""" data = { 'slug': '', 'host': Organization.objects.all()[0].pk, 'tags': [Tag.objects.first().pk], 'invoice_status': 'unknown', } # allow correct formats formats = [ '2016-06-30-Krakow', '2016-06-xx-Krakow', '2016-xx-30-Krakow', 'xxxx-06-30-Krakow', '2016-xx-xx-Krakow', 'xxxx-06-xx-Krakow', 'xxxx-xx-30-Krakow', 'xxxx-xx-xx-Krakow', '2016-06-30-Krakow-multiple-words', '2016-06-xx-Krakow-multiple-words', '2016-xx-30-Krakow-multiple-words', 'xxxx-06-30-Krakow-multiple-words', '2016-xx-xx-Krakow-multiple-words', 'xxxx-06-xx-Krakow-multiple-words', 'xxxx-xx-30-Krakow-multiple-words', 'xxxx-xx-xx-Krakow-multiple-words', ] for slug in formats: with self.subTest(slug=slug): data['slug'] = slug f = EventForm(data) self.assertEqual(f.is_valid(), True) self.assertNotIn('slug', f.errors)