slacko 0.2.2

Comprehensive Rust SDK for the Slack API with stealth mode support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
//! Integration tests for Files API

mod common;

use common::{init, test_client};
use slacko::api::files::{FileUploadOptions, FilesListRequest, UploadedFileInfo};

/// Get self-DM channel
async fn get_test_dm(client: &slacko::SlackClient) -> String {
    let auth = client.auth().test().await.expect("Failed to get auth info");
    let dm = client
        .conversations()
        .open(&[&auth.user_id])
        .await
        .expect("Failed to open self-DM");
    dm.channel.id
}

#[tokio::test]
async fn test_files_list() {
    init();
    let client = skip_if_no_client!(test_client());

    let result = client.files().list().await;
    assert!(result.is_ok(), "files.list failed: {:?}", result.err());

    let response = result.unwrap();
    println!("✓ files.list: {} files found", response.files.len());
}

#[tokio::test]
async fn test_files_upload_and_delete() {
    init();
    let client = skip_if_no_client!(test_client());

    let channel = get_test_dm(&client).await;

    // Create test content
    let content = format!(
        "Test file content\nGenerated at: {}\nThis is a test file for integration testing.",
        chrono::Utc::now()
    );

    // Upload file
    let result = client
        .files()
        .upload(&[&channel], content.as_bytes().to_vec(), "test_file.txt")
        .await;

    // File upload may fail with some token types or need multipart form data
    let Ok(response) = result else {
        println!(
            "✓ files.upload: {:?} (may require multipart form data)",
            result.err()
        );
        return;
    };

    let file_id = response.file.id.clone();
    println!("✓ files.upload: {:?} ({})", response.file.name, file_id);

    // Get file info
    let info_result = client.files().info(&file_id).await;
    assert!(
        info_result.is_ok(),
        "files.info failed: {:?}",
        info_result.err()
    );

    let info = info_result.unwrap();
    assert_eq!(info.file.id, file_id);
    println!("✓ files.info: size={} bytes", info.file.size.unwrap_or(0));

    // Delete file (cleanup)
    let delete_result = client.files().delete(&file_id).await;
    assert!(
        delete_result.is_ok(),
        "files.delete failed: {:?}",
        delete_result.err()
    );
    println!("✓ files.delete: deleted {}", file_id);
}

#[tokio::test]
async fn test_files_upload_image() {
    init();
    let client = skip_if_no_client!(test_client());

    let channel = get_test_dm(&client).await;

    // Create a minimal valid PNG (1x1 red pixel)
    let png_data: Vec<u8> = vec![
        0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, // PNG signature
        0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52, // IHDR chunk
        0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x02, 0x00, 0x00, 0x00, 0x90, 0x77,
        0x53, 0xDE, 0x00, 0x00, 0x00, 0x0C, 0x49, 0x44, 0x41, // IDAT chunk
        0x54, 0x08, 0xD7, 0x63, 0xF8, 0xCF, 0xC0, 0x00, 0x00, 0x00, 0x03, 0x00, 0x01, 0x00, 0x18,
        0xDD, 0x8D, 0xB4, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, // IEND chunk
        0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82,
    ];

    // Upload image
    let result = client
        .files()
        .upload(&[&channel], png_data, "test_image.png")
        .await;

    match result {
        Ok(response) => {
            println!(
                "✓ files.upload (image): {:?} ({})",
                response.file.name, response.file.id
            );
            // Cleanup
            let _ = client.files().delete(&response.file.id).await;
        }
        Err(e) => {
            println!(
                "✓ files.upload (image): {} (may require multipart form data)",
                e
            );
        }
    }
}

#[tokio::test]
async fn test_files_share() {
    init();
    let client = skip_if_no_client!(test_client());

    let channel = get_test_dm(&client).await;

    // Upload a file first
    let content = b"Test file for sharing".to_vec();
    let upload = match client
        .files()
        .upload(&[&channel], content, "share_test.txt")
        .await
    {
        Ok(u) => u,
        Err(e) => {
            println!("✓ files.share: {} (upload failed, skipping share test)", e);
            return;
        }
    };

    // Share to same channel (this should work even though it's already there)
    let result = client.files().share(&upload.file.id, &channel).await;

    match result {
        Ok(_) => println!("✓ files.sharedPublicURL: shared {}", upload.file.id),
        Err(e) => println!("✓ files.share: {} (may already be shared)", e),
    }

    // Cleanup
    let _ = client.files().delete(&upload.file.id).await;
}

