6
6
from tornado .httpclient import HTTPError
7
7
8
8
from jupyter_server .auth import Authorizer , IdentityProvider , User
9
+ from jupyter_server .auth .identity import PasswordIdentityProvider
9
10
10
11
11
12
async def test_get_spec (jp_fetch ):
@@ -50,6 +51,22 @@ async def get_user(self, handler):
50
51
return self .mock_user
51
52
52
53
54
+ class MockPasswordIdentityProvider (PasswordIdentityProvider ):
55
+ mock_user : MockUser
56
+
57
+ async def get_user (self , handler ):
58
+ # super returns a UUID
59
+ # return our mock user instead, as long as the request is authorized
60
+ _authenticated = super ().get_user (handler )
61
+ if isinstance (_authenticated , Awaitable ):
62
+ _authenticated = await _authenticated
63
+ authenticated = _authenticated
64
+ if isinstance (self .mock_user , dict ):
65
+ self .mock_user = MockUser (** self .mock_user )
66
+ if authenticated :
67
+ return self .mock_user
68
+
69
+
53
70
class MockAuthorizer (Authorizer ):
54
71
def is_authorized (self , handler , user , action , resource ):
55
72
permissions = user .permissions
@@ -70,6 +87,17 @@ def identity_provider(jp_serverapp):
70
87
yield idp
71
88
72
89
90
+ @pytest .fixture
91
+ def password_identity_provider (jp_serverapp ):
92
+ idp = MockPasswordIdentityProvider (parent = jp_serverapp )
93
+ authorizer = MockAuthorizer (parent = jp_serverapp )
94
+ with mock .patch .dict (
95
+ jp_serverapp .web_app .settings ,
96
+ {"identity_provider" : idp , "authorizer" : authorizer },
97
+ ):
98
+ yield idp
99
+
100
+
73
101
@pytest .mark .parametrize (
74
102
"identity, expected" ,
75
103
[
@@ -118,9 +146,44 @@ async def test_identity(jp_fetch, identity, expected, identity_provider):
118
146
119
147
120
148
@pytest .mark .parametrize ("identity" , [{"username" : "user.username" }])
121
- async def test_update_user_success (jp_fetch , identity , identity_provider ):
149
+ async def test_update_user_not_implemented_update (jp_fetch , identity , identity_provider ):
150
+ """Test successful user update."""
151
+ identity_provider .mock_user = MockUser (** identity )
152
+ payload = {
153
+ "color" : "#000000" ,
154
+ }
155
+ with pytest .raises (HTTPError ) as exc :
156
+ await jp_fetch (
157
+ "/api/me" ,
158
+ method = "PATCH" ,
159
+ body = json .dumps (payload ),
160
+ headers = {"Content-Type" : "application/json" },
161
+ )
162
+ assert exc .value .code == 501
163
+
164
+
165
+ @pytest .mark .parametrize ("identity" , [{"username" : "user.username" }])
166
+ async def test_update_user_not_implemented_persist (jp_fetch , identity , identity_provider ):
122
167
"""Test successful user update."""
123
168
identity_provider .mock_user = MockUser (** identity )
169
+ identity_provider .update_user_model = lambda * args , ** kwargs : identity_provider .mock_user
170
+ payload = {
171
+ "color" : "#000000" ,
172
+ }
173
+ with pytest .raises (HTTPError ) as exc :
174
+ await jp_fetch (
175
+ "/api/me" ,
176
+ method = "PATCH" ,
177
+ body = json .dumps (payload ),
178
+ headers = {"Content-Type" : "application/json" },
179
+ )
180
+ assert exc .value .code == 501
181
+
182
+
183
+ @pytest .mark .parametrize ("identity" , [{"username" : "user.username" }])
184
+ async def test_update_user_success (jp_fetch , identity , password_identity_provider ):
185
+ """Test successful user update."""
186
+ password_identity_provider .mock_user = MockUser (** identity )
124
187
payload = {
125
188
"color" : "#000000" ,
126
189
}
@@ -137,12 +200,12 @@ async def test_update_user_success(jp_fetch, identity, identity_provider):
137
200
138
201
139
202
@pytest .mark .parametrize ("identity" , [{"username" : "user.username" }])
140
- async def test_update_user_raise (jp_fetch , identity , identity_provider ):
203
+ async def test_update_user_raise (jp_fetch , identity , password_identity_provider ):
141
204
"""Test failing user update."""
142
- identity_provider .mock_user = MockUser (** identity )
205
+ password_identity_provider .mock_user = MockUser (** identity )
143
206
payload = {
144
207
"name" : "Updated Name" ,
145
- "color " : "#000000 " ,
208
+ "fake_prop " : "anything " ,
146
209
}
147
210
with pytest .raises (HTTPError ) as exc :
148
211
await jp_fetch (
@@ -151,6 +214,7 @@ async def test_update_user_raise(jp_fetch, identity, identity_provider):
151
214
body = json .dumps (payload ),
152
215
headers = {"Content-Type" : "application/json" },
153
216
)
217
+ assert exc .value .code == 400
154
218
155
219
156
220
@pytest .mark .parametrize (
@@ -168,10 +232,10 @@ async def test_update_user_raise(jp_fetch, identity, identity_provider):
168
232
],
169
233
)
170
234
async def test_update_user_success_custom_updatable_fields (
171
- jp_fetch , identity , expected , identity_provider
235
+ jp_fetch , identity , expected , password_identity_provider
172
236
):
173
237
"""Test successful user update."""
174
- identity_provider .mock_user = MockUser (** identity )
238
+ password_identity_provider .mock_user = MockUser (** identity )
175
239
identity_provider .updatable_fields = ["name" , "display_name" , "color" ]
176
240
payload = {
177
241
"name" : expected ["name" ],
0 commit comments