Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increased code coverage #80

Merged
merged 14 commits into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
540 changes: 299 additions & 241 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ tokio = { version = "1", features = ["full"] }
log = "0.4"
log4rs = "1"
hocon = "0.7"
sha2 = "0.9"
sha2 = "0.10"
hex = "0.4"
base64 = "0.13"
hyper-tls = "0.5"
Expand All @@ -36,10 +36,10 @@ rust-embed="6.3"
jemallocator = { version = "0.3", optional = true }

# deps for aws
aws-config = "0.2"
aws-sdk-rekognition = "0.2"
aws-sdk-s3 = "0.2"
aws-types = "0.2"
aws-config = "0.5.2"
aws-sdk-rekognition = "0.5.2"
aws-sdk-s3 = "0.5.2"
aws-types = "0.5.2"

# Overrides to address vulns
time = "0.3"
Expand Down
2 changes: 2 additions & 0 deletions dashboard-ui/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Image Proxy Admin Dashboard

**This UI is now deprecated and will be removed starting version 2.0**

This UI can be made available by running image proxy and accessing the /admin route. See [] for more information.

## `npm run start`
Expand Down
164 changes: 0 additions & 164 deletions src/db/dummy_db.rs

This file was deleted.

4 changes: 3 additions & 1 deletion src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use async_trait::async_trait;
use chrono::{DateTime, Utc};
use uuid::Uuid;

mod dummy_db;
mod postgres;

type GenericError = Box<dyn std::error::Error + Send + Sync>;
Expand Down Expand Up @@ -69,3 +68,6 @@ impl DatabaseFactory {
Ok(Box::new(db))
}
}

#[cfg(test)]
pub mod tests;
169 changes: 169 additions & 0 deletions src/db/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
use crate::{
moderation::{ModerationCategories, ModerationService},
utils::sha256,
};
use async_trait::async_trait;
use std::{collections::HashMap, sync::Mutex};
use uuid::Uuid;

use super::{DatabaseProvider, DbModerationRow, DbReportRow, Result};

pub struct DummyDatabase {
report_store: Mutex<HashMap<String, DbReportRow>>,
moderation_store: Mutex<HashMap<String, DbModerationRow>>,
}

impl Default for DummyDatabase {
fn default() -> Self {
Self::new()
}
}

impl DummyDatabase {
pub fn new() -> Self {
DummyDatabase {
report_store: Mutex::new(HashMap::new()),
moderation_store: Mutex::new(HashMap::new()),
}
}
}

#[async_trait]
impl DatabaseProvider for DummyDatabase {
async fn add_report(
&self,
id: &Uuid,
url: &str,
categories: &[ModerationCategories],
) -> Result<()> {
let url_hash = sha256(url.as_bytes());
let updated_at = chrono::Utc::now();
let row = DbReportRow {
id: id.to_string(),
url: String::from(url),
categories: Vec::from(categories),
updated_at,
};

let mut report_store = self.report_store.lock().unwrap();
report_store.insert(url_hash, row);
Ok(())
}

async fn get_reports(&self) -> Result<Vec<DbReportRow>> {
let report_store = self.report_store.lock().unwrap();
let values: Vec<DbReportRow> = report_store
.iter()
.map(|(_, value)| value.clone())
.collect();
Ok(values)
}

async fn update_moderation_result(
&self,
url: &str,
provider: ModerationService,
blocked: bool,
categories: &[ModerationCategories],
) -> Result<()> {
self.add_moderation_result(url, provider, blocked, categories)
.await
}

async fn add_moderation_result(
&self,
url: &str,
provider: ModerationService,
blocked: bool,
categories: &[ModerationCategories],
) -> Result<()> {
let url_hash = sha256(url.as_bytes());
let row = DbModerationRow {
blocked,
categories: Vec::from(categories),
provider,
url: String::from(url),
};

let mut moderation_store = self.moderation_store.lock().unwrap();
moderation_store.insert(url_hash, row);
let _store_len = moderation_store.len();
Ok(())
}

async fn get_moderation_result(&self, urls: &[String]) -> Result<Vec<DbModerationRow>> {
let moderation_store = self.moderation_store.lock().unwrap();
let urls = Vec::from(urls);
let result: Vec<DbModerationRow> = urls
.iter()
.map(|url| {
let url_hash = sha256(url.as_bytes());
moderation_store.get(&url_hash)
})
.filter(|row| row.is_some())
.map(|row| row.unwrap().clone())
.collect();
Ok(result)
}
}

#[tokio::test]
async fn test_dummy_database_moderation_fns() {
let db = DummyDatabase::new();
let url = "http://localhost/test.png".to_string();
let result = db.get_moderation_result(&[url.clone()]).await.unwrap();
assert_eq!(result.len(), 0);

let _ = db
.add_moderation_result(
url.as_str(),
ModerationService::Unknown,
true,
&[ModerationCategories::Alcohol],
)
.await;
let result = db.get_moderation_result(&[url.clone()]).await.unwrap();
assert_eq!(result.len(), 1);
let row = result.get(0).unwrap();
assert!(row.blocked);
assert_eq!(row.categories[0], ModerationCategories::Alcohol);
assert_eq!(row.url, url);
assert_eq!(row.provider, ModerationService::Unknown);

let _ = db
.update_moderation_result(
url.as_str(),
ModerationService::Unknown,
true,
&[ModerationCategories::Alcohol, ModerationCategories::Drugs],
)
.await;

let result = db.get_moderation_result(&[url.clone()]).await.unwrap();
assert_eq!(result.len(), 1);
let row = result.get(0).unwrap();
assert!(row.blocked);
assert_eq!(row.categories.len(), 2);
assert_eq!(row.categories[0], ModerationCategories::Alcohol);
assert_eq!(row.categories[1], ModerationCategories::Drugs);
assert_eq!(row.url, url);
assert_eq!(row.provider, ModerationService::Unknown);
}

#[tokio::test]
async fn test_dummy_database_report_fns() {
let db = DummyDatabase::new();
let url = "http://localhost/test.png".to_string();
let id = Uuid::new_v4();
let result = db.get_reports().await.unwrap();
assert_eq!(result.len(), 0);

let _ = db
.add_report(&id, url.as_str(), &[ModerationCategories::Alcohol])
.await;
let result = db.get_reports().await.unwrap();
assert_eq!(result.len(), 1);
let row = result.get(0).unwrap();
assert_eq!(row.url, url);
assert_eq!(row.categories[0], ModerationCategories::Alcohol);
}
Loading