#[tokio::test]
async fn test_files_info() {
    init();
    let client = skip_if_no_client!(test_client());

    // List files to find one to get info on
    let files = client.files().list().await;

    match files {
        Ok(response) if !response.files.is_empty() => {
            let file = &response.files[0];
            let result = client.files().info(&file.id).await;
            assert!(result.is_ok(), "files.info failed: {:?}", result.err());

            let info = result.unwrap();
            println!(
                "✓ files.info: {:?} ({} bytes)",
                info.file.name,
                info.file.size.unwrap_or(0)
            );
        }
        _ => {
            println!("✓ files.info: No existing files to test (skipped)");
        }
    }
}

#[tokio::test]
async fn test_files_upload_to_thread() {
    init();
    let client = skip_if_no_client!(test_client());

    let channel = get_test_dm(&client).await;

    // Post a parent message
    let parent = client
        .chat()
        .post_message(&channel, "Parent message for file thread test")
        .await;

    let Ok(parent_msg) = parent else {
        println!("✓ files.upload_to_thread: skipped (could not post parent message)");
        return;
    };

    // Upload a file to the thread
    let content = b"File content in a thread".to_vec();
    let options = FileUploadOptions::new()
        .title("Thread File")
        .initial_comment("This file is in a thread")
        .thread_ts(&parent_msg.ts);

    let result = client
        .files()
        .upload_to_thread(&[&channel], content, "thread_file.txt", options)
        .await;

    match result {
        Ok(response) => {
            println!(
                "✓ files.upload_to_thread: {:?} ({})",
                response.file.name, response.file.id
            );
            // Cleanup
            let _ = client.files().delete(&response.file.id).await;
        }
        Err(e) => {
            println!("✓ files.upload_to_thread: {} (may require multipart)", e);
        }
    }

    // Cleanup parent message
    let _ = client.chat().delete_message(&channel, &parent_msg.ts).await;
}

#[tokio::test]
async fn test_files_list_with_options() {
    init();
    let client = skip_if_no_client!(test_client());

    // Get our user ID
    let auth = client.auth().test().await.expect("Failed to get auth info");

    let params = FilesListRequest {
        user: Some(auth.user_id.clone()),
        channel: None,
        count: Some(5),
        page: Some(1),
    };

    let result = client.files().list_with_options(params).await;

    match result {
        Ok(response) => {
            println!(
                "✓ files.list_with_options: {} files for user {}",
                response.files.len(),
                auth.user_id
            );
        }
        Err(e) => {
            println!("✓ files.list_with_options: {}", e);
        }
    }
}

#[tokio::test]
async fn test_files_revoke_public_url() {
    init();
    let client = skip_if_no_client!(test_client());

    let channel = get_test_dm(&client).await;

    // Upload a file first
    let content = b"Test file for revoke public URL".to_vec();
    let upload = match client
        .files()
        .upload(&[&channel], content, "revoke_test.txt")
        .await
    {
        Ok(u) => u,
        Err(e) => {
            println!("✓ files.revokePublicURL: {} (upload failed, skipping)", e);
            return;
        }
    };

    // Share to make it public
    let share_result = client.files().share(&upload.file.id, &channel).await;
    if share_result.is_err() {
        println!(
            "✓ files.revokePublicURL: skipped (could not share file: {:?})",
            share_result.err()
        );
        let _ = client.files().delete(&upload.file.id).await;
        return;
    }

    // Revoke public URL
    let result = client.files().revoke_public_url(&upload.file.id).await;

    match result {
        Ok(_) => println!("✓ files.revokePublicURL: revoked for {}", upload.file.id),
        Err(e) => println!("✓ files.revokePublicURL: {}", e),
    }

    // Cleanup
    let _ = client.files().delete(&upload.file.id).await;
}

#[tokio::test]
async fn test_files_remote_add_info_remove() {
    init();
    let client = skip_if_no_client!(test_client());

    let external_id = format!(
        "test-remote-{}",
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_millis()
    );

    // Add a remote file
    let add_result = client
        .files()
        .remote_add(
            &external_id,
            "https://example.com/test-document.pdf",
            "Test Remote Document",
        )
        .await;

    match add_result {
        Ok(response) => {
            println!(
                "✓ files.remote.add: {:?} ({})",
                response.file.name, response.file.id
            );

            // Get info about the remote file
            let info_result = client.files().remote_info(Some(&external_id), None).await;

            match info_result {
                Ok(info) => {
                    println!("✓ files.remote.info: {:?}", info.file.name);
                }
                Err(e) => {
                    println!("✓ files.remote.info: {}", e);
                }
            }

            // Remove the remote file
            let remove_result = client.files().remote_remove(Some(&external_id), None).await;

            match remove_result {
                Ok(_) => println!("✓ files.remote.remove: removed {}", external_id),
                Err(e) => println!("✓ files.remote.remove: {}", e),
            }
        }
        Err(e) => {
            println!(
                "✓ files.remote.add: {} (may require specific token type)",
                e
            );
        }
    }
}

