dsh_sdk 0.8.1

SDK for KPN Data Services Hub
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
//! Management API token fetching for DSH.
//!
//! This module provides an interface (`ManagementApiTokenFetcher`) for fetching and
//! caching access tokens required to communicate with DSH’s management (REST) API.
//! Access tokens are automatically refreshed when expired, allowing seamless
//! integrations with the DSH platform.
//!
//! # Overview
//! - **[`AccessToken`]**: Access token from the authentication server.  
//! - **[`ManagementApiTokenFetcher`]**: A token fetcher that caches tokens and
//!   refreshes them upon expiration.  
//! - **[`ManagementApiTokenFetcherBuilder`]**: A builder for customizing the fetcher’s
//!   client, credentials, and target platform.  
//!
//! # Typical Usage
//! 1. **Instantiate** a fetcher with credentials:  
//!    ```
//!    use dsh_sdk::management_api::ManagementApiTokenFetcherBuilder;
//!    use dsh_sdk::Platform;
//!
//!    let platform = Platform::NpLz;
//!    let token_fetcher = ManagementApiTokenFetcherBuilder::new(platform)
//!         .tenant_name("my-tenant")
//!         .client_secret("my-secret")
//!         .build()
//!         .unwrap();
//!    ```
//! 2. **Fetch** the token when needed:  
//!    ```ignore
//!    let token = token_fetcher.get_token().await?;
//!    ```
//! 3. **Reuse** the same fetcher for subsequent calls; it auto-refreshes tokens.  
//!
//! For more advanced usage (custom [`reqwest::Client`] or different credential sourcing),
//! see [`ManagementApiTokenFetcher::new_with_client`] or the [`ManagementApiTokenFetcherBuilder`].

use std::fmt::Debug;
use std::ops::Add;
use std::sync::Mutex;
use std::time::{Duration, Instant};

use log::debug;
use serde::Deserialize;

use super::error::ManagementApiTokenError;
use crate::utils::Platform;

/// Represents the Access Token by DSH’s authentication service.
///
/// The fields include information about the token’s validity window,
/// token type, and scope. Typically, you won’t instantiate `AccessToken` directly:
/// use [`ManagementApiTokenFetcher::get_token`](crate::management_api::ManagementApiTokenFetcher::get_token)
/// to automatically obtain or refresh a valid token.
#[derive(Debug, Clone, Deserialize)]
pub struct AccessToken {
    access_token: String,
    expires_in: u64,
    refresh_expires_in: u32,
    token_type: String,
    #[serde(rename(deserialize = "not-before-policy"))]
    not_before_policy: u32,
    scope: String,
}

impl AccessToken {
    /// Returns a complete token string, i.e. `"{token_type} {access_token}"`.
    pub fn formatted_token(&self) -> String {
        format!("{} {}", self.token_type, self.access_token)
    }

    /// Returns the raw access token string (without the token type).
    pub fn access_token(&self) -> &str {
        &self.access_token
    }

    /// Returns the number of seconds until this token expires.
    pub fn expires_in(&self) -> u64 {
        self.expires_in
    }

    /// Returns the number of seconds until the refresh token expires.
    pub fn refresh_expires_in(&self) -> u32 {
        self.refresh_expires_in
    }

    /// Returns the token type (e.g., `"Bearer"`).
    pub fn token_type(&self) -> &str {
        &self.token_type
    }

    /// Returns the “not before” policy timestamp from the authentication server.
    pub fn not_before_policy(&self) -> u32 {
        self.not_before_policy
    }

    /// Returns the scope string (e.g., `"email"`).
    pub fn scope(&self) -> &str {
        &self.scope
    }
}

impl Default for AccessToken {
    fn default() -> Self {
        Self {
            access_token: "".to_string(),
            expires_in: 0,
            refresh_expires_in: 0,
            token_type: "".to_string(),
            not_before_policy: 0,
            scope: "".to_string(),
        }
    }
}

