Skip to content

Commit

Permalink
Merge #109: Revert unintentional change for auth key generation API e…
Browse files Browse the repository at this point in the history
…ndpoint

409f82a test: [#108] add e2e test for auth key generation API endpoint (Jose Celano)
ede0460 feat: [#108] add dev dependency reqwest (Jose Celano)
23916a6 fix: [#108] revert change in auth key generation endpoint (Jose Celano)

Pull request description:

  Tasks:

  - [x] Revert change.
  - [x] E2E test for the endpoint.

ACKs for top commit:
  da2ce7:
    ACK 409f82a

Tree-SHA512: 66422f2ab68925581fde950b1d618899189160df1d0d07c747f833d909d44fe2f945d212362cd8454f1f9c384f67b1c4dffdde66ec5e175a1cd12114977e630d
  • Loading branch information
da2ce7 committed Nov 23, 2022
2 parents 4293ba2 + 409f82a commit 7a2182c
Show file tree
Hide file tree
Showing 10 changed files with 337 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
/config.toml
/data.db
/.vscode/launch.json

101 changes: 99 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,4 @@ uuid = { version = "1", features = ["v4"] }

[dev-dependencies]
mockall = "0.11"
reqwest = { version = "0.11.13", features = ["json"] }
1 change: 1 addition & 0 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod resources;
pub mod server;
89 changes: 89 additions & 0 deletions src/api/resources/auth_key_resource.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use std::convert::From;

use serde::{Deserialize, Serialize};

use crate::key::AuthKey;
use crate::protocol::clock::DurationSinceUnixEpoch;

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct AuthKeyResource {
pub key: String,
pub valid_until: Option<u64>,
}

impl From<AuthKeyResource> for AuthKey {
fn from(auth_key_resource: AuthKeyResource) -> Self {
AuthKey {
key: auth_key_resource.key,
valid_until: auth_key_resource
.valid_until
.map(|valid_until| DurationSinceUnixEpoch::new(valid_until, 0)),
}
}
}

impl From<AuthKey> for AuthKeyResource {
fn from(auth_key: AuthKey) -> Self {
AuthKeyResource {
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::AuthKeyResource;
use crate::key::AuthKey;
use crate::protocol::clock::{DefaultClock, TimeNow};

#[test]
fn it_should_be_convertible_into_an_auth_key() {
let duration_in_secs = 60;

let auth_key_resource = AuthKeyResource {
key: "IaWDneuFNZi8IB4MPA3qW1CD0M30EZSM".to_string(), // cspell:disable-line
valid_until: Some(duration_in_secs),
};

assert_eq!(
AuthKey::from(auth_key_resource),
AuthKey {
key: "IaWDneuFNZi8IB4MPA3qW1CD0M30EZSM".to_string(), // cspell:disable-line
valid_until: Some(DefaultClock::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 = AuthKey {
key: "IaWDneuFNZi8IB4MPA3qW1CD0M30EZSM".to_string(), // cspell:disable-line
valid_until: Some(DefaultClock::add(&Duration::new(duration_in_secs, 0)).unwrap()),
};

assert_eq!(
AuthKeyResource::from(auth_key),
AuthKeyResource {
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(&AuthKeyResource {
key: "IaWDneuFNZi8IB4MPA3qW1CD0M30EZSM".to_string(), // cspell:disable-line
valid_until: Some(60)
})
.unwrap(),
"{\"key\":\"IaWDneuFNZi8IB4MPA3qW1CD0M30EZSM\",\"valid_until\":60}" // cspell:disable-line
);
}
}
9 changes: 9 additions & 0 deletions src/api/resources/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//! These are the Rest API resources.
//!
//! WIP. Not all endpoints have their resource structs.
//!
//! - [x] AuthKeys
//! - [ ] ...
//! - [ ] ...
//! - [ ] ...
pub mod auth_key_resource;
3 changes: 2 additions & 1 deletion src/api/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::time::Duration;
use serde::{Deserialize, Serialize};
use warp::{filters, reply, serve, Filter};

use super::resources::auth_key_resource::AuthKeyResource;
use crate::peer::TorrentPeer;
use crate::protocol::common::*;
use crate::tracker::TorrentTracker;
Expand Down Expand Up @@ -267,7 +268,7 @@ pub fn start(socket_addr: SocketAddr, tracker: Arc<TorrentTracker>) -> impl warp
})
.and_then(|(seconds_valid, tracker): (u64, Arc<TorrentTracker>)| async move {
match tracker.generate_auth_key(Duration::from_secs(seconds_valid)).await {
Ok(auth_key) => Ok(warp::reply::json(&auth_key)),
Ok(auth_key) => Ok(warp::reply::json(&AuthKeyResource::from(auth_key))),
Err(..) => Err(warp::reject::custom(ActionStatus::Err {
reason: "failed to generate key".into(),
})),
Expand Down
Loading

0 comments on commit 7a2182c

Please sign in to comment.