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

geo-engine / geoengine / 12767614094

14 Jan 2025 12:26PM UTC coverage: 90.64% (+0.06%) from 90.576%
12767614094

push

github

web-flow
Merge pull request #1006 from geo-engine/migrate-pro-api

Migrate-pro-api

1106 of 1152 new or added lines in 24 files covered. (96.01%)

248 existing lines in 13 files now uncovered.

133501 of 147287 relevant lines covered (90.64%)

54652.85 hits per line

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

64.38
/services/src/contexts/session.rs
1
use crate::api::handlers::get_token;
2
use crate::contexts::PostgresContext;
3
use crate::error::{self};
4
use crate::identifier;
5
use crate::projects::ProjectId;
6
use crate::projects::STRectangle;
7
use actix_http::Payload;
8
use actix_web::{web, FromRequest, HttpRequest};
9
use futures::future::{err, LocalBoxFuture};
10
use futures_util::FutureExt;
11
use geoengine_datatypes::primitives::DateTime;
12
use geoengine_datatypes::util::Identifier;
13
use serde::{Deserialize, Serialize};
14
use tokio_postgres::NoTls;
15
use utoipa::ToSchema;
16

17
use super::ApplicationContext;
18

19
identifier!(SessionId);
20

21
pub trait Session: Send + Sync + Serialize {
22
    fn id(&self) -> SessionId;
23
    fn created(&self) -> &DateTime;
24
    fn valid_until(&self) -> &DateTime;
25
    fn project(&self) -> Option<ProjectId>;
26
    fn view(&self) -> Option<&STRectangle>;
27
}
28

29
pub trait MockableSession: Session {
30
    fn mock() -> Self;
31
}
32

UNCOV
33
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, ToSchema)]
×
34
pub struct SimpleSession {
35
    id: SessionId,
36
    pub project: Option<ProjectId>,
37
    pub view: Option<STRectangle>,
38
}
39

40
impl SimpleSession {
41
    pub fn new(id: SessionId, project: Option<ProjectId>, view: Option<STRectangle>) -> Self {
90✔
42
        Self { id, project, view }
90✔
43
    }
90✔
44
}
45

46
impl Default for SimpleSession {
47
    fn default() -> Self {
91✔
48
        Self {
91✔
49
            id: SessionId::new(),
91✔
50
            project: None,
91✔
51
            view: None,
91✔
52
        }
91✔
53
    }
91✔
54
}
55

56
impl Session for SimpleSession {
57
    fn id(&self) -> SessionId {
182✔
58
        self.id
182✔
59
    }
182✔
60

61
    fn created(&self) -> &DateTime {
×
62
        &DateTime::MIN
×
63
    }
×
64

65
    fn valid_until(&self) -> &DateTime {
×
66
        &DateTime::MAX
×
67
    }
×
68

UNCOV
69
    fn project(&self) -> Option<ProjectId> {
×
UNCOV
70
        self.project
×
UNCOV
71
    }
×
72

UNCOV
73
    fn view(&self) -> Option<&STRectangle> {
×
UNCOV
74
        self.view.as_ref()
×
UNCOV
75
    }
×
76
}
77

78
impl MockableSession for SimpleSession {
79
    fn mock() -> Self {
×
80
        Self::default()
×
81
    }
×
82
}
83

84
impl FromRequest for SimpleSession {
85
    type Error = error::Error;
86
    type Future = LocalBoxFuture<'static, Result<Self, Self::Error>>;
87

UNCOV
88
    fn from_request(req: &HttpRequest, _payload: &mut Payload) -> Self::Future {
×
UNCOV
89
        let token = match get_token(req) {
×
UNCOV
90
            Ok(token) => token,
×
UNCOV
91
            Err(error) => return Box::pin(err(error)),
×
92
        };
UNCOV
93
        let pg_ctx = req.app_data::<web::Data<PostgresContext<NoTls>>>().expect(
×
UNCOV
94
            "Application context should be present because it is set during server initialization.",
×
UNCOV
95
        );
×
UNCOV
96
        let pg_ctx = pg_ctx.get_ref().clone();
×
UNCOV
97
        async move { pg_ctx.session_by_id(token).await.map_err(Into::into) }.boxed_local()
×
UNCOV
98
    }
×
99
}
100

101
#[cfg(test)]
102
mod tests {
103
    use super::*;
104

105
    use geoengine_datatypes::{primitives::TimeInstance, spatial_reference::SpatialReference};
106
    use std::str::FromStr;
107

108
    #[test]
109
    fn test_session_serialization() {
1✔
110
        let session = SimpleSession {
1✔
111
            id: SessionId::from_str("d1322969-5ada-4a2c-bacf-a3045383ba41").unwrap(),
1✔
112
            project: Some(ProjectId::from_str("c26e05b2-6709-4d96-ad00-9361ee68a25c").unwrap()),
1✔
113
            view: Some(
1✔
114
                STRectangle::new(
1✔
115
                    SpatialReference::epsg_4326(),
1✔
116
                    0.,
1✔
117
                    1.,
1✔
118
                    2.,
1✔
119
                    3.,
1✔
120
                    TimeInstance::from(DateTime::from_str("2020-01-01T00:00:00Z").unwrap()),
1✔
121
                    TimeInstance::from(DateTime::from_str("2021-01-01T00:00:00Z").unwrap()),
1✔
122
                )
1✔
123
                .unwrap(),
1✔
124
            ),
1✔
125
        };
1✔
126

1✔
127
        assert_eq!(
1✔
128
            serde_json::to_value(session).unwrap(),
1✔
129
            serde_json::json!({
1✔
130
                "id": "d1322969-5ada-4a2c-bacf-a3045383ba41", // redundant, but id is not stable
1✔
131
                "project": "c26e05b2-6709-4d96-ad00-9361ee68a25c",
1✔
132
                "view": {
1✔
133
                    "spatialReference": "EPSG:4326",
1✔
134
                    "boundingBox": {
1✔
135
                        "lowerLeftCoordinate": {"x": 0.0, "y": 1.0 },
1✔
136
                        "upperRightCoordinate": {"x": 2.0, "y": 3.0}
1✔
137
                    },
1✔
138
                    "timeInterval": {"start": 1_577_836_800_000_i64, "end": 1_609_459_200_000_i64}
1✔
139
                }
1✔
140
            })
1✔
141
        );
1✔
142
    }
1✔
143
}
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