Skip to content

Commit

Permalink
Add login and log-out functionallity.
Browse files Browse the repository at this point in the history
Adding a landing page that is a login page to the Hotails website.
In order for a user to be able to access the site,
he must be a user and enter his details on this page.
After sucsessful log-in the application recognize the user with all of his details.
  • Loading branch information
tamirmatok committed Apr 20, 2022
1 parent f93a328 commit 89a6169
Show file tree
Hide file tree
Showing 12 changed files with 36,989 additions and 22 deletions.
4 changes: 4 additions & 0 deletions Hotails/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
'review.apps.ReviewConfig',
'orders.apps.OrdersConfig',
'message.apps.MessageConfig',
'crispy_forms',
]

MIDDLEWARE = [
Expand Down Expand Up @@ -121,7 +122,10 @@
BASE_DIR / "static",
]


# Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
LOGIN_REDIRECT_URL = 'index'
LOGIN_URL = 'login'
13 changes: 9 additions & 4 deletions Hotails/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include

from django.urls import path
from main import views
from django.contrib.auth import views as auth_views

urlpatterns = [
path('admin/', admin.site.urls),
path('', include('main.urls')),

path('', views.index, name='index'),
path('login/', auth_views.LoginView.as_view(template_name='main/login.html',
redirect_authenticated_user=True), name='login'),
path('homepage/', views.homepage, name='homepage'),
path('logout/', views.logout_view, name='logout'),
path('about/', views.about, name='about'),
]
14 changes: 9 additions & 5 deletions main/templates/main/base_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@
aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
</button>
<ul class="navbar-nav mr-auto">
<li class="nav-item"><a class="nav-link text-white" href="">Profile</a></li>
<li class="nav-item"><a class="nav-link text-white" href="">Orders</a></li>
<li class="nav-item"><a class="nav-link text-white" href="">Search</a></li>
<li class="nav-item"><a class="nav-link text-white" href="">Chats</a></li>
{% if request.user.is_authenticated %}
<li class="nav-item"><a class="nav-link text-white" href="">Profile</a></li>
<li class="nav-item"><a class="nav-link text-white" href="">Orders</a></li>
<li class="nav-item"><a class="nav-link text-white" href="">Search</a></li>
<li class="nav-item"><a class="nav-link text-white" href="">Chats</a></li>
{% endif %}
<li class="nav-item"><a class="nav-link text-white" href="/about">About</a></li>
</ul>
{% if request.user.is_authenticated %}
<ul class="nav navbar-nav navbar-right">
<li class="nav-item"><a class="nav-link text-white" style="font-size:1.3rem" href="#">Logout</a></li>
<li class="nav-item"><a class="nav-link text-white" style="font-size:1.3rem" href="/logout">Logout</a></li>
</ul>
{% endif %}
</nav>
</header>

Expand Down
38 changes: 38 additions & 0 deletions main/templates/main/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{% extends "main/base_template.html" %}
{% load crispy_forms_tags %}
{% load static %}

{% block stylesheets %}
<!-- Bootstrap core CSS -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="{% static 'css/login.css' %}">
<title>Hotails.com - Login!</title>
{% endblock %}

{% block content %}

<section>
<div class="text-box">
<h1>Login now!</h1>
<div class="card" style="color: rgb(0, 0, 0); background: rgba(252, 252, 252, 0.3); width: 500px;">
<div class="card-body">
<form method="POST">
{% csrf_token %}
<fieldset class="form-group">
{{form|crispy}}
</fieldset>
<div class="form-group">
<button class="btn btn-primary mt-3" type="submit">Lets Go!</button>
</div>
</form>
</div>
</div>
</div>
</section>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-p34f1UUtsS3wqzfto5wAAmdvj+osOnFyQFpp4Ua3gs/ZVWx6oOypYoCJhGGScy+8"
crossorigin="anonymous"></script>

{% endblock %}
81 changes: 78 additions & 3 deletions main/tests.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,89 @@
import pytest
from dogowner.models import DogOwner


@pytest.fixture
def create_dog_owner_user():
return DogOwner.create(email='[email protected]',
username='dogOwnerUser01',
password='password123',
dog_name='dog name',
first_name='test',
last_name='user',
phone_number=1234567890,
dog_race='dog race',
dog_picture_url='https://www.google.com/user1.jpg',
dog_age=4,
dog_weight=2,
dog_gender='M'
)


@pytest.mark.django_db
class TestViews:
class TestAboutView:
def test_redirected_to_about_section(self, client):
response = client.get('/about')
assert response.status_code == 301
response = client.get(response.url)
assert response.status_code == 200

def test_redirected_to_homepage(self, client):
response = client.get('')

@pytest.mark.django_db
class TestLoginView:
def test_enter_login_page(self, client):
response = client.get('/login/')
assert response.status_code == 200

def test_valid_login_dog_owner_user_info(self, client, create_dog_owner_user):
previous_logged_user = client.get('/').wsgi_request.user
form = {'username': 'dogOwnerUser01',
'password': 'password123',
}

response = client.post('/login/', form, follow=True)
current_log_user = response.wsgi_request.user
assert current_log_user == create_dog_owner_user.user
assert previous_logged_user != current_log_user

def test_invalid_login_dog_owner_user_info(self, client):
form = {'username': "[email protected]",
'password': "incorrect",
}
response = client.post('/login/', form, follow=True)
assert response.wsgi_request.user.is_anonymous

def test_block_logged_user_from_login_page(self, client, create_dog_owner_user):
client.force_login(user=create_dog_owner_user.user)
response = client.get("/login/")
assert response['Location'] == '/'


@pytest.mark.django_db
class TestLogoutView:
def test_successful_logout(self, client, create_dog_owner_user):
client.force_login(user=create_dog_owner_user.user)
logged_user = client.get('/').wsgi_request.user
response = client.get('/logout/')
assert response['Location'] == '/login/'
assert response.wsgi_request.user != logged_user


@pytest.mark.django_db
class TestIndexView:
def test_root_entrypoint_redirection_unlogged_user(self, client):
response = client.get("/")
assert response.status_code == 302
assert response['Location'] == '/login/'

def test_root_entrypoint_redirection_logged_user(self, client, create_dog_owner_user):
client.force_login(user=create_dog_owner_user.user)
response = client.get("/")
assert response.status_code == 302
assert response['Location'] == '/homepage/'


class TestHomepageView:
def test_unlogged_user_access_to_homepage(self, client):
response = client.get("/homepage/")
assert response.status_code == 302
assert response['Location'] == '/login/?next=/homepage/'
7 changes: 0 additions & 7 deletions main/urls.py

This file was deleted.

16 changes: 16 additions & 0 deletions main/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from django.shortcuts import redirect
from django.contrib.auth import logout


def index(request):
if request.user.is_authenticated:
return redirect(to='homepage')

return redirect(to='login')


@login_required()
def homepage(request):
return render(request, 'main/homepage.html')


def about(request):
return render(request, 'main/about.html')


def logout_view(request):
logout(request)
return index(request)
Loading

0 comments on commit 89a6169

Please sign in to comment.