apikeys_rs/
lib.rs

1pub mod axum_layer;
2pub mod errors;
3pub mod limiters;
4pub mod manager;
5#[cfg(test)]
6mod mock;
7pub mod storage;
8pub mod traits;
9pub mod types;
10
11#[cfg(test)]
12mod tests {
13    use super::*;
14    use crate::{
15        mock::mock_api_key::get_mock_api_key,
16        storage::{memory_storage::HashMapStorage, mongodb_storage::MongoDBStorage},
17        traits::ApiKeyStorage,
18    };
19
20    #[tokio::test]
21    async fn it_can_store_an_api_key() {
22        let mut storage = HashMapStorage::new();
23
24        let key = "test_key";
25
26        let api_key = get_mock_api_key(Some(key.to_string()));
27
28        let result = storage.store_api_key(key, &api_key).await;
29
30        match result {
31            Ok(storage_key) => assert_eq!(key, storage_key, "The stored key should match the key that was passed in"),
32            Err(e) => assert_eq!(true, false, "The key should have been stored {}", e),
33        }
34    }
35
36    #[tokio::test]
37    async fn it_can_rietrieve_an_api_key() {
38        let mut storage = HashMapStorage::new();
39
40        let key = "test_key";
41
42        let api_key = get_mock_api_key(Some(key.to_string()));
43
44        let result = storage.store_api_key(key, &api_key).await;
45
46        match result {
47            Ok(storage_key) => assert_eq!(key, storage_key, "The stored key should match the key that was passed in"),
48            Err(e) => assert_eq!(true, false, "The key should have been stored {}", e),
49        }
50
51        let result = storage.retrieve_api_key(key).await;
52
53        match result {
54            Ok(retrieved_api_key) => assert_eq!(
55                api_key.key, retrieved_api_key.key,
56                "The retrieved key should match the key that was passed in"
57            ),
58            Err(e) => match e {
59                errors::ApiKeyStorageError::KeyNotFound => {
60                    assert_eq!(true, false, "The key should have been found {}", e)
61                }
62                _ => assert_eq!(true, false, "The key should have been found {}", e),
63            },
64        }
65    }
66
67    #[tokio::test]
68    async fn it_can_store_an_api_key_using_mongodb_storage() {
69        dotenv::dotenv().ok();
70        let uri = std::env::var("MONGODB_URI").expect("MONGODB_URI must be set");
71        let db_name = std::env::var("MONGODB_DB_NAME").expect("MONGODB_DB_NAME must be set");
72
73        let mut storage = MongoDBStorage::new(&uri, &db_name, None)
74            .await
75            .expect("Failed to create MongoDBStorage");
76
77        let key = "test_key";
78
79        let api_key = get_mock_api_key(Some(key.to_string()));
80
81        let _ = storage.delete_api_key(key).await;
82
83        let result = storage.store_api_key(key, &api_key).await;
84
85        match result {
86            Ok(storage_key) => assert_eq!(key, storage_key, "The stored key should match the key that was passed in"),
87            Err(e) => assert_eq!(true, false, "The key should have been stored {}", e),
88        }
89    }
90}