redis-enterprise 0.10.0

Redis Enterprise REST API client library
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
//! Module endpoint tests for Redis Enterprise

use redis_enterprise::{EnterpriseClient, ModuleHandler};
use serde_json::json;
use wiremock::matchers::{basic_auth, method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};

// Test helper functions
fn success_response(body: serde_json::Value) -> ResponseTemplate {
    ResponseTemplate::new(200).set_body_json(body)
}

fn created_response(body: serde_json::Value) -> ResponseTemplate {
    ResponseTemplate::new(201).set_body_json(body)
}

fn no_content_response() -> ResponseTemplate {
    ResponseTemplate::new(204)
}

fn test_module() -> serde_json::Value {
    json!({
        "uid": "1",
        "module_name": "RedisSearch",
        "version": 20601,
        "semantic_version": "2.6.1",
        "capabilities": ["search", "index"]
    })
}

/// Test module with platforms field as returned by the actual API
/// The API returns platforms as a map/object, not an array
fn test_module_with_platforms() -> serde_json::Value {
    json!({
        "uid": "2",
        "module_name": "search",
        "semantic_version": "2.10.15",
        "version": 21015,
        "author": "RedisLabs",
        "description": "High performance search index on top of Redis",
        "homepage": "http://redisearch.io",
        "license": "Redis Source Available License 2.0",
        "command_line_args": "",
        "capabilities": ["search", "index"],
        "min_redis_version": "7.4",
        "compatible_redis_version": "7.4",
        "display_name": "RediSearch 2",
        "is_bundled": true,
        "platforms": {
            "rhel9/x86_64": {
                "dependencies": {},
                "sha256": "6bad5fdb464af8ecdf98b63dc26d65e08774826fbc11b0b5a8da363cb97fbd8c"
            },
            "rhel8/x86_64": {
                "dependencies": {},
                "sha256": "abc123def456789"
            }
        }
    })
}

#[tokio::test]
async fn test_module_list() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/v1/modules"))
        .and(basic_auth("admin", "password"))
        .respond_with(success_response(json!([
            test_module(),
            {
                "uid": "2",
                "module_name": "RedisJSON",
                "version": 20400,
                "semantic_version": "2.4.0",
                "capabilities": ["json"]
            }
        ])))
        .mount(&mock_server)
        .await;

    let client = EnterpriseClient::builder()
        .base_url(mock_server.uri())
        .username("admin")
        .password("password")
        .build()
        .unwrap();

    let handler = ModuleHandler::new(client);
    let result = handler.list().await;

    assert!(result.is_ok());
    let modules = result.unwrap();
    assert_eq!(modules.len(), 2);
}

#[tokio::test]
async fn test_module_get() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/v1/modules/1"))
        .and(basic_auth("admin", "password"))
        .respond_with(success_response(test_module()))
        .mount(&mock_server)
        .await;

    let client = EnterpriseClient::builder()
        .base_url(mock_server.uri())
        .username("admin")
        .password("password")
        .build()
        .unwrap();

    let handler = ModuleHandler::new(client);
    let result = handler.get("1").await;

    assert!(result.is_ok());
    let module = result.unwrap();
    assert_eq!(module.uid, "1");
    assert_eq!(module.module_name, Some("RedisSearch".to_string()));
}

#[tokio::test]
async fn test_module_upload() {
    let mock_server = MockServer::start().await;

    // Mock v2 endpoint as not found
    Mock::given(method("POST"))
        .and(path("/v2/modules"))
        .and(basic_auth("admin", "password"))
        .respond_with(ResponseTemplate::new(404))
        .mount(&mock_server)
        .await;

    // Mock v1 endpoint as success
    Mock::given(method("POST"))
        .and(path("/v1/modules"))
        .and(basic_auth("admin", "password"))
        .respond_with(created_response(test_module()))
        .mount(&mock_server)
        .await;

    let client = EnterpriseClient::builder()
        .base_url(mock_server.uri())
        .username("admin")
        .password("password")
        .build()
        .unwrap();

    let handler = ModuleHandler::new(client);
    let result = handler.upload(vec![1, 2, 3, 4], "test.zip").await; // Mock binary data

    assert!(result.is_ok());
    let response = result.unwrap();
    // Response is now a Value, not a Module
    assert_eq!(response["uid"], "1");
    assert_eq!(response["module_name"], "RedisSearch");
}

#[tokio::test]
async fn test_module_delete() {
    let mock_server = MockServer::start().await;

    Mock::given(method("DELETE"))
        .and(path("/v1/modules/1"))
        .and(basic_auth("admin", "password"))
        .respond_with(no_content_response())
        .mount(&mock_server)
        .await;

    let client = EnterpriseClient::builder()
        .base_url(mock_server.uri())
        .username("admin")
        .password("password")
        .build()
        .unwrap();

    let handler = ModuleHandler::new(client);
    let result = handler.delete("1").await;

    assert!(result.is_ok());
}

/// Test that demonstrates the bug: platforms field is returned as a map by the API
/// but the Module struct expects it as a Vec<String>
#[tokio::test]
async fn test_module_list_with_platforms_map() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/v1/modules"))
        .and(basic_auth("admin", "password"))
        .respond_with(success_response(json!([test_module_with_platforms()])))
        .mount(&mock_server)
        .await;

    let client = EnterpriseClient::builder()
        .base_url(mock_server.uri())
        .username("admin")
        .password("password")
        .build()
        .unwrap();

    let handler = ModuleHandler::new(client);
    let result = handler.list().await;

    // This should succeed after the fix
    assert!(
        result.is_ok(),
        "Failed to deserialize module with platforms map: {:?}",
        result.err()
    );
    let modules = result.unwrap();
    assert_eq!(modules.len(), 1);

    let module = &modules[0];
    assert_eq!(module.uid, "2");
    assert_eq!(module.module_name, Some("search".to_string()));

    // After the fix, platforms should be accessible as a HashMap
    assert!(
        module.platforms.is_some(),
        "Platforms field should be present"
    );
}

