Posting data using Django unit testing not saving to backend - django

I am creating a django app which i which implement most of my logic before the design aspect of the application.
I created a testcase for user registration but anytime I post to the registration url, it received my posted data but never populates it to database.
class AuthTest(TestCase):
def setUp(self):
self.client = Client()
def test_reg_index(self):
response = self.client.get(reverse_lazy('register'))
return self.assertEqual(response.status_code, 200)
def test_registration(self):
data = {
'last_name': 'test_user',
'first_name': 'test_user',
'email': 'fashtop3#gmail.com',
'phone': '08035112897',
}
response = self.client.post(reverse_lazy('register'), data)
self.assertEqual(response.status_code, 302)
def test_login(self):
# self.client.login(username='foo', password='bar') # request.user is now user foo
data = {
'email': 'test_user#example.com',
'password': 'pass123',
}
response = self.client.post(reverse_lazy('login'), data)
self.assertEqual(response.status_code, 200)
class RegisterView(FormView):
"""
this handles users registration
"""
form_class = RegForm
template_name = "club/register.html"
success_url = reverse_lazy("register")
# def get(self, request, *args, **kwargs):
# return super(RegisterView, self).get(request, *args, **kwargs)
def form_invalid(self, form):
print(form.errors.as_data)
return super().form_invalid(form)
def form_valid(self, form):
"""
Process valid registration form
:param form:
:return: redirects to success_url
"""
random_password = ''.join(secrets.choice(string.ascii_uppercase + string.digits) for _ in range(6))
try:
with transaction.atomic():
"""Allow database transactions"""
data = form.cleaned_data
user = User.objects.create_user(
email=data['email'],
password=random_password,
remember_token=hashlib.sha1(str(datetime.now()).encode('utf-8')).hexdigest()
)
user.profile.last_name = data['last_name']
user.profile.first_name = data['first_name']
user.profile.phone = data['phone']
user.save()
send_notification.delay(user.pk, random_password)
print('Reg success')
messages.success(self.request, "Regular successfully registered "
"please check your mail for verification",
extra_tags="alert-success")
except IntegrityError as e:
print(e)
messages.error(self.request, "Registration Failed: Email address already exist", extra_tags="alert-danger")
return super(RegisterView, self).form_invalid(form)
except Exception as e:
print(str(e))
messages.error(self.request, "Registration Error, contact site administrator: ", extra_tags="alert-danger")
return super(RegisterView, self).form_invalid(form)
return super(RegisterView, self).form_valid(form)

The TestCase class wraps each test in a transaction, which is rolled back after the transaction (see the relevant documentation).
Even if test_registration successfully registers a user, it won't be available in test_login.
I would suggest creating a user for testing in a setUpClass method.

After like two days I got a solution to my Qts..
In order to retain your data after a success unit test you have to subclass a TestCase from unittest otherwise to perform auto rollback then import TestCase from django.test.

In order to do this, you need to use import unittest instead of from django.test import TestCase
And in your unit test class extends TestCase with unittest.TestCase
see the example below
import unittest
class MyTest(unittest.TestCase):
.......

Related

Testcase returning 401 even with force_autenthicate()

I'm trying to test a view of my project with the following TestCase:
def test_jump_story(self):
c = APIClient()
user = User.objects.get(username='test1')
c.login(username=user.username, password='123')
room_id = PokerRoom.objects.get(name='planning').id
room_index = PokerRoom.objects.get(name='planning').index
request = c.post(reverse('jumpstory', kwargs={'pk': room_id, 'index': room_index}))
c.force_authenticate(user=user)
self.assertEqual(200,request.status_code)
but it returns this <Response status_code=401, "application/json"> even using force_authenticate.
The view that i'm testing:
class jumpStory(APIView):
permission_classes = [IsAuthenticated]
def post(self, request, pk, index):
data= self.request.data
index = self.kwargs['index']
pk = self.kwargs['pk']
if PokerRoom.objects.filter(id=pk).exists():
body = {'index':index}
message_socket("JUMP_STORY", pk, body)
return Response({'success':"JUMP_STORY"}, status=200)
else:
return Response({'error':'message not sended'}, status=400)
What is wrong with my test?
Use APITestCase instead of django TestCase. from rest_framework.test import APITestCase.
from rest_framework.test import APITestCase
class MyTests(APITestCase)
def setUp(self):
user = User.objects.create(username=john)
user.set_password("1234")
user.save()
self.client.force_authenticate(user=user)
def test_jump_story(self):
# do your test

