Skip to content

Commit

Permalink
More django tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oxixes committed Jul 1, 2024
1 parent 3a18c75 commit 1cb100b
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 6 deletions.
6 changes: 0 additions & 6 deletions src/wirecloud/platform/iwidget/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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"]:
Expand Down
83 changes: 83 additions & 0 deletions src/wirecloud/platform/tests/rest_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand Down

0 comments on commit 1cb100b

Please sign in to comment.