Skip to content
This repository was archived by the owner on Aug 13, 2019. It is now read-only.

Commit

Permalink
Adds tests for password missmatch
Browse files Browse the repository at this point in the history
  • Loading branch information
paurakhsharma committed Jan 12, 2018
1 parent 792440d commit 7ac0fb2
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 3 deletions.
13 changes: 13 additions & 0 deletions .idea/dataSources.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions apps/accounts/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class SignUpForm(UserCreationForm):
}
))



class Meta:
model = User
fields = ('username', 'first_name', 'last_name', 'email', 'password1', 'password2', )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
Hi {{ user.username }},
Please click on the link to confirm your registration,

http://127.0.0.1:8000{% url 'accounts:activate' uidb64=uid token=token %}
{{ protocol }}{{ domain}}{% url 'accounts:activate' uidb64=uid token=token %}
{% endautoescape %}
72 changes: 72 additions & 0 deletions apps/accounts/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,8 @@ def setUp(self):
self.url = reverse('accounts:signup')
self.response = self.client.get(self.url)



def test_csrf(self):
"""Test for csrf token."""
self.assertContains(self.response, 'csrfmiddlewaretoken')
Expand All @@ -324,3 +326,73 @@ def test_signup_view_is_rendered(self):
"""Tests if `accounts/signup.html` and `base.html` is used."""
self.assertTemplateUsed(self.response, 'accounts/signup.html')
self.assertTemplateUsed(self.response, 'base.html')

# unable to link, need assistance
def test_contains_login_link(self):
"""Test if it contains `signup` link."""
url = reverse('accounts:login')
self.assertContains(self.response, f'href="{url}"')


def test_form_inputs(self):
"""The view must contain three inputs: csrf, username, first_name, last_name
email, password1, password2"""
self.assertContains(self.response, '<input', 7)
self.assertContains(self.response, 'type="text"', 4)
self.assertContains(self.response, 'type="password"', 2)




class SignupPostTest(TestCase):
"""Tests the login `POST` requests."""

def setUp(self):
"""Set ups the user and url for this test."""
self.url = reverse('accounts:signup')

def test_successfull_post_signup(self):
data = {
'username': 'testusername',
'first_name': 'testfirstname',
'last_name': 'testlastname',
'email': '[email protected]',
'password1': 'testpassword',
'password2': 'testpassword',

}

response = self.client.post(self.url, data)

user = User.objects.get(username = data['username'])
self.assertEqual(user.first_name, data['first_name'])
self.assertEqual(user.last_name, data['last_name'])
self.assertEqual(user.email, data['email'])
self.assertFalse(user.is_active)
self.assertTrue(user)

self.assertTemplateUsed(response, 'accounts/email_sent.html')
self.assertTemplateUsed(response, 'base.html')
self.assertEqual(response.status_code, 200)

def test_password_mismatch(self):

data = {
'username': 'testusername1',
'first_name': 'testfirstname',
'last_name': 'testlastname',
'email': '[email protected]',
'password1': 'testpassword',
'password2': 'testpassword1',

}

response = self.client.post(reverse('accounts:signup'), data)

error_messages = ["The two password fields didn't match."]
self.assertFormError(response, 'form', 'password1', error_messages)
self.assertFalse(User.objects.exists(username = data['username']))

self.assertTemplateUsed(response, 'accounts/signup.html')
self.assertTemplateUsed(response, 'base.html')
self.assertEqual(response.status_code, 200)
3 changes: 2 additions & 1 deletion apps/accounts/urls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.conf.urls import url
from .views import UserLoginView, logout_view, signup, activate as activate_view
from .views import UserLoginView, logout_view, signup, activate as activate_view


urlpatterns = [
Expand All @@ -8,4 +8,5 @@
url(r'^logout/$', logout_view, name='logout'),
url(r'^activate/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
activate_view, name='activate'),

]
4 changes: 3 additions & 1 deletion apps/accounts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ def signup(request):
mail_subject, message, to=[to_email]
)
email.send()
return HttpResponse('Please confirm your email address to complete the registration')
return render(request, 'accounts/email_sent.html', { 'email':user.email})
else:
form = SignUpForm()
return render(request, 'accounts/signup.html', {'form': form})




def activate(request, uidb64, token):
try:
uid = force_text(urlsafe_base64_decode(uidb64))
Expand Down

0 comments on commit 7ac0fb2

Please sign in to comment.