From 1ee0dc70eb2fc8cd88b6665509d0d47d60935ba9 Mon Sep 17 00:00:00 2001 From: mast3r Date: Tue, 2 Apr 2024 11:40:23 +0200 Subject: [PATCH] implemented tests --- .github/workflows/api-test.yml | 23 +++++++ tests/api-test.sh | 108 +++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 .github/workflows/api-test.yml create mode 100644 tests/api-test.sh diff --git a/.github/workflows/api-test.yml b/.github/workflows/api-test.yml new file mode 100644 index 0000000..7cacb48 --- /dev/null +++ b/.github/workflows/api-test.yml @@ -0,0 +1,23 @@ +name: "Backend tests" + +on: + push: + branches: [main] + pull_request: + branches: [main] + +jobs: + test-all: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4.1.1 + with: + submodules: recursive + - name: setup-env + run: sudo apt install cmake sqlite3 gcc curl jq + - name: build + run: cmake -B build && cmake --build build + env: + CXX: g++-13 + - name: run-test + run: ./build/src/api_test --addrport localhost:8080 & bash ./tests/api-test.sh \ No newline at end of file diff --git a/tests/api-test.sh b/tests/api-test.sh new file mode 100644 index 0000000..8606573 --- /dev/null +++ b/tests/api-test.sh @@ -0,0 +1,108 @@ +addr="http://localhost:8080" +username="$(cat ./files/testdata/default.json | jq -r .user.username)" +password="$(cat ./files/testdata/default.json | jq -r .user.password)" +authjson="{"username":"$username", "password": "$password"}" + +# /register +curl -X 'POST' -d "$authjson" $addr/register + +# /auth +token=$(curl -X 'POST' -d "$authjson" $addr/auth) + +# /user/ids/ +ids=$(curl -X 'GET' $addr/ids) +# get the one user id +uid=$(echo "$ids" | jq -r .[0]) + +# /user/data/update +curl -X 'POST' -d "{\"token\":\"$token\", \"data\":{}}" $addr/user/data/update + +# /user/get/ +userdata=$(curl -X 'GET' $addr/user/get/$uid) + +# /journals/new +curl -X 'POST' -d "{\"token\":\"$token\", \"data\":[{\"question\":\"q1\", \"answer\":\"a1\"}]}" $addr/journals/new + +# /journals/ids/ +jids=$(curl -X 'GET' $addr/journals/ids/$uid) +# get the one journal +jid=$(echo "$jids" | jq -r .[0]) + +# /journals/get/ +journal=$(curl -X 'GET' $addr/journals/get/$jid) + +# /journals/delete// +curl -X 'DELETE' $addr/journals/delete/$uid/$jid + +# /settings/update +curl -X 'POST' -d "{\"token\":\"$token\", \"settings\":{\"key\":\"value\", \"key1\": \"value1\"}}" $addr/settings/update + +# /settings/get/ +settings=$(curl -X 'GET' $addr/settings/get/$uid) + +# /questions/defaults +questions=$(curl -X 'GET' $addr/questions/defaults) + +# /questions/get/ +default_questions=$(curl -X 'GET' $addr/questions/get/default) + +echo "---------------------TEST COMPLETE---------------------" +echo "addr: $addr" +echo "username: $username" +echo "password: $password" +echo "authjson: $authjson" +echo "token: $token" +echo "ids: $ids" +echo "uid: $uid" +echo "userdata: $userdata" +echo "jids: $jids" +echo "jid: $jid" +echo "journal: $journal" +echo "settings: $settings" +echo "questions: $questions" +echo "default questions: $default_questions" + +# verify if all data is intact +if [[ "$addr" == "" ]]; then + exit 1 +fi +if [[ "$username" == "" ]]; then + exit 1 +fi +if [[ "$password" == "" ]]; then + exit 1 +fi +if [[ "$authjson" == "" ]]; then + exit 1 +fi +if [[ "$token" == "" ]]; then + exit 1 +fi +if [[ "$ids" == "" ]]; then + exit 1 +fi +if [[ "$uid" == "" ]]; then + exit 1 +fi +if [[ "$userdata" == "" ]]; then + exit 1 +fi +if [[ "$jids" == "" ]]; then + exit 1 +fi +if [[ "$jid" == "" ]]; then + exit 1 +fi +if [[ "$journal" == "" ]]; then + exit 1 +fi +if [[ "$settings" == "" ]]; then + exit 1 +fi +if [[ "$questions" == "" ]]; then + exit 1 +fi +if [[ "$default_questions" == "" ]]; then + exit 1 +fi +exit 0 \ No newline at end of file