summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--sw/auth.py13
-rw-r--r--sw/users.py4
-rw-r--r--tests/conftest.py6
-rw-r--r--tests/test_auth.py92
4 files changed, 106 insertions, 9 deletions
diff --git a/sw/auth.py b/sw/auth.py
index e1173c8..2f03a04 100644
--- a/sw/auth.py
+++ b/sw/auth.py
@@ -1,6 +1,9 @@
import functools
+import logging
+
from flask import (
+ abort,
Blueprint,
flash,
g,
@@ -99,20 +102,20 @@ def login():
session.clear()
session["user_id"] = user["id"]
- return 200
+ return "Login Successful", 200
@bp.route("/logout")
def logout():
session.clear()
- return 200
+ return "Logout Successful", 200
def login_required(view):
@functools.wraps(view)
def wrapped_view(**kwargs):
if g.user is None:
- abort("Login required", 403)
+ abort(403, "Login required")
return view(**kwargs)
@@ -123,8 +126,8 @@ def specific_user_required(view):
@functools.wraps(view)
def wrapped_view(**kwargs):
username = kwargs.get("username") or kwargs.get("user")
- if g.user != username:
- abort("Unauthorised", 403)
+ if (g.user is None) or (g.user["username"] != username):
+ abort(403, "Unauthorised")
return view(**kwargs)
diff --git a/sw/users.py b/sw/users.py
index 724965e..dffae93 100644
--- a/sw/users.py
+++ b/sw/users.py
@@ -1,9 +1,10 @@
-from flask import Blueprint, jsonify, url_for, request
+from flask import Blueprint, jsonify, url_for, request, abort
import json
from sw.db import get_db
from sw.time import now
+from sw.auth import specific_user_required
bp = Blueprint("users", __name__, url_prefix="/u")
@@ -36,6 +37,7 @@ def get_user_info(username):
@bp.route("/<username>/inbox")
+@specific_user_required
def get_user_inbox(username):
db = get_db()
diff --git a/tests/conftest.py b/tests/conftest.py
index fb8c751..c18f9de 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -1,7 +1,9 @@
import os
import tempfile
+import json
import pytest
+
from sw import create_app
from sw.db import get_db, init_db
@@ -41,7 +43,9 @@ class AuthActions(object):
def login(self, username="test", password="test"):
return self._client.post(
- "/auth/login", data=jsonify({"username": username, "password": password,})
+ "/auth/login",
+ data=json.dumps({"username": username, "password": password,}),
+ headers={"content-type": "application/json"},
)
def logout(self):
diff --git a/tests/test_auth.py b/tests/test_auth.py
index 067746d..4e28e93 100644
--- a/tests/test_auth.py
+++ b/tests/test_auth.py
@@ -1,8 +1,11 @@
import pytest
-from flask import g, session, json
+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):
@@ -21,6 +24,15 @@ def test_register(client, app):
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"),
(
@@ -35,6 +47,82 @@ def test_register_validate_input(client, username, password, message):
response = client.post(
"/auth/register",
data=json.dumps({"username": username, "password": password,}),
- content_type="application/json",
+ 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