annotate NewTests/Authorization/auth_service.py @ 576:80ba6f1d521c

new tests for authorization plugin (native only)
author Alain Mazy <am@osimis.io>
date Wed, 06 Sep 2023 17:04:36 +0200
parents
children 0649a19df194
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
576
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
1 from fastapi import FastAPI
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
2 import logging
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
3 from models import *
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
4 import pprint
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
5
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
6 # Sample Authorization service that is started when the test starts.
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
7 # It does not check token validity and simply implements a set of basic users
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
8
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
9 app = FastAPI()
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
10
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
11
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
12
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
13 @app.post("/user/get-profile")
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
14 def get_user_profile(user_profile_request: UserProfileRequest):
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
15 logging.info("get user profile: " + user_profile_request.json())
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
16
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
17 if user_profile_request.token_key == "user-token-key":
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
18 if user_profile_request.token_value == "token-uploader":
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
19 p = UserProfileResponse(
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
20 name="uploader",
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
21 permissions=["upload", "edit-labels", "delete", "view"],
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
22 authorized_labels=["*"],
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
23 validity=60
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
24 )
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
25 return p
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
26 elif user_profile_request.token_value == "token-admin":
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
27 p = UserProfileResponse(
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
28 name="admin",
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
29 permissions=["all"],
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
30 authorized_labels=["*"],
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
31 validity=60
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
32 )
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
33 return p
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
34 elif user_profile_request.token_value == "token-user-a":
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
35 p = UserProfileResponse(
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
36 name="user-a",
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
37 permissions=["view"],
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
38 authorized_labels=["label_a"],
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
39 validity=60
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
40 )
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
41 return p
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
42
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents:
diff changeset
43