Added Login View Test. Added Signup View Test. Added Reset Password View Test. Added Password Reset Confirm View Test. Added Membership Payment View Test for Post request. Added Membership Payment View test for urls, Membership Payment View Test tests for urls. Added odd cases to tests.

This commit is contained in:
Levi 2016-10-04 23:26:02 -05:00
parent 1a8b5e69a0
commit 30ff53c629
2 changed files with 244 additions and 77 deletions

View file

@ -97,70 +97,3 @@ class Command(BaseCommand):
print(e) print(e)
print("-------------------------") print("-------------------------")
continue continue
# for donator_status in donators:
# donator = donator_status.user.stripecustomer
# try:
# Donation.objects.get(created_at__month=current_month,
# created_at__year=current_year,
# donator=donator)
# except Donation.DoesNotExist:
# try:
# # Get donator last donation amount
# last_donation = Donation.objects.filter(donator=donator).last()
# donation_amount = last_donation.donation
# # Make stripe charge to a customer
# stripe_utils = StripeUtils()
# stripe_utils.CURRENCY = self.CURRENCY
# charge_response = stripe_utils.make_charge(amount=donation_amount,
# customer=donator.stripe_id)
# charge = charge_response.get('response_object')
# # Check if the payment was approved
# if not charge:
# # There is an error trying to creating the stripe charge
# context = {
# 'paymentError': charge_response.get('error'),
# }
# print("--------- STRIPE PAYMENT ERROR ---------")
# print(context)
# print("-------------------------")
# continue
# # Create a donation
# charge = charge_response.get('response_object')
# donation_data = {
# 'cc_brand': charge.source.brand,
# 'stripe_charge_id': charge.id,
# 'last4': charge.source.last4,
# 'billing_address': last_donation.billing_address.id,
# 'donator': donator.id,
# 'donation': donation_amount
# }
# donation_form = DonationForm(donation_data)
# if donation_form.is_valid():
# donation = donation_form.save()
# context = {
# 'donation': donation,
# 'base_url': "{0}://{1}".format('https', 'dynamicweb.ungleich.ch')
# }
# email_data = {
# 'subject': 'Your donation have been charged',
# 'to': donation.donator.user.email,
# 'context': context,
# 'template_name': 'donation_charge',
# 'template_path': 'nosystemd/emails/'
# }
# email = BaseEmail(**email_data)
# email.send()
# print("--------- PAYMENT DONATION SUCCESSFULL ---------")
# print("Donator: %s" % donation.donator.user.email)
# print("Amount: %s %s" % (donation.donation, self.CURRENCY))
# print("-----------------------------------------------")
# except Exception as e:
# print("--------- ERROR ---------")
# print(e)
# print("-------------------------")
# continue

View file

