tests.test_auth
1import pytest 2from hanabiapp.models.user import User 3from flask_login import current_user 4 5INVALID_MESSAGE = 'Invalid username or password' 6 7def test_valid_login(client, auth): 8 """ 9 正しいユーザー情報でログインできることをテストする関数. 10 11 - `/login` ページが正常に取得できることを確認. 12 - `auth.login()` を実行し,ログイン後に `/home` へリダイレクトされることを確認. 13 - `/home` にアクセスした際,認証済みユーザーであることを確認. 14 """ 15 assert client.get('/login').status_code == 200 16 response = auth.login() 17 assert response.headers['Location'] == '/home' 18 with client: 19 response = client.get('/home', follow_redirects=True) 20 assert response.status_code == 200 21 assert current_user.is_authenticated 22 assert current_user.username == 'test-user' 23 24@pytest.mark.parametrize(('username', 'password', 'message'), [ 25 ('wrong-user', 'test-password', INVALID_MESSAGE), 26 ('test-user', 'wrong-password', INVALID_MESSAGE), 27 ('', 'test-password', INVALID_MESSAGE), 28 ('test-user', '', INVALID_MESSAGE), 29 ('', '', INVALID_MESSAGE) 30]) 31def test_invalid_login(auth, username, password, message): 32 """ 33 無効なユーザー情報でログインした際にエラーメッセージが表示されることをテストする関数. 34 35 Args: 36 auth (AuthActions): 認証処理を管理するテスト用オブジェクト 37 username (str): テストするユーザー名 38 password (str): テストするパスワード 39 message (str): 期待するエラーメッセージ 40 """ 41 response = auth.login(username, password) 42 assert response.status_code == 401 43 assert message in response.data.decode('utf-8') 44 45def test_authenticated_user_login(client, auth): 46 """ 47 すでにログイン済みのユーザーが `/login` にアクセスした際に `/home` にリダイレクトされることをテストする関数. 48 """ 49 auth.login() 50 response = client.get('/login') 51 assert response.status_code == 302 52 assert response.headers['Location'] == '/home' 53 54def test_logout(client, auth): 55 """ 56 ユーザーが正常にログアウトできることをテストする関数. 57 58 - ログイン後,ログアウトを実行し,ユーザーが認証解除されることを確認. 59 """ 60 auth.login() 61 with client: 62 auth.logout() 63 assert not current_user.is_authenticated 64 65def test_logout_without_login(client): 66 """ 67 未ログイン状態で `/logout` にアクセスした場合に `/login` にリダイレクトされることをテストする関数. 68 """ 69 response = client.get('/logout') 70 assert response.status_code == 302 71 assert response.headers['Location'] == '/login' 72 73@pytest.mark.parametrize('endpoint', [ 74 '/home', 75 '/consent' 76]) 77def test_login_reqired_page(client, auth, endpoint): 78 """ 79 認証が必要なページに未ログイン状態でアクセスした際に `/login` にリダイレクトされることをテストする関数. 80 81 - ログインなしで `endpoint` にアクセスすると `/login` にリダイレクトされることを確認. 82 - ログイン後に `endpoint` へアクセスすると正常に表示されることを確認. 83 84 Args: 85 client (FlaskClient): テスト用のクライアント 86 auth (AuthActions): 認証処理を管理するテスト用オブジェクト 87 endpoint (str): テスト対象のエンドポイント 88 """ 89 response = client.get(endpoint) 90 assert response.status_code == 302 91 assert response.headers['Location'].startswith('/login') 92 assert 'next=' in response.headers['Location'] 93 94 auth.login() 95 response = client.get(endpoint) 96 assert response.status_code == 200
INVALID_MESSAGE =
'Invalid username or password'
def
test_valid_login(client, auth):
8def test_valid_login(client, auth): 9 """ 10 正しいユーザー情報でログインできることをテストする関数. 11 12 - `/login` ページが正常に取得できることを確認. 13 - `auth.login()` を実行し,ログイン後に `/home` へリダイレクトされることを確認. 14 - `/home` にアクセスした際,認証済みユーザーであることを確認. 15 """ 16 assert client.get('/login').status_code == 200 17 response = auth.login() 18 assert response.headers['Location'] == '/home' 19 with client: 20 response = client.get('/home', follow_redirects=True) 21 assert response.status_code == 200 22 assert current_user.is_authenticated 23 assert current_user.username == 'test-user'
正しいユーザー情報でログインできることをテストする関数.
/login
ページが正常に取得できることを確認.auth.login()
を実行し,ログイン後に/home
へリダイレクトされることを確認./home
にアクセスした際,認証済みユーザーであることを確認.
@pytest.mark.parametrize(('username', 'password', 'message'), [('wrong-user', 'test-password', INVALID_MESSAGE), ('test-user', 'wrong-password', INVALID_MESSAGE), ('', 'test-password', INVALID_MESSAGE), ('test-user', '', INVALID_MESSAGE), ('', '', INVALID_MESSAGE)])
def
test_invalid_login(auth, username, password, message):
25@pytest.mark.parametrize(('username', 'password', 'message'), [ 26 ('wrong-user', 'test-password', INVALID_MESSAGE), 27 ('test-user', 'wrong-password', INVALID_MESSAGE), 28 ('', 'test-password', INVALID_MESSAGE), 29 ('test-user', '', INVALID_MESSAGE), 30 ('', '', INVALID_MESSAGE) 31]) 32def test_invalid_login(auth, username, password, message): 33 """ 34 無効なユーザー情報でログインした際にエラーメッセージが表示されることをテストする関数. 35 36 Args: 37 auth (AuthActions): 認証処理を管理するテスト用オブジェクト 38 username (str): テストするユーザー名 39 password (str): テストするパスワード 40 message (str): 期待するエラーメッセージ 41 """ 42 response = auth.login(username, password) 43 assert response.status_code == 401 44 assert message in response.data.decode('utf-8')
無効なユーザー情報でログインした際にエラーメッセージが表示されることをテストする関数.
Arguments:
- auth (AuthActions): 認証処理を管理するテスト用オブジェクト
- username (str): テストするユーザー名
- password (str): テストするパスワード
- message (str): 期待するエラーメッセージ
def
test_authenticated_user_login(client, auth):
46def test_authenticated_user_login(client, auth): 47 """ 48 すでにログイン済みのユーザーが `/login` にアクセスした際に `/home` にリダイレクトされることをテストする関数. 49 """ 50 auth.login() 51 response = client.get('/login') 52 assert response.status_code == 302 53 assert response.headers['Location'] == '/home'
すでにログイン済みのユーザーが /login
にアクセスした際に /home
にリダイレクトされることをテストする関数.
def
test_logout(client, auth):
55def test_logout(client, auth): 56 """ 57 ユーザーが正常にログアウトできることをテストする関数. 58 59 - ログイン後,ログアウトを実行し,ユーザーが認証解除されることを確認. 60 """ 61 auth.login() 62 with client: 63 auth.logout() 64 assert not current_user.is_authenticated
ユーザーが正常にログアウトできることをテストする関数.
- ログイン後,ログアウトを実行し,ユーザーが認証解除されることを確認.
def
test_logout_without_login(client):
66def test_logout_without_login(client): 67 """ 68 未ログイン状態で `/logout` にアクセスした場合に `/login` にリダイレクトされることをテストする関数. 69 """ 70 response = client.get('/logout') 71 assert response.status_code == 302 72 assert response.headers['Location'] == '/login'
未ログイン状態で /logout
にアクセスした場合に /login
にリダイレクトされることをテストする関数.
@pytest.mark.parametrize('endpoint', ['/home', '/consent'])
def
test_login_reqired_page(client, auth, endpoint):
74@pytest.mark.parametrize('endpoint', [ 75 '/home', 76 '/consent' 77]) 78def test_login_reqired_page(client, auth, endpoint): 79 """ 80 認証が必要なページに未ログイン状態でアクセスした際に `/login` にリダイレクトされることをテストする関数. 81 82 - ログインなしで `endpoint` にアクセスすると `/login` にリダイレクトされることを確認. 83 - ログイン後に `endpoint` へアクセスすると正常に表示されることを確認. 84 85 Args: 86 client (FlaskClient): テスト用のクライアント 87 auth (AuthActions): 認証処理を管理するテスト用オブジェクト 88 endpoint (str): テスト対象のエンドポイント 89 """ 90 response = client.get(endpoint) 91 assert response.status_code == 302 92 assert response.headers['Location'].startswith('/login') 93 assert 'next=' in response.headers['Location'] 94 95 auth.login() 96 response = client.get(endpoint) 97 assert response.status_code == 200
認証が必要なページに未ログイン状態でアクセスした際に /login
にリダイレクトされることをテストする関数.
- ログインなしで
endpoint
にアクセスすると/login
にリダイレクトされることを確認. - ログイン後に
endpoint
へアクセスすると正常に表示されることを確認.
Arguments:
- client (FlaskClient): テスト用のクライアント
- auth (AuthActions): 認証処理を管理するテスト用オブジェクト
- endpoint (str): テスト対象のエンドポイント