add mongo test case: auth

This commit is contained in:
LiuYang
2018-08-30 11:38:08 +08:00
committed by 云风
parent 7e63080c03
commit ce0cdc7bd3
4 changed files with 52 additions and 1 deletions

11
examples/config.mongodb Normal file
View File

@@ -0,0 +1,11 @@
root = "./"
thread = 8
logger = nil
harbor = 0
start = "main_mongodb" -- main script
bootstrap = "snlua bootstrap" -- The service for bootstrap
luaservice = root.."service/?.lua;"..root.."test/?.lua;"..root.."examples/?.lua"
lualoader = "lualib/loader.lua"
snax = root.."examples/?.lua;"..root.."test/?.lua"
cpath = root.."cservice/?.so"
-- daemon = "./skynet.pid"

12
examples/main_mongodb.lua Normal file
View File

@@ -0,0 +1,12 @@
local skynet = require "skynet"
skynet.start(function()
print("Main Server start")
local console = skynet.newservice(
"testmongodb", "127.0.0.1", 27017, "testdb", "test", "test"
)
print("Main Server exit")
skynet.exit()
end)

View File

@@ -309,7 +309,7 @@ end
function mongo_db:auth(user, pass)
local authmod = rawget(self.connection, "authmod") or "scram_sha1"
local auth_func = auth_method[authmod]
local auth_func = auth_method["auth_" .. authmod]
assert(auth_func , "Invalid authmod")
return auth_func(self, user, pass)
end

View File

@@ -3,6 +3,11 @@ local mongo = require "skynet.db.mongo"
local bson = require "bson"
local host, port, db_name, username, password = ...
if port then
port = math.tointeger(port)
end
-- print(host, port, db_name, username, password)
local function _create_client()
return mongo.client(
@@ -14,6 +19,25 @@ local function _create_client()
)
end
function test_auth()
local c = mongo.client(
{
host = host, port = port,
}
)
db = c[db_name]
db:auth(username, password)
db.testdb:dropIndex("*")
db.testdb:drop()
local ok, err, ret = db.testdb:safe_insert({test_key = 1});
assert(ok and ret and ret.n == 1, err)
local ok, err, ret = db.testdb:safe_insert({test_key = 1});
assert(ok and ret and ret.n == 1, err)
end
function test_insert_without_index()
local db = _create_client()
db[db_name].testdb:dropIndex("*")
@@ -105,6 +129,10 @@ function test_expire_index()
end
skynet.start(function()
if username then
print("Test auth")
test_auth()
end
print("Test insert without index")
test_insert_without_index()
print("Test insert index")