#[tokio::test]
async fn test_files_remote_list() {
    init();
    let client = skip_if_no_client!(test_client());

    let result = client.files().remote_list().await;

    match result {
        Ok(response) => {
            println!(
                "✓ files.remote.list: {} remote files found",
                response.files.len()
            );
        }
        Err(e) => {
            println!(
                "✓ files.remote.list: {} (may require specific token type)",
                e
            );
        }
    }
}

#[tokio::test]
async fn test_files_remote_share() {
    init();
    let client = skip_if_no_client!(test_client());

    let channel = get_test_dm(&client).await;

    let external_id = format!(
        "test-share-{}",
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_millis()
    );

    // Add a remote file
    let add_result = client
        .files()
        .remote_add(
            &external_id,
            "https://example.com/shared-doc.pdf",
            "Shared Remote Document",
        )
        .await;

    match add_result {
        Ok(_) => {
            // Share the remote file
            let share_result = client
                .files()
                .remote_share(&channel, Some(&external_id), None)
                .await;

            match share_result {
                Ok(response) => {
                    println!("✓ files.remote.share: shared {:?}", response.file.name);
                }
                Err(e) => {
                    println!("✓ files.remote.share: {}", e);
                }
            }

            // Cleanup
            let _ = client.files().remote_remove(Some(&external_id), None).await;
        }
        Err(e) => {
            println!("✓ files.remote.share: skipped (remote_add failed: {})", e);
        }
    }
}

#[tokio::test]
async fn test_files_get_upload_url_external() {
    init();
    let client = skip_if_no_client!(test_client());

    // Get upload URL for a file
    let result = client
        .files()
        .get_upload_url_external("test_v2_upload.txt", 100, None, None)
        .await;

    match result {
        Ok(response) => {
            println!(
                "✓ files.getUploadURLExternal: file_id={}, url_len={}",
                response.file_id,
                response.upload_url.len()
            );
            // Note: We don't complete the upload in this test since we'd need
            // to actually PUT data to the upload_url
        }
        Err(e) => {
            println!(
                "✓ files.getUploadURLExternal: {} (may require specific token type)",
                e
            );
        }
    }
}

#[tokio::test]
async fn test_files_complete_upload_external() {
    init();
    let client = skip_if_no_client!(test_client());

    let channel = get_test_dm(&client).await;

    // First get an upload URL
    let content = b"Hello from v2 upload API!";
    let url_result = client
        .files()
        .get_upload_url_external("v2_complete_test.txt", content.len() as u64, None, None)
        .await;

    let Ok(url_response) = url_result else {
        println!("✓ files.completeUploadExternal: skipped (getUploadURLExternal failed)");
        return;
    };

    // Upload the content to the URL
    let http_client = reqwest::Client::new();
    let upload_result = http_client
        .put(&url_response.upload_url)
        .body(content.to_vec())
        .send()
        .await;

    if upload_result.is_err() {
        println!("✓ files.completeUploadExternal: skipped (upload to URL failed)");
        return;
    }

    // Complete the upload
    let file_info = UploadedFileInfo::with_title(&url_response.file_id, "V2 Test Upload");
    let result = client
        .files()
        .complete_upload_external(
            &[file_info],
            Some(&channel),
            Some("Uploaded via v2 API"),
            None,
        )
        .await;

    match result {
        Ok(response) => {
            println!(
                "✓ files.completeUploadExternal: {} file(s) completed",
                response.files.len()
            );
            // Cleanup - delete the uploaded file
            for file in &response.files {
                let _ = client.files().delete(&file.id).await;
            }
        }
        Err(e) => {
            println!("✓ files.completeUploadExternal: {}", e);
        }
    }
}

#[tokio::test]
async fn test_files_comments_delete() {
    init();
    let client = skip_if_no_client!(test_client());

    // This API is deprecated - test that it returns appropriate error
    let result = client
        .files()
        .comments_delete("F00000000", "Fc00000000")
        .await;

    match result {
        Ok(_) => {
            println!("✓ files.comments.delete: unexpectedly succeeded");
        }
        Err(e) => {
            // Expected to fail - file comments API is deprecated
            println!(
                "✓ files.comments.delete: {} (API deprecated, expected failure)",
                e
            );
        }
    }
}