Skip to content

Commit

Permalink
✅ test: fix tests
Browse files Browse the repository at this point in the history
Signed-off-by: xtrm <[email protected]>
  • Loading branch information
xtrm-en committed Jan 30, 2024
1 parent 71b813c commit 54b3a52
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-debug.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ jobs:
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
run: cargo test --verbose --tests
2 changes: 1 addition & 1 deletion .github/workflows/build-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ jobs:
- name: Build
run: cargo build --verbose --release
- name: Run tests
run: cargo test --verbose --release
run: cargo test --verbose --tests --release
2 changes: 1 addition & 1 deletion tests/simple-test.rs → examples/simple-test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use ft_rs::FtClient;

#[tokio::test(flavor = "multi_thread")]
#[tokio::main]
async fn main() -> ft_rs::Result<()> {
let client = FtClient::from_app(
std::env::var("FT_RS_TEST_UID").expect("FT_RS_TEST_UID not set"),
Expand Down
36 changes: 20 additions & 16 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ pub struct FtClient {
impl FtClient {
/// Creates a new client for a v2 application, providing the application's UID and secret.
///
/// # Example
///
/// ```rust
/// use ft_rs::FtClient;
///
/// fn main() {
/// let client = FtClient::from_app("my_uid", "my_super_secret_secret");
/// }
/// let client = FtClient::from_app("my_uid", "my_super_secret_secret");
/// ```
///
/// # Errors
///
/// This method will return an error if the reqwest client could not be built, or if the UID or secret are invalid.
pub fn from_app<U: Into<String>, S: Into<String>>(
app_uid: U,
app_secret: S,
Expand All @@ -45,18 +46,18 @@ impl FtClient {
.user_agent(format!("{}/{}", PKG_NAME, PKG_VERSION))
.connect_timeout(Duration::from_secs(30))
.build();

if let Err(err) = client {
return Err(FtError::ReqwestBuilderError(err));
Err(FtError::ReqwestBuilderError(err))
} else {
Ok(Self {
app_uid,
app_secret,

client: client.unwrap(),
last_valid_token: None
})
}
let client = client.unwrap();

Ok(Self {
app_uid,
app_secret,

client,
last_valid_token: None
})
}

/// Fetches a new access token from the API.
Expand All @@ -67,7 +68,7 @@ impl FtClient {
///
/// # Example
///
/// ```rust
/// ```no_run
/// use ft_rs::FtClient;
///
/// #[tokio::main]
Expand Down Expand Up @@ -107,6 +108,9 @@ impl FtClient {
}
}

/// Ensures that the last valid token is still valid, and fetches a new one if it is not.
///
/// This method is called automatically by the API Client when making a request, so there is no need to call it manually.
pub async fn ensure_valid_token(&mut self) -> Result<()> {
if let Some(token) = &self.last_valid_token {
if token.is_expired() {
Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#![deny(clippy::all)]
#![deny(clippy::pedantic)]
#![deny(clippy::nursery)]
#![deny(clippy::cargo)]
//#![deny(missing_docs)]

Expand Down
13 changes: 10 additions & 3 deletions src/models/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@ use serde::{Deserialize, Serialize};
pub struct AccessToken {
access_token: String,
token_type: String,
expires_in: u32,
expires_in: u64,
scope: String,
created_at: u32
created_at: u64
}

impl AccessToken {
/// Checks if the token is expired.
///
/// This method will return true if the token is expired, and false otherwise.
///
/// # Panics
///
/// This method will panic if the system's time is not set correctly.
pub fn is_expired(&self) -> bool {
self.created_at + self.expires_in <= (chrono::Utc::now().timestamp() + 5) as u32
self.created_at + self.expires_in <= (chrono::Utc::now().timestamp() + 5).try_into().unwrap()
}
}

0 comments on commit 54b3a52

Please sign in to comment.