forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth_key.rs
More file actions
89 lines (75 loc) · 2.53 KB
/
Copy pathauth_key.rs
File metadata and controls
89 lines (75 loc) · 2.53 KB
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
use std::convert::From;
use serde::{Deserialize, Serialize};
use crate::protocol::clock::DurationSinceUnixEpoch;
use crate::tracker::auth;
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
pub struct AuthKey {
pub key: String,
pub valid_until: Option<u64>,
}
impl From<AuthKey> for auth::Key {
fn from(auth_key_resource: AuthKey) -> Self {
auth::Key {
key: auth_key_resource.key,
valid_until: auth_key_resource
.valid_until
.map(|valid_until| DurationSinceUnixEpoch::new(valid_until, 0)),
}
}
}
impl From<auth::Key> for AuthKey {
fn from(auth_key: auth::Key) -> Self {
AuthKey {
key: auth_key.key,
valid_until: auth_key.valid_until.map(|valid_until| valid_until.as_secs()),
}
}
}
#[cfg(test)]
mod tests {
use std::time::Duration;
use super::AuthKey;
use crate::protocol::clock::{Current, TimeNow};
use crate::tracker::auth;
#[test]
fn it_should_be_convertible_into_an_auth_key() {
let duration_in_secs = 60;
let auth_key_resource = AuthKey {
key: "IaWDneuFNZi8IB4MPA3qW1CD0M30EZSM".to_string(), // cspell:disable-line
valid_until: Some(duration_in_secs),
};
assert_eq!(
auth::Key::from(auth_key_resource),
auth::Key {
key: "IaWDneuFNZi8IB4MPA3qW1CD0M30EZSM".to_string(), // cspell:disable-line
valid_until: Some(Current::add(&Duration::new(duration_in_secs, 0)).unwrap())
}
);
}
#[test]
fn it_should_be_convertible_from_an_auth_key() {
let duration_in_secs = 60;
let auth_key = auth::Key {
key: "IaWDneuFNZi8IB4MPA3qW1CD0M30EZSM".to_string(), // cspell:disable-line
valid_until: Some(Current::add(&Duration::new(duration_in_secs, 0)).unwrap()),
};
assert_eq!(
AuthKey::from(auth_key),
AuthKey {
key: "IaWDneuFNZi8IB4MPA3qW1CD0M30EZSM".to_string(), // cspell:disable-line
valid_until: Some(duration_in_secs)
}
);
}
#[test]
fn it_should_be_convertible_into_json() {
assert_eq!(
serde_json::to_string(&AuthKey {
key: "IaWDneuFNZi8IB4MPA3qW1CD0M30EZSM".to_string(), // cspell:disable-line
valid_until: Some(60)
})
.unwrap(),
"{\"key\":\"IaWDneuFNZi8IB4MPA3qW1CD0M30EZSM\",\"valid_until\":60}" // cspell:disable-line
);
}
}