Exemple #1
0
class RetrieveSelfTestCase(BaseAPITestCase):
    fixtures = [
        "test_abakus_groups.yaml",
        "test_users.yaml",
        "test_companies.yaml",
        "test_events.yaml",
    ]

    def setUp(self):
        self.user = User.objects.get(pk=1)

    def test_self_authed(self):
        self.client.force_authenticate(user=self.user)
        response = self.client.get(reverse("api:v1:user-me"))

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(self.user.penalties.valid()),
                         len(response.json()["penalties"]))
        data = response.json()
        self.assertEqual(self.user.id, data["id"])

        self.assertEqual(self.user.username, data["username"])
        self.assertEqual(self.user.first_name, data["firstName"])
        self.assertEqual(self.user.last_name, data["lastName"])
        self.assertEqual(self.user.full_name, data["fullName"])
        self.assertEqual(self.user.email, data["email"])
        self.assertEqual(self.user.is_active, data["isActive"])

    def test_self_unauthed(self):
        response = self.client.get(reverse("api:v1:user-me"))
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

    @mock.patch("django.utils.timezone.now",
                return_value=fake_time(2016, 10, 1))
    def test_own_penalties_serializer(self, mock_now):
        source = Event.objects.all().first()
        Penalty.objects.create(
            created_at=mock_now() - timedelta(days=20),
            user=self.user,
            reason="test",
            weight=1,
            source_event=source,
        )
        Penalty.objects.create(
            created_at=mock_now() - timedelta(days=19, hours=23, minutes=59),
            user=self.user,
            reason="test",
            weight=1,
            source_event=source,
        )
        self.client.force_authenticate(user=self.user)
        response = self.client.get(reverse("api:v1:user-me"))

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        self.assertEqual(len(self.user.penalties.valid()),
                         len(response.json()["penalties"]))
        self.assertEqual(len(response.json()["penalties"]), 1)
        self.assertEqual(len(response.json()["penalties"][0]), 7)
Exemple #2
0
class RetrieveSelfTestCase(BaseAPITestCase):
    fixtures = [
        'test_abakus_groups.yaml', 'test_users.yaml', 'test_companies.yaml',
        'test_events.yaml'
    ]

    def setUp(self):
        self.user = User.objects.get(pk=1)

    def test_self_authed(self):
        self.client.force_authenticate(user=self.user)
        response = self.client.get(reverse('api:v1:user-me'))

        self.assertEqual(response.status_code, 200)
        fields = ('id', 'username', 'first_name', 'last_name', 'full_name',
                  'email', 'is_active', 'penalties')
        for field in fields:
            if field == 'penalties':
                self.assertEqual(len(self.user.penalties.valid()),
                                 len(response.data['penalties']))
            else:
                self.assertEqual(getattr(self.user, field),
                                 response.data[field])

    def test_self_unauthed(self):
        response = self.client.get(reverse('api:v1:user-me'))
        self.assertEqual(response.status_code, 401)

    @mock.patch('django.utils.timezone.now',
                return_value=fake_time(2016, 10, 1))
    def test_own_penalties_serializer(self, mock_now):
        source = Event.objects.all().first()
        Penalty.objects.create(created_at=mock_now() - timedelta(days=20),
                               user=self.user,
                               reason='test',
                               weight=1,
                               source_event=source)
        Penalty.objects.create(created_at=mock_now() -
                               timedelta(days=19, hours=23, minutes=59),
                               user=self.user,
                               reason='test',
                               weight=1,
                               source_event=source)
        self.client.force_authenticate(user=self.user)
        response = self.client.get(reverse('api:v1:user-me'))

        self.assertEqual(response.status_code, 200)

        self.assertEqual(len(self.user.penalties.valid()),
                         len(response.data['penalties']))
        self.assertEqual(len(response.data['penalties']), 1)
        self.assertEqual(len(response.data['penalties'][0]), 7)
