• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

divviup / divviup-api / 16810179837

07 Aug 2025 04:27PM UTC coverage: 55.941% (+0.02%) from 55.922%
16810179837

Pull #1845

github

web-flow
Merge 02139528b into 24cadc364
Pull Request #1845: cargo fmt

20 of 24 new or added lines in 3 files covered. (83.33%)

13 existing lines in 2 files now uncovered.

3889 of 6952 relevant lines covered (55.94%)

60.24 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

48.33
/src/handler/session_store.rs
1
use crate::{Db, entity::session};
2
use async_session::{
3
    Session, async_trait,
4
    chrono::{DateTime, Utc},
5
};
6
use sea_orm::{
7
    ColumnTrait, EntityTrait, IntoActiveModel, QueryFilter,
8
    sea_query::{OnConflict, any},
9
};
10
use serde_json::json;
11
use time::OffsetDateTime;
12

13
#[derive(Debug, Clone)]
14
pub struct SessionStore {
15
    db: Db,
16
}
17

18
impl SessionStore {
19
    pub fn new(db: Db) -> Self {
274✔
20
        Self { db }
274✔
21
    }
274✔
22
}
23

24
impl TryFrom<&Session> for session::Model {
25
    type Error = serde_json::Error;
26

27
    fn try_from(session: &Session) -> Result<Self, Self::Error> {
266✔
28
        Ok(Self {
29
            id: session.id().to_string(),
266✔
30
            // unwrap safety: session object comes from the session handler, and its timestamp
31
            // we made ourselves.
32
            expiry: session
266✔
33
                .expiry()
266✔
34
                .map(|e| OffsetDateTime::from_unix_timestamp(e.timestamp()).unwrap()),
266✔
35
            // unwrap safety: if the serialization is successful, the data element
36
            // will be there.
37
            data: serde_json::from_value(
266✔
38
                serde_json::to_value(session)?.get("data").unwrap().clone(),
266✔
UNCOV
39
            )?,
×
40
        })
41
    }
266✔
42
}
43

44
impl TryFrom<session::Model> for Session {
45
    type Error = serde_json::Error;
UNCOV
46
    fn try_from(db_session: session::Model) -> Result<Session, serde_json::Error> {
×
47
        let mut session: Session = serde_json::from_value(json!({
×
48
            "id": db_session.id,
×
49
            "data": db_session.data,
×
50
        }))?;
×
51
        if let Some(x) = db_session.expiry {
×
52
            // unwrap safety: the expiry time from the database is a timestamp we made
×
53
            // ourselves.
×
54
            session.set_expiry(
×
55
                DateTime::<Utc>::from_timestamp(x.unix_timestamp(), x.nanosecond()).unwrap(),
×
56
            );
×
57
        }
×
58
        Ok(session)
×
59
    }
×
60
}
61

62
#[async_trait]
63
impl async_session::SessionStore for SessionStore {
UNCOV
64
    async fn load_session(&self, cookie_value: String) -> async_session::Result<Option<Session>> {
×
65
        let id = Session::id_from_cookie_value(&cookie_value)?;
×
66
        Ok(session::Entity::find_by_id(id)
×
67
            .filter(any![
×
68
                session::Column::Expiry.is_null(),
×
69
                session::Column::Expiry.gt(OffsetDateTime::now_utc())
×
70
            ])
×
71
            .one(&self.db)
×
72
            .await?
×
73
            .map(Session::try_from)
×
74
            .transpose()?)
×
75
    }
×
76

77
    async fn store_session(&self, session: Session) -> async_session::Result<Option<String>> {
532✔
78
        let session_model = session::Model::try_from(&session)?.into_active_model();
266✔
79

80
        session::Entity::insert(session_model)
266✔
81
            .on_conflict(
266✔
82
                OnConflict::column(session::Column::Id)
266✔
83
                    .update_columns([session::Column::Data, session::Column::Expiry])
266✔
84
                    .clone(),
266✔
85
            )
266✔
86
            .exec(&self.db)
266✔
87
            .await?;
266✔
88

89
        Ok(session.into_cookie_value())
266✔
90
    }
532✔
91

92
    async fn destroy_session(&self, session: Session) -> async_session::Result {
2✔
93
        session::Entity::delete_by_id(session.id())
1✔
94
            .exec(&self.db)
1✔
95
            .await?;
1✔
96
        Ok(())
1✔
97
    }
2✔
98

UNCOV
99
    async fn clear_store(&self) -> async_session::Result {
×
100
        session::Entity::delete_many().exec(&self.db).await?;
×
101
        Ok(())
×
102
    }
×
103
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc