Skip to content

Commit

Permalink
feat: ios permission support (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
matzuk authored Nov 18, 2024
1 parent 366b85d commit 5d69304
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub trait RapiClient {
project: Option<String>,
application_bundle: Option<Vec<ApplicationBundle>>,
library_bundle: Option<Vec<PathBuf>>,
granted_permission: Option<Vec<String>>,
) -> Result<String>;
async fn get_run(&self, id: &str) -> Result<TestRun>;

Expand Down Expand Up @@ -156,6 +157,7 @@ impl RapiClient for RapiReqwestClient {
project: Option<String>,
application_bundle: Option<Vec<ApplicationBundle>>,
library_bundle: Option<Vec<PathBuf>>,
granted_permission: Option<Vec<String>>,
) -> Result<String> {
let url = format!("{}/v2/run", self.base_url);
let params = [("api_key", self.api_key.clone())];
Expand Down Expand Up @@ -281,7 +283,8 @@ impl RapiClient for RapiReqwestClient {
test_timeout_max: test_timeout_max.clone(),
env_args: env_args_map,
test_env_args: test_env_args_map,
bundles,
bundles: bundles,
granted_permission: granted_permission.clone(),
};

let response = self.client.post(url).json(&create_request).send().await?;
Expand Down Expand Up @@ -612,6 +615,8 @@ struct CreateRunRequest {
test_env_args: Option<HashMap<String, String>>,
#[serde(rename = "bundles", default)]
bundles: Option<Vec<CreateRunBundle>>,
#[serde(rename = "granted_permission", default)]
granted_permission: Option<Vec<String>>,
}

#[derive(Serialize, Deserialize, Debug)]
Expand Down
1 change: 1 addition & 0 deletions src/cli/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ If you are interesting in library testing then please use advance mode with --li
common.project,
transformed_application_bundle,
library_bundle,
None,
)
.await
}
35 changes: 35 additions & 0 deletions src/cli/ios/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::ffi::OsStr;
use std::fmt::Display;

use anyhow::Result;
use std::collections::HashSet;
use tokio::fs::File;
use walkdir::WalkDir;

Expand Down Expand Up @@ -189,6 +190,23 @@ pub(crate) async fn infer_parameters(
Ok((device.unwrap(), xcode_version.unwrap(), os_version.unwrap()))
}

fn get_allowed_permissions() -> HashSet<&'static str> {
HashSet::from([
"calendar",
"contacts-limited",
"contacts",
"location",
"location-always",
"photos-add",
"photos",
"media-library",
"microphone",
"motion",
"reminders",
"siri",
])
}

pub(crate) async fn run(
application: std::path::PathBuf,
test_application: std::path::PathBuf,
Expand All @@ -205,6 +223,7 @@ pub(crate) async fn run(
analytics_args: super::AnalyticsArgs,
test_timeout_default: Option<u32>,
test_timeout_max: Option<u32>,
granted_permission: Option<Vec<String>>,
) -> Result<bool> {
let (device, xcode_version, os_version) = if device.is_none()
&& xcode_version.is_none()
Expand Down Expand Up @@ -278,6 +297,21 @@ Second example: If you choose --xcode-version 15.4 --os-version 17.5 then you wi
}
}

if let Some(granted_permission) = granted_permission.clone() {
let allowed_permissions = get_allowed_permissions();
let invalid_permissions: Vec<_> = granted_permission
.iter()
.filter(|perm| !allowed_permissions.contains(perm.as_str()))
.cloned()
.collect();

if !invalid_permissions.is_empty() {
return Err(InputError::IncorrectPermission {
permissions: invalid_permissions,
})?;
}
}

let present_wait: bool = match common.wait {
None => true,
Some(true) => true,
Expand Down Expand Up @@ -321,6 +355,7 @@ Second example: If you choose --xcode-version 15.4 --os-version 17.5 then you wi
common.project,
None,
None,
granted_permission,
)
.await
}
Expand Down
10 changes: 10 additions & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ impl Cli {
analytics_args,
test_timeout_default,
test_timeout_max,
granted_permission,
} => {
ios::run(
application,
Expand All @@ -110,6 +111,7 @@ impl Cli {
analytics_args,
test_timeout_default,
test_timeout_max,
granted_permission,
)
.await
}
Expand Down Expand Up @@ -547,5 +549,13 @@ Example: '--library-bundle apks/library1-debug-androidTest.apk --library-bundle
help = "Maximum test timeout in seconds, overriding all other test timeout settings"
)]
test_timeout_max: Option<u32>,

#[arg(
long,
help = "Grant permission to application.
Important: Granting is conducted before each test batch (not each test). If you need to grant before each test, please use --isolated mode.
Available permissions: calendar, contacts-limited, contacts, location, location-always, photos-add, photos, media-library, microphone, motion, reminders, siri."
)]
granted_permission: Option<Vec<String>>,
},
}
4 changes: 4 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ pub enum InputError {

#[error("{arg} arg should be a positive number")]
NonPositiveValue { arg: String },

#[error("The following permissions could not be granted: [{permissions:?}].
Available permissions: calendar, contacts-limited, contacts, location, location-always, photos-add, photos, media-library, microphone, motion, reminders, siri.")]
IncorrectPermission { permissions: Vec<String> },
}

#[derive(Error, Debug)]
Expand Down
2 changes: 2 additions & 0 deletions src/interactor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ impl TriggerTestRunInteractor {
project: Option<String>,
application_bundle: Option<Vec<ApplicationBundle>>,
library_bundle: Option<Vec<PathBuf>>,
granted_permission: Option<Vec<String>>,
) -> Result<bool> {
let client = RapiReqwestClient::new(base_url, api_key);
let steps = match (wait, output) {
Expand Down Expand Up @@ -178,6 +179,7 @@ impl TriggerTestRunInteractor {
project,
application_bundle,
library_bundle,
granted_permission,
)
.await?;

Expand Down

0 comments on commit 5d69304

Please sign in to comment.