darkwing/server/services/browser_profile_services/config/
geolocation.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//! Geolocation configuration module for browser profiles.
//!
//! This module handles the configuration of geolocation settings for browser
//! profiles, supporting both manual coordinate input and automatic geolocation
//! based on connection info.

use crate::server::{
  dtos::{
    browser_profile_dto::{BrowserProfileFullData, Mode},
    start_dto::StartRequest,
  },
  error::Error,
};
use serde::{Deserialize, Serialize};
use tracing::debug;

use super::{FromStartRequest, Navigator, Screen};

/// Configuration structure for browser geolocation settings.
///
/// This structure represents the geolocation configuration that will be applied
/// to a browser profile, including whether geolocation should be enabled and
/// the specific coordinates to be used.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct Geolocation {
  /// Whether geolocation should be installed/enabled in the browser
  pub(super) install: bool,
  /// The latitude coordinate in decimal degrees
  pub(super) latitude: f64,
  /// The longitude coordinate in decimal degrees
  pub(super) longitude: f64,
  /// The accuracy radius in meters
  pub(super) accuracy: i32,
}

impl FromStartRequest<Geolocation> for Geolocation {
  /// Creates a geolocation configuration from a start request and profile data.
  ///
  /// # Arguments
  ///
  /// * `bp` - The browser profile data containing geolocation preferences
  /// * `_request` - The start request containing connection information
  /// * `_navigator` - Navigator configuration (unused)
  /// * `_screen` - Screen configuration (unused)
  /// * `_token` - Authentication token (unused)
  ///
  /// # Returns
  ///
  /// Returns a Result containing either the configured Geolocation or an Error
  ///
  /// # Errors
  ///
  /// Returns an Error::ConfigForming if the geolocation coordinates are invalid
  fn from_start_request(
    bp: &BrowserProfileFullData,
    _request: &StartRequest,
    _navigator: &Navigator,
    _screen: &Screen,
    _token: &str,
  ) -> Result<Self, Error> {
    let connection_info = _request.connection_info.clone();

    let (latitude, longitude, accuracy) = match (
      bp.geolocation.mode,
      bp.geolocation.latitude,
      bp.geolocation.longitude,
    ) {
      (Mode::Manual, Some(lat), Some(long)) => {
        (lat, long, bp.geolocation.accuracy.unwrap_or(10.0) as i32)
      }
      _ => {
        if connection_info.geo.len() != 2 {
          return Err(Error::ConfigForming(
            "not received exactly two coordinates".into(),
          ));
        }

        (connection_info.geo[0], connection_info.geo[1], 10)
      }
    };

    let res = Self {
      install: true,
      latitude,
      longitude,
      accuracy,
    };

    debug!("res: {:?}", res);

    Ok(res)
  }
}

#[cfg(test)]
mod tests {
  use super::*;
  use crate::server::dtos::browser_profile_dto::Geolocation as GeolocationConfig;
  use crate::server::dtos::start_dto::ConnectionInfo;
  use crate::server::services::browser_profile_services::config::consts::LINUX_MOCK_PROFILE;

  fn create_base_profile() -> BrowserProfileFullData {
    BrowserProfileFullData {
      geolocation: GeolocationConfig {
        mode: Mode::Auto,
        latitude: None,
        longitude: None,
        accuracy: None,
      },
      ..LINUX_MOCK_PROFILE.clone()
    }
  }

  fn create_base_request() -> StartRequest {
    StartRequest {
      connection_info: ConnectionInfo::get_mock(),
      ..StartRequest::get_mock()
    }
  }

  #[test]
  fn test_manual_geolocation() -> Result<(), Error> {
    let mut profile = create_base_profile();
    profile.geolocation.mode = Mode::Manual;
    profile.geolocation.latitude = Some(51.5074);
    profile.geolocation.longitude = Some(-0.1278);
    profile.geolocation.accuracy = Some(15.0);

    let request = create_base_request();
    let navigator = Navigator::default();
    let screen = Screen::default();
    let token = String::new();

    let result = Geolocation::from_start_request(
      &profile, &request, &navigator, &screen, &token,
    )?;

    assert!(result.install);
    assert_eq!(result.latitude, 51.5074);
    assert_eq!(result.longitude, -0.1278);
    assert_eq!(result.accuracy, 15);
    Ok(())
  }

  #[test]
  fn test_manual_geolocation_default_accuracy() -> Result<(), Error> {
    let mut profile = create_base_profile();
    profile.geolocation.mode = Mode::Manual;
    profile.geolocation.latitude = Some(51.5074);
    profile.geolocation.longitude = Some(-0.1278);
    // No accuracy specified, should default to 10

    let request = create_base_request();
    let navigator = Navigator::default();
    let screen = Screen::default();
    let token = String::new();

    let result = Geolocation::from_start_request(
      &profile, &request, &navigator, &screen, &token,
    )?;

    assert!(result.install);
    assert_eq!(result.latitude, 51.5074);
    assert_eq!(result.longitude, -0.1278);
    assert_eq!(result.accuracy, 10);
    Ok(())
  }

  #[test]
  fn test_auto_geolocation() -> Result<(), Error> {
    let profile = create_base_profile();
    let mut request = create_base_request();
    request.connection_info = ConnectionInfo {
      geo: vec![40.7128, -74.0060],
      ..ConnectionInfo::get_mock()
    };

    let navigator = Navigator::default();
    let screen = Screen::default();
    let token = String::new();

    let result = Geolocation::from_start_request(
      &profile, &request, &navigator, &screen, &token,
    )?;

    assert!(result.install);
    assert_eq!(result.latitude, 40.7128);
    assert_eq!(result.longitude, -74.0060);
    assert_eq!(result.accuracy, 10);
    Ok(())
  }

  #[test]
  fn test_invalid_geo_coordinates() {
    let profile = create_base_profile();
    let mut request = create_base_request();
    request.connection_info = ConnectionInfo {
      geo: vec![40.7128], // Only one coordinate
      ..ConnectionInfo::get_mock()
    };

    let navigator = Navigator::default();
    let screen = Screen::default();
    let token = String::new();

    let result = Geolocation::from_start_request(
      &profile, &request, &navigator, &screen, &token,
    );

    assert!(matches!(
        result,
        Err(Error::ConfigForming(msg)) if msg == "not received exactly two coordinates"
    ));
  }

  #[test]
  fn test_no_geolocation() -> Result<(), Error> {
    let profile = create_base_profile();
    let request = create_base_request();
    let navigator = Navigator::default();
    let screen = Screen::default();
    let token = String::new();

    let result = Geolocation::from_start_request(
      &profile, &request, &navigator, &screen, &token,
    )?;

    assert!(!result.install);
    assert_eq!(result.latitude, 1.0);
    assert_eq!(result.longitude, 1.0);
    assert_eq!(result.accuracy, 1);
    Ok(())
  }
}