Skip to content
This repository has been archived by the owner on Aug 1, 2022. It is now read-only.

Commit

Permalink
feat(proxy): track endpoint (#1005)
Browse files Browse the repository at this point in the history
* Add generate_peer_id

Adding a helper to coco::control to help with generating a random PeerId

* Add track endpoint

We're adding an endpoint so that we can explicitly track a particular
peer for a particular project.

* Upper case POST

Co-authored-by: Nuno Alexandre <hi@nunoalexandre.com>

* Fix track endpoint

We fix the track endpoint to be PUT and have a path of
`/<urn>/track/<peer_id>`. This better represents the idempotent
semantics of tracking a peer.

* Format

Co-authored-by: Nuno Alexandre <hi@nunoalexandre.com>
  • Loading branch information
FintanH and NunoAlexandre committed Oct 7, 2020
1 parent b9e5a08 commit f6d9dfd
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
56 changes: 54 additions & 2 deletions proxy/api/src/http/project.rs
Expand Up @@ -16,7 +16,8 @@ pub fn filters(ctx: context::Context) -> BoxedFilter<(impl Reply,)> {
.or(create_filter(ctx.clone()))
.or(discover_filter(ctx.clone()))
.or(request_filter(ctx.clone()))
.or(get_filter(ctx))
.or(get_filter(ctx.clone()))
.or(track_filter(ctx))
.boxed()
}

Expand Down Expand Up @@ -88,7 +89,7 @@ fn contributed_filter(
.and_then(handler::list_contributed)
}

/// `GET /user/<id>
/// `GET /user/<id>`
fn user_filter(
ctx: context::Context,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
Expand All @@ -111,6 +112,19 @@ fn discover_filter(
.and_then(handler::discover)
}

/// `PUT /<urn>/track/<peer_id>`
fn track_filter(
ctx: context::Context,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
http::with_context(ctx)
.and(warp::put())
.and(path::param::<coco::Urn>())
.and(path("track"))
.and(path::param::<coco::PeerId>())
.and(path::end())
.and_then(handler::track)
}

/// Project handlers to implement conversion and translation between core domain and http request
/// fullfilment.
mod handler {
Expand Down Expand Up @@ -210,6 +224,16 @@ mod handler {

Ok(reply::json(&feed))
}

/// Track the peer for the provided project.
pub async fn track(
ctx: context::Context,
urn: coco::Urn,
peer_id: coco::PeerId,
) -> Result<impl Reply, Rejection> {
ctx.state.track(urn, peer_id).await.map_err(Error::from)?;
Ok(reply::json(&true))
}
}

/// Bundled input data for project creation.
Expand Down Expand Up @@ -628,4 +652,32 @@ mod test {

Ok(())
}

#[tokio::test]
async fn track() -> Result<(), Box<dyn std::error::Error>> {
let tmp_dir = tempfile::tempdir()?;
let ctx = context::Context::tmp(&tmp_dir).await?;
let api = super::filters(ctx.clone());

let owner = ctx.state.init_owner(&ctx.signer, "cloudhead").await?;
coco::control::setup_fixtures(&ctx.state, &ctx.signer, &owner).await?;
let projects = project::Projects::list(&ctx.state).await?;
let project = projects.contributed.first().expect("no projects setup");

let res = request()
.method("PUT")
.path(&format!(
"/{}/track/{}",
project.id,
coco::control::generate_peer_id()
))
.reply(&api)
.await;

http::test::assert_response(&res, StatusCode::OK, |have| {
assert_eq!(have, true);
});

Ok(())
}
}
11 changes: 9 additions & 2 deletions proxy/coco/src/control.rs
Expand Up @@ -5,6 +5,7 @@ use std::{convert::TryFrom, env, io, path};
use librad::{
keys,
meta::{entity, project as librad_project},
peer::PeerId,
};
use radicle_surf::vcs::git::git2;

Expand All @@ -14,6 +15,12 @@ use crate::{
user::User,
};

/// Generate a fresh `PeerId` for use in tests.
#[must_use]
pub fn generate_peer_id() -> PeerId {
PeerId::from(keys::SecretKey::new())
}

/// Deletes the local git repsoitory coco uses for its state.
///
/// # Errors
Expand Down Expand Up @@ -141,7 +148,7 @@ pub async fn track_fake_peer(
project: &librad_project::Project<entity::Draft>,
fake_user_handle: &str,
) -> (
librad::peer::PeerId,
PeerId,
librad::meta::entity::Entity<librad::meta::user::UserInfo, librad::meta::entity::Draft>,
) {
// TODO(finto): We're faking a lot of the networking interaction here.
Expand All @@ -153,7 +160,7 @@ pub async fn track_fake_peer(
let urn = project.urn();
let fake_user =
state.init_user(signer, fake_user_handle).await.unwrap_or_else(|_| panic!("User account creation for fake peer: {} failed, make sure your mocked user accounts don't clash!", fake_user_handle));
let remote = librad::peer::PeerId::from(keys::SecretKey::new());
let remote = generate_peer_id();
let monorepo = git2::Repository::open(state.monorepo()).expect("failed to open monorepo");
let prefix = format!("refs/namespaces/{}/refs/remotes/{}", urn.id, remote);

Expand Down

0 comments on commit f6d9dfd

Please sign in to comment.