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

Feature/login #2

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
67 changes: 60 additions & 7 deletions accounts/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.conf import settings
from django.contrib.auth import SESSION_KEY, get_user_model
from django.test import TestCase
from django.urls import reverse
Expand Down Expand Up @@ -177,18 +178,70 @@ def test_failure_post_with_mismatch_password(self):
self.assertIn("確認用パスワードが一致しません。", form.errors["password2"])


# class TestLoginView(TestCase):
# def test_success_get(self):
class TestLoginView(TestCase):
def setUp(self):
self.url = reverse("accounts:login")

# def test_success_post(self):
def test_success_get(self):
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "registration/login.html")

# def test_failure_post_with_not_exists_user(self):
def test_success_post(self):
User.objects.create_user(username="testuser", password="testpassword")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Userモデルの作成はsetUp関数で行って欲しいです!

valid_data = {"username": "testuser", "password": "testpassword"}
response = self.client.post(self.url, valid_data)
self.assertRedirects(
response,
settings.LOGIN_REDIRECT_URL,
status_code=302,
target_status_code=200,
)
self.assertIn(SESSION_KEY, self.client.session)

# def test_failure_post_with_empty_password(self):
def test_failure_post_with_empty_form(self):
response = self.client.post(self.url, {})
form = response.context["form"]
self.assertEqual(response.status_code, 200)
self.assertFalse(form.is_valid())
self.assertIn("このフィールドは必須です。", form.errors["username"])
self.assertIn("このフィールドは必須です。", form.errors["password"])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここの下にSESSION_KEYがclient.sessionに含まれているかのチェックを入れて欲しいです!


def test_failure_post_with_empty_username(self):
invalid_data = {
"username": "",
"password": "testpassword",
}
response = self.client.post(self.url, invalid_data)
form = response.context["form"]
self.assertEqual(response.status_code, 200)
self.assertFalse(form.is_valid())
self.assertIn("このフィールドは必須です。", form.errors["username"])

# class TestLogoutView(TestCase):
# def test_success_post(self):
def test_failure_post_with_empty_password(self):
invalid_data = {
"username": "testuser",
"password": "",
}
response = self.client.post(self.url, invalid_data)
form = response.context["form"]
self.assertEqual(response.status_code, 200)
self.assertFalse(form.is_valid())
self.assertIn("このフィールドは必須です。", form.errors["password"])


class TestLogoutView(TestCase):
def test_success_post(self):
self.client.login(username="testuser", password="testpassword")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TestLogoutViewにsetUp関数を作成し、Userモデルの作成とログイン処理をそこに移動して欲しいです!

url = reverse("logout")
response = self.client.post(url)
self.assertRedirects(
response,
settings.LOGOUT_REDIRECT_URL,
status_code=302,
target_status_code=200,
)
self.assertNotIn(SESSION_KEY, self.client.session)


# class TestUserProfileView(TestCase):
Expand Down
7 changes: 4 additions & 3 deletions accounts/urls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# from django.contrib.auth import views as auth_views
from django.contrib.auth import views as auth_views
from django.urls import path

from . import views
Expand All @@ -7,9 +8,9 @@

urlpatterns = [
path("signup/", views.SignupView.as_view(), name="signup"),
# path('login/', auth_views.LoginView.as_view(), name='login'),
# path('logout/', auth_views.LogoutView.as_view(), name='logout'),
# path('<str:username>/', views.UserProfileView.as_view(), name='user_profile'),
path("login/", auth_views.LoginView.as_view(), name="login"),
path("logout/", auth_views.LogoutView.as_view(), name="logout"),
path("profile/", views.UserProfileView.as_view(), name="user_profile"),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここのエンドポイントは最終課題テスト項目要件整理に沿って設定してほしいです!

# path('<str:username>/follow/', views.FollowView.as_view(), name='follow'),
# path('<str:username>/unfollow/', views.UnFollowView, name='unfollow'),
# path('<str:username>/following_list/', views.FollowingListView.as_view(), name='following_list'),
Expand Down
7 changes: 6 additions & 1 deletion accounts/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# from django.shortcuts import render
from django.contrib.auth import authenticate, login
from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse_lazy
from django.views.generic import CreateView
from django.views.generic import CreateView, TemplateView

from .forms import SignupForm

Expand All @@ -18,3 +19,7 @@ def form_valid(self, form):
user = authenticate(self.request, username=username, password=password)
login(self.request, user)
return response


class UserProfileView(LoginRequiredMixin, TemplateView):
template_name = "accounts/user_profile.html"
6 changes: 3 additions & 3 deletions mysite/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

LOGIN_URL = '/login'
LOGIN_URL = "/accounts/login/"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOGIN_URL, LOGIN_REDIRECT_URL, LOGOUT_REDIRECT_URLは/〇〇/〇〇の形ではなく、〇〇:〇〇にして欲しいです!


LOGIN_REDIRECT_URL = '/tweets/home'
LOGIN_REDIRECT_URL = "/accounts/profile/"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここはtweets:homeにした方がいいと思います!


LOGOUT_REDIRECT_URL = '/login'
LOGOUT_REDIRECT_URL = "/accounts/login/"
1 change: 1 addition & 0 deletions mysite/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@
path("accounts/", include("accounts.urls")),
path("tweets/", include("tweets.urls")),
path("", include("welcome.urls")),
path("accounts/", include("django.contrib.auth.urls")), # 追加 logoutを使う場合
]
2 changes: 1 addition & 1 deletion templates/accounts/signup.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{{ form.as_p }} <!--← formにはSignupFormのインスタンスが入っている。as_pとすることで、各input要素がpタグで囲まれた状態で表示される。-->
{% csrf_token %} <!--← csrf_token必須。formタグ内であればどこに書いてもOK -->
<button type="submit">ユーザー登録</button>
</form>
</form>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここのインデントはいらないと思います!


{% endblock %}

6 changes: 6 additions & 0 deletions templates/accounts/user_profile.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% extends 'base.html' %}

{% block content %}
<h1>ユーザープロフィール</h1>
<p>ユーザー名: {{ user.username }}</p>
{% endblock %}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
{% extends "base.html" %}

{% block title %}Login{% endblock %}

{% block content %}
<h2>Login</h2>
<form method="post">
{% csrf_token %}
{{ form }}
<button type="submit">ログイン</button>
{{ form.as_p }}
<button type="submit">Login</button>
</form>
{% endblock %}
4 changes: 4 additions & 0 deletions templates/tweets/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
{% block title %}Home{% endblock %} ← titleを入れる

{% block content %}
<form method="post" action="{% url 'accounts:logout' %}">
{% csrf_token %}
<button type="submit">Logout</button>
</form>
<h1>Homeです</h1>

{% endblock %}
Expand Down
Loading