Skip to content

Commit

Permalink
chore: improve tests for redis provider (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
sighphyre authored Feb 1, 2023
1 parent 1677111 commit ea8cd1b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 11 deletions.
10 changes: 9 additions & 1 deletion server/src/data_sources/redis_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ impl FeaturesProvider for RedisProvider {
fn get_client_features(&self, _token: EdgeToken) -> EdgeResult<ClientFeatures> {
let mut client = self.client.write().unwrap();
let client_features: String = client.get(FEATURE_KEY)?;

serde_json::from_str::<ClientFeatures>(&client_features).map_err(EdgeError::from)
}
}
Expand All @@ -44,7 +45,14 @@ impl TokenProvider for RedisProvider {
fn get_known_tokens(&self) -> EdgeResult<Vec<EdgeToken>> {
let mut client = self.client.write().unwrap();
let tokens: String = client.get(TOKENS_KEY)?;
serde_json::from_str::<Vec<EdgeToken>>(&tokens).map_err(EdgeError::from)

let raw_tokens = serde_json::from_str::<Vec<String>>(&tokens)?;

Ok(raw_tokens
.into_iter()
.map(EdgeToken::try_from)
.filter_map(|t| t.ok())
.collect())
}

fn secret_is_valid(&self, secret: &str) -> EdgeResult<bool> {
Expand Down
55 changes: 45 additions & 10 deletions server/tests/redis_test.rs
Original file line number Diff line number Diff line change
@@ -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<Redis>) {
let node: Container<Redis> = 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<dyn EdgeProvider> = 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<dyn EdgeProvider> = 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<dyn EdgeProvider> = Box::new(RedisProvider::new(&url).unwrap());

let is_valid_token = provider.secret_is_valid(TOKEN).unwrap();
assert!(is_valid_token)
}

0 comments on commit ea8cd1b

Please sign in to comment.