Ejemplo n.º 1
0
    def test_impact(self):
        type = ImpactTypeFactory()

        ImpactGoalFactory.create_batch(5, type=type, target=100, realized=50)

        stat = ImpactStatisticFactory.create(impact_type=type)

        self.assertEqual(stat.get_value(), 250.0)
Ejemplo n.º 2
0
    def setUp(self):
        super(ImpactTypeListAPITestCase, self).setUp()
        self.client = JSONAPITestClient()
        self.url = reverse('statistic-list')
        self.user = BlueBottleUserFactory()

        initiative = InitiativeFactory.create()
        event = EventFactory.create(
            initiative=initiative,
            owner=initiative.owner,
            start=timezone.now() - datetime.timedelta(hours=1),
            duration=0.1

        )

        initiative.states.submit(save=True)
        initiative.states.approve(save=True)

        event.refresh_from_db()

        ParticipantFactory.create_batch(5, activity=event)

        self.impact_type = ImpactTypeFactory.create()

        self.impact_goal = ImpactGoalFactory.create(
            type=self.impact_type,
            target=100,
            realized=50
        )

        self.manual = ManualStatisticFactory.create()
        self.impact = ImpactStatisticFactory(impact_type=self.impact_type)
        self.database = DatabaseStatisticFactory(query='people_involved')
Ejemplo n.º 3
0
    def setUp(self):
        super(ImpactGoalDetailsAPITestCase, self).setUp()
        self.client = JSONAPITestClient()
        self.activity = EventFactory.create()
        self.type = ImpactTypeFactory.create()
        self.goal = ImpactGoalFactory(type=self.type, activity=self.activity)
        self.url = reverse('impact-goal-details', args=(self.goal.pk, ))

        self.data = {
            'data': {
                'type': 'activities/impact-goals',
                'id': self.goal.pk,
                'attributes': {
                    'target': 1.5
                },
            }
        }
Ejemplo n.º 4
0
    def test_retrieve_impact(self):
        goals = ImpactGoalFactory.create_batch(2, activity=self.assignment)
        response = self.client.get(self.url, user=self.user)

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(
            len(response.json()['data']['relationships']['goals']['data']), 2)
        included_goals = [
            resource['id'] for resource in response.json()['included']
            if resource['type'] == 'activities/impact-goals'
        ]
        for goal in goals:
            self.assertTrue(str(goal.pk) in included_goals)
Ejemplo n.º 5
0
    def test_impact_failed(self):
        type = ImpactTypeFactory()

        goals = ImpactGoalFactory.create_batch(5,
                                               type=type,
                                               target=100,
                                               realized=50)
        activity = goals[0].activity
        activity.status = 'failed'
        activity.save()

        stat = ImpactStatisticFactory.create(impact_type=type)

        self.assertEqual(stat.get_value(), 200.0)
Ejemplo n.º 6
0
class ImpactGoalDetailsAPITestCase(BluebottleTestCase):
    def setUp(self):
        super(ImpactGoalDetailsAPITestCase, self).setUp()
        self.client = JSONAPITestClient()
        self.activity = EventFactory.create()
        self.type = ImpactTypeFactory.create()
        self.goal = ImpactGoalFactory(type=self.type, activity=self.activity)
        self.url = reverse('impact-goal-details', args=(self.goal.pk, ))

        self.data = {
            'data': {
                'type': 'activities/impact-goals',
                'id': self.goal.pk,
                'attributes': {
                    'target': 1.5
                },
            }
        }

    def test_get(self):
        response = self.client.get(
            self.url,
            user=self.activity.owner
        )
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        data = response.json()['data']

        self.assertEqual(data['type'], 'activities/impact-goals')

        self.assertEqual(
            data['attributes']['target'], self.goal.target
        )
        self.assertEqual(
            data['relationships']['type']['data']['id'],
            str(self.goal.type.pk)
        )
        self.assertEqual(
            data['relationships']['activity']['data']['id'],
            str(self.goal.activity.pk)
        )

    def test_get_non_owner(self):
        response = self.client.get(
            self.url,
            user=BlueBottleUserFactory.create()
        )
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    def test_get_anonymous(self):
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    def test_get_closed_anonymous(self):
        anonymous = Group.objects.get(name='Anonymous')
        anonymous.permissions.remove(
            Permission.objects.get(codename='api_read_event')
        )

        MemberPlatformSettings.objects.update(closed=True)
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

    def test_update(self):
        response = self.client.patch(
            self.url,
            data=json.dumps(self.data),
            user=self.activity.owner
        )
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        data = response.json()['data']

        self.assertEqual(data['type'], 'activities/impact-goals')

        self.assertEqual(
            data['attributes']['target'],
            self.data['data']['attributes']['target']
        )

        self.goal.refresh_from_db()
        self.assertEqual(
            self.goal.target,
            self.data['data']['attributes']['target']
        )

    def test_update_other_user(self):
        response = self.client.patch(
            self.url,
            data=json.dumps(self.data),
            user=BlueBottleUserFactory.create()
        )
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

    def test_update_anonymous(self):
        response = self.client.patch(
            self.url,
            data=json.dumps(self.data)
        )
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

    def test_delete(self):
        response = self.client.delete(
            self.url,
            user=self.activity.owner
        )
        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)

        with self.assertRaises(ImpactGoal.DoesNotExist):
            ImpactGoal.objects.get(pk=self.goal.pk)

    def test_delete_other_user(self):
        response = self.client.delete(
            self.url,
            user=BlueBottleUserFactory.create()
        )
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

    def test_delete_anonymous(self):
        response = self.client.delete(self.url)
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)