-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
@@ -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") | ||
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"]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||
|
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 | ||
|
@@ -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"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -127,8 +127,8 @@ | |
|
||
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" | ||
|
||
LOGIN_URL = '/login' | ||
LOGIN_URL = "/accounts/login/" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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/" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ここはtweets:homeにした方がいいと思います! |
||
|
||
LOGOUT_REDIRECT_URL = '/login' | ||
LOGOUT_REDIRECT_URL = "/accounts/login/" |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ここのインデントはいらないと思います! |
||
|
||
{% endblock %} | ||
|
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 %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Userモデルの作成はsetUp関数で行って欲しいです!