Exemple #3
0
class PenaltyTestCase(BaseTestCase):
    fixtures = [
        'test_users.yaml', 'test_abakus_groups.yaml', 'test_companies.yaml',
        'test_events.yaml'
    ]

    def setUp(self):
        self.test_user = User.objects.get(pk=1)
        self.source = Event.objects.all().first()

    def test_create_penalty(self):
        penalty = Penalty.objects.create(user=self.test_user,
                                         reason='test',
                                         weight=1,
                                         source_event=self.source)

        self.assertEqual(self.test_user.number_of_penalties(), 1)
        self.assertEqual(self.test_user, penalty.user)
        self.assertEqual('test', penalty.reason)
        self.assertEqual(1, penalty.weight)
        self.assertEqual(self.source, penalty.source_event)
        self.assertEqual(self.source.id, penalty.source_event.id)

    def test_count_weights(self):
        weights = [1, 2]
        for weight in weights:
            Penalty.objects.create(user=self.test_user,
                                   reason='test',
                                   weight=weight,
                                   source_event=self.source)

        self.assertEqual(self.test_user.number_of_penalties(), sum(weights))

    @mock.patch('django.utils.timezone.now',
                return_value=fake_time(2016, 10, 1))
    def test_only_count_active_penalties(self, mock_now):
        Penalty.objects.create(created_at=mock_now() - timedelta(days=20),
                               user=self.test_user,
                               reason='test',
                               weight=1,
                               source_event=self.source)
        Penalty.objects.create(created_at=mock_now() -
                               timedelta(days=19, hours=23, minutes=59),
                               user=self.test_user,
                               reason='test',
                               weight=1,
                               source_event=self.source)
        self.assertEqual(self.test_user.number_of_penalties(), 1)

    @override_settings(PENALTY_IGNORE_WINTER=((12, 10), (1, 10)))
    @mock.patch('django.utils.timezone.now',
                return_value=fake_time(2016, 12, 11))
    def test_frozen_penalties_count_as_active_winter(self, mock_now):
        # This penalty is created slightly less than 20 days from the freeze-point.
        # It should be counted as active.
        Penalty.objects.create(created_at=mock_now() -
                               timedelta(days=20, hours=23, minutes=59),
                               user=self.test_user,
                               reason='active',
                               weight=1,
                               source_event=self.source)

        # This penalty is created exactly 20 days from the freeze-point.
        # It should be counted as inactive.
        Penalty.objects.create(created_at=mock_now() - timedelta(days=21),
                               user=self.test_user,
                               reason='inactive',
                               weight=1,
                               source_event=self.source)

        self.assertEqual(self.test_user.number_of_penalties(), 1)
        self.assertEqual(self.test_user.penalties.valid().first().reason,
                         'active')

    @override_settings(PENALTY_IGNORE_SUMMER=((6, 12), (8, 15)))
    @mock.patch('django.utils.timezone.now',
                return_value=fake_time(2016, 6, 13))
    def test_frozen_penalties_count_as_active_summer(self, mock_now):
        # This penalty is created slightly less than 20 days from the freeze-point.
        # It should be counted as active.
        Penalty.objects.create(created_at=mock_now() -
                               timedelta(days=20, hours=23, minutes=59),
                               user=self.test_user,
                               reason='active',
                               weight=1,
                               source_event=self.source)

        # This penalty is created exactly 20 days from the freeze-point.
        # It should be counted as inactive.
        Penalty.objects.create(created_at=mock_now() - timedelta(days=21),
                               user=self.test_user,
                               reason='inactive',
                               weight=1,
                               source_event=self.source)

        self.assertEqual(self.test_user.number_of_penalties(), 1)
        self.assertEqual(self.test_user.penalties.valid().first().reason,
                         'active')
