From 1d75e540d5b9551a8f90adb3168d2f8d7324b2e3 Mon Sep 17 00:00:00 2001 From: Ewan Harris Date: Mon, 9 Sep 2024 09:23:56 +0100 Subject: [PATCH] test: add integration tests for store import and tuple write/delete This is the first example of how we can potentially create and reuse stores and models between tests based on the learnings from Auth0 CLI testing. --- .gitignore | 3 +++ tests/fixtures/basic-model.fga | 8 ++++++++ tests/fixtures/basic-store.fga.yaml | 10 ++++++++++ tests/fixtures/basic-tuples.json | 7 +++++++ tests/import-tests-cases.yaml | 10 ++++++++++ tests/scripts/run-test-suites.sh | 2 ++ tests/scripts/test-data/get-model-id.sh | 22 ++++++++++++++++++++++ tests/scripts/test-data/get-store-id.sh | 13 +++++++++++++ tests/tuple-test-cases.yaml | 16 ++++++++++++++++ 9 files changed, 91 insertions(+) create mode 100644 tests/fixtures/basic-model.fga create mode 100644 tests/fixtures/basic-store.fga.yaml create mode 100644 tests/fixtures/basic-tuples.json create mode 100644 tests/import-tests-cases.yaml mode change 100644 => 100755 tests/scripts/run-test-suites.sh create mode 100755 tests/scripts/test-data/get-model-id.sh create mode 100755 tests/scripts/test-data/get-store-id.sh create mode 100644 tests/tuple-test-cases.yaml diff --git a/.gitignore b/.gitignore index a890128..93914cb 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,6 @@ dist/ # Mock source files /mocks + +# Test files +/tests/fixtures/identifiers \ No newline at end of file diff --git a/tests/fixtures/basic-model.fga b/tests/fixtures/basic-model.fga new file mode 100644 index 0000000..954daa4 --- /dev/null +++ b/tests/fixtures/basic-model.fga @@ -0,0 +1,8 @@ +model + schema 1.1 + +type user + +type group + relations + define owner: [user] \ No newline at end of file diff --git a/tests/fixtures/basic-store.fga.yaml b/tests/fixtures/basic-store.fga.yaml new file mode 100644 index 0000000..064dfbd --- /dev/null +++ b/tests/fixtures/basic-store.fga.yaml @@ -0,0 +1,10 @@ +name: Basic Store +model_file: basic-model.fga +tuple_file: basic-tuples.json +tests: + - name: test-1 + check: + - user: user:anne + object: group:foo + assertions: + owner: true \ No newline at end of file diff --git a/tests/fixtures/basic-tuples.json b/tests/fixtures/basic-tuples.json new file mode 100644 index 0000000..017ea76 --- /dev/null +++ b/tests/fixtures/basic-tuples.json @@ -0,0 +1,7 @@ +[ + { + "user": "user:anne", + "relation": "owner", + "object": "group:foo" + } +] \ No newline at end of file diff --git a/tests/import-tests-cases.yaml b/tests/import-tests-cases.yaml new file mode 100644 index 0000000..565054b --- /dev/null +++ b/tests/import-tests-cases.yaml @@ -0,0 +1,10 @@ +config: + inherit-env: true + +tests: + 001 - it successfully imports a store: + command: fga store import --file=./tests/fixtures/basic-store.fga.yaml --max-parallel-requests=1 --max-tuples-per-write=1 + exit-code: 0 + stdout: + json: + store.name: "Basic Store" diff --git a/tests/scripts/run-test-suites.sh b/tests/scripts/run-test-suites.sh old mode 100644 new mode 100755 index 16ba11e..75e72f9 --- a/tests/scripts/run-test-suites.sh +++ b/tests/scripts/run-test-suites.sh @@ -11,4 +11,6 @@ exit_code=$? docker stop openfga-cli-tests docker rm openfga-cli-tests +rm -rf tests/fixtures/identifiers + exit $exit_code diff --git a/tests/scripts/test-data/get-model-id.sh b/tests/scripts/test-data/get-model-id.sh new file mode 100755 index 0000000..458f9b9 --- /dev/null +++ b/tests/scripts/test-data/get-model-id.sh @@ -0,0 +1,22 @@ +#! /bin/bash + +FILE=./tests/fixtures/identifiers/model-id +if [ -f "$FILE" ]; then + cat $FILE + exit 0 +fi + +STORE_ID="" +STORE_FILE=./tests/fixtures/identifiers/store-id +if [ -f "$STORE_FILE" ]; then + STORE_ID=$( cat $STORE_FILE ) +else + echo "no store created, must create a store before a model" + exit 1 +fi + +model=$( fga model write --file=./tests/fixtures/basic-model.fga --store-id="$STORE_ID" ) + +mkdir -p ./tests/fixtures/identifiers +echo "$model" | jq -r ".authorization_model_id" > $FILE +cat $FILE \ No newline at end of file diff --git a/tests/scripts/test-data/get-store-id.sh b/tests/scripts/test-data/get-store-id.sh new file mode 100755 index 0000000..b9c927e --- /dev/null +++ b/tests/scripts/test-data/get-store-id.sh @@ -0,0 +1,13 @@ +#! /bin/bash + +FILE=./tests/fixtures/identifiers/store-id +if [ -f "$FILE" ]; then + cat $FILE + exit 0 +fi + +store=$( fga store create --name "integration-test-store" ) + +mkdir -p ./tests/fixtures/identifiers +echo "$store" | jq -r ".store.id" > $FILE +cat $FILE \ No newline at end of file diff --git a/tests/tuple-test-cases.yaml b/tests/tuple-test-cases.yaml new file mode 100644 index 0000000..53dc475 --- /dev/null +++ b/tests/tuple-test-cases.yaml @@ -0,0 +1,16 @@ +config: + inherit-env: true + +tests: + 001 - it successfully writes tuples to a store: + command: fga tuple write --file=./tests/fixtures/basic-tuples.json --max-tuples-per-write=1 --max-parallel-requests=1 --store-id=$(./tests/scripts/test-data/get-store-id.sh) --model-id=$(./tests/scripts/test-data/get-model-id.sh) + exit-code: 0 + stdout: + json: + successful.0.user: "user:anne" + 002 - it successfully deletes tuples from a store: + command: fga tuple delete --file=./tests/fixtures/basic-tuples.json --max-tuples-per-write=1 --max-parallel-requests=1 --store-id=$(./tests/scripts/test-data/get-store-id.sh) --model-id=$(./tests/scripts/test-data/get-model-id.sh) + exit-code: 0 + stdout: + json: + successful.0.user: "user:anne"