|
| 1 | +//! Integration tests for Gitea issue creation API |
| 2 | +//! |
| 3 | +//! These tests verify the create_issue functionality against our Gitea fork. |
| 4 | +//! Run with: cargo test --test gitea_create_issue_test -- --ignored |
| 5 | +//! (marked as ignored to avoid running in CI without proper credentials) |
| 6 | +
|
| 7 | +use std::env; |
| 8 | + |
| 9 | +/// Test creating an issue with various label formats |
| 10 | +/// This helps debug the 422 Unprocessable Entity error |
| 11 | +#[tokio::test] |
| 12 | +#[ignore = "requires live Gitea instance"] |
| 13 | +async fn test_create_issue_with_labels() { |
| 14 | + let base_url = env::var("GITEA_URL").expect("GITEA_URL not set"); |
| 15 | + let token = env::var("GITEA_TOKEN").expect("GITEA_TOKEN not set"); |
| 16 | + let owner = env::var("GITEA_OWNER").unwrap_or_else(|_| "terraphim".to_string()); |
| 17 | + let repo = env::var("GITEA_REPO").unwrap_or_else(|_| "test-repo".to_string()); |
| 18 | + |
| 19 | + let client = reqwest::Client::new(); |
| 20 | + |
| 21 | + // Test 1: Empty labels array |
| 22 | + println!("Test 1: Empty labels array"); |
| 23 | + let resp = client |
| 24 | + .post(format!("{}/api/v1/repos/{}/{}/issues", base_url, owner, repo)) |
| 25 | + .header("Authorization", format!("token {}", token)) |
| 26 | + .header("Content-Type", "application/json") |
| 27 | + .json(&serde_json::json!({ |
| 28 | + "title": "Test issue - empty labels", |
| 29 | + "body": "Testing empty labels array", |
| 30 | + "labels": [], |
| 31 | + })) |
| 32 | + .send() |
| 33 | + .await |
| 34 | + .expect("Failed to send request"); |
| 35 | + |
| 36 | + let status = resp.status(); |
| 37 | + let body = resp.text().await.unwrap_or_default(); |
| 38 | + println!(" Status: {}", status); |
| 39 | + println!(" Body: {}", body); |
| 40 | + |
| 41 | + // Test 2: String labels array |
| 42 | + println!("\nTest 2: String labels array"); |
| 43 | + let resp = client |
| 44 | + .post(format!("{}/api/v1/repos/{}/{}/issues", base_url, owner, repo)) |
| 45 | + .header("Authorization", format!("token {}", token)) |
| 46 | + .header("Content-Type", "application/json") |
| 47 | + .json(&serde_json::json!({ |
| 48 | + "title": "Test issue - string labels", |
| 49 | + "body": "Testing string labels array", |
| 50 | + "labels": ["bug", "critical"], |
| 51 | + })) |
| 52 | + .send() |
| 53 | + .await |
| 54 | + .expect("Failed to send request"); |
| 55 | + |
| 56 | + let status = resp.status(); |
| 57 | + let body = resp.text().await.unwrap_or_default(); |
| 58 | + println!(" Status: {}", status); |
| 59 | + println!(" Body: {}", body); |
| 60 | + |
| 61 | + // Test 3: No labels field |
| 62 | + println!("\nTest 3: No labels field"); |
| 63 | + let resp = client |
| 64 | + .post(format!("{}/api/v1/repos/{}/{}/issues", base_url, owner, repo)) |
| 65 | + .header("Authorization", format!("token {}", token)) |
| 66 | + .header("Content-Type", "application/json") |
| 67 | + .json(&serde_json::json!({ |
| 68 | + "title": "Test issue - no labels", |
| 69 | + "body": "Testing without labels field", |
| 70 | + })) |
| 71 | + .send() |
| 72 | + .await |
| 73 | + .expect("Failed to send request"); |
| 74 | + |
| 75 | + let status = resp.status(); |
| 76 | + let body = resp.text().await.unwrap_or_default(); |
| 77 | + println!(" Status: {}", status); |
| 78 | + println!(" Body: {}", body); |
| 79 | + |
| 80 | + // Test 4: Integer labels (to see error) |
| 81 | + println!("\nTest 4: Integer labels (expected to fail)"); |
| 82 | + let resp = client |
| 83 | + .post(format!("{}/api/v1/repos/{}/{}/issues", base_url, owner, repo)) |
| 84 | + .header("Authorization", format!("token {}", token)) |
| 85 | + .header("Content-Type", "application/json") |
| 86 | + .json(&serde_json::json!({ |
| 87 | + "title": "Test issue - int labels", |
| 88 | + "body": "Testing integer labels", |
| 89 | + "labels": [1, 2, 3], |
| 90 | + })) |
| 91 | + .send() |
| 92 | + .await |
| 93 | + .expect("Failed to send request"); |
| 94 | + |
| 95 | + let status = resp.status(); |
| 96 | + let body = resp.text().await.unwrap_or_default(); |
| 97 | + println!(" Status: {}", status); |
| 98 | + println!(" Body: {}", body); |
| 99 | +} |
| 100 | + |
| 101 | +/// Test the actual GiteaTracker::create_issue method |
| 102 | +#[tokio::test] |
| 103 | +#[ignore = "requires live Gitea instance"] |
| 104 | +async fn test_tracker_create_issue() { |
| 105 | + use terraphim_tracker::gitea::{GiteaConfig, GiteaTracker}; |
| 106 | + |
| 107 | + let base_url = env::var("GITEA_URL").expect("GITEA_URL not set"); |
| 108 | + let token = env::var("GITEA_TOKEN").expect("GITEA_TOKEN not set"); |
| 109 | + let owner = env::var("GITEA_OWNER").unwrap_or_else(|_| "terraphim".to_string()); |
| 110 | + let repo = env::var("GITEA_REPO").unwrap_or_else(|_| "test-repo".to_string()); |
| 111 | + |
| 112 | + let config = GiteaConfig { |
| 113 | + base_url, |
| 114 | + token, |
| 115 | + owner, |
| 116 | + repo, |
| 117 | + active_states: vec!["open".to_string()], |
| 118 | + terminal_states: vec!["closed".to_string()], |
| 119 | + use_robot_api: false, |
| 120 | + }; |
| 121 | + |
| 122 | + let tracker = GiteaTracker::new(config).expect("Failed to create tracker"); |
| 123 | + |
| 124 | + // Test with string labels |
| 125 | + println!("Testing create_issue with string labels..."); |
| 126 | + let result = tracker.create_issue( |
| 127 | + "Test from tracker - string labels", |
| 128 | + "This is a test issue created by GiteaTracker", |
| 129 | + &["test", "automation"], |
| 130 | + ).await; |
| 131 | + |
| 132 | + match result { |
| 133 | + Ok(issue) => println!("✅ Success! Created issue #{}: {}", issue.number, issue.title), |
| 134 | + Err(e) => println!("❌ Failed: {}", e), |
| 135 | + } |
| 136 | + |
| 137 | + // Test with empty labels |
| 138 | + println!("\nTesting create_issue with empty labels..."); |
| 139 | + let result = tracker.create_issue( |
| 140 | + "Test from tracker - empty labels", |
| 141 | + "This is a test issue with no labels", |
| 142 | + &[], |
| 143 | + ).await; |
| 144 | + |
| 145 | + match result { |
| 146 | + Ok(issue) => println!("✅ Success! Created issue #{}: {}", issue.number, issue.title), |
| 147 | + Err(e) => println!("❌ Failed: {}", e), |
| 148 | + } |
| 149 | +} |
0 commit comments