summaryrefslogtreecommitdiff
path: root/yaksh
diff options
context:
space:
mode:
authorPrabhu Ramachandran2017-05-05 17:47:27 +0530
committerGitHub2017-05-05 17:47:27 +0530
commitb6993d79ae2f0d18375555f5ada72b52a3a2e066 (patch)
tree19326254303a5ff03697371ed198f89e1145db9b /yaksh
parent4cd448769c6cc230b7a33a1848cc45873b578468 (diff)
parentd7e6430d8c48381c6660906ab3a96251824f9031 (diff)
downloadonline_test-b6993d79ae2f0d18375555f5ada72b52a3a2e066.tar.gz
online_test-b6993d79ae2f0d18375555f5ada72b52a3a2e066.tar.bz2
online_test-b6993d79ae2f0d18375555f5ada72b52a3a2e066.zip
Merge pull request #290 from adityacp/email_verifcation_testing
Email verification testing
Diffstat (limited to 'yaksh')
-rw-r--r--yaksh/forms.py10
-rw-r--r--yaksh/send_emails.py (renamed from yaksh/email_verification.py)18
-rw-r--r--yaksh/test_views.py45
-rw-r--r--yaksh/urls.py6
-rw-r--r--yaksh/views.py9
5 files changed, 60 insertions, 28 deletions
diff --git a/yaksh/forms.py b/yaksh/forms.py
index 5dd56a2..3459be9 100644
--- a/yaksh/forms.py
+++ b/yaksh/forms.py
@@ -10,6 +10,7 @@ from taggit.managers import TaggableManager
from taggit.forms import TagField
from django.forms.models import inlineformset_factory
from django.db.models import Q
+from django.utils import timezone
from textwrap import dedent
try:
from string import letters
@@ -18,7 +19,7 @@ except ImportError:
from string import punctuation, digits
import datetime
import pytz
-from .email_verification import generate_activation_key
+from .send_emails import generate_activation_key
languages = (
("select", "Select Language"),
@@ -146,11 +147,8 @@ class UserRegisterForm(forms.Form):
new_profile.is_email_verified = True
else:
new_profile.activation_key = generate_activation_key(new_user.username)
- new_profile.key_expiry_time = datetime.datetime.strftime(
- datetime.datetime.now() + \
- datetime.timedelta(minutes=20),
- "%Y-%m-%d %H:%M:%S"
- )
+ new_profile.key_expiry_time = timezone.now() + \
+ timezone.timedelta(minutes=20)
new_profile.save()
return u_name, pwd, new_user.email, new_profile.activation_key
diff --git a/yaksh/email_verification.py b/yaksh/send_emails.py
index 721fa61..37736d5 100644
--- a/yaksh/email_verification.py
+++ b/yaksh/send_emails.py
@@ -12,6 +12,8 @@ import smtplib
# Django imports
from django.utils.crypto import get_random_string
from django.conf import settings
+from django.core.mail import send_mass_mail, send_mail
+
def generate_activation_key(username):
""" Generate hashed secret key for email activation """
@@ -19,6 +21,7 @@ def generate_activation_key(username):
secret_key = get_random_string(randint(10, 40), chars)
return hashlib.sha256((secret_key + username).encode('utf-8')).hexdigest()
+
def send_user_mail(user_mail, key):
""" Send mail to user whose email is to be verified
This function should get two args i.e user_email and secret_key.
@@ -40,20 +43,7 @@ def send_user_mail(user_mail, key):
""".format(settings.PRODUCTION_URL, key, settings.REPLY_EMAIL)
)
- user = settings.EMAIL_HOST_USER
- pwd = settings.EMAIL_HOST_PASSWORD
- smtpserver = smtplib.SMTP(settings.EMAIL_HOST, settings.EMAIL_PORT)
- smtpserver.ehlo()
- smtpserver.starttls()
- smtpserver.ehlo()
- smtpserver.esmtp_features['auth']='LOGIN DIGEST-MD5 PLAIN'
-
- smtpserver.login(user, pwd)
- header = 'To:{0}\nFrom:{1}\nSubject:{2}\n'.format(to,
- settings.SENDER_EMAIL, subject)
- message = '{0}\n{1}\n\n'.format(header, message)
- smtpserver.sendmail(user, to, message)
- smtpserver.close()
+ send_mail(subject, message, settings.SENDER_EMAIL, [to])
msg = "An activation link is sent to your registered email.\
Please activate the link within 20 minutes."
diff --git a/yaksh/test_views.py b/yaksh/test_views.py
index 3fcdde2..37e5ce4 100644
--- a/yaksh/test_views.py
+++ b/yaksh/test_views.py
@@ -7,6 +7,7 @@ from django.core.urlresolvers import reverse
from django.test import TestCase
from django.test import Client
from django.utils import timezone
+from django.core import mail
from yaksh.models import User, Profile, Question, Quiz, QuestionPaper,\
QuestionSet, AnswerPaper, Answer, Course, StandardTestCase,\
@@ -97,6 +98,31 @@ class TestProfile(TestCase):
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'yaksh/view_profile.html')
+ def test_email_verification_for_user_post(self):
+ """
+ POST request to verify email
+ """
+ self.client.login(
+ username=self.user2.username,
+ password=self.user2_plaintext_pass
+ )
+ post_response = self.client.post(reverse('yaksh:new_activation'),
+ data={'email':self.user2.email}
+ )
+ subject = mail.outbox[0].subject.replace(" ", "_")
+ activation_key = mail.outbox[0].body.split("\n")[2].split("/")[-1]
+ get_response = self.client.get(reverse('yaksh:activate',
+ kwargs={'key': activation_key}),
+ follow=True
+ )
+ updated_profile_user = User.objects.get(id=self.user2.id)
+ updated_profile = Profile.objects.get(user=updated_profile_user)
+ self.assertEqual(post_response.status_code, 200)
+ self.assertEqual(subject, "Yaksh_Email_Verification")
+ self.assertEqual(get_response.status_code, 200)
+ self.assertEqual(updated_profile.is_email_verified, True)
+ self.assertTemplateUsed(get_response, 'yaksh/activation_status.html')
+
def test_edit_profile_post(self):
"""
POST request to edit_profile view should update the user's profile
@@ -141,6 +167,25 @@ class TestProfile(TestCase):
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'yaksh/editprofile.html')
+ def test_update_email_for_user_post(self):
+ """ POST request to update email if multiple users with same email are
+ found
+ """
+ self.client.login(
+ username=self.user2.username,
+ password=self.user2_plaintext_pass
+ )
+ response = self.client.post(reverse('yaksh:update_email'),
+ data={
+ 'username': self.user2.username,
+ 'email':"demo_user2@mail.com"
+ }
+ )
+ updated_user = User.objects.get(id=self.user2.id)
+ self.assertEqual(updated_user.email, "demo_user2@mail.com")
+ self.assertEqual(response.status_code, 200)
+ self.assertTemplateUsed(response, 'yaksh/activation_status.html')
+
class TestAddQuiz(TestCase):
def setUp(self):
diff --git a/yaksh/urls.py b/yaksh/urls.py
index b02b797..e4676d3 100644
--- a/yaksh/urls.py
+++ b/yaksh/urls.py
@@ -5,9 +5,9 @@ urlpatterns = [
url(r'^$', views.index),
url(r'^login/$', views.user_login, name='login'),
url(r'^logout/$', views.user_logout),
- url(r'^update_email/$', views.update_email),
- url(r'^activate/(?P<key>.+)$', views.activate_user),
- url(r'^new_activation/$', views.new_activation),
+ url(r'^update_email/$', views.update_email, name="update_email"),
+ url(r'^activate/(?P<key>.+)$', views.activate_user, name="activate"),
+ url(r'^new_activation/$', views.new_activation, name='new_activation'),
url(r'^quizzes/$', views.quizlist_user, name='quizlist_user'),
url(r'^quizzes/(?P<enrolled>\w+)/$', views.quizlist_user, name='quizlist_user'),
url(r'^results/$', views.results_user),
diff --git a/yaksh/views.py b/yaksh/views.py
index 6a4325e..c7af5cc 100644
--- a/yaksh/views.py
+++ b/yaksh/views.py
@@ -42,7 +42,7 @@ from yaksh.forms import UserRegisterForm, UserLoginForm, QuizForm,\
from .settings import URL_ROOT
from yaksh.models import AssignmentUpload
from .file_utils import extract_files
-from .email_verification import send_user_mail, generate_activation_key
+from .send_emails import send_user_mail, generate_activation_key
from .decorators import email_verified
@@ -1540,10 +1540,8 @@ def new_activation(request, email=None):
if not user.profile.is_email_verified:
user.profile.activation_key = generate_activation_key(user.username)
- user.profile.key_expiry_time = datetime.strftime(
- datetime.now() + \
- timedelta(minutes=20), "%Y-%m-%d %H:%M:%S"
- )
+ user.profile.key_expiry_time = timezone.now() + \
+ timezone.timedelta(minutes=20)
user.profile.save()
success, msg = send_user_mail(user.email, user.profile.activation_key)
if success:
@@ -1599,6 +1597,7 @@ def download_assignment_file(request, quiz_id, question_id=None, user_id=None):
return response
@login_required
+@email_verified
def duplicate_course(request, course_id):
user = request.user
course = get_object_or_404(Course, pk=course_id)