diff --git a/.gitignore b/.gitignore index b75a144..30b03f4 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ target/ tmp/ Cargo.lock .DS_Store +.vscode/launch.json +.cargo/config.toml diff --git a/Cargo.toml b/Cargo.toml index b0e31d1..420242b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "async-mongodb-session" -version = "2.0.0" +version = "2.1.0" license = "MIT OR Apache-2.0" repository = "https://github.com/yoshuawuyts/async-mongodb-session" documentation = "https://docs.rs/async-mongodb-session" @@ -23,11 +23,12 @@ authors = [ [features] [dependencies] -mongodb = { version = "1.1.1", default-features = false, features = ["async-std-runtime"] } -async-session = "2.0.0" +mongodb = { version = "2.2.1", default-features = false, features = ["async-std-runtime", "bson-chrono-0_4"] } +# can not go higher due to dev-dependencie "tide" which requires "async-session" in version 2.0.1 +async-session = "2.0.1" [dev-dependencies] -async-std = { version = "1.8.0", features = ["attributes"] } -rand = {version = "0.7.3"} -lazy_static = "1" -tide = "0.15" +async-std = { version = "1.11.0", features = ["attributes"] } +rand = {version = "0.8.5"} +lazy_static = "1.4.0" +tide = "0.16" diff --git a/src/lib.rs b/src/lib.rs index 37ec7e7..505f01e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,8 +17,8 @@ use async_session::chrono::{Duration, Utc}; use async_session::{async_trait, Result, Session, SessionStore}; -use mongodb::bson; -use mongodb::bson::{doc, Bson}; +use mongodb::{bson, Collection}; +use mongodb::bson::{doc, Bson, Document}; use mongodb::options::{ReplaceOptions, SelectionCriteria}; use mongodb::Client; @@ -89,7 +89,7 @@ impl MongodbSessionStore { /// # Ok(()) }) } /// ``` pub async fn initialize(&self) -> Result { - &self.index_on_expiry_at().await?; + let _ = &self.index_on_expiry_at().await?; Ok(()) } @@ -162,7 +162,7 @@ impl SessionStore for MongodbSessionStore { async fn load_session(&self, cookie_value: String) -> Result> { let id = Session::id_from_cookie_value(&cookie_value)?; - let coll = self.client.database(&self.db).collection(&self.coll_name); + let coll:Collection = self.client.database(&self.db).collection(&self.coll_name); let filter = doc! { "session_id": id }; match coll.find_one(filter, None).await? { None => Ok(None), @@ -175,7 +175,7 @@ impl SessionStore for MongodbSessionStore { // https://docs.mongodb.com/manual/core/index-ttl/#timing-of-the-delete-operation // This prevents those documents being returned if let Some(expiry_at) = doc.get("expireAt").and_then(Bson::as_datetime) { - if expiry_at < &Utc::now() { + if expiry_at < &Utc::now().into() { return Ok(None); } } @@ -185,14 +185,14 @@ impl SessionStore for MongodbSessionStore { } async fn destroy_session(&self, session: Session) -> Result { - let coll = self.client.database(&self.db).collection(&self.coll_name); + let coll:Collection = self.client.database(&self.db).collection(&self.coll_name); coll.delete_one(doc! { "session_id": session.id() }, None) .await?; Ok(()) } async fn clear_store(&self) -> Result { - let coll = self.client.database(&self.db).collection(&self.coll_name); + let coll:Collection = self.client.database(&self.db).collection(&self.coll_name); coll.drop(None).await?; self.initialize().await?; Ok(()) diff --git a/tests/test.rs b/tests/test.rs index 16379c3..3928257 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -10,6 +10,8 @@ mod tests { lazy_static! { static ref HOST: String = env::var("HOST").unwrap_or_else(|_| "127.0.0.1".to_string()); static ref PORT: String = env::var("PORT").unwrap_or_else(|_| "27017".to_string()); + static ref DATABASE: String = env::var("DATABASE").unwrap_or_else(|_| "db_name".to_string()); + static ref COLLECTION: String = env::var("COLLECTION").unwrap_or_else(|_| "collection".to_string()); static ref CONNECTION_STRING: String = format!("mongodb://{}:{}/", HOST.as_str(), PORT.as_str()); } @@ -17,7 +19,7 @@ mod tests { #[test] fn test_from_client() -> async_session::Result { async_std::task::block_on(async { - let client_options = match ClientOptions::parse(&CONNECTION_STRING).await { + let client_options = match ClientOptions::parse(&*CONNECTION_STRING).await { Ok(c) => c, Err(e) => panic!("Client Options Failed: {}", e), }; @@ -26,8 +28,8 @@ mod tests { Ok(c) => c, Err(e) => panic!("Client Creation Failed: {}", e), }; - - let store = MongodbSessionStore::from_client(client, "db_name", "collection"); + + let store = MongodbSessionStore::from_client(client, &DATABASE, &COLLECTION); let mut rng = rand::thread_rng(); let n2: u16 = rng.gen(); let key = format!("key-{}", n2); @@ -47,7 +49,7 @@ mod tests { fn test_new() -> async_session::Result { async_std::task::block_on(async { let store = - MongodbSessionStore::new(&CONNECTION_STRING, "db_name", "collection").await?; + MongodbSessionStore::new(&CONNECTION_STRING, &DATABASE, &COLLECTION).await?; let mut rng = rand::thread_rng(); let n2: u16 = rng.gen(); @@ -68,7 +70,7 @@ mod tests { fn test_with_expire() -> async_session::Result { async_std::task::block_on(async { let store = - MongodbSessionStore::new(&CONNECTION_STRING, "db_name", "collection").await?; + MongodbSessionStore::new(&CONNECTION_STRING, &DATABASE, &COLLECTION).await?; store.initialize().await?; @@ -94,7 +96,7 @@ mod tests { use std::time::Duration; async_std::task::block_on(async { let store = - MongodbSessionStore::new(&CONNECTION_STRING, "db_name", "collection").await?; + MongodbSessionStore::new(&CONNECTION_STRING, &DATABASE, &COLLECTION).await?; store.initialize().await?;