summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2025-06-19 11:40:27 -0600
committermo khan <mo@mokhan.ca>2025-06-19 11:40:27 -0600
commitf08ef97f4894fde8feff08bb91bed1e1c1105b83 (patch)
treefad101bae3ff8a1a9d7e2d312425c7fc89a08203
parent594abe311e4dfbbcc477474561756bc5e5f0e539 (diff)
test: add tests
-rw-r--r--Cargo.lock302
-rw-r--r--Cargo.toml12
-rw-r--r--src/authorization.rs104
-rw-r--r--src/lib.rs3
-rw-r--r--src/main.rs35
-rw-r--r--tests/common/mod.rs35
-rw-r--r--tests/integration_tests.rs50
7 files changed, 501 insertions, 40 deletions
diff --git a/Cargo.lock b/Cargo.lock
index cfb0a9d7..9d1c3bdc 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -18,12 +18,43 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa"
[[package]]
+name = "aho-corasick"
+version = "1.1.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
+dependencies = [
+ "memchr",
+]
+
+[[package]]
name = "anyhow"
version = "1.0.98"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e16d2d3311acee920a9eb8d33b8cbc1787ce4a264e85f964c2404b969bdcd487"
[[package]]
+name = "async-stream"
+version = "0.3.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0b5a71a6f37880a80d1d7f19efd781e4b5de42c88f0722cc13bcb6cc2cfe8476"
+dependencies = [
+ "async-stream-impl",
+ "futures-core",
+ "pin-project-lite",
+]
+
+[[package]]
+name = "async-stream-impl"
+version = "0.3.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c7c24de15d275a1ecfd47a380fb4d5ec9bfe0933f309ed5e705b775596a3574d"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
name = "async-trait"
version = "0.1.88"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -46,7 +77,9 @@ version = "0.1.0"
dependencies = [
"envoy-types",
"tokio",
+ "tokio-test",
"tonic",
+ "tonic-build",
"tonic-health",
"tonic-reflection",
]
@@ -118,6 +151,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
[[package]]
+name = "bitflags"
+version = "2.9.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967"
+
+[[package]]
name = "bytes"
version = "1.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -142,7 +181,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "065b6b0018b25902cab074d44c0e2098205329b6b5a309a33cc688bc0ac9573d"
dependencies = [
"futures-core",
- "prost",
+ "prost 0.13.5",
"tonic",
]
@@ -153,6 +192,28 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f"
[[package]]
+name = "errno"
+version = "0.3.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cea14ef9355e3beab063703aa9dab15afd25f0667c341310c1e5274bb1d0da18"
+dependencies = [
+ "libc",
+ "windows-sys 0.59.0",
+]
+
+[[package]]
+name = "fastrand"
+version = "2.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be"
+
+[[package]]
+name = "fixedbitset"
+version = "0.4.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80"
+
+[[package]]
name = "fnv"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -198,6 +259,18 @@ dependencies = [
]
[[package]]
+name = "getrandom"
+version = "0.3.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "26145e563e54f2cadc477553f1ec5ee650b00862f0a58bcd12cbdc5f0ea2d2f4"
+dependencies = [
+ "cfg-if",
+ "libc",
+ "r-efi",
+ "wasi 0.14.2+wasi-0.2.4",
+]
+
+[[package]]
name = "gimli"
version = "0.31.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -229,6 +302,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5971ac85611da7067dbfcabef3c70ebb5606018acd9e2a3903a0da507521e0d5"
[[package]]
+name = "heck"
+version = "0.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
+
+[[package]]
name = "http"
version = "1.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -341,6 +420,15 @@ dependencies = [
[[package]]
name = "itertools"
+version = "0.12.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569"
+dependencies = [
+ "either",
+]
+
+[[package]]
+name = "itertools"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285"
@@ -361,6 +449,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1171693293099992e19cddea4e8b849964e9846f4acee11b3948bcc337be8776"
[[package]]
+name = "linux-raw-sys"
+version = "0.9.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cd945864f07fe9f5371a27ad7b52a172b4b499999f1d97574c9fa68373937e12"
+
+[[package]]
+name = "log"
+version = "0.4.27"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94"
+
+[[package]]
name = "matchit"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -394,11 +494,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78bed444cc8a2160f01cbcf811ef18cac863ad68ae8ca62092e8db51d51c761c"
dependencies = [
"libc",
- "wasi",
+ "wasi 0.11.1+wasi-snapshot-preview1",
"windows-sys 0.59.0",
]
[[package]]
+name = "multimap"
+version = "0.10.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1d87ecb2933e8aeadb3e3a02b828fed80a7528047e68b4f424523a0981a3a084"
+
+[[package]]
name = "object"
version = "0.36.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -420,6 +526,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e"
[[package]]
+name = "petgraph"
+version = "0.6.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b4c5cc86750666a3ed20bdaf5ca2a0344f9c67674cae0515bec2da16fbaa47db"
+dependencies = [
+ "fixedbitset",
+ "indexmap",
+]
+
+[[package]]
name = "pin-project"
version = "1.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -452,6 +568,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
+name = "prettyplease"
+version = "0.2.34"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6837b9e10d61f45f987d50808f83d1ee3d206c66acf650c3e4ae2e1f6ddedf55"
+dependencies = [
+ "proc-macro2",
+ "syn",
+]
+
+[[package]]
name = "proc-macro2"
version = "1.0.95"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -462,12 +588,56 @@ dependencies = [
[[package]]
name = "prost"
+version = "0.12.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "deb1435c188b76130da55f17a466d252ff7b1418b2ad3e037d127b94e3411f29"
+dependencies = [
+ "bytes",
+ "prost-derive 0.12.6",
+]
+
+[[package]]
+name = "prost"
version = "0.13.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2796faa41db3ec313a31f7624d9286acf277b52de526150b7e69f3debf891ee5"
dependencies = [
"bytes",
- "prost-derive",
+ "prost-derive 0.13.5",
+]
+
+[[package]]
+name = "prost-build"
+version = "0.12.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "22505a5c94da8e3b7c2996394d1c933236c4d743e81a410bcca4e6989fc066a4"
+dependencies = [
+ "bytes",
+ "heck",
+ "itertools 0.12.1",
+ "log",
+ "multimap",
+ "once_cell",
+ "petgraph",
+ "prettyplease",
+ "prost 0.12.6",
+ "prost-types 0.12.6",
+ "regex",
+ "syn",
+ "tempfile",
+]
+
+[[package]]
+name = "prost-derive"
+version = "0.12.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "81bddcdb20abf9501610992b6759a4c888aef7d1a7247ef75e2404275ac24af1"
+dependencies = [
+ "anyhow",
+ "itertools 0.12.1",
+ "proc-macro2",
+ "quote",
+ "syn",
]
[[package]]
@@ -477,7 +647,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a56d757972c98b346a9b766e3f02746cde6dd1cd1d1d563472929fdd74bec4d"
dependencies = [
"anyhow",
- "itertools",
+ "itertools 0.14.0",
"proc-macro2",
"quote",
"syn",
@@ -485,11 +655,20 @@ dependencies = [
[[package]]
name = "prost-types"
+version = "0.12.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9091c90b0a32608e984ff2fa4091273cbdd755d54935c51d520887f4a1dbd5b0"
+dependencies = [
+ "prost 0.12.6",
+]
+
+[[package]]
+name = "prost-types"
version = "0.13.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "52c2c1bf36ddb1a1c396b3601a3cec27c2462e45f07c386894ec3ccf5332bd16"
dependencies = [
- "prost",
+ "prost 0.13.5",
]
[[package]]
@@ -502,12 +681,60 @@ dependencies = [
]
[[package]]
+name = "r-efi"
+version = "5.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"
+
+[[package]]
+name = "regex"
+version = "1.11.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191"
+dependencies = [
+ "aho-corasick",
+ "memchr",
+ "regex-automata",
+ "regex-syntax",
+]
+
+[[package]]
+name = "regex-automata"
+version = "0.4.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908"
+dependencies = [
+ "aho-corasick",
+ "memchr",
+ "regex-syntax",
+]
+
+[[package]]
+name = "regex-syntax"
+version = "0.8.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
+
+[[package]]
name = "rustc-demangle"
version = "0.1.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "989e6739f80c4ad5b13e0fd7fe89531180375b18520cc8c82080e4dc4035b84f"
[[package]]
+name = "rustix"
+version = "1.0.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c71e83d6afe7ff64890ec6b71d6a69bb8a610ab78ce364b3352876bb4c801266"
+dependencies = [
+ "bitflags",
+ "errno",
+ "libc",
+ "linux-raw-sys",
+ "windows-sys 0.59.0",
+]
+
+[[package]]
name = "rustversion"
version = "1.0.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -573,6 +800,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263"
[[package]]
+name = "tempfile"
+version = "3.20.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e8a64e3985349f2441a1a9ef0b853f869006c3855f2cda6862a94d26ebb9d6a1"
+dependencies = [
+ "fastrand",
+ "getrandom",
+ "once_cell",
+ "rustix",
+ "windows-sys 0.59.0",
+]
+
+[[package]]
name = "tokio"
version = "1.45.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -612,6 +852,19 @@ dependencies = [
]
[[package]]
+name = "tokio-test"
+version = "0.4.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2468baabc3311435b55dd935f702f42cd1b8abb7e754fb7dfb16bd36aa88f9f7"
+dependencies = [
+ "async-stream",
+ "bytes",
+ "futures-core",
+ "tokio",
+ "tokio-stream",
+]
+
+[[package]]
name = "tokio-util"
version = "0.7.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -643,7 +896,7 @@ dependencies = [
"hyper-util",
"percent-encoding",
"pin-project",
- "prost",
+ "prost 0.13.5",
"socket2",
"tokio",
"tokio-stream",
@@ -654,12 +907,25 @@ dependencies = [
]
[[package]]
+name = "tonic-build"
+version = "0.10.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9d021fc044c18582b9a2408cd0dd05b1596e3ecdb5c4df822bb0183545683889"
+dependencies = [
+ "prettyplease",
+ "proc-macro2",
+ "prost-build",
+ "quote",
+ "syn",
+]
+
+[[package]]
name = "tonic-health"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb87334d340313fefa513b6e60794d44a86d5f039b523229c99c323e4e19ca4b"
dependencies = [
- "prost",
+ "prost 0.13.5",
"tokio",
"tokio-stream",
"tonic",
@@ -671,8 +937,8 @@ version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9687bd5bfeafebdded2356950f278bba8226f0b32109537c4253406e09aafe1"
dependencies = [
- "prost",
- "prost-types",
+ "prost 0.13.5",
+ "prost-types 0.13.5",
"tokio",
"tokio-stream",
"tonic",
@@ -768,6 +1034,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b"
[[package]]
+name = "wasi"
+version = "0.14.2+wasi-0.2.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9683f9a5a998d873c0d21fcbe3c083009670149a8fab228644b8bd36b2c48cb3"
+dependencies = [
+ "wit-bindgen-rt",
+]
+
+[[package]]
name = "windows-sys"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -848,3 +1123,12 @@ name = "windows_x86_64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
+
+[[package]]
+name = "wit-bindgen-rt"
+version = "0.39.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1"
+dependencies = [
+ "bitflags",
+]
diff --git a/Cargo.toml b/Cargo.toml
index fc79f61b..a13b9464 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -3,9 +3,21 @@ name = "authzd"
version = "0.1.0"
edition = "2024"
+[[bin]]
+name = "authzd"
+path = "src/main.rs"
+
+[lib]
+name = "authzd"
+path = "src/lib.rs"
+
[dependencies]
envoy-types = "0.6.0"
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
tonic = "*"
tonic-health = "0.13.1"
tonic-reflection = "0.13.1"
+
+[dev-dependencies]
+tokio-test = "0.4"
+tonic-build = "0.10"
diff --git a/src/authorization.rs b/src/authorization.rs
new file mode 100644
index 00000000..22cd695c
--- /dev/null
+++ b/src/authorization.rs
@@ -0,0 +1,104 @@
+use envoy_types::ext_authz::v3::CheckResponseExt;
+use envoy_types::ext_authz::v3::pb::{CheckRequest, CheckResponse};
+use tonic::{Request, Response, Status};
+
+#[derive(Debug, Default)]
+pub struct PolicyServer;
+
+#[tonic::async_trait]
+impl envoy_types::ext_authz::v3::pb::Authorization for PolicyServer {
+ async fn check(
+ &self,
+ request: Request<CheckRequest>,
+ ) -> Result<Response<CheckResponse>, Status> {
+ let request = request.into_inner();
+
+ let client_headers = request
+ .attributes
+ .as_ref()
+ .and_then(|attr| attr.request.as_ref())
+ .and_then(|req| req.http.as_ref())
+ .map(|http| &http.headers)
+ .ok_or_else(|| Status::invalid_argument("client headers not populated by envoy"))?;
+
+ let mut request_status = Status::unauthenticated("not authorized");
+
+ if let Some(authorization) = client_headers.get("authorization") {
+ if authorization == "Bearer valid-token" {
+ request_status = Status::ok("request is valid");
+ }
+ }
+
+ Ok(Response::new(CheckResponse::with_status(request_status)))
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use envoy_types::ext_authz::v3::pb::{Authorization, CheckRequest};
+ use std::collections::HashMap;
+ use tonic::Request;
+
+ fn create_test_request_with_headers(headers: HashMap<String, String>) -> Request<CheckRequest> {
+ use envoy_types::pb::envoy::service::auth::v3::{AttributeContext, attribute_context};
+
+ let http_request = attribute_context::HttpRequest {
+ headers,
+ ..Default::default()
+ };
+
+ let request_context = attribute_context::Request {
+ http: Some(http_request),
+ ..Default::default()
+ };
+
+ let attributes = AttributeContext {
+ request: Some(request_context),
+ ..Default::default()
+ };
+
+ let check_request = CheckRequest {
+ attributes: Some(attributes),
+ ..Default::default()
+ };
+
+ Request::new(check_request)
+ }
+
+ fn create_headers_with_auth(auth_value: &str) -> HashMap<String, String> {
+ let mut headers = HashMap::new();
+ headers.insert("authorization".to_string(), auth_value.to_string());
+ headers
+ }
+
+ #[tokio::test]
+ async fn test_check_allows_valid_bearer_token() {
+ let token = String::from("valid-token");
+ let server = PolicyServer::default();
+ let headers = create_headers_with_auth(&format!("Bearer {}", token));
+ let request = create_test_request_with_headers(headers);
+
+ let response = server.check(request).await;
+
+ assert!(response.is_ok());
+ let check_response = response.unwrap().into_inner();
+ assert!(check_response.status.is_some());
+ let status = check_response.status.unwrap();
+ assert_eq!(status.code, tonic::Code::Ok.into());
+ }
+
+ #[tokio::test]
+ async fn test_check_denies_invalid_bearer_token() {
+ let server = PolicyServer::default();
+ let request = create_test_request_with_headers(HashMap::new());
+
+ let response = server.check(request).await;
+
+ assert!(response.is_ok());
+ let check_response = response.unwrap().into_inner();
+ assert!(check_response.status.is_some());
+ let status = check_response.status.unwrap();
+ assert_eq!(status.code, tonic::Code::Unauthenticated.into());
+ }
+}
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 00000000..cb28e34b
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1,3 @@
+pub mod authorization;
+
+pub use authorization::PolicyServer;
diff --git a/src/main.rs b/src/main.rs
index 05d57719..25e2c88f 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,35 +1,8 @@
-use envoy_types::ext_authz::v3::pb::{
- Authorization, AuthorizationServer, CheckRequest, CheckResponse,
-};
-use envoy_types::ext_authz::v3::{CheckRequestExt, CheckResponseExt};
-use tonic::{Request, Response, Status, transport::Server};
+use envoy_types::ext_authz::v3::pb::AuthorizationServer;
+use tonic::transport::Server;
-#[derive(Debug, Default)]
-struct PolicyServer;
-
-#[tonic::async_trait]
-impl Authorization for PolicyServer {
- async fn check(
- &self,
- request: Request<CheckRequest>,
- ) -> Result<Response<CheckResponse>, Status> {
- let request = request.into_inner();
-
- let client_headers = request
- .get_client_headers()
- .ok_or_else(|| Status::invalid_argument("client headers not populated by envoy"))?;
-
- let mut request_status = Status::unauthenticated("not authorized");
-
- if let Some(authorization) = client_headers.get("authorization") {
- if authorization == "Bearer valid-token" {
- request_status = Status::ok("request is valid");
- }
- }
-
- Ok(Response::new(CheckResponse::with_status(request_status)))
- }
-}
+pub mod authorization;
+use authorization::PolicyServer;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
diff --git a/tests/common/mod.rs b/tests/common/mod.rs
new file mode 100644
index 00000000..e2137146
--- /dev/null
+++ b/tests/common/mod.rs
@@ -0,0 +1,35 @@
+use envoy_types::ext_authz::v3::pb::CheckRequest;
+use std::collections::HashMap;
+use tonic::Request;
+
+pub fn create_test_request_with_headers(headers: HashMap<String, String>) -> Request<CheckRequest> {
+ use envoy_types::pb::envoy::service::auth::v3::{AttributeContext, attribute_context};
+
+ let http_request = attribute_context::HttpRequest {
+ headers,
+ ..Default::default()
+ };
+
+ let request_context = attribute_context::Request {
+ http: Some(http_request),
+ ..Default::default()
+ };
+
+ let attributes = AttributeContext {
+ request: Some(request_context),
+ ..Default::default()
+ };
+
+ let check_request = CheckRequest {
+ attributes: Some(attributes),
+ ..Default::default()
+ };
+
+ Request::new(check_request)
+}
+
+pub fn create_headers_with_auth(auth_value: &str) -> HashMap<String, String> {
+ let mut headers = HashMap::new();
+ headers.insert("authorization".to_string(), auth_value.to_string());
+ headers
+}
diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs
new file mode 100644
index 00000000..28cdf959
--- /dev/null
+++ b/tests/integration_tests.rs
@@ -0,0 +1,50 @@
+use authzd::PolicyServer;
+use envoy_types::ext_authz::v3::pb::Authorization;
+
+mod common;
+
+#[tokio::test]
+async fn test_success_response() {
+ let server = PolicyServer::default();
+
+ let headers = common::create_headers_with_auth("Bearer valid-token");
+ let request = common::create_test_request_with_headers(headers);
+
+ let response = server.check(request).await;
+ assert!(response.is_ok());
+
+ let check_response = response.unwrap().into_inner();
+ assert!(check_response.status.is_some());
+
+ let status = check_response.status.unwrap();
+ assert_eq!(status.code, tonic::Code::Ok.into());
+}
+
+#[tokio::test]
+async fn test_multiple() {
+ let server = PolicyServer::default();
+
+ let test_cases = vec![
+ ("Bearer valid-token", true),
+ ("Bearer invalid-token", false),
+ ("Basic valid-token", false),
+ ("", false),
+ ];
+
+ for (auth_value, should_succeed) in test_cases {
+ let headers = common::create_headers_with_auth(auth_value);
+ let request = common::create_test_request_with_headers(headers);
+
+ let response = server.check(request).await;
+ assert!(response.is_ok());
+
+ let check_response = response.unwrap().into_inner();
+ let status = check_response.status.unwrap();
+
+ if should_succeed {
+ assert_eq!(status.code, tonic::Code::Ok.into());
+ } else {
+ assert_eq!(status.code, tonic::Code::Unauthenticated.into());
+ }
+ }
+}