/// A fetcher for obtaining and storing access tokens, enabling authenticated
/// requests to DSH’s management (REST) API.
///
/// This struct caches the token in memory and refreshes it automatically
/// once expired.
///
/// # Usage
/// - **`new`**: Construct a fetcher with provided credentials.  
/// - **`new_with_client`**: Provide a custom [`reqwest::Client`] if needed.  
/// - **`get_token`**: Returns the current token if still valid, or fetches a new one.  
///
/// # Example
/// ```no_run
/// use dsh_sdk::management_api::ManagementApiTokenFetcher;
/// use dsh_sdk::Platform;
///
/// # use std::error::Error;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn Error>> {
/// let platform = Platform::NpLz;
/// let client_id = platform.management_api_client_id("my-tenant");
/// let client_secret = "my-secret".to_string();
/// let token_fetcher = ManagementApiTokenFetcher::new(
///     client_id,
///     client_secret,
///     platform.endpoint_management_api_token().to_string()
/// );
///
/// let token = token_fetcher.get_token().await?;
/// println!("Obtained token: {}", token);
/// # Ok(())
/// # }
/// ```
pub struct ManagementApiTokenFetcher {
    access_token: Mutex<AccessToken>,
    fetched_at: Mutex<Instant>,
    client_id: String,
    client_secret: String,
    client: reqwest::Client,
    auth_url: String,
}

impl ManagementApiTokenFetcher {
    /// Creates a new token fetcher.
    ///
    /// # Example
    /// ```no_run
    /// use dsh_sdk::management_api::ManagementApiTokenFetcher;
    /// use dsh_sdk::Platform;
    ///
    /// # #[tokio::main]
    /// # async fn main() {
    /// let platform = Platform::NpLz;
    /// let client_id = platform.management_api_client_id("my-tenant");
    /// let client_secret = "my-secret";
    /// let token_fetcher = ManagementApiTokenFetcher::new(
    ///     client_id,
    ///     client_secret,
    ///     platform.endpoint_management_api_token()
    /// );
    ///
    /// let token = token_fetcher.get_token().await.unwrap();
    /// println!("Token: {}", token);
    /// # }
    /// ```
    pub fn new(
        client_id: impl Into<String>,
        client_secret: impl Into<String>,
        auth_url: impl Into<String>,
    ) -> Self {
        Self::new_with_client(
            client_id,
            client_secret,
            auth_url,
            reqwest::Client::default(),
        )
    }

    /// Returns a [`ManagementApiTokenFetcherBuilder`] for more flexible creation
    /// of a token fetcher (e.g., specifying a custom client).
    pub fn builder(platform: Platform) -> ManagementApiTokenFetcherBuilder {
        ManagementApiTokenFetcherBuilder::new(platform)
    }

    /// Creates a new fetcher with a **custom** [`reqwest::Client`].
    ///
    /// # Example
    /// ```no_run
    /// use dsh_sdk::management_api::ManagementApiTokenFetcher;
    /// use dsh_sdk::Platform;
    ///
    /// # #[tokio::main]
    /// # async fn main() {
    /// let platform = Platform::NpLz;
    /// let client_id = platform.management_api_client_id("my-tenant");
    /// let client_secret = "my-secret";
    /// let custom_client = reqwest::Client::new();
    /// let token_fetcher = ManagementApiTokenFetcher::new_with_client(
    ///     client_id,
    ///     client_secret,
    ///     platform.endpoint_management_api_token().to_string(),
    ///     custom_client
    /// );
    /// let token = token_fetcher.get_token().await.unwrap();
    /// println!("Token: {}", token);
    /// # }
    /// ```
    pub fn new_with_client(
        client_id: impl Into<String>,
        client_secret: impl Into<String>,
        auth_url: impl Into<String>,
        client: reqwest::Client,
    ) -> Self {
        Self {
            access_token: Mutex::new(AccessToken::default()),
            fetched_at: Mutex::new(Instant::now()),
            client_id: client_id.into(),
            client_secret: client_secret.into(),
            client,
            auth_url: auth_url.into(),
        }
    }

