diff --git a/democracy/tests/test_hearing.py b/democracy/tests/test_hearing.py index 0e5fcc3f..e8b0a2e1 100644 --- a/democracy/tests/test_hearing.py +++ b/democracy/tests/test_hearing.py @@ -1206,6 +1206,25 @@ def test_PUT_hearing_success(valid_hearing_json, john_smith_api_client): assert updated_data['created_at'] == created_at assert_hearing_equals(data, updated_data, john_smith_api_client.user, create=False) +# Test that updating hearing with project returns a response with phases' is_active value +@pytest.mark.django_db +def test_PUT_hearing_with_project_phase_is_active(valid_hearing_json_with_project, john_smith_api_client): + response = john_smith_api_client.post(endpoint, data=valid_hearing_json_with_project, format='json') + data = get_data_from_response(response, status_code=201) + _update_hearing_data(data) + + # add is_active which is not included in POST response + phases = valid_hearing_json_with_project['project']['phases'] + for index, phase in enumerate(phases): + data['project']['phases'][index]['is_active'] = phase['is_active'] + + response = john_smith_api_client.put('%s%s/' % (endpoint, data['id']), data=data, format='json') + updated_data = get_data_from_response(response, status_code=200) + + for index, phase in enumerate(phases): + assert updated_data['project']['phases'][index]['is_active'] == phase['is_active'] + + assert_hearing_equals(data, updated_data, john_smith_api_client.user, create=False) # Test that a user cannot PUT a hearing without the translation @pytest.mark.django_db @@ -1453,6 +1472,27 @@ def test_PATCH_hearing(valid_hearing_json, john_smith_api_client): data = get_data_from_response(response, status_code=200) assert data['closed'] == True +# Test that updating hearing with project returns a response with phases' is_active value +@pytest.mark.django_db +def test_PATCH_hearing_with_project_phase_is_active(valid_hearing_json_with_project, john_smith_api_client): + valid_hearing_json_with_project['close_at'] = datetime.datetime.now() + datetime.timedelta(days=1) + response = john_smith_api_client.post(endpoint, data=valid_hearing_json_with_project, format='json') + data = get_data_from_response(response, status_code=201) + + # add is_active which is not included in POST response + phases = valid_hearing_json_with_project['project']['phases'] + for index, phase in enumerate(phases): + data['project']['phases'][index]['is_active'] = phase['is_active'] + + assert data['closed'] == False + before = datetime.datetime.now() - datetime.timedelta(days=1) + response = john_smith_api_client.patch('%s%s/' % (endpoint, data['id']), data={'close_at': before}, format='json') + data = get_data_from_response(response, status_code=200) + + assert data['closed'] == True + for index, phase in enumerate(phases): + assert data['project']['phases'][index]['is_active'] == phase['is_active'] + # Test that a user cannot PATCH a hearing without the translation @pytest.mark.django_db diff --git a/democracy/views/project.py b/democracy/views/project.py index ebca0094..96d58f33 100644 --- a/democracy/views/project.py +++ b/democracy/views/project.py @@ -37,6 +37,11 @@ def to_representation(self, instance): # do not return ordering explicitly self.fields.pop('ordering') data = super().to_representation(instance) + + # include is_active when updating a hearing with a project + if self.context['request'].method in ['PUT', 'PATCH']: + data['is_active'] = self.context['view'].get_object().project_phase_id == instance.pk + if 'hearing' in self.context: data['is_active'] = self.context['hearing'].project_phase_id == instance.pk return data