Browse Source

Added initial tavern tests

develop-api-fb
chodak166 3 months ago
parent
commit
ab7079c30a
  1. 8
      README.md
  2. 4
      apps/app_api/src/api/v1.rs
  3. 0
      apps/app_api/src/api/v1/info.rs
  4. 6
      config.toml
  5. 6
      lib/src/auth/infrastructure/jwt.rs
  6. 14
      tavern-tests/export.sh
  7. 3
      tavern-tests/requirements.txt
  8. 13
      tavern-tests/tavern-run-all.sh
  9. 16
      tavern-tests/tavern-run-single.sh
  10. 29
      tavern-tests/test_plans/common_stages.yaml
  11. 5
      tavern-tests/test_plans/includes.yaml
  12. 19
      tavern-tests/test_plans/info_test.tavern.yaml

8
README.md

@ -77,7 +77,7 @@ The application supports two authentication methods:
4. Make authenticated requests:
```bash
curl -X GET "http://localhost:3000/api/v1/health" \
curl -X GET "http://localhost:3000/api/v1/info" \
-H "Authorization: Bearer <your-firebase-token>"
```
@ -91,7 +91,7 @@ The application supports two authentication methods:
2. Make authenticated requests:
```bash
curl -X GET "http://localhost:3000/api/v1/health" \
curl -X GET "http://localhost:3000/api/v1/info" \
-H "X-API-Key: my-secret-api-key"
```
@ -109,9 +109,9 @@ cargo run --release -- listen --host 127.0.0.1 --port 8080
### API Endpoints
#### Health Check
#### info Check
```bash
GET /api/v1/health
GET /api/v1/info
```
#### Dictionary Management

4
apps/app_api/src/api/v1.rs

@ -1,6 +1,6 @@
pub mod auth;
pub mod dictionary;
pub mod health;
pub mod info;
pub mod major;
use crate::state::AppState;
@ -12,7 +12,7 @@ use std::sync::Arc;
pub fn routes(state: Arc<AppState>) -> Router<Arc<AppState>> {
Router::new()
.nest("/health", health::routes())
.nest("/info", info::routes())
.nest("/auth", auth::routes())
.nest("/dicts", dictionary::routes())
.nest("/major", major::routes())

0
apps/app_api/src/api/v1/health.rs → apps/app_api/src/api/v1/info.rs

6
config.toml

@ -5,9 +5,9 @@ host = "0.0.0.0"
port = 3000
[auth]
firebase_project_id = "test-project"
firebase_emulator_url = "http://192.168.1.23:9099"
api_tokens = ["dev-api-key"]
firebase_project_id = "phomnemic"
# firebase_emulator_url = "http://192.168.1.23:9099"
api_tokens = ["AIzaSyCCgWH9Qg5vLTMFYLTuU0tyLFKBgtBkucE", "test-api-key"]
[database]
url = "sqlite:app.db"

6
lib/src/auth/infrastructure/jwt.rs

@ -168,7 +168,7 @@ impl JwtAuthenticator {
format!("{}/.well-known/jwks.json", emulator.trim_end_matches('/'))
} else {
format!(
"https://www.googleapis.com/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com"
"https://www.googleapis.com/service_accounts/v1/jwk/securetoken@system.gserviceaccount.com"
)
};
@ -243,7 +243,9 @@ impl Authenticator for JwtAuthenticator {
.kid
.ok_or_else(|| AuthError::AuthenticationFailed("Token missing key ID".to_string()))?;
let public_keys = self.get_public_keys().await?;
let public_keys = self.get_public_keys().await.map_err(|e| {
AuthError::AuthenticationFailed(format!("Failed to fetch public keys: {}", e))
})?;
let decoding_key = public_keys
.get(&kid)
.ok_or_else(|| AuthError::AuthenticationFailed("Unknown key ID".to_string()))?;

14
tavern-tests/export.sh

@ -0,0 +1,14 @@
if [ -d .venv ]; then
source .venv/bin/activate
else
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
fi
export TEST_SERVER_ADDRESS="127.0.0.1:3000"
export TEST_API_BASE="/api/v1"
export TEST_API_KEY="test-api-key"
export TEST_USER_ID="test-user-id"

3
tavern-tests/requirements.txt

@ -0,0 +1,3 @@
pyyaml
tavern
allure-pytest

13
tavern-tests/tavern-run-all.sh

@ -0,0 +1,13 @@
#!/usr/bin/env bash
if [ -z "$TEST_SERVER_ADDRESS" ]; then
source export.sh
fi
tavern-ci --alluredir=reports test_plans/info_test.tavern.yaml
if command -v allure > /dev/null; then
allure generate --clean --single-file --output /tmp/vm-allure-report --name index.html reports
fi
# allure package: https://github.com/allure-framework/allure2/releases/download/2.34.0/allure_2.34.0-1_all.deb

16
tavern-tests/tavern-run-single.sh

@ -0,0 +1,16 @@
#!/usr/bin/env bash
if [ -z "$1" ]; then
echo "Usage: $0 <test plan>"
exit 1
fi
if [ -z "$TEST_SERVER_ADDRESS" ]; then
source export.sh
fi
tavern-ci --alluredir=reports $1
allure generate --clean --single-file --output /tmp/vm-allure-report --name index.html reports
# allure package: https://github.com/allure-framework/allure2/releases/download/2.34.0/allure_2.34.0-1_all.deb

29
tavern-tests/test_plans/common_stages.yaml

@ -0,0 +1,29 @@
---
stages:
- id: clear_test_user
name: "Purge test user data"
request:
url: "http://{server_address}/{api_base}/user/{test_user_id}/clear"
method: POST
headers:
Content-Type: application/json
Authorization: Bearer {api_key}
response:
strict: False
status_code: 200
- id: register_test_user
name: "Register test user"
request:
url: "http://{server_address}/{api_base}/user"
method: POST
headers:
Content-Type: application/json
Authorization: Bearer {api_key}
body:
username: {test_user_id}
password: password
response:
strict: False
status_code: 201

5
tavern-tests/test_plans/includes.yaml

@ -0,0 +1,5 @@
variables:
server_address: "{tavern.env_vars.TEST_SERVER_ADDRESS}"
api_base: "{tavern.env_vars.TEST_API_BASE}"
api_key: "{tavern.env_vars.TEST_API_KEY}"
user_id: "{tavern.env_vars.TEST_USER_ID}"

19
tavern-tests/test_plans/info_test.tavern.yaml

@ -0,0 +1,19 @@
test_name: "Test server API info endpoint"
includes:
- !include includes.yaml
stages:
- name: "Check version"
request:
url: "http://{server_address}/{api_base}/info/version"
method: GET
headers:
X-API-Key: "{api_key}"
response:
strict: False
status_code: 200
json:
name: !anystr
version: !anystr
Loading…
Cancel
Save