Skip to content

Commit

Permalink
Adds: is_global property to Setting struct.
Browse files Browse the repository at this point in the history
  • Loading branch information
korvyashka committed Jan 25, 2024
1 parent 07b8039 commit c7f317d
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 23 deletions.
54 changes: 54 additions & 0 deletions src/commands/edge_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1759,6 +1759,7 @@ mod tests {
"title": "nsetting".to_string(),
"optional": true,
"help_text": "For how long to display the map overlay every time the rover has moved to a new position.".to_string(),
"is_global": false,
}]));
});

Expand Down Expand Up @@ -2838,4 +2839,57 @@ settings:

assert!(result.is_ok());
}

#[test]
fn test_create_is_global_setting_should_pass_is_global_property() {
let mock_server = MockServer::start();

let config = Config::new(mock_server.base_url());
let authentication = Authentication::new_with_config(config, "token");
let command = EdgeAppCommand::new(authentication);

// v4/edge-apps/settings?app_id=eq.{}
let settings_mock_create = mock_server.mock(|when, then| {
when.method(POST)
.path("/v4/edge-apps/settings")
.header("Authorization", "Token token")
.header(
"user-agent",
format!("screenly-cli {}", env!("CARGO_PKG_VERSION")),
)
.json_body(json!({
"app_id": "01H2QZ6Z8WXWNDC0KQ198XCZEW",
"type": "secret",
"default_value": "",
"title": "ssetting",
"optional": false,
"help_text": "help text",
"is_global": true
}));
then.status(201).json_body(json!(
[{
"app_id": "01H2QZ6Z8WXWNDC0KQ198XCZEW",
"type": "secret",
"default_value": "",
"title": "ssetting",
"optional": false,
"help_text": "help text",
"is_global": true,
}]));
});

let setting = Setting {
type_: SettingType::Secret,
title: "ssetting".to_string(),
optional: false,
default_value: Some("".to_string()),
is_global: true,
help_text: "help text".to_string(),
};
command
.create_setting("01H2QZ6Z8WXWNDC0KQ198XCZEW".to_string(), &setting)
.unwrap();

settings_mock_create.assert();
}
}
25 changes: 25 additions & 0 deletions src/commands/edge_app_manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,31 @@ settings:
assert_eq!(manifest, deserialized_manifest);
}

#[test]
fn test_serialize_deserialize_cycle_with_is_global_setting_should_pass() {
let manifest = EdgeAppManifest {
app_id: Some("test_app".to_string()),
user_version: Some("test_version".to_string()),
description: Some("test_description".to_string()),
icon: Some("test_icon".to_string()),
author: Some("test_author".to_string()),
homepage_url: Some("test_url".to_string()),
entrypoint: Some("entrypoint.html".to_owned()),
settings: vec![Setting {
title: "username".to_string(),
type_: SettingType::String,
default_value: Some("stranger".to_string()),
optional: true,
is_global: true,
help_text: "An example of a setting that is used in index.html".to_string(),
}],
};

let deserialized_manifest = serialize_deserialize_cycle(manifest.clone()).unwrap();

assert_eq!(manifest, deserialized_manifest);
}

#[test]
fn test_serialize_deserialize_cycle_should_pass_on_valid_struct_missing_optional_fields() {
let manifest = EdgeAppManifest {
Expand Down
17 changes: 6 additions & 11 deletions src/commands/edge_app_settings.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use std::collections::HashMap;

use std::str::FromStr;
use std::ops::Not;
use std::str::FromStr;

use serde::{Deserialize, Deserializer, Serialize};
use strum::IntoEnumIterator;
use strum_macros::{Display, EnumIter, EnumString};

use crate::commands::serde_utils::{deserialize_string_field, serialize_non_empty_string_field, deserialize_bool_field};
use crate::commands::serde_utils::{
deserialize_bool_field, deserialize_string_field, serialize_non_empty_string_field,
};

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Default, EnumString, Display, EnumIter)]
pub enum SettingType {
Expand Down Expand Up @@ -38,8 +40,8 @@ pub struct Setting {
deserialize_with = "deserialize_help_text"
)]
pub help_text: String,
#[serde(deserialize_with = "deserialize_is_global", skip_serializing_if = "<&bool>::not")]
pub is_global: bool
#[serde(default = "bool::default", skip_serializing_if = "<&bool>::not")]
pub is_global: bool,
}

pub fn serialize_settings<S>(settings: &[Setting], serializer: S) -> Result<S::Ok, S::Error>
Expand Down Expand Up @@ -122,10 +124,3 @@ where
{
deserialize_string_field("help_text", true, deserializer)
}

fn deserialize_is_global<'de, D>(deserializer: D) -> Result<bool, D::Error>
where
D: Deserializer<'de>,
{
deserialize_bool_field("is_global", deserializer)
}
44 changes: 44 additions & 0 deletions src/commands/edge_app_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,50 @@ mod tests {
assert_eq!(changes.creates.len(), 2);
}

#[test]
fn test_detect_changed_settings_when_is_global_changed_on_setting_should_detect_changes() {
// Arrange
let manifest = EdgeAppManifest {
app_id: Some("01H2QZ6Z8WXWNDC0KQ198XCZEW".to_string()),
user_version: Some("1".to_string()),
description: Some("asdf".to_string()),
icon: Some("asdf".to_string()),
author: Some("asdf".to_string()),
homepage_url: Some("asdfasdf".to_string()),
entrypoint: Some("entrypoint.html".to_owned()),
settings: vec![
Setting {
type_: SettingType::String,
default_value: Some("5".to_string()),
title: "display_time".to_string(),
optional: true,
is_global: true,
help_text: "For how long to display the map overlay every time the rover has moved to a new position.".to_string(),
},
],
};

let remote_settings = vec![
Setting {
type_: SettingType::String,
default_value: Some("5".to_string()),
title: "display_time".to_string(),
optional: true,
is_global: false,
help_text: "For how long to display the map overlay every time the rover has moved to a new position.".to_string(),
},
];

// Act
let result = detect_changed_settings(&manifest, &remote_settings);

// Assert
assert!(result.is_ok());
let changes = result.unwrap();
assert_eq!(changes.creates.len(), 0);
assert_eq!(changes.updates.len(), 1);
}

#[test]
fn test_detect_changed_files_no_changes() {
// Arrange
Expand Down
12 changes: 0 additions & 12 deletions src/commands/serde_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,3 @@ where
pub fn string_field_is_none_or_empty(opt: &Option<String>) -> bool {
opt.as_ref().map_or(true, |s| s.is_empty())
}

pub fn deserialize_bool_field<'de, D>(
field_name: &'static str,
deserializer: D,
) -> Result<bool, D::Error>
where
D: Deserializer<'de>,
{
let value: bool = true;
// let value: bool = bool::deserialize(deserializer)?;
Ok(value)
}

0 comments on commit c7f317d

Please sign in to comment.