From 3ec8eb2d3ad08507e968578919c0f9b210fb29b1 Mon Sep 17 00:00:00 2001 From: YashSahsani Date: Fri, 21 Jun 2024 15:48:57 -0400 Subject: [PATCH 1/7] feat: add tests --- Auth/tests.py | 69 +++++++++++++++++++++++++++++++++++++++++++++-- Auth/views.py | 5 ++-- GreenCart/urls.py | 22 +++------------ 3 files changed, 73 insertions(+), 23 deletions(-) diff --git a/Auth/tests.py b/Auth/tests.py index 7ce503c..07cae33 100644 --- a/Auth/tests.py +++ b/Auth/tests.py @@ -1,3 +1,68 @@ -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='testuser@example.com', + 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': 'testuser@example.com', + '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': 'wronguser@example.com', + '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': 'test@green.com', + '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.') \ No newline at end of file diff --git a/Auth/views.py b/Auth/views.py index 60c6924..a4dec7f 100644 --- a/Auth/views.py +++ b/Auth/views.py @@ -17,7 +17,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') @@ -31,7 +30,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.') @@ -47,7 +46,7 @@ def post(self, request): 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: print(form.errors) diff --git a/GreenCart/urls.py b/GreenCart/urls.py index d2302e4..02002ec 100644 --- a/GreenCart/urls.py +++ b/GreenCart/urls.py @@ -1,23 +1,9 @@ -""" -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.urls import path, include -admin.site.site_header="GreenCart Admin" -admin.site.site_title ="GreenCart Admin Portal" + +admin.site.index_title = "Welcome to GreenCart Admin Portal" +admin.site.site_title = "GreenCart Admin Portal" +admin.site.site_header = "GreenCart Admin Portal" urlpatterns = [ path('admin/', admin.site.urls), From 8d515ecd6c6ed381d52c13de234c69f8e2f5358e Mon Sep 17 00:00:00 2001 From: Yash Sahsani Date: Fri, 21 Jun 2024 15:51:25 -0400 Subject: [PATCH 2/7] Create django.yml --- .github/workflows/django.yml | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 .github/workflows/django.yml diff --git a/.github/workflows/django.yml b/.github/workflows/django.yml new file mode 100644 index 0000000..53ece2d --- /dev/null +++ b/.github/workflows/django.yml @@ -0,0 +1,30 @@ +name: Django CI + +on: + push: + branches: [ "master" ] + pull_request: + branches: [ "master" ] + +jobs: + build: + + runs-on: ubuntu-latest + strategy: + max-parallel: 4 + matrix: + python-version: [3.7, 3.8, 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 From a82604c20329639f5c55946b3c2091b85535705c Mon Sep 17 00:00:00 2001 From: YashSahsani Date: Fri, 21 Jun 2024 15:52:18 -0400 Subject: [PATCH 3/7] fix: ci --- .github/workflows/django.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/django.yml b/.github/workflows/django.yml index 53ece2d..5389222 100644 --- a/.github/workflows/django.yml +++ b/.github/workflows/django.yml @@ -2,7 +2,7 @@ name: Django CI on: push: - branches: [ "master" ] + branches: [ "master" ,"feat/*"] pull_request: branches: [ "master" ] From f0ee9a8f1ebe32a5a3a83f60fc8c3d03e63bee00 Mon Sep 17 00:00:00 2001 From: YashSahsani Date: Fri, 21 Jun 2024 15:54:41 -0400 Subject: [PATCH 4/7] fix: ci --- .github/workflows/django.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/django.yml b/.github/workflows/django.yml index 5389222..0b9a218 100644 --- a/.github/workflows/django.yml +++ b/.github/workflows/django.yml @@ -13,7 +13,7 @@ jobs: strategy: max-parallel: 4 matrix: - python-version: [3.7, 3.8, 3.9] + python-version: [3.9] steps: - uses: actions/checkout@v4 From 5415b17d5fc05b1608ba244015e046a6b2b10630 Mon Sep 17 00:00:00 2001 From: YashSahsani Date: Fri, 21 Jun 2024 15:57:24 -0400 Subject: [PATCH 5/7] test: add test --- Auth/tests.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Auth/tests.py b/Auth/tests.py index 07cae33..ac79995 100644 --- a/Auth/tests.py +++ b/Auth/tests.py @@ -65,4 +65,16 @@ def test_post_signup_success(self): 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.') \ No newline at end of file + 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': 'test2@t.com', + 'password1': 'testpassword', + 'password2': 'testpassword' + }) + self.assertRedirects(response, reverse('Auth:signup')) + messages_list = list(messages.get_messages(response.wsgi_request)) + self.assertEqual(len(messages_list), 1) + self.assertEqual(str(messages_list[0]), 'Please check your inputs') \ No newline at end of file From 81df96fa027519bc92fa91daa485ef1df9dbe19c Mon Sep 17 00:00:00 2001 From: YashSahsani Date: Sat, 22 Jun 2024 20:45:41 -0400 Subject: [PATCH 6/7] feat: add test and workflow --- .github/workflows/django.yml | 5 +---- Auth/tests.py | 5 +++-- Auth/views.py | 11 +---------- 3 files changed, 5 insertions(+), 16 deletions(-) diff --git a/.github/workflows/django.yml b/.github/workflows/django.yml index 0b9a218..263fce3 100644 --- a/.github/workflows/django.yml +++ b/.github/workflows/django.yml @@ -1,14 +1,11 @@ -name: Django CI +name: Django Test CI on: - push: - branches: [ "master" ,"feat/*"] pull_request: branches: [ "master" ] jobs: build: - runs-on: ubuntu-latest strategy: max-parallel: 4 diff --git a/Auth/tests.py b/Auth/tests.py index ac79995..90e006a 100644 --- a/Auth/tests.py +++ b/Auth/tests.py @@ -70,10 +70,11 @@ def test_post_signup_failure(self): response = self.client.post(reverse('Auth:signup'), { 'first_name': 'test', 'last_name': 'user', - 'email': 'test2@t.com', + 'email': 'test@test.com', 'password1': 'testpassword', - 'password2': 'testpassword' + 'password2': 'testpassword2' }) + self.assertEqual(response.status_code, 302) self.assertRedirects(response, reverse('Auth:signup')) messages_list = list(messages.get_messages(response.wsgi_request)) self.assertEqual(len(messages_list), 1) diff --git a/Auth/views.py b/Auth/views.py index a4dec7f..e8f9e93 100644 --- a/Auth/views.py +++ b/Auth/views.py @@ -43,15 +43,13 @@ 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, str(form.cleaned_data.get('first_name'))+' account has been created successfully. Please login to continue.') return redirect('Auth:login') else: - print(form.errors) messages.error(request, 'Please check your inputs') - return redirect('Auth:signup', {'form': form}) + return redirect('Auth:signup') def forgot_password(request): @@ -68,23 +66,16 @@ def forgot_password(request): def reset_password(request): email = request.GET.get('email') - print("email:"+str(email)) if not email: messages.error(request, 'Invalid password reset link.') return redirect('forgot_password') - print(request.method) if request.method == 'POST': form = ResetPasswordForm(request.POST, email=email) - print("form:"+str(form.is_valid())) - print("form:"+str(form.errors)) 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')) else: From 21ac6711f77cba5f339fa6da7ab7573e1a19eb5f Mon Sep 17 00:00:00 2001 From: YashSahsani Date: Sat, 22 Jun 2024 20:51:33 -0400 Subject: [PATCH 7/7] fix: test --- Auth/tests.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Auth/tests.py b/Auth/tests.py index 90e006a..d661eb5 100644 --- a/Auth/tests.py +++ b/Auth/tests.py @@ -74,8 +74,7 @@ def test_post_signup_failure(self): 'password1': 'testpassword', 'password2': 'testpassword2' }) - self.assertEqual(response.status_code, 302) - self.assertRedirects(response, reverse('Auth:signup')) - messages_list = list(messages.get_messages(response.wsgi_request)) - self.assertEqual(len(messages_list), 1) - self.assertEqual(str(messages_list[0]), 'Please check your inputs') \ No newline at end of file + self.assertEqual(response.status_code, 400) + self.assertTemplateUsed(response, 'Auth/signup.html') + self.assertIsInstance(response.context['form'], SignupForm) + \ No newline at end of file