Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get wallet pubkey id if not available #92

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
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
14 changes: 12 additions & 2 deletions honey-badger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,18 @@ impl Auth {
.ok_or_permanent_failure("Newly refreshed token is not valid long enough")
}

pub fn get_wallet_pubkey_id(&self) -> Option<String> {
self.provider.lock().unwrap().get_wallet_pubkey_id()
pub fn get_wallet_pubkey_id(&self) -> Result<String> {
match self.provider.lock().unwrap().get_wallet_pubkey_id() {
Some(id) => Ok(id),
None => {
self.query_token()?;
self.provider
.lock()
.unwrap()
.get_wallet_pubkey_id()
.ok_or_permanent_failure("Failed to get pubkey id for an authenticated wallet")
}
}
}

// Not exposed in UDL, used in tests.
Expand Down
47 changes: 26 additions & 21 deletions honey-badger/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,14 @@ fn test_invalid_url() {
)
.unwrap();

let id = auth.get_wallet_pubkey_id();
assert!(id.is_none());
let result = auth.get_wallet_pubkey_id();
assert!(matches!(
result,
Err(Error::RuntimeError {
code: GraphQlRuntimeErrorCode::NetworkError,
..
})
));

let result = auth.query_token();
assert!(matches!(
Expand All @@ -55,8 +61,14 @@ fn test_502_return() {
)
.unwrap();

let id = auth.get_wallet_pubkey_id();
assert!(id.is_none());
let result = auth.get_wallet_pubkey_id();
assert!(matches!(
result,
Err(Error::RuntimeError {
code: GraphQlRuntimeErrorCode::RemoteServiceUnavailable,
..
})
));

let result = auth.query_token();
assert!(matches!(
Expand All @@ -80,21 +92,19 @@ fn test_basic_auth() {
)
.unwrap();

let id = auth.get_wallet_pubkey_id();
assert!(id.is_none());
let _id = auth.get_wallet_pubkey_id().unwrap();

let token = auth.query_token().unwrap();
let next_token = auth.query_token().unwrap();
assert_eq!(token, next_token);

let id = auth.get_wallet_pubkey_id();
assert!(id.is_some());
let id = auth.get_wallet_pubkey_id().unwrap();

sleep(Duration::from_secs(1));
let refreshed_token = auth.refresh_token().unwrap();
assert_ne!(token, refreshed_token);

assert_eq!(auth.get_wallet_pubkey_id(), id);
assert_eq!(auth.get_wallet_pubkey_id().unwrap(), id);
}

#[test]
Expand All @@ -109,21 +119,19 @@ fn test_owner_auth() {
)
.unwrap();

let id = auth.get_wallet_pubkey_id();
assert!(id.is_none());
let _id = auth.get_wallet_pubkey_id().unwrap();

let token = auth.query_token().unwrap();
let next_token = auth.query_token().unwrap();
assert_eq!(token, next_token);

let id = auth.get_wallet_pubkey_id();
assert!(id.is_some());
let id = auth.get_wallet_pubkey_id().unwrap();

sleep(Duration::from_secs(1));
let refreshed_token = auth.refresh_token().unwrap();
assert_ne!(token, refreshed_token);

assert_eq!(auth.get_wallet_pubkey_id(), id);
assert_eq!(auth.get_wallet_pubkey_id().unwrap(), id);
}

#[test]
Expand All @@ -138,8 +146,7 @@ fn test_employee_with_no_owner_auth() {
)
.unwrap();

let id = auth.get_wallet_pubkey_id();
assert!(id.is_none());
let _id = auth.get_wallet_pubkey_id().unwrap();

let result = auth.query_token();
assert!(matches!(result, Err(Error::InvalidInput { .. })));
Expand All @@ -159,21 +166,19 @@ fn test_employee_auth() {
)
.unwrap();

let id = auth.get_wallet_pubkey_id();
assert!(id.is_none());
let _id = auth.get_wallet_pubkey_id().unwrap();

let token = auth.query_token().unwrap();
let next_token = auth.query_token().unwrap();
assert_eq!(token, next_token);

let id = auth.get_wallet_pubkey_id();
assert!(id.is_some());
let id = auth.get_wallet_pubkey_id().unwrap();

sleep(Duration::from_secs(1));
let refreshed_token = auth.refresh_token().unwrap();
assert_ne!(token, refreshed_token);

assert_eq!(auth.get_wallet_pubkey_id(), id);
assert_eq!(auth.get_wallet_pubkey_id().unwrap(), id);
}

#[test]
Expand Down
Loading