Skip to content

Commit

Permalink
Get wallet pubkey id if not available
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgranhao committed Feb 8, 2024
1 parent 5c9dc87 commit 180fd8d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 23 deletions.
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

0 comments on commit 180fd8d

Please sign in to comment.