Skip to content
This repository was archived by the owner on Jan 2, 2026. It is now read-only.

Commit aec9b37

Browse files
committed
chore: add tests, fix tests
1 parent bd39a39 commit aec9b37

File tree

6 files changed

+241
-7
lines changed

6 files changed

+241
-7
lines changed

.sqlx/query-9c4bfdb692922504b9a008b8504c5684391b14eaaf1946237e13c3a939bd8a10.json renamed to .sqlx/query-00736c782f4b8b83e6c8d0c207fc0f04555db3ebb9ffaf1ad53e5169c2fcf4b1.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fixtures/idcert_integration_tests.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
-- Contains data for testing HomeServerCert::get_idcert_by method
33

44
-- Algorithm identifiers including ED25519
5-
INSERT INTO algorithm_identifiers (id, algorithm_identifier, common_name, parameters) VALUES
5+
INSERT INTO algorithm_identifiers (id, algorithm_identifier, common_name, parameters_der_encoded) VALUES
66
(1, 'rsaEncryption', 'RSA', NULL),
77
(2, 'id-ecPublicKey', 'EC', NULL),
88
(3, '1.3.101.112', 'Edwards-curve Digital Signature Algorithm (EdDSA) Ed25519', NULL);

fixtures/local_actor_tests.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
-- This builds on the basic actor setup and adds specific test data for LocalActor methods
33

44
-- Algorithm identifiers for public keys (needed for actor setup)
5-
INSERT INTO algorithm_identifiers (id, algorithm_identifier, common_name, parameters) VALUES
5+
INSERT INTO algorithm_identifiers (id, algorithm_identifier, common_name, parameters_der_encoded) VALUES
66
(1, 'rsaEncryption', 'RSA', NULL),
77
(2, 'id-ecPublicKey', 'EC', NULL);
88

fixtures/tokens_base_fixture.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
-- Contains common database setup that can be reused across multiple test scenarios
33

44
-- Algorithm identifiers for public keys
5-
INSERT INTO algorithm_identifiers (id, algorithm_identifier, common_name, parameters) VALUES
5+
INSERT INTO algorithm_identifiers (id, algorithm_identifier, common_name, parameters_der_encoded) VALUES
66
(1, 'rsaEncryption', 'RSA', NULL),
77
(2, 'id-ecPublicKey', 'EC', NULL);
88