'WSGIRequest' object has no attribute 'template' (Attribute Error while rendering a class based view in django)

What my view does, is takes an even from id, and returns an html page containing a table of all the details.
It is a simple enough view:
class Edetails(View, SuperuserRequiredMixin):
template = 'events/details.html'
def get(request, self, pk):
event = Event.objects.get(id=pk)
count = event.participants.count()
participants = event.participants.all()
ctx = {
'count': count,
'participants': participants,
'pk': pk
}
return render(request, self.template, ctx)
This is the SuperUserRequiredMixin:
class SuperuserRequiredMixin(AccessMixin):
"""
Mixin allows you to require a user with `is_superuser` set to True.
"""
def dispatch(self, request, *args, **kwargs):
if not request.user.is_superuser:
return self.handle_no_permission(request)
return super(SuperuserRequiredMixin, self).dispatch(
request, *args, **kwargs)
And the Access Mixin:
class AccessMixin(object):
"""
'Abstract' mixin that gives access mixins the same customizable
functionality.
"""
login_url = None
raise_exception = False
redirect_field_name = REDIRECT_FIELD_NAME # Set by django.contrib.auth
redirect_unauthenticated_users = False
def get_login_url(self):
"""
Override this method to customize the login_url.
"""
login_url = self.login_url or settings.LOGIN_URL
if not login_url:
raise ImproperlyConfigured(
'Define {0}.login_url or settings.LOGIN_URL or override '
'{0}.get_login_url().'.format(self.__class__.__name__))
return force_string(login_url)
def get_redirect_field_name(self):
"""
Override this method to customize the redirect_field_name.
"""
if self.redirect_field_name is None:
raise ImproperlyConfigured(
'{0} is missing the '
'redirect_field_name. Define {0}.redirect_field_name or '
'override {0}.get_redirect_field_name().'.format(
self.__class__.__name__))
return self.redirect_field_name
def handle_no_permission(self, request):
if self.raise_exception:
if (self.redirect_unauthenticated_users
and not request.user.is_authenticated):
return self.no_permissions_fail(request)
else:
if (inspect.isclass(self.raise_exception)
and issubclass(self.raise_exception, Exception)):
raise self.raise_exception
if callable(self.raise_exception):
ret = self.raise_exception(request)
if isinstance(ret, (HttpResponse, StreamingHttpResponse)):
return ret
raise PermissionDenied
return self.no_permissions_fail(request)
def no_permissions_fail(self, request=None):
"""
Called when the user has no permissions and no exception was raised.
This should only return a valid HTTP response.
By default we redirect to login.
"""
return redirect_to_login(request.get_full_path(),
self.get_login_url(),
self.get_redirect_field_name())
However, when i click on the link to send a get request to the view:
View Participants
I get this error message:
AttributeError at /Edetails2
'WSGIRequest' object has no attribute 'template'
I don't understand what this means. How can i fix this?

mail chimp subscription not working with django

