class DiningListCleanTestCase(TestCase):
    @classmethod
    def setUpTestData(cls):
        cls.user = User.objects.create_user('ankie')
        cls.association = Association.objects.create()

    def setUp(self):
        self.dining_list = DiningList(date=date(2123, 1, 2),
                                      sign_up_deadline=datetime(
                                          2100, 2, 2, tzinfo=timezone.utc),
                                      association=self.association)

    def test_sign_up_deadline_valid(self):
        self.dining_list.full_clean()  # Shouldn't raise

    def test_sign_up_deadline_after_date(self):
        self.dining_list.sign_up_deadline = datetime(2123, 1, 3, 18, 00)
        self.assertRaises(ValidationError, self.dining_list.full_clean)
    def test_association_unique_for_date(self):
        """Test that there can be only one dining slot for an association for each date"""
        # Save one dining list
        self.form.save()

        # Try creating another one with same association
        dl = DiningList(date=self.dining_date)
        data = {
            'dish': 'Kwark',
            'association': str(self.association1.pk),
            'max_diners': '18',
            'serve_time': '17:00'
        }
        form = CreateSlotForm(self.user1, data, instance=dl)
        self.assertFalse(form.is_valid())
        self.assertTrue(form.has_error('association'))
 def setUp(self):
     self.association1 = Association.objects.create(name="Quadrivium")
     self.user1 = User.objects.create_user('jan')
     UserMembership.objects.create(related_user=self.user1,
                                   association=self.association1,
                                   is_verified=True,
                                   verified_on=timezone.now())
     # Date two days in the future
     self.dining_date = timezone.now().date() + timedelta(days=2)
     self.form_data = {
         'dish': 'Kwark',
         'association': str(self.association1.pk),
         'max_diners': '18',
         'serve_time': '17:00'
     }
     self.dining_list = DiningList(date=self.dining_date)
     self.form = CreateSlotForm(self.user1,
                                self.form_data,
                                instance=self.dining_list)
class DiningListTestCase(TestCase):
    @classmethod
    def setUpTestData(cls):
        cls.user = User.objects.create_user('ankie')
        cls.association = Association.objects.create()

    def setUp(self):
        self.dining_list = DiningList(date=date(2123, 1, 2),
                                      sign_up_deadline=datetime(
                                          2100, 2, 2, tzinfo=timezone.utc),
                                      association=self.association)

    def test_is_open(self):
        list = DiningList.objects.create(date=date(2015, 1, 1),
                                         association=self.association,
                                         sign_up_deadline=datetime(
                                             2015,
                                             1,
                                             1,
                                             17,
                                             00,
                                             tzinfo=timezone.utc))

        with patch.object(
                timezone,
                'now',
                return_value=datetime(2015, 1, 1, 16, 59,
                                      tzinfo=timezone.utc)) as mock_now:
            self.assertTrue(list.is_open())
        with patch.object(
                timezone,
                'now',
                return_value=datetime(2015, 1, 1, 17, 00,
                                      tzinfo=timezone.utc)) as mock_now:
            self.assertFalse(list.is_open())
        with patch.object(
                timezone,
                'now',
                return_value=datetime(2015, 1, 1, 17, 1,
                                      tzinfo=timezone.utc)) as mock_now:
            self.assertFalse(list.is_open())

    def test_is_owner(self):
        self.dining_list.save()
        self.dining_list.owners.add(self.user)
        self.assertTrue(self.dining_list.is_owner(self.user))

    def test_is_owner_false(self):
        self.dining_list.save()
        self.assertFalse(self.dining_list.is_owner(self.user))
    def test_invalid_association(self):
        """Tests using an association which the user is not a member of

        NOTE: when there is 1 available association, the form sets the association field to disabled. This means that
        the value that is sent with the form is totally ignored in favor of the initial value of the field. Therefore
        setting a different invalid association in the form data results in the form data actually being valid, but it
        does not use this association, instead it uses the other association which the user is actually a member of.

        Source: https://docs.djangoproject.com/en/2.2/ref/forms/fields/#disabled
        """
        association = Association.objects.create(name='Knights')
        form_data = {
            'dish': 'Boter',
            'association': str(association.pk),
            'max_diners': '20',
            'serve_time': '18:00'
        }
        form = CreateSlotForm(self.user1,
                              form_data,
                              instance=DiningList(date=self.dining_date))
        self.assertTrue(form.is_valid())
        # Check that the actual association is not Knights but Quadrivium
        self.assertEqual(self.association1, form.instance.association)
 def setUp(self):
     self.dining_list = DiningList(date=date(2123, 1, 2),
                                   sign_up_deadline=datetime(
                                       2100, 2, 2, tzinfo=timezone.utc),
                                   association=self.association)