    /// Obtains the token from cache if still valid, otherwise fetches a new one.
    ///
    /// The returned string is formatted as `"{token_type} {access_token}"`.
    ///
    /// # Errors
    /// - [`ManagementApiTokenError::FailureTokenFetch`]:
    ///   If the network request fails or times out when fetching a new token.
    /// - [`ManagementApiTokenError::StatusCode`]:
    ///   If the authentication server returns a non-success HTTP status code.
    ///
    /// # Example
    /// ```no_run
    /// use dsh_sdk::management_api::ManagementApiTokenFetcher;
    /// # #[tokio::main]
    /// # async fn main() {
    ///     let tf = ManagementApiTokenFetcher::new(
    ///         "client_id".to_string(),
    ///         "client_secret".to_string(),
    ///         "http://example.com/auth".to_string()
    ///     );
    ///     match tf.get_token().await {
    ///         Ok(token) => println!("Got token: {}", token),
    ///         Err(e) => eprintln!("Error fetching token: {}", e),
    ///     }
    /// }
    /// ```
    pub async fn get_token(&self) -> Result<String, ManagementApiTokenError> {
        if self.is_valid() {
            Ok(self.access_token.lock().unwrap().formatted_token())
        } else {
            debug!("Token is expired, fetching new token");
            let access_token = self.fetch_access_token_from_server().await?;
            let mut token = self.access_token.lock().unwrap();
            let mut fetched_at = self.fetched_at.lock().unwrap();
            *token = access_token;
            *fetched_at = Instant::now();
            Ok(token.formatted_token())
        }
    }

    /// Determines if the internally cached token is still valid.
    ///
    /// A token is considered valid if its remaining lifetime
    /// (minus a 5-second safety margin) is greater than zero.
    pub fn is_valid(&self) -> bool {
        let access_token = self.access_token.lock().unwrap_or_else(|mut e| {
            **e.get_mut() = AccessToken::default();
            self.access_token.clear_poison();
            e.into_inner()
        });
        let fetched_at = self.fetched_at.lock().unwrap_or_else(|e| {
            self.fetched_at.clear_poison();
            e.into_inner()
        });
        // Check if 'expires_in' has elapsed (+ 5-second safety margin)
        fetched_at.elapsed().add(Duration::from_secs(5))
            < Duration::from_secs(access_token.expires_in)
    }

    /// Fetches a fresh `AccessToken` from the authentication server.
    ///
    /// # Errors
    /// - [`ManagementApiTokenError::FailureTokenFetch`]:
    ///   If the network request fails or times out.
    /// - [`ManagementApiTokenError::StatusCode`]:
    ///   If the server returns a non-success status code.
    pub async fn fetch_access_token_from_server(
        &self,
    ) -> Result<AccessToken, ManagementApiTokenError> {
        let response = self
            .client
            .post(&self.auth_url)
            .form(&[
                ("client_id", self.client_id.as_ref()),
                ("client_secret", self.client_secret.as_ref()),
                ("grant_type", "client_credentials"),
            ])
            .send()
            .await
            .map_err(ManagementApiTokenError::FailureTokenFetch)?;

        if !response.status().is_success() {
            Err(ManagementApiTokenError::StatusCode {
                status_code: response.status(),
                error_body: response.text().await.unwrap_or_default(),
            })
        } else {
            response
                .json::<AccessToken>()
                .await
                .map_err(ManagementApiTokenError::FailureTokenFetch)
        }
    }
}

impl Debug for ManagementApiTokenFetcher {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ManagementApiTokenFetcher")
            .field("access_token", &self.access_token)
            .field("fetched_at", &self.fetched_at)
            .field("client_id", &self.client_id)
            // For security, obfuscate the secret
            .field("client_secret", &"xxxxxx")
            .field("auth_url", &self.auth_url)
            .finish()
    }
}

/// A builder for constructing a [`ManagementApiTokenFetcher`].
///
/// This builder allows customization of the token fetcher by specifying:
/// - **client_id** or **tenant_name** (tenant name is used to generate the client_id)
/// - **client_secret**
/// - **custom [`reqwest::Client`]** (optional)
/// - **platform** (e.g., [`Platform::NpLz`] or [`Platform::Poc`])
///
/// # Example
/// ```
/// use dsh_sdk::management_api::ManagementApiTokenFetcherBuilder;
/// use dsh_sdk::Platform;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let platform = Platform::NpLz;
/// let client_id = "robot:dev-lz-dsh:my-tenant".to_string();
/// let client_secret = "secret".to_string();
/// let token_fetcher = ManagementApiTokenFetcherBuilder::new(platform)
///     .client_id(client_id)
///     .client_secret(client_secret)
///     .build()?;
/// // Use `token_fetcher`
/// # Ok(())
/// # }
/// ```
pub struct ManagementApiTokenFetcherBuilder {
    client: Option<reqwest::Client>,
    client_id: Option<String>,
    client_secret: Option<String>,
    platform: Platform,
    tenant_name: Option<String>,
}