Hi everyone I integrate my Django Web app with mail chimp . in my admin panel when I open marketing preference it give me error . and it do not subscribe my users when I click on subscribe my user remain unsubscribe when i hit save .the error I got is
{'type': 'https://mailchimp.com/developer/marketing/docs/errors/', 'title': 'Invalid Resource', 'status': 400, 'detail': "The resource submitted could not be validated. For field-specific details, see the 'errors' array.", 'instance': '2b647b4f-6e58-439f-8c91-31a3223600a9', 'errors': [{'field': 'email_address', 'message': 'This value should not be blank.'}]}
my models.py file is:
class MarketingPreference(models.Model):
user =models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
subscribed =models.BooleanField(default=True)
mailchimp_subscribed = models.NullBooleanField(blank=True)
mailchimp_msg =models.TextField(null=True , blank=True)
timestamp =models.DateTimeField(auto_now_add=True)
updated =models.DateTimeField(auto_now=True)
def __str__(self):
return self.user.email
def marketing_pref_create_reciever(sender, instance, created, *args, **kwargs):
if created:
status_code, response_data = Mailchimp().subscribe(instance.user.email)
print(status_code, response_data)
post_save.connect(marketing_pref_create_reciever, sender=MarketingPreference)
def marketing_pref_update_reciever(sender, instance, *args, **kwargs):
if instance.subscribed != instance.mailchimp_subscribed:
if instance.subscribed:
#subscribing user
status_code, response_data = Mailchimp().subscribe(instance.user.email)
else:
#unsubscribing user
status_code, response_data = Mailchimp().unsubscribe(instance.user.email)
if response_data['status'] =='subscribed':
instance.subscribed = True
instance.mailchimp_subscribed = True
instance.mailchimp_msg = response_data
else:
instance.subscribed = False
instance.mailchimp_subscribed = False
instance.mailchimp_msg = response_data
pre_save.connect(marketing_pref_update_reciever, sender=MarketingPreference)
def make_marketing_pref_reciever(sender, instance, created, *args, **kwargs):
if created:
MarketingPreference.objects.get_or_create(user=instance)
post_save.connect(make_marketing_pref_reciever , sender=settings.AUTH_USER_MODEL)
my utils.py is:
MAILCHIMP_API_KEY = getattr(settings, "MAILCHIMP_API_KEY" , None)
MAILCHIMP_DATA_CENTER = getattr(settings, "MAILCHIMP_DATA_CENTER" , None)
MAILCHIMP_EMAIL_LIST_ID = getattr(settings, "MAILCHIMP_EMAIL_LIST_ID" , None)
def check_email(email):
if not re.match(r".+#.+\..+",email):
raise ValueError("String passed is not a valid email address")
return email
def get_subscriber_hash(member_email):
#check email
check_email(member_email)
member_email = member_email.lower().encode()
m = hashlib.md5(member_email)
return m.hexdigest()
class Mailchimp(object):
def __init__(self):
super(Mailchimp, self).__init__()
self.key = MAILCHIMP_API_KEY
self.api_url = "https://{dc}.api.mailchimp.com/3.0/".format(dc=MAILCHIMP_DATA_CENTER)
self.list_id = MAILCHIMP_EMAIL_LIST_ID
self.list_endpoint = '{api_url}/lists/{list_id}'.format(api_url=self.api_url, list_id=self.list_id)
def get_members_endpoint(self):
return self.list_endpoint + "/members"
def change_subscription_status(self, email, status='unsubscribed'):
hashed_email = get_subscriber_hash(email)
endpoint = self.get_members_endpoint() +"/" + hashed_email
data = {
"status":self.check_valid_status(status)
}
r = requests.put(endpoint, auth=("",self.key), data=json.dumps(data))
return r.status_code, r.json()
def check_subscription_status(self,email):
hashed_email = get_subscriber_hash(email)
endpoint = self.get_members_endpoint() +"/" + hashed_email
r = requests.get(endpoint, auth=("", self.key))
return r.status_code, r.json()
def check_valid_status(self, status):
choices = ['subscribed' , 'unsubscribed', 'cleaned' , 'pending']
if status not in choices:
raise ValueError("not a valid choice for email status")
return status
def add_email(self,email):
status = "subscribed"
self.check_valid_status(status)
data = {
"email_address":email,
"status": status
}
endpoint = self.get_members_endpoint()
r = requests.post(endpoint, auth=("",self.key), data=json.dumps(data))
return self.change_subscription_status(email, status='subscribed')
def unsubscribe(self, email):
return self.change_subscription_status(email, status='unsubscribed')
def subscribe(self, email):
return self.change_subscription_status(email, status='subscribed')
def pending(self, email):
return self.change_subscription_status(email, status='pending')
mixins.py is:
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt
class CsrfExemptMixin(object):
#method_decorator(csrf_exempt)
def dispatch(self, request,*args, **kwargs):
return super(CsrfExemptMixin, self).dispatch(request,*args, **kwargs)
and my views.py is:
from .mixins import CsrfExemptMixin
from .models import MarketingPreference
from . utils import Mailchimp
MAILCHIMP_EMAIL_LIST_ID = getattr(settings, "MAILCHIMP_EMAIL_LIST_ID" , None)
# Create your views here.
class MarketingPreferenceUpdateView(SuccessMessageMixin, UpdateView):
form_class = MarketingPreferenceForm
template_name = 'base/forms.html'
success_url = '/settings/email/' #this is a builtin method and by default it will go to marketig preference
success_message = 'Your email preferences have been updated. Thank you'
def dispatch(self, *args, **kwargs): #when user came with incognito email will not show to him it will redirect him back to login page
user = self.request.user
if not user.is_authenticated:
return redirect("/login/?next=/settings/email/") #HttpResponse("not allowed", status=400)
return super(MarketingPreferenceUpdateView, self).dispatch(*args,**kwargs)#(request, *args...)
def get_context_data(self, *args, **kwargs):
context = super(MarketingPreferenceUpdateView, self).get_context_data(*args,**kwargs)
context['title'] = 'Update Email Preference'
return context
def get_object(self):
user = self.request.user
obj , created = MarketingPreference.objects.get_or_create(user=user)
return obj
class MailchimpWebhookView(CsrfExemptMixin,View): #it will not work because our web application is not deployes yet and webhook mailchimp do not work with local host
#def get(self, *args, **kwargs):
# return HttpResponse('thank you', status=200)
def post(self, request, *args, **kwargs):
data = request.POST
list_id = data.get('data[list_id]')
if str(list_id) == str(MAILCHIMP_EMAIL_LIST_ID): # I CHECK THAT DATA DATA IS THE RIGHT LIST
hook_type = data.get("type")
email = data.get('data[email]')
response_status, response = Mailchimp().check_subscription_status(email)
sub_status = response['status']
is_subbed = None
mailchimp_subbed = None
if sub_status == "subscribed":
is_subbed, mailchimp_subbed = (True, True)
elif sub_status == "unsubscribed":
is_subbed, mailchimp_subbed = (False, False)
if is_subbed is not None and mailchimp_subbed is not None:
qs = MarketingPreference.objects.filter(user__email__iexact=email)
if qs.exists():
qs.update(subscribed=is_subbed, mailchimp_subscribed=mailchimp_subbed, mailchimp_msg=str(data))
return HttpResponse('thank you', status=200)
Here you have already assigned request.POST to data which is now a dictonary, so to get a value from dictonary you should use the field name of the form widget, as data == request.POST now.
Problem is you are getting wrong key.So your email will always be empty
list_id = data.get('data[list_id]')
email = data.get('data[email]')#You are getting wrong key
It should be like this
list_id = data.get('list_id')
email = data.get('email')

