Fix API to allow returning user list with the master user

This commit is contained in:
Inex Code 2022-05-12 18:55:57 +03:00
parent 36bf1a80bf
commit 1ac5f72433
6 changed files with 34 additions and 5 deletions

View file

@ -68,7 +68,7 @@ def create_app(test_config=None):
def spec():
if app.config["ENABLE_SWAGGER"] == "1":
swag = swagger(app)
swag["info"]["version"] = "1.2.4"
swag["info"]["version"] = "1.2.5"
swag["info"]["title"] = "SelfPrivacy API"
swag["info"]["description"] = "SelfPrivacy API"
swag["securityDefinitions"] = {

View file

@ -64,7 +64,7 @@ class Tokens(Resource):
schema:
type: object
properties:
token:
token_name:
type: string
description: Token name to delete
required: true

View file

@ -23,4 +23,4 @@ class ApiVersion(Resource):
401:
description: Unauthorized
"""
return {"version": "1.2.4"}
return {"version": "1.2.5"}

View file

@ -24,8 +24,15 @@ class Users(Resource):
401:
description: Unauthorized
"""
parser = reqparse.RequestParser(bundle_errors=True)
parser.add_argument("withMainUser", type=bool, required=False)
args = parser.parse_args()
with_main_user = False if args["withMainUser"] is None else args["withMainUser"]
with ReadUserData() as data:
users = []
if with_main_user:
users.append(data["username"])
if "users" in data:
for user in data["users"]:
users.append(user["username"])
@ -96,7 +103,10 @@ class Users(Resource):
if "users" not in data:
data["users"] = []
# Return 400 if user already exists
# Return 409 if user already exists
if data["username"] == args["username"]:
return {"error": "User already exists"}, 409
for user in data["users"]:
if user["username"] == args["username"]:
return {"error": "User already exists"}, 409

View file

@ -2,7 +2,7 @@ from setuptools import setup, find_packages
setup(
name="selfprivacy_api",
version="1.2.3",
version="1.2.5",
packages=find_packages(),
scripts=[
"selfprivacy_api/app.py",

View file

@ -130,12 +130,24 @@ def test_get_one_user(authorized_client, one_user, mock_subprocess_popen):
assert response.json == ["user1"]
def test_get_one_user_with_main(authorized_client, one_user, mock_subprocess_popen):
response = authorized_client.get("/users?withMainUser=true")
assert response.status_code == 200
assert response.json == ["tester", "user1"]
def test_get_no_users(authorized_client, no_users, mock_subprocess_popen):
response = authorized_client.get("/users")
assert response.status_code == 200
assert response.json == []
def test_get_no_users_with_main(authorized_client, no_users, mock_subprocess_popen):
response = authorized_client.get("/users?withMainUser=true")
assert response.status_code == 200
assert response.json == ["tester"]
def test_get_undefined_users(
authorized_client, undefined_settings, mock_subprocess_popen
):
@ -199,6 +211,13 @@ def test_post_existing_user(authorized_client, one_user, mock_subprocess_popen):
assert response.status_code == 409
def test_post_existing_main_user(authorized_client, one_user, mock_subprocess_popen):
response = authorized_client.post(
"/users", json={"username": "tester", "password": "password"}
)
assert response.status_code == 409
def test_post_user_to_undefined_users(
authorized_client, undefined_settings, mock_subprocess_popen
):