@@ -263,6 +263,25 @@ def post(url, data, headers, files, allow_redirects, auth=None):
263263
264264 Configuration .read ().remoteckan ().session = MockSession ()
265265
266+ @pytest .fixture (scope = "function" )
267+ def show_current_user (self ):
268+ class MockSession :
269+ @staticmethod
270+ def post (url , data , headers , files , allow_redirects , auth = None ):
271+ if "show" not in url :
272+ return MockResponse (
273+ 404 ,
274+ '{"success": false, "error": {"message": "TEST ERROR: Not show", "__type": "TEST ERROR: Not Show Error"}, "help": "http://test-data.humdata.org/api/3/action/help_show?name=user_show"}' ,
275+ )
276+ result = json .dumps (resultdict )
277+ return MockResponse (
278+ 200 ,
279+ '{"success": true, "result": %s, "help": "http://test-data.humdata.org/api/3/action/help_show?name=user_show"}'
280+ % result ,
281+ )
282+
283+ Configuration .read ().remoteckan ().session = MockSession ()
284+
266285 @pytest .fixture (scope = "function" )
267286 def post_list (self ):
268287 class MockSession :
@@ -304,6 +323,42 @@ def post(url, data, headers, files, allow_redirects, auth=None):
304323
305324 Configuration .read ().remoteckan ().session = MockSession ()
306325
326+ @pytest .fixture (scope = "function" )
327+ def post_check_current_user_write_access (self ):
328+ class MockSession :
329+ @staticmethod
330+ def post (url , data , headers , files , allow_redirects , auth = None ):
331+ decodedata = data .decode ("utf-8" )
332+ datadict = json .loads (decodedata )
333+ if "user" in url :
334+ if "show" in url :
335+ result = json .dumps (resultdict )
336+ return MockResponse (
337+ 200 ,
338+ '{"success": true, "result": %s, "help": "http://test-data.humdata.org/api/3/action/help_show?name=user_show"}'
339+ % result ,
340+ )
341+ elif "list" in url :
342+ return MockResponse (
343+ 200 ,
344+ '{"success": true, "result": %s, "help": "http://test-data.humdata.org/api/3/action/help_show?name=organization_list"}'
345+ % json .dumps (orglist ),
346+ )
347+ elif "organization" in url :
348+ if "show" in url :
349+ result = json .dumps (orgdict )
350+ if (
351+ datadict ["id" ] == "b67e6c74-c185-4f43-b561-0e114a736f19"
352+ or datadict ["id" ] == "TEST1"
353+ ):
354+ return MockResponse (
355+ 200 ,
356+ '{"success": true, "result": %s, "help": "http://test-data.humdata.org/api/3/action/help_show?name=user_show"}'
357+ % result ,
358+ )
359+
360+ Configuration .read ().remoteckan ().session = MockSession ()
361+
307362 @pytest .fixture (scope = "function" )
308363 def post_listorgs_invalid (self ):
309364 class MockSession :
@@ -522,6 +577,10 @@ def test_update_json(self, configuration, static_json):
522577 assert user ["name" ] == "MyUser1"
523578 assert user ["about" ] == "other"
524579
580+ def test_get_current_user (self , configuration , show_current_user ):
581+ user = User .get_current_user ()
582+ assert user ["name" ] == "MyUser1"
583+
525584 def test_get_all_users (self , configuration , post_list , mocksmtp ):
526585 users = User .get_all_users ()
527586 assert len (users ) == 2
@@ -636,6 +695,18 @@ def test_get_organizations_invalid_user(self, configuration, post_listorgs_inval
636695 assert user .get_organization_dicts () == []
637696 assert User .get_current_user_organization_dicts () == []
638697
698+ def test_check_current_user_write_access (
699+ self , configuration , post_check_current_user_write_access
700+ ):
701+ username = User .check_current_user_write_access (
702+ "b67e6c74-c185-4f43-b561-0e114a736f19"
703+ )
704+ assert username == "MyUser1"
705+ username = User .check_current_user_write_access ("acled" )
706+ assert username == "MyUser1"
707+ with pytest .raises (PermissionError ):
708+ User .check_current_user_write_access ("lala" )
709+
639710 def test_get_token_list (self , configuration , post_tokenlist ):
640711 user = User .read_from_hdx ("9f3e9973-7dbe-4c65-8820-f48578e3ffea" )
641712 tokens = user .get_token_list ()
0 commit comments