Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/add tests #9

Closed
wants to merge 10 commits into from
Closed
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
27 changes: 27 additions & 0 deletions .github/workflows/django.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Django Test CI

on:
pull_request:
branches: [ "master" ]

jobs:
build:
runs-on: ubuntu-latest
strategy:
max-parallel: 4
matrix:
python-version: [3.9]

steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install Dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run Tests
run: |
python manage.py test
81 changes: 79 additions & 2 deletions Auth/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,80 @@
from django.test import TestCase
from django.test import TestCase, Client
from django.urls import reverse
from .models import User
from django.contrib import messages
from Auth.forms import LoginForm, SignupForm

# Create your tests here.
class LoginViewTests(TestCase):

def setUp(self):
self.client = Client()
self.user = User.objects.create_user(
first_name='test',
last_name='user',
email='[email protected]',
password='testpassword'
)

def test_get_login(self):
response = self.client.get(reverse('Auth:login'))
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'Auth/login.html')
self.assertIsInstance(response.context['form'], LoginForm)

def test_post_login_success(self):
response = self.client.post(reverse('Auth:login'), {
'email': '[email protected]',
'password': 'testpassword',
'remember_me': True
})
self.assertRedirects(response, reverse('Shop:home'))
messages_list = list(messages.get_messages(response.wsgi_request))
self.assertEqual(len(messages_list), 1)
self.assertEqual(str(messages_list[0]), 'Welcome test!')

def test_post_login_failure(self):
response = self.client.post(reverse('Auth:login'), {
'email': '[email protected]',
'password': 'wrongpassword',
})
self.assertRedirects(response, reverse('Auth:login'))
messages_list = list(messages.get_messages(response.wsgi_request))
self.assertEqual(len(messages_list), 1)
self.assertEqual(str(messages_list[0]), 'Login failed. Please check your username and password.')


class SignupViewTests(TestCase):

def setUp(self):
self.client = Client()

def test_get_signup(self):
response = self.client.get(reverse('Auth:signup'))
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'Auth/signup.html')
self.assertIsInstance(response.context['form'], SignupForm)

def test_post_signup_success(self):
response = self.client.post(reverse('Auth:signup'), {
'first_name': 'test',
'last_name': 'user',
'email': '[email protected]',
'password1': 'testpassword',
'password2': 'testpassword'
})
self.assertRedirects(response, reverse('Auth:login'))
messages_list = list(messages.get_messages(response.wsgi_request))
self.assertEqual(len(messages_list), 1)
self.assertEqual(str(messages_list[0]), 'test account has been created successfully. Please login to continue.')
def test_post_signup_failure(self):
response = self.client.post(reverse('Auth:signup'), {
'first_name': 'test',
'last_name': 'user',
'email': '[email protected]',
'password1': 'testpassword',
'password2': 'testpassword2'
})
self.assertEqual(response.status_code, 400)
self.assertTemplateUsed(response, 'Auth/signup.html')
self.assertIsInstance(response.context['form'], SignupForm)

9 changes: 2 additions & 7 deletions Auth/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ def get(self, request):

def post(self, request):
form = LoginForm(request.POST)
print("form:"+str(form.is_valid()))
if not form.is_valid():
messages.error(request, 'Please check your inputs')
return redirect('Auth:login')
Expand All @@ -33,7 +32,7 @@ def post(self, request):
request.session.set_expiry(getSecondsOfOneYear())
else:
request.session.set_expiry(0)
messages.success(request, 'You have been logged in')
messages.success(request, 'Welcome '+str(user.first_name)+'!')
return redirect('Shop:home')
else:
messages.error(request, 'Login failed. Please check your username and password.')
Expand All @@ -46,10 +45,9 @@ def get(self, request):

def post(self, request):
form = SignupForm(request.POST)
print("form:"+str(form.is_valid()))
if form.is_valid():
form.save()
messages.success(request, 'Account created successfully')
messages.success(request, str(form.cleaned_data.get('first_name'))+' account has been created successfully. Please login to continue.')
return redirect('Auth:login')
else:
response = HttpResponse()
Expand Down Expand Up @@ -79,12 +77,9 @@ def reset_password(request):
form = ResetPasswordForm(request.POST, email=email)
if form.is_valid():
user = User.objects.filter(email=email).first()
print("user:"+str(user))
if user:
print("password:"+str(form.cleaned_data['password1']))
user.set_password(form.cleaned_data['password1'])
user.save()
print("saved:")
messages.success(request, 'Your password has been successfully reset.')
return redirect(reverse('Auth:login'))
return render(request, 'Auth/reset_password.html', {'form': form})
Expand Down
25 changes: 6 additions & 19 deletions GreenCart/urls.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,19 @@
"""
URL configuration for GreenCart project.

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from payments.views import payment_view, payment_success

# Admin site configuration
admin.site.index_title = "Welcome to GreenCart Admin Portal"
admin.site.site_header="GreenCart Admin"
admin.site.site_title ="GreenCart Admin Portal"
from payments.views import payment_view, payment_success

urlpatterns = [
path('admin/', admin.site.urls),
path('', include('GreenCartEcom.urls','GreenCartEcom'), name='GreenCartEcom'),
path('', include('GreenCartEcom.urls', 'GreenCartEcom'), name='GreenCartEcom'),
path('cart/', include('add_to_cart.urls', 'AddToCart'), name='AddToCart'),
path('auth/', include('Auth.urls','Auth'), name='Auth'),
path('shop/', include('Shop.urls','Shop'), name='Shop'),
Expand All @@ -36,4 +23,4 @@
]

if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Loading