@@ -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