Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move test
Browse files Browse the repository at this point in the history
kariy committed Jun 15, 2024
1 parent 7042f7a commit f6f7bc8
Showing 2 changed files with 29 additions and 31 deletions.
30 changes: 0 additions & 30 deletions src/credential.rs
Original file line number Diff line number Diff line change
@@ -112,11 +112,8 @@ pub fn get_file_path<P: AsRef<Path>>(config_dir: P) -> PathBuf {

#[cfg(test)]
mod tests {
use tokio::sync::mpsc::channel;

use super::LegacyCredentials;
use crate::credential::{AccessToken, Credentials, CREDENTIALS_FILE};
use crate::session::{callback_server, SessionDetails};
use crate::utils;
use std::fs;

@@ -158,31 +155,4 @@ mod tests {
let actual = Credentials::load_at(config_dir).unwrap();
assert_eq!(expected, actual);
}

#[tokio::test]
async fn test_callback_server() {
let (tx, mut rx) = channel::<SessionDetails>(1);
let server = callback_server(tx).expect("failed to create server");

// get the callback url
let port = server.local_addr().unwrap().port();
let url = format!("http://localhost:{port}/callback");

// start the callback server
tokio::spawn(server.start());

// call the callback url
let session = SessionDetails::default();
let res = reqwest::Client::new()
.post(url)
.json(&session)
.send()
.await
.expect("failed to call callback url");

assert!(dbg!(res.status()).is_success());

let actual = rx.recv().await.expect("failed to receive session");
assert_eq!(session, actual)
}
}
30 changes: 29 additions & 1 deletion src/session.rs
Original file line number Diff line number Diff line change
@@ -197,7 +197,7 @@ fn prepare_query_params(
}

/// Create the callback server that will receive the session token from the browser.
pub(super) fn callback_server(tx: Sender<SessionDetails>) -> anyhow::Result<LocalServer> {
fn callback_server(tx: Sender<SessionDetails>) -> anyhow::Result<LocalServer> {
let handler = move |tx: State<Sender<SessionDetails>>, session: Json<SessionDetails>| async move {
trace!("Received session token from the browser.");
tx.0.send(session.0).await.expect("qed; channel closed");
@@ -224,6 +224,7 @@ mod tests {
use crate::utils;
use starknet::{core::types::FieldElement, macros::felt};
use std::path::Path;
use tokio::sync::mpsc::channel;

fn authenticate(config_dir: impl AsRef<Path>) {
let token = AccessToken {
@@ -284,4 +285,31 @@ mod tests {
let err = store_at(config_dir, chain, &session).unwrap_err();
assert!(err.to_string().contains("No credentials found"))
}

#[tokio::test]
async fn test_callback_server() {
let (tx, mut rx) = channel::<SessionDetails>(1);
let server = super::callback_server(tx).expect("failed to create server");

// get the callback url
let port = server.local_addr().unwrap().port();
let url = format!("http://localhost:{port}/callback");

// start the callback server
tokio::spawn(server.start());

// call the callback url
let session = SessionDetails::default();
let res = reqwest::Client::new()
.post(url)
.json(&session)
.send()
.await
.expect("failed to call callback url");

assert!(dbg!(res.status()).is_success());

let actual = rx.recv().await.expect("failed to receive session");
assert_eq!(session, actual)
}
}

0 comments on commit f6f7bc8

Please sign in to comment.