I am trying to test piece of code which depends on cookie. code in question is like this:
try:
json_string = urllib.unquote(request.COOKIES['add_global_user']).decode('utf-8')
initial = json.loads(json_string)
if 'company_name' in initial.keys():
company_type = get_company_type(initial['company_name'])
download_notifications = initial.get('download_notifs', False)
form = AddUserForm(request=request, initial=initial)
form.fields['needs_lms_access'].initial = True
response = render(request, "accounts/users/adduserglobal.html", locals())
response.delete_cookie("add_global_user", path="/accounts/user/add")
return response
except KeyError:
_logger.debug("KeyError, no add_global_user cookie found, displaying form w/o data")
form = AddUserForm(request=request, initial={'first_name':'keyerror'})
except ValueError:
_logger.debug("ValueError, No valid JSON could be decoded, displaying form w/o data; %s",
request.COOKIES['add_global_user'])
form = AddUserForm(request=request, initial={'first_name':'valueerror'})
except Exception as e:
_logger.debug("Exception occurred while retrieving form data from cookie, %s", str(e))
form = AddUserForm(request=request, initial={'first_name':str(e)})
I am using django-webtest to write test cases, my attempt to test is:
class TestAddUser(WebTest):
def test_bound_form_with_cookie(self):
company = G(Company)
user = G(User, email='superuser@tarams.com', is_superuser=True)
cookie_dict = {
"email": "email",
"first_name": "3rd",
"last_name": "2018",
}
self.app.set_cookie("add_global_user", json.dumps(cookie_dict))
response = self.app.get('/accounts/user/add/', user=user)
assert response.context['form'].initial['email'] == 'email'
running it django-webtest calls set_cookie method of DjangoTestApp whose source code is:
def set_cookie(self, *args, **kwargs):
self.extra_environ = self._update_environ(self.extra_environ)
return super(DjangoTestApp, self).set_cookie(*args, **kwargs)
and it throws error:
AttributeError: 'super' object has no attribute 'set_cookie'
I have given considerable time but could not find anything or workaround. Help appreciated. Thanks
Aucun commentaire:
Enregistrer un commentaire