| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
use crate::common::*; |
| 5 |
|
| 6 |
const DEVICES_PATH: &str = "/api/v1/sync/devices"; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#[tokio::test] |
| 11 |
async fn register_device_success() { |
| 12 |
let kit = MockKit::start().await; |
| 13 |
kit.post(DEVICES_PATH).json(device_json()).await; |
| 14 |
|
| 15 |
let device = kit |
| 16 |
.authed() |
| 17 |
.register_device("MacBook", "macos") |
| 18 |
.await |
| 19 |
.unwrap(); |
| 20 |
assert_eq!(device.device_name, "Test Device"); |
| 21 |
} |
| 22 |
|
| 23 |
#[tokio::test] |
| 24 |
async fn register_device_retries_on_transient() { |
| 25 |
let kit = MockKit::start().await; |
| 26 |
kit.post(DEVICES_PATH) |
| 27 |
.code(502) |
| 28 |
.once() |
| 29 |
.text("Bad Gateway") |
| 30 |
.await; |
| 31 |
kit.post(DEVICES_PATH).json(device_json()).await; |
| 32 |
|
| 33 |
let result = kit.authed().register_device("MacBook", "macos").await; |
| 34 |
assert!(result.is_ok(), "Should succeed after retry: {result:?}"); |
| 35 |
} |
| 36 |
|
| 37 |
#[tokio::test] |
| 38 |
async fn list_devices_success() { |
| 39 |
let kit = MockKit::start().await; |
| 40 |
kit.get(DEVICES_PATH).json(json!([device_json()])).await; |
| 41 |
|
| 42 |
let devices = kit.authed().list_devices().await.unwrap(); |
| 43 |
assert_eq!(devices.len(), 1); |
| 44 |
assert_eq!(devices[0].device_name, "Test Device"); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
#[tokio::test] |
| 50 |
async fn list_devices_without_auth_returns_not_authenticated() { |
| 51 |
let kit = MockKit::start().await; |
| 52 |
let err = kit.client().list_devices().await.unwrap_err(); |
| 53 |
assert!(matches!(err, SyncKitError::NotAuthenticated)); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
#[tokio::test] |
| 59 |
async fn register_device_with_empty_name() { |
| 60 |
let kit = MockKit::start().await; |
| 61 |
kit.post(DEVICES_PATH).json(device_json()).await; |
| 62 |
|
| 63 |
|
| 64 |
let result = kit.authed().register_device("", "macos").await; |
| 65 |
assert!(result.is_ok(), "Empty device name should not panic"); |
| 66 |
} |
| 67 |
|
| 68 |
#[tokio::test] |
| 69 |
async fn register_device_with_unicode_name() { |
| 70 |
let kit = MockKit::start().await; |
| 71 |
|
| 72 |
let (user_id, app_id) = test_ids(); |
| 73 |
kit.post(DEVICES_PATH) |
| 74 |
.json(json!({ |
| 75 |
"id": Uuid::new_v4(), |
| 76 |
"app_id": app_id, |
| 77 |
"user_id": user_id, |
| 78 |
"device_name": "\u{30DE}\u{30C3}\u{30AF}\u{30D6}\u{30C3}\u{30AF}", |
| 79 |
"platform": "macos", |
| 80 |
"last_seen_at": "2025-01-01T00:00:00Z", |
| 81 |
"created_at": "2025-01-01T00:00:00Z", |
| 82 |
})) |
| 83 |
.await; |
| 84 |
|
| 85 |
let device = kit |
| 86 |
.authed() |
| 87 |
.register_device("\u{30DE}\u{30C3}\u{30AF}\u{30D6}\u{30C3}\u{30AF}", "macos") |
| 88 |
.await |
| 89 |
.unwrap(); |
| 90 |
assert_eq!( |
| 91 |
device.device_name, |
| 92 |
"\u{30DE}\u{30C3}\u{30AF}\u{30D6}\u{30C3}\u{30AF}" |
| 93 |
); |
| 94 |
} |
| 95 |
|
| 96 |
#[tokio::test] |
| 97 |
async fn list_devices_empty_array() { |
| 98 |
let kit = MockKit::start().await; |
| 99 |
kit.get(DEVICES_PATH).json(json!([])).await; |
| 100 |
|
| 101 |
let devices = kit.authed().list_devices().await.unwrap(); |
| 102 |
assert!(devices.is_empty()); |
| 103 |
} |
| 104 |
|