diff options
| author | Mitsuo Tokumori <[email protected]> | 2023-10-29 06:59:37 -0500 |
|---|---|---|
| committer | Mitsuo Tokumori <[email protected]> | 2023-10-29 06:59:37 -0500 |
| commit | 9baa55436bc5a98d118a17656bbf25e563522964 (patch) | |
| tree | daa01118bd11f5cf13d386d2c727a384563df1da /tests/test_auth.py | |
| parent | 71e7d6516608486f67afad5aad1f7b7f9a45886f (diff) | |
| download | ustayml-9baa55436bc5a98d118a17656bbf25e563522964.tar.gz ustayml-9baa55436bc5a98d118a17656bbf25e563522964.tar.bz2 ustayml-9baa55436bc5a98d118a17656bbf25e563522964.zip | |
Add unit tests
Diffstat (limited to 'tests/test_auth.py')
| -rw-r--r-- | tests/test_auth.py | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/tests/test_auth.py b/tests/test_auth.py new file mode 100644 index 0000000..401b61f --- /dev/null +++ b/tests/test_auth.py @@ -0,0 +1,62 @@ +import pytest +from flask import g, session +from flaskr.db import get_db + + +def test_register(client, app): + assert client.get('/auth/register').status_code == 200 + response = client.post( + '/auth/register', data={'username': 'a', 'password': 'a'} + ) + assert response.headers["Location"] == "/auth/login" + + with app.app_context(): + assert get_db().execute( + "SELECT * FROM user WHERE username = 'a'", + ).fetchone() is not None + + [email protected](('username', 'password', 'message'), ( + ('', '', b'Username is required.'), + ('a', '', b'Password is required.'), + ('test', 'test', b'already registered'), +)) +def test_register_validate_input(client, username, password, message): + response = client.post( + '/auth/register', + data={'username': username, 'password': password} + ) + assert message in response.data + + +def test_login(client, auth): + assert client.get('/auth/login').status_code == 200 + response = auth.login() + assert response.headers["Location"] == "/" + + with client: + """ + Using client in a with block allows accessing context variables such as + session after the response is returned. Normally, accessing session + outside of a request would raise an error. + """ + client.get('/') + assert session['user_id'] == 1 + assert g.user['username'] == 'test' + + [email protected](('username', 'password', 'message'), ( + ('a', 'test', b'Incorrect username.'), + ('test', 'a', b'Incorrect password.'), +)) +def test_login_validate_input(auth, username, password, message): + response = auth.login(username, password) + assert message in response.data + + +def test_logout(client, auth): + auth.login() + + with client: + auth.logout() + assert 'user_id' not in session |