impl ManagementApiTokenFetcherBuilder {
    /// Creates a new builder configured for the specified [`Platform`].
    ///
    /// # Arguments
    /// - `platform`: The target platform (e.g., `Platform::NpLz`) to determine
    ///   default endpoints for fetching tokens.
    pub fn new(platform: Platform) -> Self {
        Self {
            client: None,
            client_id: None,
            client_secret: None,
            platform,
            tenant_name: None,
        }
    }

    /// Sets an explicit client ID for authentication.
    ///
    /// If you also specify `tenant_name`, the client ID here takes precedence.
    pub fn client_id(mut self, client_id: impl Into<String>) -> Self {
        self.client_id = Some(client_id.into());
        self
    }

    /// Sets a client secret required for token fetching.
    pub fn client_secret(mut self, client_secret: impl Into<String>) -> Self {
        self.client_secret = Some(client_secret.into());
        self
    }

    /// Sets a tenant name from which the client ID will be derived.
    ///
    /// This will use `platform.rest_client_id(tenant_name)` unless `client_id`
    /// is already set.
    pub fn tenant_name(mut self, tenant_name: impl Into<String>) -> Self {
        self.tenant_name = Some(tenant_name.into());
        self
    }

    /// Supplies a custom [`reqwest::Client`] if you need specialized settings
    /// (e.g., proxy configuration, timeouts, etc.).
    pub fn client(mut self, client: reqwest::Client) -> Self {
        self.client = Some(client);
        self
    }

    /// Builds a [`ManagementApiTokenFetcher`] based on the provided configuration.
    ///
    /// # Errors
    /// - [`ManagementApiTokenError::UnknownClientSecret`]:
    ///   If the client secret is unset.
    /// - [`ManagementApiTokenError::UnknownClientId`]:
    ///   If neither `client_id` nor `tenant_name` is provided.
    ///
    /// # Example
    /// ```
    /// use dsh_sdk::management_api::{ManagementApiTokenFetcherBuilder, ManagementApiTokenError};
    /// use dsh_sdk::Platform;
    ///
    /// # fn main() -> Result<(), ManagementApiTokenError> {
    /// let fetcher = ManagementApiTokenFetcherBuilder::new(Platform::NpLz)
    ///     .client_id("robot:dev-lz-dsh:my-tenant".to_string())
    ///     .client_secret("secret".to_string())
    ///     .build()?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn build(self) -> Result<ManagementApiTokenFetcher, ManagementApiTokenError> {
        let client_secret = self
            .client_secret
            .ok_or(ManagementApiTokenError::UnknownClientSecret)?;

        let client_id = self
            .client_id
            .or_else(|| {
                self.tenant_name
                    .as_ref()
                    .map(|tenant_name| self.platform.management_api_client_id(tenant_name))
            })
            .ok_or(ManagementApiTokenError::UnknownClientId)?;

        let client = self.client.unwrap_or_default();
        let token_fetcher = ManagementApiTokenFetcher::new_with_client(
            client_id,
            client_secret,
            self.platform.endpoint_management_api_token().to_string(),
            client,
        );
        Ok(token_fetcher)
    }
}

impl Debug for ManagementApiTokenFetcherBuilder {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let client_secret = self
            .client_secret
            .as_ref()
            .map(|_| "Some(\"client_secret\")");
        f.debug_struct("ManagementApiTokenFetcherBuilder")
            .field("client_id", &self.client_id)
            .field("client_secret", &client_secret)
            .field("platform", &self.platform)
            .field("tenant_name", &self.tenant_name)
            .finish()
    }
}

#[cfg(test)]
mod test {
    use super::*;

    fn create_mock_tf() -> ManagementApiTokenFetcher {
        ManagementApiTokenFetcher {
            access_token: Mutex::new(AccessToken::default()),
            fetched_at: Mutex::new(Instant::now()),
            client_id: "client_id".to_string(),
            client_secret: "client_secret".to_string(),
            client: reqwest::Client::new(),
            auth_url: "http://localhost".to_string(),
        }
    }

