Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
modified and added tests
  • Loading branch information
ashfaq1934 committed Jun 14, 2023
commit fe6236db934d399af3d4a60582058be474c8d072
10 changes: 5 additions & 5 deletions project/accounts/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class LoginViewTests(BaseTestCase):
"""A class to test login view"""

def setUp(self) -> None:
super(LoginViewTests, self).setUp()
super().setUp()
url = reverse("accounts_login")
self.response = self.client.get(url)

Expand Down Expand Up @@ -48,7 +48,7 @@ def test_login_view_redirects_on_success(self):

response = self.client.post(
reverse("accounts_login"),
{"username": "newuser", "password": "password123"},
{"username": "newuser", "password": "password123", "next": reverse("base")},
)
self.assertRedirects(
response,
Expand Down Expand Up @@ -117,7 +117,7 @@ class SettingsViewTests(BaseTestCase):
"""A class to test settings view"""

def setUp(self) -> None:
super(SettingsViewTests, self).setUp()
super().setUp()
self.user.profile.first_name = "Gorkem"
self.user.profile.last_name = "Arslan"
self.user.profile.save()
Expand Down Expand Up @@ -202,7 +202,7 @@ class UserProfileView(BaseTestCase):
"""A class to test user profile view"""

def setUp(self) -> None:
super(UserProfileView, self).setUp()
super().setUp()
self.user.profile.first_name = "First"
self.user.profile.last_name = "Last"
self.user.profile.about_me = "About"
Expand All @@ -222,7 +222,7 @@ class UserProfileCivis(BaseTestCase):
"""A class to test user profiles following view"""

def setUp(self) -> None:
super(UserProfileCivis, self).setUp()
super().setUp()

self.user2 = get_user_model().objects.create_user(
username="newuser2", email="test2@test2.com", password="password123"
Expand Down
9 changes: 9 additions & 0 deletions project/core/templates/global_nav.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@
</div>
</div>
</nav>
<div id="messages-container">
{% if messages %}
{% for message in messages %}
<div id="messages" class="{% if message.tags == 'error'%}card card-panel red lighten-1 dark-white-text {% endif %}bold-text">
{{message}}
</div>
{% endfor %}
{% endif %}
</div>
<div class="notifications">
<div id="modal1" class="modal notifications-modal bottom-sheet">
<div class="modal-content">
Expand Down
9 changes: 9 additions & 0 deletions project/core/templates/static/less/base.less
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,15 @@ body {
color: @text !important;
}

#messages-container {
margin-right: 0.5%;
margin-left: 0.5%;
}

.errorlist {
margin: unset;
}

// Custom Input Colors
.white-background-input-field input:focus, .white-background-input-field textarea:focus {
border-bottom: 1px solid @purple !important;
Expand Down
6 changes: 3 additions & 3 deletions project/threads/templates/threads/thread.html
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@
<span class="text">Add Civi</span>
</button>
{% else %}
<a class="btn waves-effect"
<a class="btn waves-effect purple-background"
href="{% url 'accounts_login' %}?next={% url 'thread-detail' thread.id %}">
<i class="material-icons left">note_add</i>
<span class="text">Add Civi</span>
Expand Down Expand Up @@ -626,7 +626,7 @@
<form action="{% url 'civi-create' thread.id %}" method="POST">
{% csrf_token %}
<div class="civi-types">
<button class="btn btn-small waves-effect civi-type-button" value="problem" id="add-problem"
<button class="btn btn-small waves-effect civi-type-button purple-background white-text" value="problem" id="add-problem"
data-type="problem" type="button">
PROBLEM
</button>
Expand Down Expand Up @@ -724,7 +724,7 @@
<div class="white-background-input-field">
<div class="header-lato">Upload Images:</div>
<div class="file-field input-field">
<i class="upload-image btn waves-effect hide waves-input-wrapper" style=""><input
<i class="upload-image btn waves-effect hide waves-input-wrapper"><input
class="waves-button-input" type="submit" value="Upload"></i>
<div class="btn attachment-image-pick dark-blue-background">
<span>Add Images</span>
Expand Down
61 changes: 61 additions & 0 deletions project/threads/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from categories.models import Category
from django.contrib.auth import get_user_model
from django.contrib.messages import get_messages
from django.test import TestCase
from django.urls import reverse
from rest_framework.test import APIClient
Expand Down Expand Up @@ -155,3 +156,63 @@ def test_existing_threads(self):
url = reverse("thread-detail", args=["1"])
response = self.client.get(url)
self.assertEqual(response.status_code, 200)


class CiviViewTests(BaseTestCase):
def setUp(self) -> None:
super().setUp()
self.client.login(username=self.user.username, password="password123")
self.civi = Civi.objects.create(
title="title",
body="body",
c_type="problem",
thread=Thread.objects.get(id=self.thread.id),
author=get_user_model().objects.get(id=self.user.id),
)

def test_create(self):
data = {
"title": "Civi Title",
"body": "Civi Body",
"c_type": "problem",
}
response = self.client.post(
reverse("civi-create", kwargs={"thread_id": self.thread.id}), data=data
)
civi = Civi.objects.get(title="Civi Title")
self.assertRedirects(
response,
expected_url=reverse("thread-detail", kwargs={"pk": self.thread.id}),
status_code=302,
target_status_code=200,
)
self.assertEqual(data["title"], civi.title)

def test_form_invalid(self):
data = {
"title": "Civi Title",
"body": "Civi Body",
}
response = self.client.post(
reverse("civi-create", kwargs={"thread_id": self.thread.id}), data=data
)
messages = list(get_messages(response.wsgi_request))
self.assertIn("error", str(messages[0]))
self.assertRedirects(
response,
expected_url=reverse("thread-detail", kwargs={"pk": self.thread.id}),
status_code=302,
)

def test_delete(self):
civi_id = self.civi.id
response = self.client.post(
reverse("civi-delete", kwargs={"thread_id": self.thread.id, "pk": civi_id})
)
self.assertRedirects(
response,
expected_url=reverse("thread-detail", kwargs={"pk": self.thread.id}),
status_code=302,
target_status_code=200,
)
self.assertFalse(Civi.objects.filter(pk=civi_id).exists())
16 changes: 7 additions & 9 deletions project/threads/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from accounts.utils import get_account
from categories.models import Category
from core.custom_decorators import login_required
from django.http import HttpResponse
from django.contrib import messages
from django.http import HttpResponse, HttpResponseRedirect
from django.template.response import TemplateResponse
from django.urls import reverse_lazy
from django.views.decorators.csrf import csrf_exempt
Expand Down Expand Up @@ -152,14 +153,11 @@ def get_form_kwargs(self):
kwargs["data"] = data
return kwargs

# def form_invalid(self, form):
# try:
# return super().form_invalid(form)
# except:
# print(form.errors)
# return reverse_lazy(
# "thread-detail", kwargs={"pk": self.kwargs.get("thread_id")}
# )
def form_invalid(self, form):
messages.error(self.request, form.errors)
return HttpResponseRedirect(
reverse_lazy("thread-detail", kwargs={"pk": self.kwargs.get("thread_id")})
)

def get_success_url(self):
return reverse_lazy(
Expand Down