Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion payjoin-cli/src/app/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl Config {
{
match built_config.get::<V1Config>("v1") {
Ok(v1) => {
if v1.pj_endpoint.port().is_none() != (v1.port == 0) {
if v1.port == 0 && v1.pj_endpoint.port().is_some() {
return Err(ConfigError::Message(
"If --port is 0, --pj-endpoint may not have a port".to_owned(),
));
Expand Down Expand Up @@ -362,3 +362,50 @@ where
.map(Some),
}
}

#[cfg(all(test, feature = "v1"))]
mod tests {
use clap::Parser;

use super::*;
use crate::cli::Cli;

fn cli(port: &str, endpoint: &str) -> Cli {
Cli::parse_from([
"payjoin-cli",
"--bip78",
"--port",
port,
"--pj-endpoint",
endpoint,
"receive",
"50000",
])
}

#[test]
fn rejects_random_port_with_explicit_endpoint_port() {
let err = Config::new(&cli("0", "https://example.com:443/")).unwrap_err();
assert!(err.to_string().contains("port"), "unexpected error: {err}");
}

#[test]
fn accepts_random_port_with_implicit_endpoint_port() {
Config::new(&cli("0", "https://example.com/")).unwrap();
}

#[test]
fn accepts_explicit_port_with_implicit_endpoint_port() {
Config::new(&cli("3000", "https://example.com/")).unwrap();
}

#[test]
fn accepts_explicit_port_with_matching_explicit_endpoint_port() {
Config::new(&cli("3000", "https://example.com:3000/")).unwrap();
}

#[test]
fn accepts_explicit_port_with_different_explicit_endpoint_port() {
Config::new(&cli("3000", "https://example.com:443/")).unwrap();
}
Comment on lines +408 to +410
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the expected behavior here? Should the config override the existing endpoint port?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected behavior - the test should not fail, because this is a valid configuratin combination.

On the precedence between the config file and CLI args - I checked locally that CLI args have higher priority. If port and/or pj_endpoint are set in both the config file and CLI args, the CLI values are used.

I don't think we should rewrite the port inside pj_endpoint from port, because they can legitimately difffer. For example, when the receiver runs behind an nginx reverse proxy, port is the local bind port while pj_endpoint's port is the public one that ends up in the BIP21 URL.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh right ok

}
Loading