Why is my Django test failing when the view actually works?

I have a class based view that works as designed in the browser. I'm trying to write unit tests for the view and they keep failing. I'm wondering why. The view (the UserPassesTest is whether the user is a superuser or not):
class EditUserView(LoginRequiredMixin, UserPassesTestMixin, TemplateView):
"""handles get and post for adding a new AEUser"""
template_name = 'editUser.html'
title = 'Edit User'
def get(self, request, *args, **kwargs):
"""handles the GET"""
post_url = reverse('edit_user', args=[kwargs['user_id']])
usr = get_object_or_404(AEUser, pk=kwargs['user_id'])
form = EditUserForm(initial={'is_active':usr.is_active, 'is_superuser':usr.is_superuser}, \
user=usr, request=request)
return render(request, self.template_name, \
{'title_text':self.title, 'post_url':post_url, 'form':form})
The Test Case:
class TestEditUser(TestCase):
"""test the AddUser view"""
#classmethod
def setUpTestData(cls):
cls.user = AEUser.objects.create_user(username='shawn', email='shawn#gmail.com', password='test')
cls.user.is_superuser = True
cls.user.save()
def setUp(self):
self.client = Client()
def test_get(self):
"""tests the GET"""
self.client.login(username=self.user.username, password=self.user.password)
get_url = reverse('edit_user', args=[self.user.id])
response = self.client.get(get_url, follow=True)
self.assertEqual(self.user.is_superuser, True)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'editUser.html')
I have 3 asserts in the test case. If I comment out the last two, and only assert that the user is a superuser, the test passes. For whatever reason, though, on the other two asserts, I get failures. The error I receive is:
AssertionError: False is not true : Template 'editUser.html' was not a template used to render the response. Actual template(s) used: 404.html, base.html, which leads me to believe the get_object_or_404 call is what's triggering the failure. Where am I going wrong with this test case? Thanks!
Test should be:
class TestEditUser(TestCase):
"""test the AddUser view"""
#classmethod
def setUpTestData(cls):
cls.user = AEUser.objects.create_user(username='shawn', email='shawn#gmail.com', password='test')
cls.user.is_superuser = True
cls.user.save()
def setUp(self):
self.client = Client()
def test_get(self):
"""tests the GET"""
self.client.login(username=self.user.username, password='test')
get_url = reverse('edit_user', args=[self.user.id])
response = self.client.get(get_url, follow=True)
self.assertEqual(self.user.is_superuser, True)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'editUser.html')