/// Test getting a single module with platforms field
#[tokio::test]
async fn test_module_get_with_platforms_map() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/v1/modules/2"))
        .and(basic_auth("admin", "password"))
        .respond_with(success_response(test_module_with_platforms()))
        .mount(&mock_server)
        .await;

    let client = EnterpriseClient::builder()
        .base_url(mock_server.uri())
        .username("admin")
        .password("password")
        .build()
        .unwrap();

    let handler = ModuleHandler::new(client);
    let result = handler.get("2").await;

    // This should succeed after the fix
    assert!(
        result.is_ok(),
        "Failed to deserialize module with platforms map: {:?}",
        result.err()
    );
    let module = result.unwrap();
    assert_eq!(module.uid, "2");
    assert_eq!(module.module_name, Some("search".to_string()));
    assert!(
        module.platforms.is_some(),
        "Platforms field should be present"
    );
}

// ===========================================================================
// Closes #55: user-defined module endpoints.
// Verified against Redis Enterprise Software 8.0.10-81 on 2026-04-21:
// - GET  /v2/local/modules/user-defined/artifacts -> 200 OK with []
// - POST /v2/modules/user-defined with {}         -> 406 invalid_module
// - POST /v2/local/modules/user-defined/artifacts -> 400 no_module
// - DELETE /v2/modules/user-defined/1             -> 404 module_delete_failed
// All four confirm the routes are served by the live cluster.
// ===========================================================================

#[tokio::test]
async fn test_module_list_user_defined_artifacts() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/v2/local/modules/user-defined/artifacts"))
        .and(basic_auth("admin", "password"))
        .respond_with(success_response(
            json!([{"module_name": "myext", "version": "1.0.0"}]),
        ))
        .mount(&mock_server)
        .await;

    let client = EnterpriseClient::builder()
        .base_url(mock_server.uri())
        .username("admin")
        .password("password")
        .build()
        .unwrap();

    let handler = ModuleHandler::new(client);
    let result = handler.list_user_defined_artifacts().await.unwrap();
    assert_eq!(result[0]["module_name"], "myext");
    assert_eq!(result[0]["version"], "1.0.0");
}

#[tokio::test]
async fn test_module_upload_user_defined_artifact() {
    let mock_server = MockServer::start().await;

    Mock::given(method("POST"))
        .and(path("/v2/local/modules/user-defined/artifacts"))
        .and(basic_auth("admin", "password"))
        .respond_with(success_response(
            json!({"module_name": "myext", "version": "1.0.0"}),
        ))
        .mount(&mock_server)
        .await;

    let client = EnterpriseClient::builder()
        .base_url(mock_server.uri())
        .username("admin")
        .password("password")
        .build()
        .unwrap();

    let handler = ModuleHandler::new(client);
    let result = handler
        .upload_user_defined_artifact(vec![1, 2, 3, 4], "myext-1.0.0.zip")
        .await
        .unwrap();
    assert_eq!(result["module_name"], "myext");
}

#[tokio::test]
async fn test_module_register_user_defined() {
    let mock_server = MockServer::start().await;

    Mock::given(method("POST"))
        .and(path("/v2/modules/user-defined"))
        .and(basic_auth("admin", "password"))
        .and(wiremock::matchers::body_json(json!({
            "module_name": "myext",
            "version": "1.0.0"
        })))
        .respond_with(success_response(json!({"uid": "42"})))
        .mount(&mock_server)
        .await;

    let client = EnterpriseClient::builder()
        .base_url(mock_server.uri())
        .username("admin")
        .password("password")
        .build()
        .unwrap();

    let handler = ModuleHandler::new(client);
    let result = handler
        .register_user_defined(json!({
            "module_name": "myext",
            "version": "1.0.0"
        }))
        .await
        .unwrap();
    assert_eq!(result["uid"], "42");
}

#[tokio::test]
async fn test_module_delete_user_defined() {
    let mock_server = MockServer::start().await;

    Mock::given(method("DELETE"))
        .and(path("/v2/modules/user-defined/42"))
        .and(basic_auth("admin", "password"))
        .respond_with(ResponseTemplate::new(200))
        .mount(&mock_server)
        .await;

    let client = EnterpriseClient::builder()
        .base_url(mock_server.uri())
        .username("admin")
        .password("password")
        .build()
        .unwrap();

    let handler = ModuleHandler::new(client);
    assert!(handler.delete_user_defined("42").await.is_ok());
}

#[tokio::test]
async fn test_module_delete_user_defined_artifact() {
    let mock_server = MockServer::start().await;

    Mock::given(method("DELETE"))
        .and(path("/v2/local/modules/user-defined/artifacts/myext/1.0.0"))
        .and(basic_auth("admin", "password"))
        .respond_with(ResponseTemplate::new(200))
        .mount(&mock_server)
        .await;

    let client = EnterpriseClient::builder()
        .base_url(mock_server.uri())
        .username("admin")
        .password("password")
        .build()
        .unwrap();

    let handler = ModuleHandler::new(client);
    assert!(
        handler
            .delete_user_defined_artifact("myext", "1.0.0")
            .await
            .is_ok()
    );
}