-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added unit tests * Added docs * Update README.md * Added logging
- Loading branch information
Showing
7 changed files
with
406 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
name: test | ||
|
||
on: [push, pull_request] | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
name: Checkout project | ||
|
||
- uses: leafo/gh-actions-lua@v9 | ||
name: Install Lua | ||
with: | ||
luaVersion: "5.1.5" | ||
|
||
- name: Setup telescope | ||
run: | | ||
wget -O telescope.zip https://github.com/defold/telescope/archive/refs/heads/master.zip | ||
unzip telescope.zip | ||
mv telescope-master/tsc . | ||
mv telescope-master/telescope.lua . | ||
mv telescope-master/telescope . | ||
chmod +x tsc | ||
ls -la | ||
- name: Run tests | ||
run: | | ||
lua -v | ||
./tsc -f test/test_socket.lua test/test_client.lua test/test_session.lua |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,7 @@ Thumbs.db | |
*.pyc | ||
.project | ||
.cproject | ||
builtins | ||
builtins | ||
telescope.lua | ||
telescope/compat_env.lua | ||
tsc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
local uuid = require "nakama.util.uuid" | ||
|
||
local M = {} | ||
|
||
----------------- | ||
-- TEST HELPER -- | ||
----------------- | ||
|
||
local http_request_response = {} | ||
local http_request_queue = {} | ||
local socket_send_queue = {} | ||
|
||
function M.set_http_response(path, response) | ||
assert(path, response) | ||
http_request_response[path] = response | ||
end | ||
|
||
function M.get_http_request() | ||
return table.remove(http_request_queue) | ||
end | ||
|
||
function M.get_socket_message() | ||
return table.remove(socket_send_queue) | ||
end | ||
|
||
function M.receive_socket_message(socket, message) | ||
socket.on_message(socket, message) | ||
end | ||
|
||
function M.reset() | ||
http_request_response = {} | ||
http_request_queue = {} | ||
socket_send_queue = {} | ||
end | ||
|
||
---------------- | ||
-- ENGINE API -- | ||
---------------- | ||
|
||
function M.uuid() | ||
return uuid("") | ||
end | ||
|
||
function M.http(config, url_path, query_params, method, post_data, retry_policy, cancellation_token, callback) | ||
local request = { | ||
config = config, | ||
url_path = url_path, | ||
query_params = query_params, | ||
method = method, | ||
post_data = post_data | ||
} | ||
table.insert(http_request_queue, request) | ||
|
||
local response = http_request_response[url_path] | ||
callback(response) | ||
end | ||
|
||
function M.socket_create(config, on_message) | ||
local socket = { | ||
on_message = on_message | ||
} | ||
return socket | ||
end | ||
|
||
function M.socket_connect(socket, callback) | ||
local result = true | ||
callback(result) | ||
end | ||
|
||
function M.socket_send(socket, message, callback) | ||
table.insert(socket_send_queue, message) | ||
local result = {} | ||
callback(result) | ||
end | ||
|
||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
local nakama = require "nakama.nakama" | ||
local test_engine = require "nakama.engine.test" | ||
local json = require "nakama.util.json" | ||
local log = require "nakama.util.log" | ||
log.print() | ||
|
||
context("Nakama client", function() | ||
|
||
before(function() | ||
test_engine.reset() | ||
end) | ||
after(function() end) | ||
|
||
local function config() | ||
return { | ||
host = "127.0.0.1", | ||
port = 7350, | ||
use_ssl = false, | ||
username = "defaultkey", | ||
password = "", | ||
engine = test_engine, | ||
timeout = 10, -- connection timeout in seconds | ||
} | ||
end | ||
|
||
local function pprint(t) | ||
for k,v in pairs(t) do | ||
print(k, v) | ||
end | ||
end | ||
|
||
test("It should be able to create a client", function() | ||
local config = config() | ||
local client = nakama.create_client(config) | ||
assert_not_nil(client) | ||
end) | ||
|
||
test("It should be able to authenticate", function() | ||
local token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiI1MjJkMGI5MS00NmQzLTRjY2ItYmIwYS0wNTFjYjUyOGNhMDMiLCJ1c24iOiJicml0emwiLCJleHAiOjE2NjE1OTA5Nzl9.r3h4QraXsXl-XmGQueYecjeb6223vtd1s-Ak1K_FrGM" | ||
local data = { token = token } | ||
local url_path = "/v2/account/authenticate/email" | ||
test_engine.set_http_response(url_path, data) | ||
|
||
coroutine.wrap(function() | ||
local client = nakama.create_client(config()) | ||
local email = "super@heroes.com" | ||
local password = "batsignal" | ||
client.authenticate_email(email, password) | ||
|
||
local request = test_engine.get_http_request(1) | ||
assert_not_nil(request) | ||
assert_equal(request.url_path, url_path) | ||
assert_equal(request.method, "POST") | ||
|
||
local pd = json.decode(request.post_data) | ||
assert_equal(pd.password, password) | ||
assert_equal(pd.email, email) | ||
assert_not_nil(request.query_params) | ||
end)() | ||
end) | ||
|
||
test("It should create a session on successful authentication", function() | ||
local token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiI1MjJkMGI5MS00NmQzLTRjY2ItYmIwYS0wNTFjYjUyOGNhMDMiLCJ1c24iOiJicml0emwiLCJleHAiOjE2NjE1OTA5Nzl9.r3h4QraXsXl-XmGQueYecjeb6223vtd1s-Ak1K_FrGM" | ||
local refresh_token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiI1MjJkMGI5MS00NmQzLTRjY2ItYmIwYS0wNTFjYjUyOGNhMDMiLCJ1c24iOiJicml0emwiLCJleHAiOjE2NjE1ODczNzl9.AWASctuZx9A8YliCLSj9jtOi4fuXUZaWtRdNz1mMEEw" | ||
local data = { | ||
token = token, | ||
} | ||
local url_path = "/v2/account/authenticate/email" | ||
test_engine.set_http_response(url_path, data) | ||
|
||
coroutine.wrap(function() | ||
local client = nakama.create_client(config()) | ||
local email = "super@heroes.com" | ||
local password = "batsignal" | ||
local session = client.authenticate_email(email, password) | ||
assert_not_nil(session) | ||
assert_not_nil(session.created) | ||
assert_not_nil(session.expires) | ||
assert_equal(session.token, data.token) | ||
assert_equal(session.user_id, "522d0b91-46d3-4ccb-bb0a-051cb528ca03") | ||
assert_equal(session.username, "britzl") | ||
end)() | ||
end) | ||
|
||
test("It should be able to use coroutines", function() | ||
test_engine.set_http_response("/v2/account", {}) | ||
|
||
local done = false | ||
print("before coroutine") | ||
coroutine.wrap(function() | ||
print("in coroutine create client") | ||
local client = nakama.create_client(config()) | ||
print("in coroutine get account") | ||
local result = client.get_account() | ||
print("in coroutine result", result) | ||
assert_not_nil(result) | ||
print("in coroutine done") | ||
done = true | ||
end)() | ||
print("after coroutine") | ||
assert_true(done) | ||
end) | ||
|
||
test("It should be able to use callbacks", function() | ||
test_engine.set_http_response("/v2/account", {}) | ||
|
||
local done = false | ||
local client = nakama.create_client(config()) | ||
client.get_account(function(result) | ||
assert_not_nil(result) | ||
done = true | ||
end) | ||
assert_true(done) | ||
end) | ||
end) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
local session = require "nakama.session" | ||
|
||
context("Session", function() | ||
before(function() end) | ||
after(function() end) | ||
|
||
test("It should be able to detect when a session has expired", function() | ||
local now = os.time() | ||
local expired_session = { | ||
expires = now - 1 | ||
} | ||
local non_expired_session = { | ||
expires = now + 1 | ||
} | ||
assert_true(session.expired(expired_session)) | ||
assert_false(session.expired(non_expired_session)) | ||
end) | ||
|
||
test("It should be able to create a session", function() | ||
local token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiI1MjJkMGI5MS00NmQzLTRjY2ItYmIwYS0wNTFjYjUyOGNhMDMiLCJ1c24iOiJicml0emwiLCJleHAiOjE2NjE1OTA5Nzl9.r3h4QraXsXl-XmGQueYecjeb6223vtd1s-Ak1K_FrGM" | ||
local refresh_token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiI1MjJkMGI5MS00NmQzLTRjY2ItYmIwYS0wNTFjYjUyOGNhMDMiLCJ1c24iOiJicml0emwiLCJleHAiOjE2NjE1ODczNzl9.AWASctuZx9A8YliCLSj9jtOi4fuXUZaWtRdNz1mMEEw" | ||
local data = { | ||
token = token, | ||
refresh_token = refresh_token | ||
} | ||
|
||
local s = session.create(data) | ||
assert(s.created) | ||
assert_equal(s.token, token) | ||
assert_equal(s.expires, 1661590979) | ||
assert_equal(s.username, "britzl") | ||
assert_equal(s.user_id, "522d0b91-46d3-4ccb-bb0a-051cb528ca03") | ||
end) | ||
end) |
Oops, something went wrong.