src/database/algorithm_identifier.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ impl AlgorithmIdentifier {
5656
}
5757
let parameters_der_encoded_reformatted =
5858
parameters_der_encoded.into_iter().map(|num| *num as i16).collect::<Vec<_>>();
59+
let parameters_for_query = if parameters_der_encoded_reformatted.is_empty() {
60+
None
61+
} else {
62+
Some(parameters_der_encoded_reformatted.as_slice())
63+
};
5964
let record = query!(
6065
r#"
6166
SELECT id, algorithm_identifier, common_name, parameters_der_encoded
@@ -64,12 +69,12 @@ impl AlgorithmIdentifier {
6469
($1::int IS NULL OR id = $1)
6570
AND ($2::text IS NULL OR algorithm_identifier = $2)
6671
AND ($3::text IS NULL OR common_name = $3)
67-
AND ($4::smallint [] IS NULL OR parameters_der_encoded = $4)
72+
AND ($4::smallint [] IS NULL OR parameters_der_encoded = $4 OR (parameters_der_encoded IS NULL AND $4::smallint [] = '{}'))
6873
"#,
6974
id,
7075
algorithm_identifier.map(|a| a.to_string()),
7176
common_name,
72-
&parameters_der_encoded_reformatted,
77+
parameters_for_query,
7378
)
7479
.fetch_all(&db.pool)
7580
.await?;

src/database/public_key_info.rs

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,232 @@ impl PublicKeyInfo {
153153
}
154154
}
155155
}
156+
157+
#[cfg(test)]
158+
mod tests {
159+
use std::str::FromStr;
160+
161+
use sqlx::{Pool, Postgres};
162+
163+
use super::*;
164+
use crate::crypto::ed25519::{DigitalPublicKey, DigitalSignature, generate_keypair};
165+
166+
#[sqlx::test(fixtures("../../fixtures/tokens_base_fixture.sql"))]
167+
async fn test_get_by_empty_parameters(pool: Pool<Postgres>) {
168+
let db = Database { pool };
169+
170+
let result = PublicKeyInfo::get_by(&db, None, None, None, None).await.unwrap();
171+
172+
assert!(result.is_empty(), "Expected empty result when all parameters are None");
173+
}
174+
175+
#[sqlx::test(fixtures("../../fixtures/tokens_base_fixture.sql"))]
176+
async fn test_get_by_id(pool: Pool<Postgres>) {
177+
let db = Database { pool };
178+
179+
let result = PublicKeyInfo::get_by(&db, None, None, None, Some(1)).await.unwrap();
180+
181+
assert_eq!(result.len(), 1);
182+
assert_eq!(result[0].id(), 1);
183+
assert_eq!(
184+
result[0].uaid,
185+
Some(Uuid::from_str("00000000-0000-0000-0000-000000000001").unwrap())
186+
);
187+
assert_eq!(result[0].pubkey, "test_pubkey_1");
188+
assert_eq!(result[0].algorithm_identifier, 1);
189+
}
190+
191+
#[sqlx::test(fixtures("../../fixtures/tokens_base_fixture.sql"))]
192+
async fn test_get_by_uaid(pool: Pool<Postgres>) {
193+
let db = Database { pool };
194+
let test_uaid = Uuid::from_str("00000000-0000-0000-0000-000000000002").unwrap();
195+
196+
let result = PublicKeyInfo::get_by(&db, Some(test_uaid), None, None, None).await.unwrap();
197+
198+
assert_eq!(result.len(), 1);
199+
assert_eq!(result[0].uaid, Some(test_uaid));
200+
assert_eq!(result[0].pubkey, "test_pubkey_2");
201+
}
202+
203+
#[sqlx::test(fixtures("../../fixtures/tokens_base_fixture.sql"))]
204+
async fn test_get_by_pubkey(pool: Pool<Postgres>) {
205+
let db = Database { pool };
206+
207+
let result =
208+
PublicKeyInfo::get_by(&db, None, Some("test_pubkey_3".to_string()), None, None)
209+
.await
210+
.unwrap();
211+
212+
assert_eq!(result.len(), 1);
213+
assert_eq!(result[0].pubkey, "test_pubkey_3");
214+
assert_eq!(
215+
result[0].uaid,
216+
Some(Uuid::from_str("00000000-0000-0000-0000-000000000003").unwrap())
217+
);
218+
}
219+
220+
#[sqlx::test(fixtures("../../fixtures/tokens_base_fixture.sql"))]
221+
async fn test_get_by_algorithm_identifier(pool: Pool<Postgres>) {
222+
let db = Database { pool };
223+
224+
let result = PublicKeyInfo::get_by(&db, None, None, Some(1), None).await.unwrap();
225+
226+
// Should find all public keys with algorithm_identifier = 1 (RSA)
227+
assert!(result.len() >= 6); // Based on the fixture data
228+
for key_info in &result {
229+
assert_eq!(key_info.algorithm_identifier, 1);
230+
}
231+
}
232+
233+
#[sqlx::test(fixtures("../../fixtures/tokens_base_fixture.sql"))]
234+
async fn test_get_by_multiple_parameters(pool: Pool<Postgres>) {
235+
let db = Database { pool };
236+
let test_uaid = Uuid::from_str("00000000-0000-0000-0000-000000000001").unwrap();
237+
238+
let result =
239+
PublicKeyInfo::get_by(&db, Some(test_uaid), None, Some(1), None).await.unwrap();
240+
241+
// Should find public keys for user 1 with algorithm_identifier = 1
242+
assert_eq!(result.len(), 2); // User 1 has 2 keys in the fixture
243+
for key_info in &result {
244+
assert_eq!(key_info.uaid, Some(test_uaid));
245+
assert_eq!(key_info.algorithm_identifier, 1);
246+
}
247+
}
248+
249+
#[sqlx::test(fixtures("../../fixtures/tokens_base_fixture.sql"))]
250+
async fn test_get_by_nonexistent_data(pool: Pool<Postgres>) {
251+
let db = Database { pool };
252+
let nonexistent_uaid = Uuid::from_str("99999999-9999-9999-9999-999999999999").unwrap();
253+
254+
let result =
255+
PublicKeyInfo::get_by(&db, Some(nonexistent_uaid), None, None, None).await.unwrap();
256+
257+
assert!(result.is_empty(), "Expected empty result for nonexistent UAID");
258+
}
259+
260+
#[sqlx::test(fixtures("../../fixtures/tokens_base_fixture.sql"))]
261+
async fn test_insert_new_key_with_uaid(pool: Pool<Postgres>) {
262+
let db = Database { pool };
263+
let (_private_key, public_key) = generate_keypair();
264+
let test_uaid = Uuid::from_str("00000000-0000-0000-0000-000000000001").unwrap();
265+
266+
let result = PublicKeyInfo::insert::<DigitalSignature, DigitalPublicKey>(
267+
&db,
268+
&public_key,
269+
Some(test_uaid),
270+
)
271+
.await;
272+
273+
// This should fail because Ed25519 is not in the base fixture (only RSA and EC)
274+
assert!(result.is_err(), "Expected error because Ed25519 algorithm is not in the fixture");
275+
}
276+
277+
#[sqlx::test(fixtures("../../fixtures/tokens_base_fixture.sql"))]
278+
async fn test_insert_new_key_without_uaid(pool: Pool<Postgres>) {
279+
let db = Database { pool };
280+
let (_private_key, public_key) = generate_keypair();
281+
282+
let result =
283+
PublicKeyInfo::insert::<DigitalSignature, DigitalPublicKey>(&db, &public_key, None)
284+
.await;
285+
286+
// This should fail because Ed25519 is not in the base fixture
287+
assert!(result.is_err(), "Expected error because Ed25519 algorithm is not in the fixture");
288+
}
289+
290+
#[sqlx::test(fixtures("../../fixtures/idcert_integration_tests.sql"))]
291+
async fn test_insert_ed25519_key_success(pool: Pool<Postgres>) {
292+
let db = Database { pool };
293+
let (_private_key, public_key) = generate_keypair();
294+
let test_uaid = Uuid::from_str("00000000-0000-0000-0000-000000000010").unwrap();
295+
296+
let result = PublicKeyInfo::insert::<DigitalSignature, DigitalPublicKey>(
297+
&db,
298+
&public_key,
299+
Some(test_uaid),
300+
)
301+
.await;
302+
303+
match result {
304+
Ok(key_info) => {
305+
assert_eq!(key_info.uaid, Some(test_uaid));
306+
assert_eq!(key_info.algorithm_identifier, 3); // Ed25519 algorithm ID from idcert fixture
307+
assert!(key_info.id() > 0, "Expected positive ID for inserted key");
308+
}
309+
Err(e) => {
310+
panic!("Expected successful insertion with Ed25519 key, but got error: {e:?}");
311+
}
312+
}
313+
}
314+
315+
#[sqlx::test(fixtures("../../fixtures/idcert_integration_tests.sql"))]
316+
async fn test_insert_duplicate_key_error(pool: Pool<Postgres>) {
317+
let db = Database { pool };
318+
let (_private_key, public_key) = generate_keypair();
319+
let test_uaid = Uuid::from_str("00000000-0000-0000-0000-000000000010").unwrap();
320+
321+
// Insert the key once
322+
let first_result = PublicKeyInfo::insert::<DigitalSignature, DigitalPublicKey>(
323+
&db,
324+
&public_key,
325+
Some(test_uaid),
326+
)
327+
.await;
328+
assert!(first_result.is_ok(), "First insertion should succeed");
329+
330+
// Try to insert the same key again
331+
let second_result = PublicKeyInfo::insert::<DigitalSignature, DigitalPublicKey>(
332+
&db,
333+
&public_key,
334+
Some(test_uaid),
335+
)
336+
.await;
337+
assert!(second_result.is_err(), "Second insertion should fail due to duplicate");
338+
}
339+
340+
#[sqlx::test(fixtures("../../fixtures/idcert_integration_tests.sql"))]
341+
async fn test_insert_with_nonexistent_uaid(pool: Pool<Postgres>) {
342+
let db = Database { pool };
343+
let (_private_key, public_key) = generate_keypair();
344+
let nonexistent_uaid = Uuid::from_str("99999999-9999-9999-9999-999999999999").unwrap();
345+
346+
let result = PublicKeyInfo::insert::<DigitalSignature, DigitalPublicKey>(
347+
&db,
348+
&public_key,
349+
Some(nonexistent_uaid),
350+
)
351+
.await;
352+
353+
assert!(result.is_err(), "Expected error when inserting with nonexistent UAID");
354+
}
355+
356+
#[sqlx::test(fixtures("../../fixtures/idcert_integration_tests.sql"))]
357+
async fn test_get_by_after_insert(pool: Pool<Postgres>) {
358+
let db = Database { pool };
359+
let (_private_key, public_key) = generate_keypair();
360+
let test_uaid = Uuid::from_str("00000000-0000-0000-0000-000000000011").unwrap();
361+
362+
// Insert a new key
363+
let inserted_key = PublicKeyInfo::insert::<DigitalSignature, DigitalPublicKey>(
364+
&db,
365+
&public_key,
366+
Some(test_uaid),
367+
)
368+
.await
369+
.unwrap();
370+
371+
// Retrieve it using get_by
372+
let retrieved_keys =
373+
PublicKeyInfo::get_by(&db, None, None, None, Some(inserted_key.id() as i32))
374+
.await
375+
.unwrap();
376+
377+
assert_eq!(retrieved_keys.len(), 1);
378+
let retrieved_key = &retrieved_keys[0];
379+
assert_eq!(retrieved_key.id(), inserted_key.id());
380+
assert_eq!(retrieved_key.uaid, inserted_key.uaid);
381+
assert_eq!(retrieved_key.pubkey, inserted_key.pubkey);
382+
assert_eq!(retrieved_key.algorithm_identifier, inserted_key.algorithm_identifier);
383+
}
384+
}

0 commit comments

Comments
 (0)