diff --git a/src/wirecloud/platform/iwidget/utils.py b/src/wirecloud/platform/iwidget/utils.py index f33ab5694..2c143c09d 100644 --- a/src/wirecloud/platform/iwidget/utils.py +++ b/src/wirecloud/platform/iwidget/utils.py @@ -127,9 +127,6 @@ def check_intervals(data): # The screen size intervals should cover the interval [0, +inf) and should not overlap nor have gaps, # each interval is defined by the properties 'moreOrEqual' and 'lessOrEqual' - if not isinstance(data, list) or not all(isinstance(i, dict) and ('moreOrEqual' in i and 'lessOrEqual' in i) for i in data): - raise ValueError('data must be a list of dictionaries with "moreOrEqual" and "lessOrEqual" keys') - data.sort(key=lambda x: x.get('moreOrEqual', float('-inf'))) if data[0].get('moreOrEqual') != 0: @@ -143,9 +140,6 @@ def check_intervals(data): raise ValueError('The last interval must extend to infinity') def update_position(iwidget, key, data): - if not 'layoutConfigurations' in data: - raise ValueError('Missing layoutConfigurations field') - # Check if we have duplicate ids in the layoutConfigurations ids = set() for layoutConfig in data["layoutConfigurations"]: diff --git a/src/wirecloud/platform/tests/rest_api.py b/src/wirecloud/platform/tests/rest_api.py index f32d0e9c8..4feda1dee 100644 --- a/src/wirecloud/platform/tests/rest_api.py +++ b/src/wirecloud/platform/tests/rest_api.py @@ -2403,6 +2403,89 @@ def test_iwidget_entry_post_upgrade_operator(self): response_data = json.loads(response.content.decode('utf-8')) self.assertTrue(isinstance(response_data, dict)) + def test_iwidget_entry_post_invalid_screen_size(self): + url = reverse('wirecloud.iwidget_entry', kwargs={'workspace_id': 2, 'tab_id': 101, 'iwidget_id': 2}) + + # Authenticate + self.client.login(username='user_with_workspaces', password='admin') + + # Make the requests + data = {'layoutConfigurations': [{'id': 0, 'action': 'update', 'moreOrEqual': -3}]} + response = self.client.post(url, json.dumps(data), content_type='application/json; charset=UTF-8', HTTP_ACCEPT='application/json') + self.assertEqual(response.status_code, 422) + + data = {'layoutConfigurations': [{'id': 0, 'action': 'update', 'moreOrEqual': 'notavalidvalue'}]} + response = self.client.post(url, json.dumps(data), content_type='application/json; charset=UTF-8', HTTP_ACCEPT='application/json') + self.assertEqual(response.status_code, 400) + + data = {'layoutConfigurations': [{'id': 0, 'action': 'update', 'moreOrEqual': 1}]} + response = self.client.post(url, json.dumps(data), content_type='application/json; charset=UTF-8', HTTP_ACCEPT='application/json') + self.assertEqual(response.status_code, 422) + + data = {'layoutConfigurations': [{'id': 0, 'action': 'update', 'lessOrEqual': 1}]} + response = self.client.post(url, json.dumps(data), content_type='application/json; charset=UTF-8', HTTP_ACCEPT='application/json') + self.assertEqual(response.status_code, 422) + + data = {'layoutConfigurations': [{'action': 'update'}]} + response = self.client.post(url, json.dumps(data), content_type='application/json; charset=UTF-8', HTTP_ACCEPT='application/json') + self.assertEqual(response.status_code, 422) + + data = {'layoutConfigurations': [{'id': 0, 'lessOrEqual': 1}]} + response = self.client.post(url, json.dumps(data), content_type='application/json; charset=UTF-8', HTTP_ACCEPT='application/json') + self.assertEqual(response.status_code, 422) + + data = {'layoutConfigurations': [{'id': 0, 'action': 'add', 'lessOrEqual': 1}]} + response = self.client.post(url, json.dumps(data), content_type='application/json; charset=UTF-8', HTTP_ACCEPT='application/json') + self.assertEqual(response.status_code, 422) + + data = {'layoutConfigurations': [{'id': 0, 'action': 'update'}, {'id': 0, 'action': 'update'}]} + response = self.client.post(url, json.dumps(data), content_type='application/json; charset=UTF-8', HTTP_ACCEPT='application/json') + self.assertEqual(response.status_code, 422) + + def test_iwidget_entry_post_delete_screen_size(self): + url = reverse('wirecloud.iwidget_entry', kwargs={'workspace_id': 2, 'tab_id': 101, 'iwidget_id': 2}) + data = {'layoutConfigurations': [ + {'id': 0, 'action': 'update', 'moreOrEqual': 0, 'lessOrEqual': 800}, + {'id': 1, 'action': 'update', 'moreOrEqual': 801, 'lessOrEqual': -1} + ]} + + # Authenticate + self.client.login(username='user_with_workspaces', password='admin') + + # Make the requests + response = self.client.post(url, json.dumps(data), content_type='application/json; charset=UTF-8', HTTP_ACCEPT='application/json') + self.assertEqual(response.status_code, 204) + + data = {'layoutConfigurations': [{'id': 0, 'action': 'update', 'lessOrEqual': -1}, {'id': 1, 'action': 'delete'}]} + response = self.client.post(url, json.dumps(data), content_type='application/json; charset=UTF-8', HTTP_ACCEPT='application/json') + self.assertEqual(response.status_code, 204) + + + def test_iwidget_entry_post_invalid_layout(self): + url = reverse('wirecloud.iwidget_entry', kwargs={'workspace_id': 2, 'tab_id': 101, 'iwidget_id': 2}) + + # Authenticate + self.client.login(username='user_with_workspaces', password='admin') + + # Make the requests + data = {'layout': -3} + response = self.client.post(url, json.dumps(data), content_type='application/json; charset=UTF-8', HTTP_ACCEPT='application/json') + self.assertEqual(response.status_code, 422) + + def test_iwidget_entry_post_valid_layout(self): + url = reverse('wirecloud.iwidget_entry', kwargs={'workspace_id': 2, 'tab_id': 101, 'iwidget_id': 2}) + + # Authenticate + self.client.login(username='user_with_workspaces', password='admin') + + # Make the requests + data = {'layout': 0} + response = self.client.post(url, json.dumps(data), content_type='application/json; charset=UTF-8', HTTP_ACCEPT='application/json') + self.assertEqual(response.status_code, 204) + + iwidget = IWidget.objects.get(pk=2) + self.assertEqual(iwidget.layout, 0) + def check_iwidget_entry_post_invalid_position_value(self, field, value, error_code): url = reverse('wirecloud.iwidget_entry', kwargs={'workspace_id': 2, 'tab_id': 101, 'iwidget_id': 2})