    /// Ensures `AccessToken` is properly deserialized and returns expected fields.
    #[test]
    fn test_access_token() {
        let token_str = r#"{
          "access_token": "secret_access_token",
          "expires_in": 600,
          "refresh_expires_in": 0,
          "token_type": "Bearer",
          "not-before-policy": 0,
          "scope": "email"
        }"#;
        let token: AccessToken = serde_json::from_str(token_str).unwrap();
        assert_eq!(token.access_token(), "secret_access_token");
        assert_eq!(token.expires_in(), 600);
        assert_eq!(token.refresh_expires_in(), 0);
        assert_eq!(token.token_type(), "Bearer");
        assert_eq!(token.not_before_policy(), 0);
        assert_eq!(token.scope(), "email");
        assert_eq!(token.formatted_token(), "Bearer secret_access_token");
    }

    /// Validates the default constructor yields an empty `AccessToken`.
    #[test]
    fn test_access_token_default() {
        let token = AccessToken::default();
        assert_eq!(token.access_token(), "");
        assert_eq!(token.expires_in(), 0);
        assert_eq!(token.refresh_expires_in(), 0);
        assert_eq!(token.token_type(), "");
        assert_eq!(token.not_before_policy(), 0);
        assert_eq!(token.scope(), "");
        assert_eq!(token.formatted_token(), " ");
    }

    /// Verifies that a default token is considered invalid since it expires immediately.
    #[test]
    fn test_rest_token_fetcher_is_valid_default_token() {
        let tf = create_mock_tf();
        assert!(!tf.is_valid(), "Default token should be invalid");
    }

    /// Demonstrates that `is_valid` returns true if a token is configured with future expiration.
    #[test]
    fn test_rest_token_fetcher_is_valid_valid_token() {
        let tf = create_mock_tf();
        tf.access_token.lock().unwrap().expires_in = 600;
        assert!(
            tf.is_valid(),
            "Token with 600s lifetime should be valid initially"
        );
    }

    /// Confirms `is_valid` returns false after the token’s entire lifetime has elapsed.
    #[test]
    fn test_rest_token_fetcher_is_valid_expired_token() {
        let tf = create_mock_tf();
        tf.access_token.lock().unwrap().expires_in = 600;
        *tf.fetched_at.lock().unwrap() = Instant::now() - Duration::from_secs(600);
        assert!(!tf.is_valid(), "Token should expire after 600s have passed");
    }

    /// Tests behavior when a token is “poisoned” (i.e., panicked while locked).
    #[test]
    fn test_rest_token_fetcher_is_valid_poisoned_token() {
        let tf = create_mock_tf();
        tf.access_token.lock().unwrap().expires_in = 600;
        let tf_arc = std::sync::Arc::new(tf);
        let tf_clone = tf_arc.clone();
        assert!(tf_arc.is_valid(), "Token should be valid before poison");
        let handle = std::thread::spawn(move || {
            let _unused = tf_clone.access_token.lock().unwrap();
            panic!("Poison token");
        });
        let _ = handle.join();
        assert!(
            !tf_arc.is_valid(),
            "Token should be reset to default after poisoning"
        );
    }

    /// Checks success scenario for fetching a new token from a mock server.
    #[tokio::test]
    async fn test_fetch_access_token_from_server() {
        let mut auth_server = mockito::Server::new_async().await;
        auth_server
            .mock("POST", "/")
            .with_status(200)
            .with_body(
                r#"{
          "access_token": "secret_access_token",
          "expires_in": 600,
          "refresh_expires_in": 0,
          "token_type": "Bearer",
          "not-before-policy": 0,
          "scope": "email"
        }"#,
            )
            .create();
        let mut tf = create_mock_tf();
        tf.auth_url = auth_server.url();
        let token = tf.fetch_access_token_from_server().await.unwrap();
        assert_eq!(token.access_token(), "secret_access_token");
        assert_eq!(token.expires_in(), 600);
    }

    /// Checks that an HTTP 400 response is handled as an error.
    #[tokio::test]
    async fn test_fetch_access_token_from_server_error() {
        let mut auth_server = mockito::Server::new_async().await;
        auth_server
            .mock("POST", "/")
            .with_status(400)
            .with_body("Bad request")
            .create();
        let mut tf = create_mock_tf();
        tf.auth_url = auth_server.url();
        let err = tf.fetch_access_token_from_server().await.unwrap_err();
        match err {
            ManagementApiTokenError::StatusCode {
                status_code,
                error_body,
            } => {
                assert_eq!(status_code, reqwest::StatusCode::BAD_REQUEST);
                assert_eq!(error_body, "Bad request");
            }
            _ => panic!("Unexpected error: {:?}", err),
        }
    }

    /// Ensures the builder sets `client_id` explicitly.
    #[test]
    fn test_token_fetcher_builder_client_id() {
        let platform = Platform::NpLz;
        let client_id = "robot:dev-lz-dsh:my-tenant";
        let client_secret = "secret";
        let tf = ManagementApiTokenFetcherBuilder::new(platform)
            .client_id(client_id.to_string())
            .client_secret(client_secret.to_string())
            .build()
            .unwrap();
        assert_eq!(tf.client_id, client_id);
        assert_eq!(tf.client_secret, client_secret);
        assert_eq!(tf.auth_url, Platform::NpLz.endpoint_management_api_token());
    }

    /// Ensures the builder can auto-generate `client_id` from the `tenant_name`.
    #[test]
    fn test_token_fetcher_builder_tenant_name() {
        let platform = Platform::NpLz;
        let tenant_name = "my-tenant";
        let client_secret = "secret";
        let tf = ManagementApiTokenFetcherBuilder::new(platform)
            .tenant_name(tenant_name.to_string())
            .client_secret(client_secret.to_string())
            .build()
            .unwrap();
        assert_eq!(
            tf.client_id,
            format!("robot:{}:{}", Platform::NpLz.realm(), tenant_name)
        );
        assert_eq!(tf.client_secret, client_secret);
        assert_eq!(tf.auth_url, Platform::NpLz.endpoint_management_api_token());
    }

    /// Validates that a custom `reqwest::Client` can be injected into the builder.
    #[test]
    fn test_token_fetcher_builder_custom_client() {
        let platform = Platform::NpLz;
        let client_id = "robot:dev-lz-dsh:my-tenant";
        let client_secret = "secret";
        let custom_client = reqwest::Client::builder()
            .tls_backend_rustls()
            .build()
            .unwrap();
        let tf = ManagementApiTokenFetcherBuilder::new(platform)
            .client_id(client_id.to_string())
            .client_secret(client_secret.to_string())
            .client(custom_client.clone())
            .build()
            .unwrap();
        assert_eq!(tf.client_id, client_id);
        assert_eq!(tf.client_secret, client_secret);
        assert_eq!(tf.auth_url, Platform::NpLz.endpoint_management_api_token());
    }

    /// Tests precedence of `client_id` over a derived tenant-based client ID.
    #[test]
    fn test_token_fetcher_builder_client_id_precedence() {
        let platform = Platform::NpLz;
        let tenant = "my-tenant";
        let client_id_override = "override";
        let client_secret = "secret";
        let tf = ManagementApiTokenFetcherBuilder::new(platform)
            .tenant_name(tenant.to_string())
            .client_id(client_id_override.to_string())
            .client_secret(client_secret.to_string())
            .build()
            .unwrap();
        assert_eq!(tf.client_id, client_id_override);
        assert_eq!(tf.client_secret, client_secret);
        assert_eq!(tf.auth_url, Platform::NpLz.endpoint_management_api_token());
    }

    /// Ensures builder returns errors if `client_id` or `client_secret` are missing.
    #[test]
    fn test_token_fetcher_builder_build_error() {
        let err = ManagementApiTokenFetcherBuilder::new(Platform::NpLz)
            .client_secret("client_secret".to_string())
            .build()
            .unwrap_err();
        assert!(matches!(err, ManagementApiTokenError::UnknownClientId));

        let err = ManagementApiTokenFetcherBuilder::new(Platform::NpLz)
            .tenant_name("tenant_name".to_string())
            .build()
            .unwrap_err();
        assert!(matches!(err, ManagementApiTokenError::UnknownClientSecret));
    }
}