@ -1,11 +1,25 @@
import json import json
from model_mommy import mommy
from unittest import mock
from django.test import TestCase from django.test import TestCase
from django.conf import settings
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.core.urlresolvers import resolve from django.core.urlresolvers import resolve
from cms.test_utils.testcases import CMSTestCase from cms.test_utils.testcases import CMSTestCase
from django.contrib.auth.tokens import default_token_generator
from django.utils.http import urlsafe_base64_encode
from django.utils.encoding import force_bytes
from cms.api import create_page from cms.api import create_page
from membership.models import CustomUser, StripeCustomer
from utils.tests import BaseTestCase
from .views import LoginView, SignupView, PasswordResetView, PasswordResetConfirmView,\
MembershipPricingView, MembershipPaymentView
from .models import MembershipType
class ContactViewTest(TestCase): class ContactViewTest(TestCase):
def setUp(self): def setUp(self):
@ -38,14 +52,234 @@ class ViewsTest(CMSTestCase):
self.assertEqual(res2.status_code, 200) self.assertEqual(res2.status_code, 200)
class CalendarApiTestCase(TestCase): class MembershipPricingViewTest(BaseTestCase):
def test_api_response(self):
calendar_api_url_1 = reverse('digitalglarus:calendar_api_1', kwargs={'month': '3', 'year': '2016'})
res1 = self.client.get(calendar_api_url_1)
pd = json.loads(res1.content.decode('utf-8'))
self.assertEqual(pd['month'], '3')
self.assertEqual(pd['year'], '2016')
# TODO:check post def setUp(self):
# calendar_api_url = reverse('digitalglarus:calendar_api') super(MembershipPricingViewTest, self).setUp()
# res = self.client.get(calendar_api_url)
self.membership_type = mommy.make(MembershipType)
self.url = reverse('digitalglarus:membership_pricing')
self.view = MembershipPricingView
self.expected_template = 'digitalglarus/membership_pricing.html'
def test_url_resolve_to_view_correctly(self):
found = resolve(self.url)
self.assertEqual(found.func.__name__, self.view.__name__)
def test_get(self):
# Anonymous user should get data
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['membership_type'], self.membership_type)
self.assertTemplateUsed(response, self.expected_template)
class MembershipPaymentViewTest(BaseTestCase):
def setUp(self):
super(MembershipPaymentViewTest, self).setUp()
self.membership_type = mommy.make(MembershipType)
self.url = reverse('digitalglarus:membership_payment')
self.view = MembershipPaymentView
self.expected_template = 'digitalglarus/membership_payment.html'
# post data
self.billing_address = {
'street_address': 'street name',
'city': 'MyCity',
'postal_code': '32123123123123',
'country': 'VE',
'token': 'a23kfmslwxhkwis',
'membership_type': self.membership_type.id
}
def test_url_resolve_to_view_correctly(self):
found = resolve(self.url)
self.assertEqual(found.func.__name__, self.view.__name__)
def test_get(self):
# Anonymous user should get redirect to login
response = self.client.get(self.url)
expected_url = "%s?next=%s" % (reverse('digitalglarus:signup'),
reverse('digitalglarus:membership_payment'))
self.assertRedirects(response, expected_url=expected_url,
status_code=302, target_status_code=200)
# Logged user should get the page
response = self.customer_client.get(self.url, follow=True)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['stripe_key'],
settings.STRIPE_API_PUBLIC_KEY)
self.assertEqual(response.context['membership_type'],
self.membership_type)
@mock.patch('utils.stripe_utils.StripeUtils.create_customer')
def test_post(self, stripe_mocked_call):
# Anonymous user should get redirect to login
response = self.client.post(self.url)
expected_url = "%s?next=%s" % (reverse('digitalglarus:signup'),
reverse('digitalglarus:membership_payment'))
self.assertRedirects(response, expected_url=expected_url,
status_code=302, target_status_code=200)
# Customer user should be able to pay
stripe_mocked_call.return_value = {
'paid': True,
'response_object': self.stripe_mocked_customer,
'error': None
}
response = self.customer_client.post(self.url, self.billing_address)
import pdb
pdb.set_trace()
self.assertEqual(response.status_code, 200)
self.assertTrue(StripeCustomer.objects.filter(user__email=self.customer.email).exists())
stripe_customer = StripeCustomer.objects.get(user__email=self.customer.email)
# self.assertEqual(stripe_customer.user, self.customer)
# self.assertTrue(HostingOrder.objects.filter(customer=stripe_customer).exists())
# hosting_order = HostingOrder.objects.filter(customer=stripe_customer)[0]
# vm_plan = {
# 'cores': hosting_order.vm_plan.cores,
# 'memory': hosting_order.vm_plan.memory,
# 'disk_size': hosting_order.vm_plan.disk_size,
# 'price': hosting_order.vm_plan.price,
# 'hosting_company': hosting_order.vm_plan.vm_type.hosting_company,
# 'configuration': hosting_order.vm_plan.configuration
# }
# self.assertEqual(vm_plan, self.session_data.get('vm_specs'))
class LoginViewTest(TestCase):
def setUp(self):
self.url = reverse('digitalglarus:login')
self.view = LoginView
self.expected_template = 'digitalglarus/login.html'
self.user = mommy.make('membership.CustomUser')
self.password = 'fake_password'
self.user.set_password(self.password)
self.user.save()
def test_url_resolve_to_view_correctly(self):
found = resolve(self.url)
self.assertEqual(found.func.__name__, self.view.__name__)
def test_get(self):
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, self.expected_template)
def test_anonymous_user_can_login(self):
data = {
'email': self.user.email,
'password': self.password
}
response = self.client.post(self.url, data=data, follow=True)
self.assertEqual(response.context['user'], self.user)
self.assertEqual(response.status_code, 200)
class SignupViewTest(TestCase):
def setUp(self):
self.url = reverse('digitalglarus:signup')
self.expected_template = 'digitalglarus/signup.html'
self.view = SignupView
self.signup_data = {
'name': 'ungleich',
'email': 'test@ungleich.com',
'password': 'fake_password',
'confirm_password': 'fake_password',
}
def test_url_resolve_to_view_correctly(self):
found = resolve(self.url)
self.assertEqual(found.func.__name__, self.view.__name__)
def test_get(self):
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, self.expected_template)
def test_anonymous_user_can_signup(self):
response = self.client.post(self.url, data=self.signup_data, follow=True)
self.user = CustomUser.objects.get(email=self.signup_data.get('email'))
self.assertEqual(response.context['user'], self.user)
self.assertEqual(response.status_code, 200)
class PasswordResetViewTest(BaseTestCase):
def setUp(self):
super(PasswordResetViewTest, self).setUp()
self.url = reverse('digitalglarus:reset_password')
self.view = PasswordResetView
self.expected_template = 'digitalglarus/reset_password.html'
self.user = mommy.make('membership.CustomUser')
self.password = 'fake_password'
self.user.set_password(self.password)
self.user.save()
self.post_data = {
'email': self.user.email
}
def test_url_resolve_to_view_correctly(self):
found = resolve(self.url)
self.assertEqual(found.func.__name__, self.view.__name__)
def test_get(self):
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, self.expected_template)
def test_post(self):
response = self.client.post(self.url, data=self.post_data, follow=True)
self.assertEqual(response.status_code, 200)
def test_test_generate_email_context(self):
context = self.setup_view(self.view()).\
test_generate_email_context(self.user)
self.assertEqual(context.get('user'), self.user)
self.assertEqual(context.get('site_name'), 'ungleich')
self.assertEqual(len(context.get('token')), 24)
class PasswordResetConfirmViewTest(BaseTestCase):
def setUp(self):
super(PasswordResetConfirmViewTest, self).setUp()
self.view = PasswordResetConfirmView
self.expected_template = 'digitalglarus/confirm_reset_password.html'
self.user = mommy.make('membership.CustomUser')
self.password = 'fake_password'
self.user.set_password(self.password)
self.user.save()
self.token = default_token_generator.make_token(self.user)
self.uid = urlsafe_base64_encode(force_bytes(self.user.pk))
self.url = reverse('digitalglarus:reset_password_confirm',
kwargs={'token': self.token, 'uidb64': self.uid})
self.post_data = {
'new_password1': 'new_password',
'new_password2': 'new_password'
}
def test_url_resolve_to_view_correctly(self):
found = resolve(self.url)
self.assertEqual(found.func.__name__, self.view.__name__)
def test_get(self):
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, self.expected_template)
def test_post(self):
response = self.client.post(self.url, data=self.post_data, follow=True)
self.assertEqual(response.status_code, 200)
self.assertTrue(not response.context['form'].errors)