Skip to content

Commit

Permalink
Make settings value possibly any json type
Browse files Browse the repository at this point in the history
Before it was forced to be a string
  • Loading branch information
johan-bjareholt committed Oct 29, 2020
1 parent 03fd9cd commit 6ecb206
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
9 changes: 7 additions & 2 deletions aw-models/src/key_value.rs
@@ -1,6 +1,7 @@
use chrono::{DateTime, Utc};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::Value;

#[derive(Serialize, Deserialize, JsonSchema)]
pub struct Key {
Expand All @@ -10,12 +11,16 @@ pub struct Key {
#[derive(Serialize, Deserialize, JsonSchema, Clone, Debug, PartialEq)]
pub struct KeyValue {
pub key: String,
pub value: String,
pub value: Value,
pub timestamp: Option<DateTime<Utc>>,
}

impl KeyValue {
pub fn new<T: Into<String>>(key: T, value: T, timestamp: DateTime<Utc>) -> KeyValue {
pub fn new<K: Into<String>, V: Into<Value>>(
key: K,
value: V,
timestamp: DateTime<Utc>,
) -> KeyValue {
KeyValue {
key: key.into(),
value: value.into(),
Expand Down
2 changes: 1 addition & 1 deletion aw-server/src/endpoints/settings.rs
Expand Up @@ -31,7 +31,7 @@ pub fn setting_set(
let setting_key = parse_key(data.key)?;

let datastore: MutexGuard<'_, Datastore> = endpoints_get_lock!(state.datastore);
let result = datastore.insert_key_value(&setting_key, &data.value);
let result = datastore.insert_key_value(&setting_key, &data.value.to_string());

match result {
Ok(_) => Ok(Status::Created),
Expand Down
21 changes: 11 additions & 10 deletions aw-server/tests/api.rs
Expand Up @@ -14,6 +14,7 @@ mod api_tests {

use chrono::{DateTime, Utc};
use rocket::http::{ContentType, Header, Status};
use serde_json::{json, Value};

use aw_server::config;
use aw_server::endpoints;
Expand Down Expand Up @@ -459,10 +460,10 @@ mod api_tests {
assert_eq!(res.body_string().unwrap(), r#"{"message":"EmptyQuery"}"#);
}

fn set_setting_request(client: &Client, key: &str, value: &str) -> Status {
fn set_setting_request(client: &Client, key: &str, value: Value) -> Status {
let body = serde_json::to_string(&KeyValue {
key: key.to_string(),
value: value.to_string(),
value: value,
timestamp: None,
})
.unwrap();
Expand Down Expand Up @@ -500,7 +501,7 @@ mod api_tests {
let client = rocket::local::Client::new(server).expect("valid instance");

// Test getting not found (getting nonexistent key)
let res = set_setting_request(&client, "thisisaverylongkthisisaverylongkthisisaverylongkthisisaverylongkthisisaverylongkthisisaverylongkthisisaverylongkthisisaverylongk", "");
let res = set_setting_request(&client, "thisisaverylongkthisisaverylongkthisisaverylongkthisisaverylongkthisisaverylongkthisisaverylongkthisisaverylongkthisisaverylongk", json!(""));
assert_eq!(res, rocket::http::Status::BadRequest);
}

Expand All @@ -510,7 +511,7 @@ mod api_tests {
let client = rocket::local::Client::new(server).expect("valid instance");

// Test value creation
let response_status = set_setting_request(&client, "test_key", "test_value");
let response_status = set_setting_request(&client, "test_key", json!("test_value"));
assert_eq!(response_status, rocket::http::Status::Created);
}

Expand All @@ -529,9 +530,9 @@ mod api_tests {
let server = setup_testserver();
let client = rocket::local::Client::new(server).expect("valid instance");

let response1_status = set_setting_request(&client, "test_key", "");
let response1_status = set_setting_request(&client, "test_key", json!(""));
assert_eq!(response1_status, rocket::http::Status::Created);
let response2_status = set_setting_request(&client, "test_key_2", "");
let response2_status = set_setting_request(&client, "test_key_2", json!(""));
assert_eq!(response2_status, rocket::http::Status::Created);

let mut res = client.get("/api/0/settings/").dispatch();
Expand All @@ -549,7 +550,7 @@ mod api_tests {
let client = rocket::local::Client::new(server).expect("valid instance");

let timestamp = Utc::now();
let response_status = set_setting_request(&client, "test_key", "test_value");
let response_status = set_setting_request(&client, "test_key", json!("test_value"));
assert_eq!(response_status, rocket::http::Status::Created);

// Test getting
Expand All @@ -569,7 +570,7 @@ mod api_tests {
let client = rocket::local::Client::new(server).expect("valid instance");

let timestamp = Utc::now();
let post_1_status = set_setting_request(&client, "test_key", "test_value");
let post_1_status = set_setting_request(&client, "test_key", json!("test_value"));
assert_eq!(post_1_status, rocket::http::Status::Created);

let mut res = client.get("/api/0/settings/test_key").dispatch();
Expand All @@ -583,7 +584,7 @@ mod api_tests {
);

let timestamp_2 = Utc::now();
let post_2_status = set_setting_request(&client, "test_key", "changed_test_value");
let post_2_status = set_setting_request(&client, "test_key", json!("changed_test_value"));
assert_eq!(post_2_status, rocket::http::Status::Created);

let mut res = client.get("/api/0/settings/test_key").dispatch();
Expand All @@ -602,7 +603,7 @@ mod api_tests {
let server = setup_testserver();
let client = rocket::local::Client::new(server).expect("valid instance");

let response_status = set_setting_request(&client, "test_key", "");
let response_status = set_setting_request(&client, "test_key", json!(""));
assert_eq!(response_status, rocket::http::Status::Created);

// Test deleting
Expand Down

0 comments on commit 6ecb206

Please sign in to comment.