Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/surveilr/www.surveilr.com i…
Browse files Browse the repository at this point in the history
…nto main
  • Loading branch information
temiye18 committed Oct 8, 2024
2 parents ff7ba91 + 9cb61b1 commit b562e7a
Show file tree
Hide file tree
Showing 18 changed files with 1,819 additions and 4 deletions.
15 changes: 15 additions & 0 deletions lib/pattern/info-assurance-controls/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,18 @@ To execute test and ensure that `surveilr` is functioning correctly:

The `-A` flag provides all necessary permissions for the tests to run, including
file system access and network permissions.

## How to Run the Tap Tests

### RUN THIS TEST this using CLI

`rm -f tap.sql && deno run -A ./tap.sql.ts > tap.sql`

This will generate a tap.sql file from tap.sql.ts

`cat tap.sql | sqlite3 resource-surveillance.sqlite.db && sqlite3 resource-surveillance.sqlite.db -cmd "
SELECT * FROM synthetic_test_suite;"`

This script demonstrates how to create a Test Anything Protocol (TAP) report
using SQLite, following TAP version 14. It includes multiple test cases, and
subtests are formatted with indentation per TAP 14's subtest style.
63 changes: 63 additions & 0 deletions lib/pattern/info-assurance-controls/tap.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
DROP VIEW IF EXISTS "synthetic_test_suite";
CREATE VIEW "synthetic_test_suite" AS
WITH
tap_version AS (SELECT 'TAP version 14' AS tap_result),
tap_plan AS (SELECT '1..6' AS tap_result),
-- 0: Check if a view 'control_regimes' exists
"Check if a view 'control_regimes' exists" AS (
WITH test_case AS (
SELECT count(name) as exist FROM sqlite_master WHERE type = 'view' AND name = 'control_regimes'
)
SELECT CASE WHEN exist = 1 THEN 'ok 0 ' || ('View "control_regimes" exists in the DB') ELSE 'not ok 0 ' || ('View "control_regimes" not exists in the DB') END AS tap_result FROM test_case
),
-- 1: Ensure 'control_regimes' view has values
"Ensure 'control_regimes' view has values " AS (
WITH test_case AS (
SELECT COUNT(*) AS control_regimes_count FROM control_regimes
)
SELECT CASE WHEN control_regimes_count > 0 THEN 'ok 1 ' || ('control_regimes_count is greater than 0') ELSE 'not ok 1 ' || ('control_regimes_count should be greater than 0, is ' || control_regimes_count || ' instead') END AS tap_result FROM test_case
),
-- 2: Check if a view 'control_group' exists
"Check if a view 'control_group' exists" AS (
WITH test_case AS (
SELECT count(name) as exist FROM sqlite_master WHERE type = 'view' AND name = 'control_group'
)
SELECT CASE WHEN exist = 1 THEN 'ok 2 ' || ('View "control_group" exists in the DB') ELSE 'not ok 2 ' || ('View "control_group" not exists in the DB') END AS tap_result FROM test_case
),
-- 3: Ensure 'control_group' view has values
"Ensure 'control_group' view has values " AS (
WITH test_case AS (
SELECT COUNT(*) AS control_group_count FROM control_group
)
SELECT CASE WHEN control_group_count > 0 THEN 'ok 3 ' || ('control_group_count is greater than 0') ELSE 'not ok 3 ' || ('control_group_count should be greater than 0, is ' || control_group_count || ' instead') END AS tap_result FROM test_case
),
-- 4: Check if a view 'control' exists
"Check if a view 'control' exists" AS (
WITH test_case AS (
SELECT count(name) as exist FROM sqlite_master WHERE type = 'view' AND name = 'control'
)
SELECT CASE WHEN exist = 1 THEN 'ok 4 ' || ('View "control" exists in the DB') ELSE 'not ok 4 ' || ('View "control" not exists in the DB') END AS tap_result FROM test_case
),
-- 5: Ensure 'control' view has values
"Ensure 'control' view has values " AS (
WITH test_case AS (
SELECT COUNT(*) AS control_count FROM control
)
SELECT CASE WHEN control_count > 0 THEN 'ok 5 ' || ('control_count is greater than 0') ELSE 'not ok 5 ' || ('control_count should be greater than 0, is ' || control_count || ' instead') END AS tap_result FROM test_case
)
SELECT tap_result FROM tap_version
UNION ALL
SELECT tap_result FROM tap_plan
UNION ALL
SELECT tap_result FROM "Check if a view 'control_regimes' exists"
UNION ALL
SELECT tap_result FROM "Ensure 'control_regimes' view has values "
UNION ALL
SELECT tap_result FROM "Check if a view 'control_group' exists"
UNION ALL
SELECT tap_result FROM "Ensure 'control_group' view has values "
UNION ALL
SELECT tap_result FROM "Check if a view 'control' exists"
UNION ALL
SELECT tap_result FROM "Ensure 'control' view has values ";
SELECT * FROM synthetic_test_suite;
67 changes: 67 additions & 0 deletions lib/pattern/info-assurance-controls/tap.sql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { tapNB } from "../../std/notebook/mod.ts";
type TestCaseContext = tapNB.TestCaseContext;
export class SyntheticTestSuite extends tapNB.TestSuiteNotebook {
// any method that ends in DDL, SQL, DML, or DQL will be "arbitrary SQL"
// and included in the SQL stream before all the test cases

"Check if a view 'control_regimes' exists"(ctx: TestCaseContext) {
const viewName = "control_regimes";
return this.assertThat(ctx)`
SELECT count(name) as exist FROM sqlite_master WHERE type = 'view' AND name = '${viewName}'`
.case(
`exist = 1`,
`View "${viewName}" exists in the DB`,
`View "${viewName}" not exists in the DB`,
);
}

"Ensure 'control_regimes' view has values "(ctx: TestCaseContext) {
return this.assertThat<"control_regimes_count">(ctx)`
SELECT COUNT(*) AS control_regimes_count FROM control_regimes`
.greaterThan("control_regimes_count", 0);
}

"Check if a view 'control_group' exists"(ctx: TestCaseContext) {
const viewName = "control_group";
return this.assertThat(ctx)`
SELECT count(name) as exist FROM sqlite_master WHERE type = 'view' AND name = '${viewName}'`
.case(
`exist = 1`,
`View "${viewName}" exists in the DB`,
`View "${viewName}" not exists in the DB`,
);
}

"Ensure 'control_group' view has values "(ctx: TestCaseContext) {
return this.assertThat<"control_group_count">(ctx)`
SELECT COUNT(*) AS control_group_count FROM control_group`
.greaterThan("control_group_count", 0);
}

"Check if a view 'control' exists"(ctx: TestCaseContext) {
const viewName = "control";
return this.assertThat(ctx)`
SELECT count(name) as exist FROM sqlite_master WHERE type = 'view' AND name = '${viewName}'`
.case(
`exist = 1`,
`View "${viewName}" exists in the DB`,
`View "${viewName}" not exists in the DB`,
);
}

"Ensure 'control' view has values "(ctx: TestCaseContext) {
return this.assertThat<"control_count">(ctx)`
SELECT COUNT(*) AS control_count FROM control`
.greaterThan("control_count", 0);
}
}

// this will be used by any callers who want to serve it as a CLI with SDTOUT
if (import.meta.main) {
const SQL = await tapNB.TestSuiteNotebook.SQL(
new SyntheticTestSuite("synthetic_test_suite"),
);

console.log(SQL.join("\n"));
console.log(`SELECT * FROM synthetic_test_suite;`);
}
15 changes: 15 additions & 0 deletions lib/pattern/info-assurance-policies/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,18 @@ To execute test and ensure that `surveilr` is functioning correctly:

The `-A` flag provides all necessary permissions for the tests to run, including
file system access and network permissions.

## How to Run the Tap Tests

### RUN THIS TEST this using CLI

`rm -f tap.sql && deno run -A ./tap.sql.ts > tap.sql`

This will generate a tap.sql file from tap.sql.ts

`cat tap.sql | sqlite3 resource-surveillance.sqlite.db && sqlite3 resource-surveillance.sqlite.db -cmd "
SELECT * FROM synthetic_test_suite;"`

This script demonstrates how to create a Test Anything Protocol (TAP) report
using SQLite, following TAP version 14. It includes multiple test cases, and
subtests are formatted with indentation per TAP 14's subtest style.
Loading

0 comments on commit b562e7a

Please sign in to comment.