Skip to content

Commit

Permalink
Merge branch 'master' into stabilize-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
OskarPersson committed Feb 6, 2020
2 parents a1d02b2 + 35e2302 commit 24431aa
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 27 deletions.
2 changes: 1 addition & 1 deletion ESSArch_Core/profiles/models.py
Expand Up @@ -356,7 +356,7 @@ class Meta:
ordering = ['version']

def clean(self):
data = getattr(self.data, 'data', {})
data = self.data or {}
data = fill_specification_data(data.copy(), ip=self.information_package, sa=self.submission_agreement)
validate_template(self.submission_agreement.template, data)

Expand Down
27 changes: 19 additions & 8 deletions ESSArch_Core/profiles/tests/test_views.py
@@ -1,8 +1,5 @@
from unittest import mock

from django.contrib.auth import get_user_model
from django.contrib.auth.models import Permission
from django.core.exceptions import ValidationError
from django.test import TestCase
from django.urls import reverse
from rest_framework import status
Expand Down Expand Up @@ -162,7 +159,19 @@ def test_without_permission(self):
def test_with_permission(self):
self.user.user_permissions.add(Permission.objects.get(codename='lock_sa'))

sa = SubmissionAgreement.objects.create(policy=self.policy)
sa = SubmissionAgreement.objects.create(
policy=self.policy,
template=[
{
"key": "foo",
"type": "input",
"templateOptions": {
"type": "text",
"required": True,
},
}
],
)
ip = InformationPackage.objects.create(submission_agreement=sa)
sa_ip_data = SubmissionAgreementIPData.objects.create(
user=self.user,
Expand All @@ -179,11 +188,13 @@ def test_with_permission(self):

ip.submission_agreement_locked = False
ip.save()

sa_ip_data.data = {'bar': 'foo'}
sa_ip_data.save()

with self.subTest('invalid data'):
with mock.patch('ESSArch_Core.profiles.views.SubmissionAgreementIPData.clean',
side_effect=ValidationError('invalid data')):
res = self.client.post(self.get_url(sa), data={'ip': str(ip.pk)})
self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST)
res = self.client.post(self.get_url(sa), data={'ip': str(ip.pk)})
self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST)

def test_as_responsible(self):
sa = SubmissionAgreement.objects.create(policy=self.policy)
Expand Down
24 changes: 6 additions & 18 deletions ESSArch_Core/profiles/utils.py
Expand Up @@ -32,16 +32,6 @@ def __init__(self, *args, **kw):
self._raw_dict = dict(*args, **kw)

def __getitem__(self, key):
if isinstance(key, str):
try:
if key.startswith('PARAMETER_') or key.startswith('_PARAMETER_'):
return Parameter.objects.get(entity__iexact=key.split('PARAMETER_', 1)[1]).value

if key.startswith('PATH_') or key.startswith('_PATH_'):
return Path.objects.get(entity__iexact=key.split('PATH_', 1)[1]).value
except (Parameter.DoesNotExist, Path.DoesNotExist):
return None

val = self._raw_dict.__getitem__(key)
if isinstance(val, tuple) and callable(val[0]):
func, *args = val
Expand All @@ -65,14 +55,6 @@ def to_dict(self):
else:
d[k] = v

for p in Parameter.objects.iterator():
d['_PARAMETER_%s' % p.entity.upper()] = p.value
d['PARAMETER_%s' % p.entity.upper()] = p.value

for p in Path.objects.iterator():
d['_PATH_%s' % p.entity.upper()] = p.value
d['PATH_%s' % p.entity.upper()] = p.value

return d

def copy(self):
Expand Down Expand Up @@ -224,4 +206,10 @@ def fill_specification_data(data=None, sa=None, ip=None, ignore=None):
for (profile_type, key) in profile_ids:
data[key] = (_get_profile_id_by_type, profile_type, ip)

for p in Parameter.objects.iterator():
data['_PARAMETER_%s' % p.entity.upper()] = p.value

for p in Path.objects.iterator():
data['_PATH_%s' % p.entity.upper()] = p.value

return data

0 comments on commit 24431aa

Please sign in to comment.