Writing a test in Django with request.user is not passing when it should

Here's what I'm trying to do.
Use a mixin to validate ownership of an object.
Test the detail url to make sure that request.user == obj.owner
I expect the detail test to pass with a 200 assertion. But it's giving me a 302. But when I do a print from the mixing the request.user and owner are the same.
Here's my mixin:
from django.contrib.auth.mixins import UserPassesTestMixin
class IsOwnerMixin(UserPassesTestMixin):
"""
a custom mixin that checks to see if the user is the owner of the object
"""
def test_func(self):
# get the object
obj = self.get_object()
# if the obj.user == the logged in user they can see it otherwise boo!
if self.request.user == obj.owner:
return True
else:
return False
Here's my View:
class AwesomeDetail(LoginRequiredMixin, IsOwnerMixin, DetailView):
"""
An awesome detail
"""
model = models.Awesome
template_name = "awesomeness/detail.html"
Here's my Test:
from django.test import TestCase, RequestFactory
from django.test.client import Client
from django.contrib.auth.models import AnonymousUser, User, Group
from project.awesomness import views, models
class UrlsViewsTest(TestCase):
def setUp(self):
self.factory = RequestFactory()
self.user = User.objects.create_user(id='1', username='name', email='email#email.com', password='top_secret')
self.awesome = models.Awesome.objects.create(id='2', owner=self.user)
self.not_owner = User.objects.create_user(id='3', username='trouble', email='otheremail#otheremail.com', password='top_secret')
def test_awesome_detail(self):
"""
Test the awesome detail URL
"""
request = self.factory.get('/awesome/2/')
request.user = self.user
response = views.AwesomeDetail.as_view()(request, pk=2)
self.assertEqual(response.status_code, 200)
def test_awesome_not_owner(self):
"""
Test the awesome detail with a user that is not the owner
"""
request = self.factory.get('/awesome/2/')
request.user = self.not_owner
response = views.AwesomeDetail.as_view()(request, pk=2)
self.assertEqual(response.status_code, 302)
def test_awesome_detail_anonymous_user(self):
"""
Test the awesome detail with a user that is anonymous
"""
request = self.factory.get('/awesome/2/')
request.user = AnonymousUser()
response = views.AwesomeDetail.as_view()(request, pk=2)
self.assertEqual(response.status_code, 302)
And finally, here's the result:
Creating test database for alias 'default'...
F..
======================================================================
FAIL: test_awesome_detail (project.awesomeness.tests.UrlsViewsTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/dave/sandbox/project/project/awesomeness/tests.py", line 25, in test_awesome_detail
self.assertEqual(response.status_code, 200)
AssertionError: 302 != 200
----------------------------------------------------------------------
Ran 3 tests in 0.202s
FAILED (failures=1)
Destroying test database for alias 'default'...