async def handle_logout(self, request): try: user_devid = request.match_info.get("devid", "") if not user_devid == "": user = bumper.user_by_deviceid(user_devid) if user: if bumper.check_token(user["userid"], request.query["accessToken"]): # Deactivate old tokens and authcodes bumper.user_revoke_token( user["userid"], request.query["accessToken"] ) body = { "code": bumper.RETURN_API_SUCCESS, "data": None, "msg": "操作成功", "time": bumper.get_milli_time(datetime.utcnow().timestamp()), } return web.json_response(body) except Exception as e: confserverlog.exception("{}".format(e))
def test_user_db(): if os.path.exists("tests/tmp.db"): os.remove("tests/tmp.db") # Remove existing db # Test os_db_path platform.system = mock.MagicMock(return_value="Windows") p = platform.system() os.getenv = mock.MagicMock(return_value="C:\AppData") o = os.getenv("APPDATA") assert_equals(bumper.os_db_path(), os.path.join(os.getenv("APPDATA"), "bumper.db")) platform.system = mock.MagicMock(return_value="Linux") assert_equals(bumper.os_db_path(), os.path.expanduser("~/.config/bumper.db")) bumper.db = "tests/tmp.db" # Set db location for testing bumper.user_add("testuser") # Add testuser assert_equals(bumper.user_get("testuser")["userid"], "testuser") # Test that testuser was created and returned bumper.user_add_device("testuser", "dev_1234") # Add device to testuser assert_equals(bumper.user_by_deviceid("dev_1234")["userid"], "testuser") # Test that testuser was found by deviceid bumper.user_remove_device("testuser", "dev_1234") # Remove device from testuser assert_true( "dev_1234" not in bumper.user_get("testuser") ["devices"]) # Test that dev_1234 was not found in testuser devices bumper.user_add_bot("testuser", "bot_1234") # Add bot did to testuser assert_true("bot_1234" in bumper.user_get("testuser") ["bots"]) # Test that bot was found in testuser's bot list bumper.user_remove_bot("testuser", "bot_1234") # Remove bot did from testuser assert_true("bot_1234" not in bumper.user_get("testuser") ["bots"]) # Test that bot was not found in testuser's bot list bumper.user_add_token("testuser", "token_1234") # Add token to testuser assert_true(bumper.check_token( "testuser", "token_1234")) # Test that token was found for testuser assert_true(bumper.user_get_token( "testuser", "token_1234")) # Test that token was returned for testuser bumper.user_add_authcode( "testuser", "token_1234", "auth_1234") # Add authcode to token_1234 for testuser assert_true(bumper.check_authcode( "testuser", "auth_1234")) # Test that authcode was found for testuser bumper.user_revoke_authcode("testuser", "token_1234", "auth_1234") # Remove authcode from testuser assert_false(bumper.check_authcode( "testuser", "auth_1234")) # Test that authcode was not found for testuser bumper.user_revoke_token("testuser", "token_1234") # Remove token from testuser assert_false(bumper.check_token( "testuser", "token_1234")) # Test that token was not found for testuser bumper.user_add_token("testuser", "token_1234") # Add token_1234 bumper.user_add_token("testuser", "token_4321") # Add token_4321 assert_equals(len(bumper.user_get_tokens("testuser")), 2) # Test 2 tokens are available bumper.user_revoke_all_tokens("testuser") # Revoke all tokens assert_equals(len(bumper.user_get_tokens("testuser")), 0) # Test 0 tokens are available db = TinyDB("tests/tmp.db") tokens = db.table("tokens") tokens.insert({ "userid": "testuser", "token": "token_1234", "expiration": "{}".format(datetime.datetime.now() + datetime.timedelta(seconds=-10)), }) # Add expired token db.close() assert_equals(len(bumper.user_get_tokens("testuser")), 1) # Test 1 tokens are available bumper.user_revoke_expired_tokens("testuser") # Revoke expired tokens assert_equals(len(bumper.user_get_tokens("testuser")), 0) # Test 0 tokens are available db = TinyDB("tests/tmp.db") tokens = db.table("tokens") tokens.insert({ "userid": "testuser", "token": "token_1234", "expiration": "{}".format(datetime.datetime.now() + datetime.timedelta(seconds=-10)), }) # Add expired token db.close() assert_equals(len(bumper.user_get_tokens("testuser")), 1) # Test 1 tokens are available bumper.revoke_expired_tokens() # Revoke expired tokens assert_equals(len(bumper.user_get_tokens("testuser")), 0) # Test 0 tokens are available
def test_user_db(): bumper.db = "tests/tmp.db" # Set db location for testing bumper.user_add("testuser") # Add testuser assert (bumper.user_get("testuser")["userid"] == "testuser" ) # Test that testuser was created and returned bumper.user_add_device("testuser", "dev_1234") # Add device to testuser assert (bumper.user_by_deviceid("dev_1234")["userid"] == "testuser" ) # Test that testuser was found by deviceid bumper.user_remove_device("testuser", "dev_1234") # Remove device from testuser assert "dev_1234" not in bumper.user_get("testuser")["devices"] # Test that dev_1234 was not found in testuser devices bumper.user_add_bot("testuser", "bot_1234") # Add bot did to testuser assert "bot_1234" in bumper.user_get("testuser")["bots"] # Test that bot was found in testuser's bot list bumper.user_remove_bot("testuser", "bot_1234") # Remove bot did from testuser assert "bot_1234" not in bumper.user_get("testuser")["bots"] # Test that bot was not found in testuser's bot list bumper.user_add_token("testuser", "token_1234") # Add token to testuser assert bumper.check_token("testuser", "token_1234") # Test that token was found for testuser assert bumper.user_get_token("testuser", "token_1234") # Test that token was returned for testuser bumper.user_add_authcode( "testuser", "token_1234", "auth_1234") # Add authcode to token_1234 for testuser assert bumper.check_authcode("testuser", "auth_1234") # Test that authcode was found for testuser bumper.user_revoke_authcode("testuser", "token_1234", "auth_1234") # Remove authcode from testuser assert bumper.check_authcode("testuser", "auth_1234") == False # Test that authcode was not found for testuser bumper.user_revoke_token("testuser", "token_1234") # Remove token from testuser assert (bumper.check_token("testuser", "token_1234") == False ) # Test that token was not found for testuser bumper.user_add_token("testuser", "token_1234") # Add token_1234 bumper.user_add_token("testuser", "token_4321") # Add token_4321 assert len( bumper.user_get_tokens("testuser")) == 2 # Test 2 tokens are available bumper.user_revoke_all_tokens("testuser") # Revoke all tokens assert len( bumper.user_get_tokens("testuser")) == 0 # Test 0 tokens are available db = TinyDB("tests/tmp.db") tokens = db.table("tokens") tokens.insert({ "userid": "testuser", "token": "token_1234", "expiration": "{}".format(datetime.now() + timedelta(seconds=-10)), }) # Add expired token db.close() assert len( bumper.user_get_tokens("testuser")) == 1 # Test 1 tokens are available bumper.user_revoke_expired_tokens("testuser") # Revoke expired tokens assert len( bumper.user_get_tokens("testuser")) == 0 # Test 0 tokens are available db = TinyDB("tests/tmp.db") tokens = db.table("tokens") tokens.insert({ "userid": "testuser", "token": "token_1234", "expiration": "{}".format(datetime.now() + timedelta(seconds=-10)), }) # Add expired token db.close() assert len( bumper.user_get_tokens("testuser")) == 1 # Test 1 tokens are available bumper.revoke_expired_tokens() # Revoke expired tokens assert len( bumper.user_get_tokens("testuser")) == 0 # Test 0 tokens are available