Repository: akvo/akvo-rsr

Issue:
POST /indicator_framework/ errors
Request URL: http://localhost/rest/v1/indicator_framework/?format=json
Request Method: POST
Data:
```
{"type":"2","measure":"1","periods":[],"dimension_names":[],"result":1}
```

Response:

```
{"dimension_names":["This list may not be empty."]}
```

The dimensions (dimension_names) should not be mandatory.


Discussion:

Resolving patch:
diff --git a/akvo/rest/serializers/indicator.py b/akvo/rest/serializers/indicator.py
--- a/akvo/rest/serializers/indicator.py
+++ b/akvo/rest/serializers/indicator.py
@@ -6,7 +6,7 @@
 
 from akvo.rest.serializers.indicator_period import IndicatorPeriodFrameworkSerializer
 from akvo.rest.serializers.rsr_serializer import BaseRSRSerializer
-from akvo.rsr.models import Indicator
+from akvo.rsr.models import Indicator, IndicatorDimensionName
 
 from rest_framework import serializers
 
@@ -27,8 +27,10 @@ class Meta:
 class IndicatorFrameworkSerializer(BaseRSRSerializer):
 
     periods = IndicatorPeriodFrameworkSerializer(many=True, required=False)
-    parent_indicator = serializers.ReadOnlyField()
+    parent_indicator = serializers.ReadOnlyField(source='parent_indicator_id')
     children_aggregate_percentage = serializers.ReadOnlyField()
+    dimension_names = serializers.PrimaryKeyRelatedField(
+        many=True, required=False, queryset=IndicatorDimensionName.objects.all())
 
     class Meta:
         model = Indicator
diff --git a/akvo/rest/serializers/indicator_period.py b/akvo/rest/serializers/indicator_period.py
--- a/akvo/rest/serializers/indicator_period.py
+++ b/akvo/rest/serializers/indicator_period.py
@@ -27,7 +27,7 @@ class Meta:
 class IndicatorPeriodFrameworkSerializer(BaseRSRSerializer):
 
     data = serializers.SerializerMethodField('get_updates')
-    parent_period = serializers.ReadOnlyField()
+    parent_period = serializers.ReadOnlyField(source='parent_period_id')
     percent_accomplishment = serializers.ReadOnlyField()
 
     class Meta:


Test patch:
diff --git a/akvo/rsr/tests/base.py b/akvo/rsr/tests/base.py
--- a/akvo/rsr/tests/base.py
+++ b/akvo/rsr/tests/base.py
@@ -79,8 +79,8 @@ def make_parent(parent, project):
         )
 
     @staticmethod
-    def make_partner(project, org):
-        Partnership.objects.create(project=project, organisation=org)
+    def make_partner(project, org, role=None):
+        Partnership.objects.create(project=project, organisation=org, iati_organisation_role=role)
 
     @staticmethod
     def make_org_admin(user, org):
diff --git a/akvo/rsr/tests/rest/test_indicator.py b/akvo/rsr/tests/rest/test_indicator.py
--- a/akvo/rsr/tests/rest/test_indicator.py
+++ b/akvo/rsr/tests/rest/test_indicator.py
@@ -9,67 +9,25 @@
 
 import json
 
-from django.conf import settings
-from django.contrib.auth.models import Group
-from django.test import TestCase, Client
+from akvo.rsr.models import Partnership, Result, Indicator, IndicatorPeriod
+from akvo.rsr.tests.base import BaseTestCase
 
-from akvo.rsr.models import Project, Organisation, Partnership, User, Employment, Result, Indicator, IndicatorPeriod
-from akvo.utils import check_auth_groups
 
-
-class RestIndicatorTestCase(TestCase):
+class RestIndicatorTestCase(BaseTestCase):
     """Tests the indicator REST endpoints."""
 
     def setUp(self):
         """ Setup a minimal DB for the tests. """
 
-        self.project = Project.objects.create(
-            title="REST test project",
-        )
-
-        # Create groups
-        check_auth_groups(settings.REQUIRED_AUTH_GROUPS)
-
-        # Create organisation
-        self.reporting_org = Organisation.objects.create(
-            id=1337,
-            name="Test REST reporting",
-            long_name="Test REST reporting org",
-            new_organisation_type=22
-        )
-
-        # Create partnership
-        self.partnership = Partnership.objects.create(
-            project=self.project,
-            organisation=self.reporting_org,
-            iati_organisation_role=Partnership.IATI_REPORTING_ORGANISATION,
-        )
-
-        # Create active user
-        self.user = User.objects.create_user(
-            username="Normal user REST",
-            email="user.rest@test.akvo.org",
-            password="password",
-        )
-        self.user.is_active = True
-        self.user.is_admin = True
-        self.user.is_superuser = True
-        self.user.save()
-
-        # Create employment
-        self.employment = Employment.objects.create(
-            user=self.user,
-            organisation=self.reporting_org,
-            is_approved=True,
-        )
+        super(RestIndicatorTestCase, self).setUp()
 
-        self.c = Client(HTTP_HOST=settings.RSR_DOMAIN)
+        self.project = self.create_project("REST test project")
+        self.reporting_org = self.create_organisation("Test REST reporting")
+        self.make_partner(self.project, self.reporting_org, Partnership.IATI_REPORTING_ORGANISATION)
 
-    def tearDown(self):
-        Project.objects.all().delete()
-        User.objects.all().delete()
-        Organisation.objects.all().delete()
-        Group.objects.all().delete()
+        self.user = self.create_user(
+            "user.rest@test.akvo.org", "password", is_admin=True, is_superuser=True)
+        self.employment = self.make_employment(self.user, self.reporting_org, 'Users')
 
     def test_rest_indicator_pagination(self):
         """Test that paginating the indicator results works."""
@@ -94,6 +52,27 @@ def test_rest_indicator_pagination(self):
             periods = filter(lambda x: x['indicator'] == indicator_id, indicator_periods)
             self.assertEqual(len(periods), n_periods)
 
+    def test_indicator_framework(self):
+        # Given
+        result = Result.objects.create(project=self.project)
+        indicator = Indicator.objects.create(result=result)
+        IndicatorPeriod.objects.create(indicator=indicator)
+        child_project = self.create_project('Child Project')
+        self.make_parent(self.project, child_project)
+        child_project.import_results()
+        self.c.login(username=self.user.username, password="password")
+        url = '/rest/v1/indicator_framework/?format=json'
+
+        # When
+        response = self.c.get(url)
+
+        # Then
+        self.assertEqual(response.data['count'], 2)
+        indicators = set(
+            [indicator.id] + list(indicator.child_indicators.values_list('id', flat=True))
+        )
+        self.assertEqual(indicators, {indicator['id'] for indicator in response.data['results']})
+
     def get_indicator_periods(self, project_id):
         periods = []
         next_url = '/rest/v1/indicator_period/?format=json&indicator__result__project={}&limit=50'.format(project_id)