Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Unit test for about us and contact page #21

Merged
merged 8 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/django.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ jobs:
python-version: ['3.10']

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install Dependencies
Expand Down
14 changes: 14 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"configurations": [
{
"name": "Python Debugger: Django",
"type": "debugpy",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"args": [
"runserver"
],
"django": true
}
]
}
3 changes: 0 additions & 3 deletions contacts/tests.py

This file was deleted.

Empty file added contacts/tests/__init__.py
Empty file.
51 changes: 51 additions & 0 deletions contacts/tests/test_forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from django.test import TestCase
from contacts.form import ContactForm
from contacts.models import Contact


class TestContactForm(TestCase):

def test_contact_form_valid(self):
# Positive scenario data with actual users
form_data = {
'name': 'Mashkur',
'email': '[email protected]',
'subject': 'Complaint',
'message': 'Less menu items'
}

# With valid data
form = ContactForm(data=form_data)

# Form is valid
self.assertTrue(form.is_valid())

def test_contact_form_invalid_missing_email(self):
# Negative scenario data with actual users
invalid_data = {
'name': 'Mashkur',
'email': '',
'subject': 'Complaint',
'message': 'Less menu items'
}

# With invalid data missing email
form = ContactForm(data=invalid_data)

# Form is invalid
self.assertFalse(form.is_valid())

def test_contact_form_invalid_missing_name(self):
# Invalid data
invalid_data = {
'name': '',
'email': '[email protected]',
'subject': 'Complaint',
'message': 'Less menu items'
}

# With invalid data missing name
form = ContactForm(data=invalid_data)

# Form is invalid
self.assertFalse(form.is_valid())
33 changes: 33 additions & 0 deletions contacts/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from django.test import TestCase
from contacts.models import Contact


class TestContactModel(TestCase):

def test_contact_creation(self):
# Verifying input field by creating a contact
contact = Contact.objects.create(
name='Pravin Kannappan',
email='[email protected]',
subject='Enquiry',
message='How long does it take for delivery'
)

# Verify if the contact was created successfully
self.assertIsNotNone(contact)
self.assertEqual(contact.name, 'Pravin Kannappan')
self.assertEqual(contact.email, '[email protected]')
self.assertEqual(contact.subject, 'Enquiry')
self.assertEqual(contact.message, 'How long does it take for delivery')

def test_contact_str_representation(self):
# Verifying input field by creating a contact
contact = Contact.objects.create(
name='RajiniKanth',
email='[email protected]',
subject='Enquiry',
message='How long does it take for delivery'
)

# Check if the __str__ method returns the expected string representation
self.assertEqual(str(contact), 'RajiniKanth')
65 changes: 65 additions & 0 deletions contacts/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from django.test import TestCase, Client
from django.urls import reverse
from django.core import mail
from contacts.form import ContactForm
from contacts.models import Contact


class TestContactView(TestCase):

def setUp(self):
self.client = Client()
self.view_contact_url = reverse('contacts:view-contact')
self.view_about_url = reverse('contacts:view-about')
self.valid_form_data = {
'name': 'Pravin',
'email': '[email protected]',
'subject': 'Compliment',
'message': 'Food was good'
}

def test_view_contact_get(self):
# Verify GET request
response = self.client.get(self.view_contact_url)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'contact.html')
self.assertIsInstance(response.context['form'], ContactForm)

def test_view_contact_post_valid_form(self):
# Verify valid POST request
response = self.client.post(
self.view_contact_url, data=self.valid_form_data)
self.assertEqual(response.status_code, 302) # Redirect status code
self.assertRedirects(response, self.view_contact_url)

# Verify that the form was saved upon sending
self.assertEqual(Contact.objects.count(), 1)

# Verify if the an email was sent after post request
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(mail.outbox[0].subject, 'Compliment')

def test_view_contact_post_invalid_form(self):
# Verify if invalid POST request (missing required fields)
invalid_data = self.valid_form_data.copy()
invalid_data['email'] = '' # Email is required (Empty string)
response = self.client.post(self.view_contact_url, data=invalid_data)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'contact.html')
self.assertFalse(response.context['form'].is_valid())
# No contact should be saved
self.assertEqual(Contact.objects.count(), 0)
self.assertEqual(len(mail.outbox), 0) # No email should be sent


class TestAboutView(TestCase):
# Verifying by setting up client defining URL for the about view
def setUp(self):
self.client = Client()
self.view_about_url = reverse('contacts:view-about')
# Verify the response status code is 200 (indicating success), and verifies that the correct template

def test_view_about_get(self):
response = self.client.get(self.view_about_url)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'about.html')
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ psycopg==3.1.18
psycopg-binary==3.1.18
sqlparse==0.4.4
typing_extensions==4.11.0
tzdata==2024.1
whitenoise==6.6.0
12 changes: 6 additions & 6 deletions users/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.urls import path
from . import views
from .views import home, RegisterView, CustomLoginView, update_user
from .views import RegisterView, CustomLoginView, update_user
from django.contrib.auth import views as auth_views


Expand All @@ -11,16 +11,16 @@

urlpatterns = [
path(
"profile-management", views.ViewUserProfileView.as_view(), name="view-profile"
"profile", views.ViewUserProfileView.as_view(), name="view-profile"
),

path("edit-management", update_user, name="edit-profile"),
path("profile/edit", update_user, name="edit-profile"),


path("register/", RegisterView.as_view(), name="users-register"),
path("register", RegisterView.as_view(), name="users-register"),

path(
"login/",
"login",
CustomLoginView.as_view(
redirect_authenticated_user=True,
template_name="login.html",
Expand All @@ -29,5 +29,5 @@
name="login",
),

path("logout/", auth_views.LogoutView.as_view(), name="logout"),
path("logout", auth_views.LogoutView.as_view(), name="logout"),
]
19 changes: 4 additions & 15 deletions yummy/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,28 +143,17 @@
BASE_DIR / "static"
]

# added by mash

# Image uploads
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
# ended by mash

# added by naqibullah : if user is admin after log


# if user is customer
LOGIN_REDIRECT_URL = '/'

# if the user is admin ?
# LOGIN_REDIRECT_URL = '/dashboard'


# User authentication
LOGIN_URL = 'users/login'
LOGIN_REDIRECT_URL = '/' # if the user is Customer
# LOGIN_REDIRECT_URL = '/dashboard' # if the user is Admin
LOGOUT_REDIRECT_URL = '/'

# added by naqibullah finished

# Default primary key field type
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field
Expand Down
7 changes: 1 addition & 6 deletions yummy/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@

from django.conf.urls.static import static

from django.contrib.auth import views as auth_views
from users.views import CustomLoginView

from users.forms import LoginForm

urlpatterns = [
path('', views.index, name='index'),
Expand All @@ -38,6 +34,5 @@
path('menus/', include(('menus.urls', 'menus'), namespace='menus')),
path('orders/', include(('orders.urls', 'orders'), namespace='orders')),
path('contacts/', include(('contacts.urls', 'contacts'), namespace='contacts')),

] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
# ended by mash
Loading