From ea8cd1ba7fb36afb039f31ec4ba000a2b7271700 Mon Sep 17 00:00:00 2001 From: Simon Hornby Date: Wed, 1 Feb 2023 13:12:00 +0200 Subject: [PATCH] chore: improve tests for redis provider (#28) --- server/src/data_sources/redis_provider.rs | 10 ++++- server/tests/redis_test.rs | 55 ++++++++++++++++++----- 2 files changed, 54 insertions(+), 11 deletions(-) diff --git a/server/src/data_sources/redis_provider.rs b/server/src/data_sources/redis_provider.rs index 1ab339ae..6d79da9f 100644 --- a/server/src/data_sources/redis_provider.rs +++ b/server/src/data_sources/redis_provider.rs @@ -36,6 +36,7 @@ impl FeaturesProvider for RedisProvider { fn get_client_features(&self, _token: EdgeToken) -> EdgeResult { let mut client = self.client.write().unwrap(); let client_features: String = client.get(FEATURE_KEY)?; + serde_json::from_str::(&client_features).map_err(EdgeError::from) } } @@ -44,7 +45,14 @@ impl TokenProvider for RedisProvider { fn get_known_tokens(&self) -> EdgeResult> { let mut client = self.client.write().unwrap(); let tokens: String = client.get(TOKENS_KEY)?; - serde_json::from_str::>(&tokens).map_err(EdgeError::from) + + let raw_tokens = serde_json::from_str::>(&tokens)?; + + Ok(raw_tokens + .into_iter() + .map(EdgeToken::try_from) + .filter_map(|t| t.ok()) + .collect()) } fn secret_is_valid(&self, secret: &str) -> EdgeResult { diff --git a/server/tests/redis_test.rs b/server/tests/redis_test.rs index 81b310f2..1fe7c3f3 100644 --- a/server/tests/redis_test.rs +++ b/server/tests/redis_test.rs @@ -1,31 +1,66 @@ use std::fs; -use redis::Commands; -use testcontainers::{clients, images}; +use redis::{Client, Commands}; +use testcontainers::{clients::Cli, images::redis::Redis, Container}; use unleash_edge::{ - data_sources::redis_provider::{RedisProvider, FEATURE_KEY}, + data_sources::redis_provider::{RedisProvider, FEATURE_KEY, TOKENS_KEY}, types::{EdgeProvider, EdgeToken}, }; -#[tokio::test] -async fn redis_provider_returns_expected_data() { - let docker = clients::Cli::default(); - let node = docker.run(images::redis::Redis::default()); +const TOKEN: &str = "*:development.03fa5f506428fe80ed5640c351c7232e38940814d2923b08f5c05fa7"; + +fn setup_redis(docker: &Cli) -> (Client, String, Container) { + let node: Container = docker.run(Redis::default()); let host_port = node.get_host_port_ipv4(6379); let url = format!("redis://127.0.0.1:{host_port}"); - let mut client = redis::Client::open(url.clone()).unwrap(); + (redis::Client::open(url.clone()).unwrap(), url, node) +} + +#[tokio::test] +async fn redis_provider_returns_expected_data() { + let docker = Cli::default(); + let (mut client, url, _node) = setup_redis(&docker); let content = fs::read_to_string("../examples/features.json").expect("Could not read file"); - //Wants a type annotation but we don't care about the result so we immediately discard the data coming back + //This wants a type hint but we don't care about the result so we immediately discard the data coming back let _: () = client.set(FEATURE_KEY, content).unwrap(); let provider: Box = Box::new(RedisProvider::new(&url).unwrap()); let features = provider - .get_client_features(EdgeToken::try_from("secret-123".to_string()).unwrap()) + .get_client_features(EdgeToken::try_from(TOKEN.to_string()).unwrap()) .unwrap(); assert!(!features.features.is_empty()); } + +#[tokio::test] +async fn redis_provider_returns_token_info() { + let docker = Cli::default(); + let (mut client, url, _node) = setup_redis(&docker); + + let _: () = client.set(TOKENS_KEY, format!("[\"{TOKEN}\"]")).unwrap(); + + let provider: Box = Box::new(RedisProvider::new(&url).unwrap()); + + let tokens = provider.get_known_tokens().unwrap(); + assert_eq!( + *tokens[0].environment.as_ref().unwrap(), + "development".to_string() + ); +} + +#[tokio::test] +async fn redis_provider_correctly_determines_secret_to_be_valid() { + let docker = Cli::default(); + let (mut client, url, _node) = setup_redis(&docker); + + let _: () = client.set(TOKENS_KEY, format!("[\"{TOKEN}\"]")).unwrap(); + + let provider: Box = Box::new(RedisProvider::new(&url).unwrap()); + + let is_valid_token = provider.secret_is_valid(TOKEN).unwrap(); + assert!(is_valid_token) +}