♻️ refactor: extract mock credential file creation into helper function

- Add create_mock_credential_file() helper to eliminate duplication
- Replace identical credential JSON creation in two integration tests
- Improves test maintainability and reduces code duplication
- All tests continue to pass with same behavior
This commit is contained in:
Jeremiah Russell
2025-10-21 13:55:45 +01:00
committed by Jeremiah Russell
parent 4fd125189a
commit fc9a1418b9

View File

@@ -2,9 +2,30 @@
use assert_cmd::prelude::*; use assert_cmd::prelude::*;
use assert_fs::prelude::*; use assert_fs::prelude::*;
use assert_fs::fixture::ChildPath;
use predicates::prelude::*; use predicates::prelude::*;
use std::process::Command; use std::process::Command;
/// Creates a mock OAuth2 credential file with test data.
///
/// This helper function creates a valid OAuth2 credential JSON file
/// suitable for testing credential file handling without using real credentials.
fn create_mock_credential_file(credential_file: &ChildPath) {
credential_file
.write_str(
r#"{
"installed": {
"client_id": "test-client-id.googleusercontent.com",
"client_secret": "test-client-secret",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"redirect_uris": ["http://localhost"]
}
}"#,
)
.unwrap();
}
#[test] #[test]
fn test_init_help() { fn test_init_help() {
let mut cmd = Command::cargo_bin("cull-gmail").unwrap(); let mut cmd = Command::cargo_bin("cull-gmail").unwrap();
@@ -63,19 +84,7 @@ fn test_init_dry_run_with_credential_file() {
let credential_file = temp_dir.child("credential.json"); let credential_file = temp_dir.child("credential.json");
// Create a mock credential file // Create a mock credential file
credential_file create_mock_credential_file(&credential_file);
.write_str(
r#"{
"installed": {
"client_id": "test-client-id.googleusercontent.com",
"client_secret": "test-client-secret",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"redirect_uris": ["http://localhost"]
}
}"#,
)
.unwrap();
let mut cmd = Command::cargo_bin("cull-gmail").unwrap(); let mut cmd = Command::cargo_bin("cull-gmail").unwrap();
cmd.args([ cmd.args([
@@ -199,19 +208,7 @@ fn test_init_with_credential_file_copy() {
let credential_file = temp_dir.child("source_credential.json"); let credential_file = temp_dir.child("source_credential.json");
// Create a mock credential file // Create a mock credential file
credential_file create_mock_credential_file(&credential_file);
.write_str(
r#"{
"installed": {
"client_id": "test-client-id.googleusercontent.com",
"client_secret": "test-client-secret",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"redirect_uris": ["http://localhost"]
}
}"#,
)
.unwrap();
let mut cmd = Command::cargo_bin("cull-gmail").unwrap(); let mut cmd = Command::cargo_bin("cull-gmail").unwrap();
cmd.args([ cmd.args([