Ditches useless tests.

This commit is contained in:
2026-10-25 22:18:19 +01:00
parent 58621b0500
commit 1d16be2137
10 changed files with 135 additions and 512 deletions
+1 -1
View File
@@ -154,5 +154,5 @@ fn create_response(
.cloned()
.ok_or(AppError::Unknown)?;
Ok(AccountMemberResponse::from_domain(account_member, user))
Ok(AccountMemberResponse::new(account_member, user))
}
+2 -44
View File
@@ -3,7 +3,7 @@ use uuid::Uuid;
use crate::domain;
#[derive(Debug, Clone, Deserialize)]
#[derive(Debug, Deserialize)]
pub struct AccountRequest {
pub name: String,
}
@@ -17,7 +17,7 @@ impl AccountRequest {
}
}
#[derive(Debug, Clone, Serialize)]
#[derive(Debug, Serialize)]
pub struct AccountResponse {
pub id: Uuid,
pub name: String,
@@ -31,45 +31,3 @@ impl From<domain::Account> for AccountResponse {
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn request() -> AccountRequest {
AccountRequest {
name: "Savings".to_owned(),
}
}
#[test]
fn serializes_account_response() {
let id = Uuid::nil();
let account = request().into_domain(id).unwrap();
let response = serde_json::to_value(AccountResponse::from(account)).unwrap();
assert_eq!(response["id"], id.to_string());
assert_eq!(response["name"], "Savings");
}
#[test]
fn deserializes_account_response_as_request() {
let account = request().into_domain(Uuid::nil()).unwrap();
let response = serde_json::to_value(AccountResponse::from(account)).unwrap();
let request: AccountRequest = serde_json::from_value(response).unwrap();
assert_eq!(request.name, "Savings");
}
#[test]
fn converts_between_dto_and_domain() {
let id = Uuid::new_v4();
let account = request().into_domain(id).unwrap();
assert_eq!(account.id, id);
assert_eq!(account.name, "Savings");
let response = AccountResponse::from(account);
assert_eq!(response.id, id);
assert_eq!(response.name, "Savings");
}
}
+13 -70
View File
@@ -3,7 +3,7 @@ use uuid::Uuid;
use crate::domain;
#[derive(Debug, Clone, Deserialize)]
#[derive(Debug, Deserialize)]
pub struct AccountMemberRequest {
pub user: UserRequest,
}
@@ -22,94 +22,37 @@ impl AccountMemberRequest {
}
}
#[derive(Debug, Clone, Deserialize)]
pub struct UserRequest {
pub id: Uuid,
}
#[derive(Debug, Clone, Serialize)]
#[derive(Debug, Serialize)]
pub struct AccountMemberResponse {
pub id: Uuid,
pub user: UserResponse,
}
impl AccountMemberResponse {
pub fn from_domain(member: domain::AccountMember, user: domain::User) -> Self {
pub fn new(member: domain::AccountMember, user: domain::User) -> Self {
Self {
id: member.id,
user: UserResponse {
id: user.id,
name: user.name,
},
user: user.into(),
}
}
}
#[derive(Debug, Clone, Serialize)]
#[derive(Debug, Deserialize)]
pub struct UserRequest {
pub id: Uuid,
}
#[derive(Debug, Serialize)]
pub struct UserResponse {
pub id: Uuid,
pub name: Option<String>,
}
#[cfg(test)]
mod tests {
use super::*;
fn request(user_id: Uuid) -> AccountMemberRequest {
AccountMemberRequest {
user: UserRequest { id: user_id },
}
}
fn response(id: Uuid, user: domain::User) -> AccountMemberResponse {
AccountMemberResponse {
id,
user: UserResponse {
impl From<domain::User> for UserResponse {
fn from(user: domain::User) -> Self {
Self {
id: user.id,
name: user.name,
},
}
}
fn user() -> domain::User {
domain::User {
id: Uuid::nil(),
name: Some("Test User".to_string()),
}
}
#[test]
fn serializes_account_member_response() {
let id = Uuid::new_v4();
let response = serde_json::to_value(response(id, user())).unwrap();
assert_eq!(response["id"], id.to_string());
assert_eq!(response["user"]["id"], Uuid::nil().to_string());
assert_eq!(response["user"]["name"], "Test User");
assert!(response.get("account_id").is_none());
}
#[test]
fn deserializes_account_member_response_as_request() {
let response = serde_json::to_value(response(Uuid::new_v4(), user())).unwrap();
let request: AccountMemberRequest = serde_json::from_value(response).unwrap();
assert_eq!(request.user.id, Uuid::nil());
}
#[test]
fn converts_between_dto_and_domain() {
let id = Uuid::new_v4();
let account_id = Uuid::new_v4();
let user_id = Uuid::new_v4();
let member = request(user_id).into_domain(id, account_id).unwrap();
assert_eq!(member.id, id);
assert_eq!(member.account_id, account_id);
assert_eq!(member.user_id, user_id);
let response = response(id, user());
assert_eq!(response.id, id);
assert_eq!(response.user.id, Uuid::nil());
assert_eq!(response.user.name, Some("Test User".to_string()));
}
}
+8 -25
View File
@@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};
use crate::domain;
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Direction {
In,
@@ -29,36 +29,19 @@ impl From<domain::Direction> for Direction {
#[cfg(test)]
mod tests {
use super::Direction;
use crate::domain;
use super::*;
#[test]
fn serializes_direction() {
assert_eq!(serde_json::to_value(Direction::In).unwrap(), "in");
assert_eq!(serde_json::to_value(Direction::Out).unwrap(), "out");
let response = Direction::In;
let serialized = serde_json::to_value(response).unwrap();
assert_eq!(serialized, "in");
}
#[test]
fn deserializes_direction() {
let incoming: Direction = serde_json::from_value("in".into()).unwrap();
assert!(matches!(incoming, Direction::In));
let outgoing: Direction = serde_json::from_value("out".into()).unwrap();
assert!(matches!(outgoing, Direction::Out));
}
#[test]
fn converts_between_dto_and_domain() {
let domain_in: domain::Direction = Direction::In.into();
assert_eq!(domain_in, domain::Direction::In);
let domain_out: domain::Direction = Direction::Out.into();
assert_eq!(domain_out, domain::Direction::Out);
let dto_in: Direction = domain::Direction::In.into();
assert!(matches!(dto_in, Direction::In));
let dto_out: Direction = domain::Direction::Out.into();
assert!(matches!(dto_out, Direction::Out));
let request = r#" "in" "#;
let deserialized: Direction = serde_json::from_str(request).unwrap();
assert!(matches!(deserialized, Direction::In));
}
}
+65 -141
View File
@@ -5,13 +5,13 @@ use uuid::Uuid;
use super::direction::Direction;
use crate::domain;
#[derive(Debug, Clone, Deserialize)]
#[derive(Debug, Deserialize)]
#[serde(rename_all = "snake_case", tag = "type")]
pub enum ScheduledTransactionRequest {
Recurring(RecurringTransactionRequest),
}
#[derive(Debug, Clone, Serialize)]
#[derive(Debug, Serialize)]
#[serde(rename_all = "snake_case", tag = "type")]
pub enum ScheduledTransactionResponse {
Recurring(RecurringTransactionResponse),
@@ -23,7 +23,7 @@ impl From<RecurringTransactionResponse> for ScheduledTransactionResponse {
}
}
#[derive(Debug, Clone, Deserialize)]
#[derive(Debug, Deserialize)]
pub struct RecurringTransactionRequest {
pub name: String,
pub account: AccountRequest,
@@ -37,28 +37,18 @@ impl RecurringTransactionRequest {
self,
id: Uuid,
) -> Result<domain::RecurringTransaction, domain::DomainError> {
let recurrence = domain::Recurrence {
every: self.schedule.recurrence.every.try_into()?,
unit: self.schedule.recurrence.unit.into(),
};
Ok(domain::RecurringTransaction {
id,
name: self.name,
account_id: self.account.id,
amount: self.amount_cents.try_into()?,
direction: self.direction.into(),
schedule: domain::Schedule::new(
self.schedule.start,
self.schedule.end,
recurrence,
self.schedule.weekend_adjustment.map(Into::into),
)?,
schedule: self.schedule.into_domain()?,
})
}
}
#[derive(Debug, Clone, Serialize)]
#[derive(Debug, Serialize)]
pub struct RecurringTransactionResponse {
pub id: Uuid,
pub name: String,
@@ -77,10 +67,7 @@ impl RecurringTransactionResponse {
Self {
id: transaction.id,
name: transaction.name,
account: AccountResponse {
id: account.id,
name: account.name,
},
account: account.into(),
amount_cents: transaction.amount.get(),
direction: transaction.direction.into(),
schedule: ScheduleResponse::new(transaction.schedule, schedule_next),
@@ -88,18 +75,27 @@ impl RecurringTransactionResponse {
}
}
#[derive(Debug, Clone, Deserialize)]
#[derive(Debug, Deserialize)]
pub struct AccountRequest {
pub id: Uuid,
}
#[derive(Debug, Clone, Serialize)]
#[derive(Debug, Serialize)]
pub struct AccountResponse {
pub id: Uuid,
pub name: String,
}
#[derive(Debug, Clone, Deserialize)]
impl From<domain::Account> for AccountResponse {
fn from(account: domain::Account) -> Self {
Self {
id: account.id,
name: account.name,
}
}
}
#[derive(Debug, Deserialize)]
pub struct ScheduleRequest {
pub start: DateTime<Utc>,
pub end: Option<DateTime<Utc>>,
@@ -107,7 +103,18 @@ pub struct ScheduleRequest {
pub weekend_adjustment: Option<WeekendAdjustment>,
}
#[derive(Debug, Clone, Serialize)]
impl ScheduleRequest {
pub fn into_domain(self) -> Result<domain::Schedule, domain::DomainError> {
Ok(domain::Schedule::new(
self.start,
self.end,
self.recurrence.into_domain()?,
self.weekend_adjustment.map(Into::into),
)?)
}
}
#[derive(Debug, Serialize)]
pub struct ScheduleResponse {
pub start: DateTime<Utc>,
pub end: Option<DateTime<Utc>>,
@@ -128,12 +135,21 @@ impl ScheduleResponse {
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize)]
pub struct Recurrence {
pub every: u32,
pub unit: RecurrenceUnit,
}
impl Recurrence {
pub fn into_domain(self) -> Result<domain::Recurrence, domain::DomainError> {
Ok(domain::Recurrence {
every: self.every.try_into()?,
unit: self.unit.into(),
})
}
}
impl From<domain::Recurrence> for Recurrence {
fn from(recurrence: domain::Recurrence) -> Self {
Self {
@@ -143,7 +159,7 @@ impl From<domain::Recurrence> for Recurrence {
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RecurrenceUnit {
Day,
@@ -174,7 +190,7 @@ impl From<domain::RecurrenceUnit> for RecurrenceUnit {
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum WeekendAdjustment {
PreviousBusinessDay,
@@ -201,140 +217,48 @@ impl From<domain::WeekendAdjustment> for WeekendAdjustment {
#[cfg(test)]
mod tests {
use chrono::{TimeZone, Utc};
use uuid::Uuid;
use chrono::TimeZone;
use super::{
Recurrence, RecurrenceUnit, RecurringTransactionRequest, RecurringTransactionResponse,
ScheduleRequest, ScheduledTransactionRequest, ScheduledTransactionResponse,
WeekendAdjustment,
};
use crate::{domain, routes::dtos::direction::Direction};
use super::*;
fn scheduled_response() -> ScheduledTransactionResponse {
let transaction = request().into_domain(Uuid::nil()).unwrap();
ScheduledTransactionResponse::from(RecurringTransactionResponse::new(
transaction,
domain::Account {
fn response() -> ScheduledTransactionResponse {
ScheduledTransactionResponse::Recurring(RecurringTransactionResponse {
id: Uuid::nil(),
name: "Checking".to_owned(),
},
None,
))
}
fn request() -> RecurringTransactionRequest {
RecurringTransactionRequest {
name: "Rent".to_owned(),
account: super::AccountRequest { id: Uuid::nil() },
account: AccountResponse {
id: Uuid::nil(),
name: "test".into(),
},
amount_cents: 1234,
direction: Direction::Out,
schedule: ScheduleRequest {
start: Utc.with_ymd_and_hms(2026, 9, 24, 12, 0, 0).unwrap(),
schedule: ScheduleResponse {
start: Utc.with_ymd_and_hms(1970, 1, 1, 0, 0, 0).unwrap(),
end: None,
recurrence: Recurrence {
every: 2,
unit: RecurrenceUnit::Month,
},
weekend_adjustment: Some(WeekendAdjustment::NextBusinessDay),
weekend_adjustment: Some(WeekendAdjustment::PreviousBusinessDay),
next: None,
},
}
})
}
#[test]
fn serializes_recurring_transaction_response_with_nested_schedule() {
let response = serde_json::to_value(scheduled_response()).unwrap();
assert_eq!(response["type"], "recurring");
assert_eq!(response["id"], Uuid::nil().to_string());
assert_eq!(response["name"], "Rent");
assert_eq!(response["amount_cents"], 1234);
assert_eq!(response["direction"], "out");
assert_eq!(response["account"]["id"], Uuid::nil().to_string());
assert_eq!(response["account"]["name"], "Checking");
assert!(response.get("source_account").is_none());
assert!(response.get("destination_account").is_none());
assert!(response["schedule"]["start"].is_string());
assert!(response["schedule"]["end"].is_null());
assert_eq!(response["schedule"]["recurrence"]["every"], 2);
assert_eq!(response["schedule"]["recurrence"]["unit"], "month");
fn serializes_recurring_transaction() {
let serialized = serde_json::to_value(response()).unwrap();
assert_eq!(serialized["type"], "recurring");
assert_eq!(serialized["schedule"]["recurrence"]["unit"], "month");
assert_eq!(
response["schedule"]["weekend_adjustment"],
"next_business_day"
);
assert!(response["schedule"]["next"].is_null());
assert!(response.get("next").is_none());
assert!(response.get("data").is_none());
}
#[test]
fn serializes_recurring_transaction_response_with_account() {
let account_id = Uuid::new_v4();
let mut transaction = request();
transaction.account = super::AccountRequest { id: account_id };
let transaction = transaction.into_domain(Uuid::nil()).unwrap();
let account = domain::Account {
id: account_id,
name: "Checking".to_owned(),
};
let response = RecurringTransactionResponse::new(transaction, account, None);
let response = serde_json::to_value(response).unwrap();
assert_eq!(response["account"]["id"], account_id.to_string());
assert_eq!(response["account"]["name"], "Checking");
}
#[test]
fn deserializes_scheduled_transaction_response_as_request() {
let response = serde_json::to_value(scheduled_response()).unwrap();
let request: ScheduledTransactionRequest = serde_json::from_value(response).unwrap();
let ScheduledTransactionRequest::Recurring(request) = request;
assert_eq!(request.name, "Rent");
assert_eq!(request.account.id, Uuid::nil());
assert_eq!(request.amount_cents, 1234);
assert!(matches!(request.direction, Direction::Out));
assert_eq!(
request.schedule.start,
Utc.with_ymd_and_hms(2026, 9, 24, 12, 0, 0).unwrap()
);
assert_eq!(request.schedule.end, None);
assert_eq!(request.schedule.recurrence.every, 2);
assert!(matches!(
request.schedule.recurrence.unit,
RecurrenceUnit::Month
));
assert!(matches!(
request.schedule.weekend_adjustment,
Some(WeekendAdjustment::NextBusinessDay)
));
}
#[test]
fn converts_between_dto_and_domain() {
let id = Uuid::new_v4();
let transaction = request().into_domain(id).unwrap();
let recurrence = transaction.schedule.recurrence();
assert_eq!(transaction.id, id);
assert_eq!(transaction.amount.get(), 1234);
assert_eq!(transaction.direction, domain::Direction::Out);
assert_eq!(transaction.account_id, Uuid::nil());
assert_eq!(recurrence.every.get(), 2);
assert_eq!(recurrence.unit, domain::RecurrenceUnit::Month);
assert_eq!(
transaction.schedule.weekend_adjustment(),
Some(domain::WeekendAdjustment::NextBusinessDay)
serialized["schedule"]["weekend_adjustment"],
"previous_business_day"
);
}
#[test]
fn requires_account_in_recurring_transaction_request() {
let mut missing_account = serde_json::to_value(scheduled_response()).unwrap();
missing_account.as_object_mut().unwrap().remove("account");
assert!(serde_json::from_value::<ScheduledTransactionRequest>(missing_account).is_err());
let mut null_account = serde_json::to_value(scheduled_response()).unwrap();
null_account["account"] = serde_json::Value::Null;
assert!(serde_json::from_value::<ScheduledTransactionRequest>(null_account).is_err());
fn deserializes_scheduled_transaction() {
let request = serde_json::to_value(response()).unwrap();
let deserialized: ScheduledTransactionRequest = serde_json::from_value(request).unwrap();
let ScheduledTransactionRequest::Recurring(_) = deserialized;
}
}
+26 -10
View File
@@ -2,7 +2,7 @@ use chrono::Utc;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use crate::domain::{AccessToken, RefreshToken};
use crate::domain;
#[derive(Deserialize)]
#[serde(rename_all = "snake_case", tag = "grant_type")]
@@ -32,7 +32,10 @@ pub struct TokenResponse {
}
impl TokenResponse {
pub fn new(access_token: AccessToken, refresh_token: Option<RefreshToken>) -> Self {
pub fn new(
access_token: domain::AccessToken,
refresh_token: Option<domain::RefreshToken>,
) -> Self {
Self {
access_token: access_token.secret.secret().to_string(),
token_type: TokenType::Bearer,
@@ -53,18 +56,31 @@ mod tests {
use super::*;
#[test]
fn serializes_token_response() {
let response = TokenResponse {
fn serializes_token_response_camel_case() {
let token_response = TokenResponse {
access_token: "access-token".to_string(),
token_type: TokenType::Bearer,
expires_in: 3600,
refresh_token: Some("refresh-token".to_string()),
refresh_token: None,
};
let value = serde_json::to_value(response).unwrap();
assert_eq!(value["access_token"], "access-token");
assert_eq!(value["token_type"], "bearer");
assert_eq!(value["expires_in"], 3600);
assert_eq!(value["refresh_token"], "refresh-token");
let serialized = serde_json::to_value(token_response).unwrap();
assert_eq!(serialized["token_type"], "bearer");
assert_eq!(serialized["refresh_token"], serde_json::Value::Null);
}
#[test]
fn deserializes_token_request_camel_case() {
let token_request = r#"{
"grant_type": "client_credentials",
"client_id": "00000000-0000-0000-0000-000000000000",
"client_secret": "secret"
}"#;
let deserialized: TokenRequest = serde_json::from_str(token_request).unwrap();
assert!(matches!(
deserialized,
TokenRequest::ClientCredentials { .. }
));
}
}
+10 -105
View File
@@ -5,7 +5,7 @@ use uuid::Uuid;
use super::direction::Direction;
use crate::domain;
#[derive(Debug, Clone, Deserialize)]
#[derive(Debug, Deserialize)]
pub struct TransactionRequest {
pub account: AccountRequest,
pub amount_cents: u64,
@@ -27,7 +27,7 @@ impl TransactionRequest {
}
}
#[derive(Debug, Clone, Serialize)]
#[derive(Debug, Serialize)]
pub struct TransactionResponse {
pub id: Uuid,
pub account: AccountResponse,
@@ -41,10 +41,7 @@ impl TransactionResponse {
pub fn new(transaction: domain::Transaction, account: domain::Account) -> Self {
Self {
id: transaction.id,
account: AccountResponse {
id: account.id,
name: account.name,
},
account: account.into(),
amount_cents: transaction.amount.get(),
direction: transaction.direction.into(),
date: transaction.date,
@@ -53,114 +50,22 @@ impl TransactionResponse {
}
}
#[derive(Debug, Clone, Deserialize)]
#[derive(Debug, Deserialize)]
pub struct AccountRequest {
pub id: Uuid,
}
#[derive(Debug, Clone, Serialize)]
#[derive(Debug, Serialize)]
pub struct AccountResponse {
pub id: Uuid,
pub name: String,
}
#[cfg(test)]
mod tests {
use chrono::TimeZone;
use super::*;
fn request() -> TransactionRequest {
TransactionRequest {
account: AccountRequest { id: Uuid::nil() },
amount_cents: 4250,
direction: Direction::Out,
date: Utc.with_ymd_and_hms(2026, 9, 29, 12, 0, 0).unwrap(),
description: "Groceries".to_string(),
impl From<domain::Account> for AccountResponse {
fn from(account: domain::Account) -> Self {
Self {
id: account.id,
name: account.name,
}
}
#[test]
fn serializes_transaction_response() {
let id = Uuid::nil();
let date = request().date;
let transaction = request().into_domain(id).unwrap();
let account = domain::Account {
id: Uuid::nil(),
name: "Checking".to_owned(),
};
let response =
serde_json::to_value(TransactionResponse::new(transaction, account)).unwrap();
assert_eq!(response["id"], id.to_string());
assert_eq!(response["account"]["id"], Uuid::nil().to_string());
assert_eq!(response["account"]["name"], "Checking");
assert_eq!(response["amount_cents"], 4250);
assert_eq!(response["direction"], "out");
assert_eq!(response["date"], serde_json::to_value(date).unwrap());
assert_eq!(response["description"], "Groceries");
}
#[test]
fn deserializes_transaction_response_as_request() {
let date = request().date;
let transaction = request().into_domain(Uuid::nil()).unwrap();
let account = domain::Account {
id: Uuid::nil(),
name: "Checking".to_owned(),
};
let response =
serde_json::to_value(TransactionResponse::new(transaction, account)).unwrap();
let request: TransactionRequest = serde_json::from_value(response).unwrap();
assert_eq!(request.amount_cents, 4250);
assert_eq!(request.account.id, Uuid::nil());
assert!(matches!(request.direction, Direction::Out));
assert_eq!(request.date, date);
assert_eq!(request.description, "Groceries");
}
#[test]
fn requires_account_in_transaction_request() {
let transaction = request().into_domain(Uuid::nil()).unwrap();
let account = domain::Account {
id: Uuid::nil(),
name: "Checking".to_owned(),
};
let mut response =
serde_json::to_value(TransactionResponse::new(transaction, account)).unwrap();
response.as_object_mut().unwrap().remove("account");
assert!(serde_json::from_value::<TransactionRequest>(response).is_err());
}
#[test]
fn converts_between_dto_and_domain() {
let id = Uuid::new_v4();
let request = request();
let date = request.date;
let account_id = request.account.id;
let transaction = request.into_domain(id).unwrap();
assert_eq!(transaction.id, id);
assert_eq!(transaction.account_id, account_id);
assert_eq!(transaction.amount.get(), 4250);
assert_eq!(transaction.direction, domain::Direction::Out);
assert_eq!(transaction.date, date);
let response = TransactionResponse::new(
transaction,
domain::Account {
id: account_id,
name: "Checking".to_owned(),
},
);
assert_eq!(response.id, id);
assert_eq!(response.account.id, account_id);
assert_eq!(response.account.name, "Checking");
assert_eq!(response.amount_cents, 4250);
assert!(matches!(response.direction, Direction::Out));
assert_eq!(response.date, date);
assert_eq!(response.description, "Groceries");
}
}
+2 -44
View File
@@ -3,7 +3,7 @@ use uuid::Uuid;
use crate::domain;
#[derive(Debug, Clone, Deserialize)]
#[derive(Debug, Deserialize)]
pub struct UserRequest {
pub name: Option<String>,
}
@@ -17,7 +17,7 @@ impl UserRequest {
}
}
#[derive(Debug, Clone, Serialize)]
#[derive(Debug, Serialize)]
pub struct UserResponse {
pub id: Uuid,
pub name: Option<String>,
@@ -31,45 +31,3 @@ impl From<domain::User> for UserResponse {
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn request() -> UserRequest {
UserRequest {
name: Some("Test User".to_string()),
}
}
#[test]
fn serializes_user_response() {
let id = Uuid::nil();
let user = request().into_domain(id).unwrap();
let response = serde_json::to_value(UserResponse::from(user)).unwrap();
assert_eq!(response["id"], id.to_string());
assert_eq!(response["name"], "Test User");
}
#[test]
fn deserializes_user_response_as_request() {
let user = request().into_domain(Uuid::nil()).unwrap();
let response = serde_json::to_value(UserResponse::from(user)).unwrap();
let request: UserRequest = serde_json::from_value(response).unwrap();
assert_eq!(request.name, Some("Test User".to_string()));
}
#[test]
fn converts_between_dto_and_domain() {
let id = Uuid::new_v4();
let user = request().into_domain(id).unwrap();
assert_eq!(user.id, id);
assert_eq!(user.name, Some("Test User".to_string()));
let response = UserResponse::from(user);
assert_eq!(response.id, id);
assert_eq!(response.name, Some("Test User".to_string()));
}
}
+2 -31
View File
@@ -3,7 +3,7 @@ use uuid::Uuid;
use crate::domain;
#[derive(Debug, Clone, Deserialize)]
#[derive(Debug, Deserialize)]
pub struct UserAppRequest {}
impl UserAppRequest {
@@ -12,7 +12,7 @@ impl UserAppRequest {
}
}
#[derive(Debug, Clone, Serialize)]
#[derive(Debug, Serialize)]
pub struct UserAppResponse {
pub id: Uuid,
}
@@ -22,32 +22,3 @@ impl From<domain::UserApp> for UserAppResponse {
Self { id: app.id }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn serializes_user_app_response() {
let id = Uuid::new_v4();
let response = UserAppResponse::from(domain::UserApp {
id,
user_id: Uuid::new_v4(),
});
assert_eq!(
serde_json::to_value(response).unwrap()["id"],
id.to_string()
);
}
#[test]
fn accepts_empty_put_payload() {
let request: UserAppRequest = serde_json::from_str("{}").unwrap();
let user_id = Uuid::new_v4();
let id = Uuid::new_v4();
let app = request.into_domain(user_id, id);
assert_eq!(app, domain::UserApp { id, user_id });
}
}
+3 -38
View File
@@ -4,7 +4,7 @@ use uuid::Uuid;
use crate::domain;
#[derive(Debug, Clone, Deserialize)]
#[derive(Debug, Deserialize)]
pub struct UserAppSecretRequest {
pub expires_at: DateTime<Utc>,
}
@@ -27,7 +27,7 @@ impl UserAppSecretRequest {
}
}
#[derive(Debug, Clone, Serialize)]
#[derive(Debug, Serialize)]
pub struct UserAppSecretResponse {
pub id: Uuid,
pub app_id: Uuid,
@@ -46,44 +46,9 @@ impl From<domain::UserAppSecret> for UserAppSecretResponse {
}
}
#[derive(Debug, Clone, Serialize)]
#[derive(Debug, Serialize)]
pub struct CreatedUserAppSecretResponse {
#[serde(flatten)]
pub credential: UserAppSecretResponse,
pub secret: String,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_timezone_aware_expiration() {
let request: UserAppSecretRequest =
serde_json::from_str(r#"{"expires_at":"2030-01-01T12:00:00+02:00"}"#).unwrap();
assert_eq!(request.expires_at.to_rfc3339(), "2030-01-01T10:00:00+00:00");
}
#[test]
fn rejects_expiration_without_timezone() {
assert!(
serde_json::from_str::<UserAppSecretRequest>(r#"{"expires_at":"2030-01-01T12:00:00"}"#)
.is_err()
);
}
#[test]
fn read_response_does_not_expose_secret_hash() {
let response = UserAppSecretResponse {
id: Uuid::new_v4(),
app_id: Uuid::new_v4(),
expires_at: Utc::now(),
created_at: Utc::now(),
};
let value = serde_json::to_value(response).unwrap();
assert!(value.get("secret").is_none());
assert!(value.get("secret_hash").is_none());
}
}