Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,34 @@ pub async fn req_path(
main_handler(req).await
}

pub async fn srv_req_path_with_app_data(
const REQ_TIMEOUT: Duration = Duration::from_secs(8);
pub async fn req_path_with_app_data(
path: impl AsRef<str>,
app_data: Data<AppState>,
) -> actix_web::dev::ServiceRequest {
test::TestRequest::get()
.uri(path.as_ref())
.app_data(app_data)
.insert_header(("cookie", "test_cook=123"))
.insert_header(("authorization", "Basic dGVzdDp0ZXN0")) // test:test
.to_srv_request()
) -> anyhow::Result<actix_web::dev::ServiceResponse> {
req_path_with_app_data_and_accept(path, app_data, header::Accept::html()).await
}

const REQ_TIMEOUT: Duration = Duration::from_secs(8);
pub async fn req_path_with_app_data(
pub async fn req_path_with_app_data_json(
path: impl AsRef<str>,
app_data: Data<AppState>,
) -> anyhow::Result<actix_web::dev::ServiceResponse> {
req_path_with_app_data_and_accept(path, app_data, header::Accept::json()).await
}

async fn req_path_with_app_data_and_accept(
path: impl AsRef<str>,
app_data: Data<AppState>,
accept: header::Accept,
) -> anyhow::Result<actix_web::dev::ServiceResponse> {
let path = path.as_ref();
let req = srv_req_path_with_app_data(path, app_data).await;
let req = test::TestRequest::get()
.uri(path)
.app_data(app_data)
.insert_header(("cookie", "test_cook=123"))
.insert_header(("authorization", "Basic dGVzdDp0ZXN0"))
.insert_header(accept)
.to_srv_request();
let resp = tokio::time::timeout(REQ_TIMEOUT, main_handler(req))
.await
.map_err(|e| anyhow::anyhow!("Request to {path} timed out: {e}"))?
Expand Down
11 changes: 7 additions & 4 deletions tests/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ async fn test_routing_with_prefix() {

let app_data = actix_web::web::Data::new(state);
let resp = req_path_with_app_data(
"/prefix/tests/sql_test_files/it_works_simple.sql",
"/prefix/tests/sql_test_files/component_rendering/simple.sql",
app_data.clone(),
)
.await
Expand Down Expand Up @@ -121,9 +121,12 @@ async fn test_routing_with_prefix() {
.to_string();
assert!(resp.to_lowercase().contains("forbidden"), "{resp}");

let resp = req_path_with_app_data("/tests/sql_test_files/it_works_simple.sql", app_data)
.await
.unwrap();
let resp = req_path_with_app_data(
"/tests/sql_test_files/component_rendering/simple.sql",
app_data,
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::MOVED_PERMANENTLY);
let location = resp
.headers()
Expand Down
2 changes: 1 addition & 1 deletion tests/core/select_temp_t.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
-- see tests/sql_test_files/it_works_temp_table_accessible_in_run_sql.sql
-- see tests/sql_test_files/component_rendering/temp_table_accessible_in_run_sql_nomssql.sql
select 'text' as component, x as contents from temp_t;
16 changes: 12 additions & 4 deletions tests/data_formats/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ async fn test_json_columns() {
#[actix_web::test]
async fn test_accept_json_returns_json_array() -> actix_web::Result<()> {
let resp = req_with_accept(
"/tests/sql_test_files/it_works_simple.sql",
"/tests/sql_test_files/component_rendering/simple.sql",
"application/json",
)
.await?;
Expand All @@ -117,7 +117,7 @@ async fn test_accept_json_returns_json_array() -> actix_web::Result<()> {
#[actix_web::test]
async fn test_accept_ndjson_returns_jsonlines() -> actix_web::Result<()> {
let resp = req_with_accept(
"/tests/sql_test_files/it_works_simple.sql",
"/tests/sql_test_files/component_rendering/simple.sql",
"application/x-ndjson",
)
.await?;
Expand All @@ -143,7 +143,11 @@ async fn test_accept_ndjson_returns_jsonlines() -> actix_web::Result<()> {

#[actix_web::test]
async fn test_accept_html_returns_html() -> actix_web::Result<()> {
let resp = req_with_accept("/tests/sql_test_files/it_works_simple.sql", "text/html").await?;
let resp = req_with_accept(
"/tests/sql_test_files/component_rendering/simple.sql",
"text/html",
)
.await?;
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(
resp.headers().get(header::CONTENT_TYPE).unwrap(),
Expand All @@ -156,7 +160,11 @@ async fn test_accept_html_returns_html() -> actix_web::Result<()> {

#[actix_web::test]
async fn test_accept_wildcard_returns_html() -> actix_web::Result<()> {
let resp = req_with_accept("/tests/sql_test_files/it_works_simple.sql", "*/*").await?;
let resp = req_with_accept(
"/tests/sql_test_files/component_rendering/simple.sql",
"*/*",
)
.await?;
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(
resp.headers().get(header::CONTENT_TYPE).unwrap(),
Expand Down
6 changes: 3 additions & 3 deletions tests/server_timing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ async fn test_server_timing_disabled_in_production() -> actix_web::Result<()> {
let app_data = make_app_data_from_config(config).await;

let req = crate::common::get_request_to_with_data(
"/tests/sql_test_files/it_works_simple.sql",
"/tests/sql_test_files/component_rendering/simple.sql",
app_data,
)
.await?
Expand All @@ -32,7 +32,7 @@ async fn test_server_timing_enabled_in_development() -> actix_web::Result<()> {
let app_data = make_app_data_from_config(config).await;

let req = crate::common::get_request_to_with_data(
"/tests/sql_test_files/it_works_sqrt.sql",
"/tests/sql_test_files/data/postgres_cast_syntax.sql",
app_data,
)
.await?
Expand Down Expand Up @@ -72,7 +72,7 @@ async fn test_server_timing_enabled_in_development() -> actix_web::Result<()> {

#[actix_web::test]
async fn test_server_timing_format() -> actix_web::Result<()> {
let req = get_request_to("/tests/sql_test_files/it_works_sqrt.sql")
let req = get_request_to("/tests/sql_test_files/data/postgres_cast_syntax.sql")
.await?
.to_srv_request();
let resp = main_handler(req).await?;
Expand Down
26 changes: 14 additions & 12 deletions tests/sql_test_files/README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
The sql files in this folder are all tested automatically.
The sql files in this folder are all tested automatically. They are organized in
two subdirectories:

## `it_works_` files
## `component_rendering/`

Files with names starting with `it_works` should all
return a page that contains the text "It works !" and does not contain the
text "error" (case insensitive) when executed.
Files that depend on SQLPage's HTML rendering (components, shells, redirects,
etc.). Every file that does not start with `error_` must render a page that
contains the text "It works !" and no occurrence of the word "error" (case
insensitive). `error_` files should return a page containing the word "error"
and the rest of the file name. Files may include `nosqlite`, `nomssql`,
`nopostgres` or `nomysql` in their name to skip incompatible backends.

If a file name contains `nosqlite`, `nomssql`, `nopostgres` or `nomysql`, then
the test will be ignored when running against the corresponding database.
This allows using syntax that is not supported on all databases in some tests.
## `data/`

## `error_` files

Files with names starting with `error` should all return a page that contains
the text "error" and the rest of the file name when executed.
Files that only validate data-processing functions should live here. They must
return rows with an `actual` column plus either `expected` (exact match) or
`expected_contains` (substring match). Tests in this directory are fetched as
JSON and validated row by row.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
select 'dynamic' as component,
sqlpage.run_sql('tests/sql_test_files/component_rendering/dynamic_shell.sql') as properties;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
select 'debug' as component,
sqlpage.run_sql(
'tests/sql_test_files/component_rendering/error_too_many_nested_inclusions.sql'
) as contents;
2 changes: 2 additions & 0 deletions tests/sql_test_files/component_rendering/run_sql.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
select 'dynamic' as component,
sqlpage.run_sql('tests/sql_test_files/component_rendering/simple.sql') as properties;
1 change: 1 addition & 0 deletions tests/sql_test_files/data/basic_auth_password.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select 'test' as expected, sqlpage.basic_auth_password() as actual;
1 change: 1 addition & 0 deletions tests/sql_test_files/data/basic_auth_username.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select 'test' as expected, sqlpage.basic_auth_username() as actual;
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
set success = 'It works !';
set failure = 'You should never see this';

select 'text' as component,
select 'It works !' as expected,
case $success
when $success then $success
when $failure then $failure
end AS contents;
end as actual;
2 changes: 2 additions & 0 deletions tests/sql_test_files/data/client_ip.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
select NULL as expected,
sqlpage.client_ip() as actual;
1 change: 1 addition & 0 deletions tests/sql_test_files/data/coalesce_eval.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select 'https://example.com' as expected, sqlpage.link(coalesce($i_do_not_exist, 'https://example.com')) as actual;
3 changes: 3 additions & 0 deletions tests/sql_test_files/data/concat_str_in_pseudofunction.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
select '%2F1' as expected, sqlpage.url_encode('/' || $x) as actual;
select '%2F1' as expected, sqlpage.url_encode(CONCAT('/', $x)) as actual;
select NULL as expected, sqlpage.url_encode(CONCAT('/', $thisisnull)) as actual;
1 change: 1 addition & 0 deletions tests/sql_test_files/data/cookie.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select '123' as expected, sqlpage.cookie('test_cook') as actual;
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ create table my_tmp_store(x varchar(100));

insert into my_tmp_store(x) values ('It works !');

select 'card' as component;
select x as description from my_tmp_store;
select 'It works !' as expected, x as actual from my_tmp_store;
2 changes: 2 additions & 0 deletions tests/sql_test_files/data/decimal.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
set result = CAST(0.47 AS DECIMAL(3,2));
select '0.47' as expected, $result as actual;
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
drop table if exists files_to_read;
create table files_to_read(filepath varchar(100));
insert into files_to_read(filepath) values ('tests/it_works.txt');
select 'text' as component, sqlpage.read_file_as_text(filepath) as contents from files_to_read;
select 'It works !' as expected,
sqlpage.read_file_as_text(filepath) as actual
from files_to_read;
2 changes: 2 additions & 0 deletions tests/sql_test_files/data/env_var_empty.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
select NULL as expected,
sqlpage.environment_variable('I_DO_NOT_EXIST') as actual;
2 changes: 2 additions & 0 deletions tests/sql_test_files/data/exec.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
select 'It works !' as expected_contains,
sqlpage.exec('echo', 'It', $thisisnull, 'works', '!') as actual;
6 changes: 6 additions & 0 deletions tests/sql_test_files/data/fetch_base64.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
set res = sqlpage.fetch('{
"url": "http://localhost:' || $echo_port || '/hello_world",
"response_encoding": "base64"
}');
select 'R0VUIC9oZWxsb193b3Js' as expected_contains,
$res as actual;
6 changes: 6 additions & 0 deletions tests/sql_test_files/data/fetch_hex.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
set res = sqlpage.fetch('{
"url": "http://localhost:' || $echo_port || '/hello_world",
"response_encoding": "hex"
}');
select '474554202f68656c6c6f5f776f726c64' as expected_contains,
$res as actual;
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,5 @@ set res = sqlpage.fetch(json_object(
'body', json_array('hello', 'world')
));
set expected = 'POST /post|accept-encoding: br, gzip, deflate, zstd|content-length: 17|content-type: application/json|host: localhost:' || $echo_port || '|user-agent: sqlpage|x-custom: 1|["hello","world"]';
select 'text' as component,
case $res
when $expected then 'It works !'
else 'It failed ! Expected:
' || $expected || '
Got:
' || $res
end as contents;
select $expected as expected,
$res as actual;
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,5 @@ set url = 'http://localhost:' || $echo_port || '/post';
set fetch_request = '{"method": "POST", "url": "' || $url || '", "headers": {"x-custom": "1"}, "body": {"hello": "world"}}';
set res = sqlpage.fetch($fetch_request);
set expected = 'POST /post|accept-encoding: br, gzip, deflate, zstd|content-length: 18|content-type: application/json|host: localhost:' || $echo_port || '|user-agent: sqlpage|x-custom: 1|{"hello": "world"}';
select 'text' as component,
case $res
when $expected then 'It works !'
else 'It failed ! Expected:
' || COALESCE($expected, 'null') || '
Got:
' || COALESCE($res, 'null')
end as contents;
select $expected as expected,
$res as actual;
4 changes: 4 additions & 0 deletions tests/sql_test_files/data/fetch_simple.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set url = 'http://localhost:' || $echo_port || '/hello_world';
set res = sqlpage.fetch($url);
select 'GET /hello_world' as expected_contains,
$res as actual;
3 changes: 3 additions & 0 deletions tests/sql_test_files/data/fetch_with_meta_error.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
set res = sqlpage.fetch_with_meta('http://not-a-real-url');

select '"error":"Request failed' as expected_contains, $res as actual;
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ set fetch_req = '{
}';
set res = sqlpage.fetch_with_meta($fetch_req);

select 'text' as component,
case
when $res LIKE '%"status":200%' AND $res LIKE '%"headers":{%' AND $res LIKE '%"body":"%' then 'It works !'
else 'Error! Got: ' || $res
end as contents;
select '"status":200' as expected_contains,
'"headers":{' as expected_contains,
'"body":"' as expected_contains,
$res as actual;
2 changes: 2 additions & 0 deletions tests/sql_test_files/data/hash_password.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
select '$argon2id$' as expected_contains,
coalesce(sqlpage.hash_password($x), 'NULL') as actual;
1 change: 1 addition & 0 deletions tests/sql_test_files/data/hash_password_null.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select NULL as expected, sqlpage.hash_password(null) as actual;
1 change: 1 addition & 0 deletions tests/sql_test_files/data/header.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select 'test_cook=123' as expected, sqlpage.header('cookie') as actual;
1 change: 1 addition & 0 deletions tests/sql_test_files/data/headers.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select '"cookie":"test_cook=123"' as expected_contains, sqlpage.headers() as actual;
2 changes: 2 additions & 0 deletions tests/sql_test_files/data/hmac_base64.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
select '97yD9DBThCSxMpjmqm+xQ+9NWaFJRhdZl0edvC0aPNg=' as expected,
sqlpage.hmac('The quick brown fox jumps over the lazy dog', 'key', 'sha256-base64') as actual;
2 changes: 2 additions & 0 deletions tests/sql_test_files/data/hmac_default.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
select sqlpage.hmac('test data', 'test key', 'sha256') as expected,
sqlpage.hmac('test data', 'test key') as actual;
2 changes: 2 additions & 0 deletions tests/sql_test_files/data/hmac_sha256.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
select 'f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8' as expected,
sqlpage.hmac('The quick brown fox jumps over the lazy dog', 'key', 'sha256') as actual;
2 changes: 2 additions & 0 deletions tests/sql_test_files/data/hmac_sha512.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
select 'b42af09057bac1e2d41708e48a902e09b5ff7f12ab428a4fe86653c73dd248fb82f948a549f7b791a5b41915ee4d1ec3935357e4e2317250d0372afa2ebeeb3a' as expected,
sqlpage.hmac('The quick brown fox jumps over the lazy dog', 'key', 'sha512') as actual;
7 changes: 7 additions & 0 deletions tests/sql_test_files/data/hmac_shopify_webhook.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Test Shopify webhook HMAC validation with base64 output
select 'QNyObTlKbMx2qDlPF/ZOZcBqg5OgPg+2oky3zldc0Gw=' as expected,
sqlpage.hmac(
'{"id":1234567890,"email":"[email protected]","total_price":"123.45"}',
'test-webhook-secret',
'sha256-base64'
) as actual;
10 changes: 10 additions & 0 deletions tests/sql_test_files/data/immutable_variables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
set x = 'set_value';
set set_only = 'only_in_set';

select 'set_value' as expected, $x as actual;
select 'only_in_set' as expected, $set_only as actual;
select '{"x":"1"}' as expected, sqlpage.variables('get') as actual;
select '"x":"set_value"' as expected_contains, sqlpage.variables('set') as actual;
select '"set_only":"only_in_set"' as expected_contains, sqlpage.variables('set') as actual;
select '"x":"set_value"' as expected_contains, sqlpage.variables() as actual;
select '"set_only":"only_in_set"' as expected_contains, sqlpage.variables() as actual;
1 change: 1 addition & 0 deletions tests/sql_test_files/data/link.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select 'test.sql?x=123' as expected, sqlpage.link('test.sql', json_object('x', 123)) as actual;
1 change: 1 addition & 0 deletions tests/sql_test_files/data/link_null.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select 'test.sql' as expected, sqlpage.link('test.sql', json_object('x', null)) as actual;
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- in SQLite, we provide our own unicode-aware lower function
-- see https://github.com/sqlpage/SQLPage/issues/452
select 'text' as component, COALESCE(lower(NULL), 'It works !') AS contents;
select 'It works !' as expected,
coalesce(lower(NULL), 'It works !') as actual;
1 change: 1 addition & 0 deletions tests/sql_test_files/data/nested_sqlpage_functions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select 'It%20works%20%21' as expected, sqlpage.url_encode(sqlpage.read_file_as_text('tests/it_works.txt')) as actual;
2 changes: 2 additions & 0 deletions tests/sql_test_files/data/path.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
select '/tests/sql_test_files/data/path.sql' as expected,
sqlpage.path() as actual;
1 change: 1 addition & 0 deletions tests/sql_test_files/data/postgres_cast_syntax.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select 2 as expected, $x::decimal + 1 as actual;
2 changes: 2 additions & 0 deletions tests/sql_test_files/data/protocol.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
select 'http' as expected,
sqlpage.protocol() as actual;
1 change: 1 addition & 0 deletions tests/sql_test_files/data/query_string.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select '?x=1' as expected, sqlpage.link('', sqlpage.variables('get')) as actual;
1 change: 1 addition & 0 deletions tests/sql_test_files/data/random_string.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select '' as expected, sqlpage.random_string(0) as actual;
3 changes: 3 additions & 0 deletions tests/sql_test_files/data/read_file_as_data_url.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
set actual = sqlpage.read_file_as_data_url('tests/it_works.txt');
select 'data:text/plain;base64,SXQgd29ya3MgIQ==' as expected,
coalesce($actual, 'NULL') as actual;
2 changes: 2 additions & 0 deletions tests/sql_test_files/data/read_file_as_text.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
select 'It works !' as expected,
sqlpage.read_file_as_text('tests/it_works.txt') as actual;
Loading