Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

refactor(test_utils): Refactor `test_utils` crate and add `folder` support with updated documentation (#2487)

Pa1NarK6b52ac3d398d
diff --git a/crates/test_utils/README.md b/crates/test_utils/README.md
index 3cbda63e52..bfed3479b3 100644
--- a/crates/test_utils/README.md
+++ b/crates/test_utils/README.md
@@ -2,48 +2,66 @@
The heart of `newman`(with directory support) and `UI-tests`
-## Newman Usage
+## Newman
-- Make sure you that you've _**do not**_ have the official newman installed but rather the `newman` fork with directory support
- - `newman` can be installed by running `npm install -g 'https://github.com/knutties/newman.git#feature/newman-dir'`
- - To see the features that the fork of `newman` supports, click [_**here**_](https://github.com/knutties/newman/blob/feature/newman-dir/DIR_COMMANDS.md)
-- Add the connector credentials to the `connector_auth.toml` / `auth.toml`
+- Make sure you that you _**do not**_ have the newman (from the Postman team) installed but rather the `newman` fork with directory support
+- The `newman` fork can be installed by running `npm install -g 'https://github.com/knutties/newman.git#feature/newman-dir'`
+- To see the features that the fork of `newman` supports, click [_**here**_](https://github.com/knutties/newman/blob/feature/newman-dir/DIR_COMMANDS.md)
+
+## Test Utils Usage
+
+- Add the connector credentials to the `connector_auth.toml` / `auth.toml` by creating a copy of the `sample_auth.toml` from `router/tests/connectors/sample_auth.toml`
- Export the auth file path as an environment variable:
```shell
export CONNECTOR_AUTH_FILE_PATH=/path/to/auth.toml
```
-- Run the tests:
+
+### Supported Commands
+
+Required fields:
+
+- `--admin_api_key` -- Admin API Key of the environment. `test_admin` is the Admin API Key for running locally
+- `--base_url` -- Base URL of the environment. `http://127.0.0.1:8080` / `http://localhost:8080` is the Base URL for running locally
+- `--connector_name` -- Name of the connector that you wish to run. Example: `adyen`, `shift4`, `stripe`
+
+Optional fields:
+
+- `--folder` -- To run individual folders in the collection
+ - Use double quotes to specify folder name. If you wish to run multiple folders, separate them with a comma (`,`)
+ - Example: `--folder "QuickStart"` or `--folder "Health check,QuickStart"`
+- `--verbose` -- A boolean to print detailed logs (requests and responses)
+
+**Note:** Passing `--verbose` will also print the connector as well as admin API keys in the logs. So, make sure you don't push the commands with `--verbose` to any public repository.
+
+### Running tests
+
+- Tests can be run with the following command:
```shell
- cargo run --package test_utils --bin test_utils -- --connector_name=<connector_name> --base_url=<base_url> --admin_api_key=<admin_api_key>
+ cargo run --package test_utils --bin test_utils -- --connector_name=<connector_name> --base_url=<base_url> --admin_api_key=<admin_api_key> \
+ # optionally
+ --folder "<folder_name_1>,<folder_name_2>,...<folder_name_n>" --verbose
```
-**Note:** You can optionally pass `--verbose` to see the logs of the tests. But make sure you that passing `--verbose` will also print the API-Keys in the logs. So, make sure you don't push the logs to any public repository. Below is an example:
-```shell
-cargo run --package test_utils --bin test_utils -- --connector_name=<connector_name> --base_url=<base_url> --admin_api_key=<admin_api_key> --verbose
-```
-
-### Running newman locally
+**Note**: You can omit `--package test_utils` at the time of running the above command since it is optional.
-Execute the following commands:
-```shell
-export CONNECTOR_AUTH_FILE_PATH=/path/to/auth.toml
-cargo run --package test_utils --bin test_utils -- --connector_name=<connector_name> --base_url=http://127.0.0.1:8080 --admin_api_key=test_admin
-# Optionally, you can add `--verbose` in the end
-```
## UI tests
To run the UI tests, run the following command:
+
```shell
cargo test --package test_utils --test connectors -- <connector_ui_name>::<optionally_name_of_specific_function_run> --test-threads=1
```
+
### Example
Below is an example to run UI test to only run the `GooglePay` payment test for `adyen` connector:
+
```shell
cargo test --package test_utils --test connectors -- adyen_uk_ui::should_make_gpay_payment_test --test-threads=1
```
Below is an example to run all the UI tests for `adyen` connector:
+
```shell
cargo test --package test_utils --test connectors -- adyen_uk_ui:: --test-threads=1
-```
\ No newline at end of file
+```
diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs
index 82b4addfb9..f7cde3667d 100644
--- a/crates/test_utils/src/lib.rs
+++ b/crates/test_utils/src/lib.rs
@@ -1 +1,2 @@
pub mod connector_auth;
+pub mod newman_runner;
diff --git a/crates/test_utils/src/main.rs b/crates/test_utils/src/main.rs
index 155870ee2c..637122e468 100644
--- a/crates/test_utils/src/main.rs
+++ b/crates/test_utils/src/main.rs
@@ -1,141 +1,9 @@
-use std::{
- env,
- process::{exit, Command},
-};
+use std::process::{exit, Command};
-use clap::{arg, command, Parser};
-use masking::PeekInterface;
-use test_utils::connector_auth::{ConnectorAuthType, ConnectorAuthenticationMap};
-
-// Just by the name of the connector, this function generates the name of the collection dir
-// Example: CONNECTOR_NAME="stripe" -> OUTPUT: postman/collection-dir/stripe
-#[inline]
-fn get_path(name: impl AsRef<str>) -> String {
- format!("postman/collection-dir/{}", name.as_ref())
-}
-
-#[derive(Parser)]
-#[command(version, about = "Postman collection runner using newman!", long_about = None)]
-struct Args {
- /// Name of the connector
- #[arg(short, long = "connector_name")]
- connector_name: String,
- /// Base URL of the Hyperswitch environment
- #[arg(short, long = "base_url")]
- base_url: String,
- /// Admin API Key of the environment
- #[arg(short, long = "admin_api_key")]
- admin_api_key: String,
- /// Optional Verbose logs
- #[arg(short, long)]
- verbose: bool,
-}
+use test_utils::newman_runner;
fn main() {
- let args = Args::parse();
-
- let connector_name = args.connector_name;
- let base_url = args.base_url;
- let admin_api_key = args.admin_api_key;
-
- let collection_path = get_path(&connector_name);
- let auth_map = ConnectorAuthenticationMap::new();
-
- let inner_map = auth_map.inner();
-
- // Newman runner
- // Depending on the conditions satisfied, variables are added. Since certificates of stripe have already
- // been added to the postman collection, those conditions are set to true and collections that have
- // variables set up for certificate, will consider those variables and will fail.
-
- let mut newman_command = Command::new("newman");
- newman_command.args(["dir-run", &collection_path]);
- newman_command.args(["--env-var", &format!("admin_api_key={admin_api_key}")]);
- newman_command.args(["--env-var", &format!("baseUrl={base_url}")]);
-
- if let Some(auth_type) = inner_map.get(&connector_name) {
- match auth_type {
- ConnectorAuthType::HeaderKey { api_key } => {
- // newman_command.args(["--env-var", &format!("connector_api_key={}", api_key.map(|val| val))]);
- newman_command.args([
- "--env-var",
- &format!("connector_api_key={}", api_key.peek()),
- ]);
- }
- ConnectorAuthType::BodyKey { api_key, key1 } => {
- newman_command.args([
- "--env-var",
- &format!("connector_api_key={}", api_key.peek()),
- "--env-var",
- &format!("connector_key1={}", key1.peek()),
- ]);
- }
- ConnectorAuthType::SignatureKey {
- api_key,
- key1,
- api_secret,
- } => {
- newman_command.args([
- "--env-var",
- &format!("connector_api_key={}", api_key.peek()),
- "--env-var",
- &format!("connector_key1={}", key1.peek()),
- "--env-var",
- &format!("connector_api_secret={}", api_secret.peek()),
- ]);
- }
- ConnectorAuthType::MultiAuthKey {
- api_key,
- key1,
- key2,
- api_secret,
- } => {
- newman_command.args([
- "--env-var",
- &format!("connector_api_key={}", api_key.peek()),
- "--env-var",
- &format!("connector_key1={}", key1.peek()),
- "--env-var",
- &format!("connector_key1={}", key2.peek()),
- "--env-var",
- &format!("connector_api_secret={}", api_secret.peek()),
- ]);
- }
- // Handle other ConnectorAuthType variants
- _ => {
- eprintln!("Invalid authentication type.");
- }
- }
- } else {
- eprintln!("Connector not found.");
- }
-
- // Add additional environment variables if present
- if let Ok(gateway_merchant_id) = env::var("GATEWAY_MERCHANT_ID") {
- newman_command.args([
- "--env-var",
- &format!("gateway_merchant_id={gateway_merchant_id}"),
- ]);
- }
-
- if let Ok(gpay_certificate) = env::var("GPAY_CERTIFICATE") {
- newman_command.args(["--env-var", &format!("certificate={gpay_certificate}")]);
- }
-
- if let Ok(gpay_certificate_keys) = env::var("GPAY_CERTIFICATE_KEYS") {
- newman_command.args([
- "--env-var",
- &format!("certificate_keys={gpay_certificate_keys}"),
- ]);
- }
-
- newman_command.arg("--delay-request").arg("5");
-
- newman_command.arg("--color").arg("on");
-
- if args.verbose {
- newman_command.arg("--verbose");
- }
+ let mut newman_command: Command = newman_runner::command_generate();
// Execute the newman command
let output = newman_command.spawn();
diff --git a/crates/test_utils/src/newman_runner.rs b/crates/test_utils/src/newman_runner.rs
new file mode 100644
index 0000000000..c51556f8f2
--- /dev/null
+++ b/crates/test_utils/src/newman_runner.rs
@@ -0,0 +1,155 @@
+use std::{env, process::Command};
+
+use clap::{arg, command, Parser};
+use masking::PeekInterface;
+
+use crate::connector_auth::{ConnectorAuthType, ConnectorAuthenticationMap};
+
+#[derive(Parser)]
+#[command(version, about = "Postman collection runner using newman!", long_about = None)]
+struct Args {
+ /// Admin API Key of the environment
+ #[arg(short, long = "admin_api_key")]
+ admin_api_key: String,
+ /// Base URL of the Hyperswitch environment
+ #[arg(short, long = "base_url")]
+ base_url: String,
+ /// Name of the connector
+ #[arg(short, long = "connector_name")]
+ connector_name: String,
+ /// Folder name of specific tests
+ #[arg(short, long = "folder")]
+ folders: Option<String>,
+ /// Optional Verbose logs
+ #[arg(short, long)]
+ verbose: bool,
+}
+
+// Just by the name of the connector, this function generates the name of the collection dir
+// Example: CONNECTOR_NAME="stripe" -> OUTPUT: postman/collection-dir/stripe
+#[inline]
+fn get_path(name: impl AsRef<str>) -> String {
+ format!("postman/collection-dir/{}", name.as_ref())
+}
+
+pub fn command_generate() -> Command {
+ let args = Args::parse();
+
+ let connector_name = args.connector_name;
+ let base_url = args.base_url;
+ let admin_api_key = args.admin_api_key;
+
+ let collection_path = get_path(&connector_name);
+ let auth_map = ConnectorAuthenticationMap::new();
+
+ let inner_map = auth_map.inner();
+
+ // Newman runner
+ // Depending on the conditions satisfied, variables are added. Since certificates of stripe have already
+ // been added to the postman collection, those conditions are set to true and collections that have
+ // variables set up for certificate, will consider those variables and will fail.
+
+ let mut newman_command = Command::new("newman");
+ newman_command.args(["dir-run", &collection_path]);
+ newman_command.args(["--env-var", &format!("admin_api_key={admin_api_key}")]);
+ newman_command.args(["--env-var", &format!("baseUrl={base_url}")]);
+
+ if let Some(auth_type) = inner_map.get(&connector_name) {
+ match auth_type {
+ ConnectorAuthType::HeaderKey { api_key } => {
+ newman_command.args([
+ "--env-var",
+ &format!("connector_api_key={}", api_key.peek()),
+ ]);
+ }
+ ConnectorAuthType::BodyKey { api_key, key1 } => {
+ newman_command.args([
+ "--env-var",
+ &format!("connector_api_key={}", api_key.peek()),
+ "--env-var",
+ &format!("connector_key1={}", key1.peek()),
+ ]);
+ }
+ ConnectorAuthType::SignatureKey {
+ api_key,
+ key1,
+ api_secret,
+ } => {
+ newman_command.args([
+ "--env-var",
+ &format!("connector_api_key={}", api_key.peek()),
+ "--env-var",
+ &format!("connector_key1={}", key1.peek()),
+ "--env-var",
+ &format!("connector_api_secret={}", api_secret.peek()),
+ ]);
+ }
+ ConnectorAuthType::MultiAuthKey {
+ api_key,
+ key1,
+ key2,
+ api_secret,
+ } => {
+ newman_command.args([
+ "--env-var",
+ &format!("connector_api_key={}", api_key.peek()),
+ "--env-var",
+ &format!("connector_key1={}", key1.peek()),
+ "--env-var",
+ &format!("connector_key2={}", key2.peek()),
+ "--env-var",
+ &format!("connector_api_secret={}", api_secret.peek()),
+ ]);
+ }
+ // Handle other ConnectorAuthType variants
+ _ => {
+ eprintln!("Invalid authentication type.");
+ }
+ }
+ } else {
+ eprintln!("Connector not found.");
+ }
+
+ // Add additional environment variables if present
+ if let Ok(gateway_merchant_id) = env::var("GATEWAY_MERCHANT_ID") {
+ newman_command.args([
+ "--env-var",
+ &format!("gateway_merchant_id={gateway_merchant_id}"),
+ ]);
+ }
+
+ if let Ok(gpay_certificate) = env::var("GPAY_CERTIFICATE") {
+ newman_command.args(["--env-var", &format!("certificate={gpay_certificate}")]);
+ }
+
+ if let Ok(gpay_certificate_keys) = env::var("GPAY_CERTIFICATE_KEYS") {
+ newman_command.args([
+ "--env-var",
+ &format!("certificate_keys={gpay_certificate_keys}"),
+ ]);
+ }
+
+ newman_command.arg("--delay-request").arg("7"); // 7 milli seconds delay
+
+ newman_command.arg("--color").arg("on");
+
+ // Add flags for running specific folders
+ if let Some(folders) = &args.folders {
+ let folder_names: Vec<String> = folders.split(',').map(|s| s.trim().to_string()).collect();
+
+ for folder_name in folder_names {
+ if !folder_name.contains("QuickStart") {
+ // This is a quick fix, "QuickStart" is intentional to have merchant account and API keys set up
+ // This will be replaced by a more robust and efficient account creation or reuse existing old account
+ newman_command.args(["--folder", "QuickStart"]);
+ }
+ newman_command.args(["--folder", &folder_name]);
+ }
+ }
+
+ if args.verbose {
+ newman_command.arg("--verbose");
+ }
+
+ newman_command
+}