Exemple #4
0
class PenaltyTestCase(BaseTestCase):
    fixtures = [
        "test_users.yaml",
        "test_abakus_groups.yaml",
        "test_companies.yaml",
        "test_events.yaml",
    ]

    def setUp(self):
        self.test_user = User.objects.get(pk=1)
        self.source = Event.objects.all().first()

    def test_create_penalty(self):
        penalty = Penalty.objects.create(
            user=self.test_user, reason="test", weight=1, source_event=self.source
        )

        self.assertEqual(self.test_user.number_of_penalties(), 1)
        self.assertEqual(self.test_user, penalty.user)
        self.assertEqual("test", penalty.reason)
        self.assertEqual(1, penalty.weight)
        self.assertEqual(self.source, penalty.source_event)
        self.assertEqual(self.source.id, penalty.source_event.id)

    def test_count_weights(self):
        weights = [1, 2]
        for weight in weights:
            Penalty.objects.create(
                user=self.test_user,
                reason="test",
                weight=weight,
                source_event=self.source,
            )

        self.assertEqual(self.test_user.number_of_penalties(), sum(weights))

    @mock.patch("django.utils.timezone.now", return_value=fake_time(2016, 10, 1))
    def test_only_count_active_penalties(self, mock_now):
        Penalty.objects.create(
            created_at=mock_now() - timedelta(days=20),
            user=self.test_user,
            reason="test",
            weight=1,
            source_event=self.source,
        )
        Penalty.objects.create(
            created_at=mock_now() - timedelta(days=19, hours=23, minutes=59),
            user=self.test_user,
            reason="test",
            weight=1,
            source_event=self.source,
        )
        self.assertEqual(self.test_user.number_of_penalties(), 1)

    @override_settings(PENALTY_IGNORE_WINTER=((12, 10), (1, 10)))
    @mock.patch("django.utils.timezone.now", return_value=fake_time(2016, 12, 10))
    def test_frozen_penalties_count_as_active_winter(self, mock_now):
        # This penalty is created slightly less than 20 days from the freeze-point.
        # It should be counted as active.
        Penalty.objects.create(
            created_at=mock_now() - timedelta(days=20, hours=23, minutes=59),
            user=self.test_user,
            reason="active",
            weight=1,
            source_event=self.source,
        )

        # This penalty is created exactly 20 days from the freeze-point.
        # It should be counted as inactive.
        Penalty.objects.create(
            created_at=mock_now() - timedelta(days=21),
            user=self.test_user,
            reason="inactive",
            weight=1,
            source_event=self.source,
        )

        self.assertEqual(self.test_user.number_of_penalties(), 1)
        self.assertEqual(self.test_user.penalties.valid().first().reason, "active")

    @override_settings(PENALTY_IGNORE_SUMMER=((6, 12), (8, 15)))
    @mock.patch("django.utils.timezone.now", return_value=fake_time(2016, 6, 12))
    def test_frozen_penalties_count_as_active_summer(self, mock_now):
        # This penalty is created slightly less than 20 days from the freeze-point.
        # It should be counted as active.
        Penalty.objects.create(
            created_at=mock_now() - timedelta(days=20, hours=23, minutes=59),
            user=self.test_user,
            reason="active",
            weight=1,
            source_event=self.source,
        )

        # This penalty is created exactly 20 days from the freeze-point.
        # It should be counted as inactive.
        Penalty.objects.create(
            created_at=mock_now() - timedelta(days=21),
            user=self.test_user,
            reason="inactive",
            weight=1,
            source_event=self.source,
        )

        self.assertEqual(self.test_user.number_of_penalties(), 1)
        self.assertEqual(self.test_user.penalties.valid().first().reason, "active")

    @override_settings(PENALTY_IGNORE_WINTER=((12, 22), (1, 10)))
    @mock.patch("django.utils.timezone.now", return_value=fake_time(2019, 12, 23))
    def test_penalty_offset_is_calculated_correctly(self, mock_now):
        # This penalty is set to expire the day before the penalty freeze
        # It should not be active
        inactive = Penalty.objects.create(
            created_at=mock_now().replace(day=1),
            user=self.test_user,
            reason="inactive",
            weight=1,
            source_event=self.source,
        )
        self.assertEqual(self.test_user.number_of_penalties(), 0)
        self.assertEqual(
            (inactive.exact_expiration.month, inactive.exact_expiration.day), (12, 21)
        )

        # This penalty is set to expire the same day as the freeze
        active = Penalty.objects.create(
            created_at=mock_now().replace(day=2),
            user=self.test_user,
            reason="active",
            weight=1,
            source_event=self.source,
        )
        self.assertEqual(self.test_user.number_of_penalties(), 1)
        self.assertEqual(
            (active.exact_expiration.month, active.exact_expiration.day), (1, 11)
        )