summaryrefslogtreecommitdiff
path: root/tests/test_auth.py
blob: 4e28e9379601b1ee11e929494902459de5e56703 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import pytest

from flask import g, session, json, url_for

from sw.db import get_db
from sw.auth import login_required, specific_user_required

from werkzeug.datastructures import Headers


def test_register(client, app):
    response = client.post(
        "/auth/register",
        data=json.dumps({"username": "a", "password": "a",}),
        content_type="application/json",
    )

    # TODO assert "/u/a" in response.headers["Location"]

    with app.app_context():
        db_result = (
            get_db().execute("select * from users where username='a'",).fetchone()
        )
        assert db_result is not None


def test_login_right_credentials(client, auth):
    assert auth.login().status_code == 200

    with client:
        client.get("/")
        assert session["user_id"] == 1
        assert g.user["username"] == "test"


@pytest.mark.parametrize(
    ("username", "password", "message"),
    (
        (None, "", b"Username required"),
        ("", "", b"Username must not be empty"),
        ("a", None, b"Password required"),
        ("a", "", b"Password must not be empty"),
        ("test", "test", b"Username test is taken"),
    ),
)
def test_register_validate_input(client, username, password, message):
    response = client.post(
        "/auth/register",
        data=json.dumps({"username": username, "password": password,}),
        headers={"Content-Type": "application/json"},
    )
    assert message in response.data


def test_login_required_no_user(client, app):
    test_endpoint = "/test/dummy_endpoint"

    @app.route(test_endpoint)
    @login_required
    def dummy_endpoint():
        return "Should be unreachable", 200

    response = client.get(test_endpoint)

    assert response.status_code == 403
    assert b"Should be unreachable" not in response.data


def test_login_required_with_user(client, app, auth):
    test_endpoint = "/test/dummy_endpoint"

    @app.route(test_endpoint)
    @login_required
    def dummy_endpoint():
        return "Should succeed", 200

    auth.login()
    with client:
        response = client.get(test_endpoint)

        assert response.status_code == 200
        assert b"Should succeed" in response.data


def test_specific_user_required_no_user(client, app):
    test_endpoint = "/test/dummy_endpoint/<username>"

    @app.route(test_endpoint)
    @specific_user_required
    def dummy_endpoint(username):
        return "Should be unreachable", 200

    response = client.get("/test/dummy_endpoint/test")

    assert response.status_code == 403
    assert b"Should be unreachable" not in response.data


def test_specific_user_required_wrong_user(client, app, auth):
    test_endpoint = "/test/dummy_endpoint/<username>"

    @app.route(test_endpoint)
    @specific_user_required
    def dummy_endpoint(username):
        return "Should be unreachable", 200

    auth.login()
    with client:
        response = client.get("/test/dummy_endpoint/other")

        assert response.status_code == 403
        assert b"Should be unreachable" not in response.data


def test_specific_user_required_right_user(client, app, auth):
    test_endpoint = "/test/dummy_endpoint/<username>"

    @app.route(test_endpoint)
    @specific_user_required
    def dummy_endpoint(username):
        return "Should succeed", 200

    auth.login()
    with client:
        response = client.get("/test/dummy_endpoint/test")

        assert response.status_code == 200
        assert b"Should succeed" in response.data