Skip to content

Commit

Permalink
Adding tests to the subscription service made
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerson2102 committed Nov 29, 2024
1 parent 400df92 commit 8f49e26
Show file tree
Hide file tree
Showing 3 changed files with 265 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/http/subscription.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use axum::{
extract::State,
http::StatusCode,
Json,
};
use axum::{extract::State, http::StatusCode, Json};
use sqlx::PgPool;

use super::types::{CreateSubscriptionRequest, CreateSubscriptionResponse};
Expand All @@ -20,7 +16,20 @@ pub async fn create_subscription(
return Err(StatusCode::BAD_REQUEST);
}

let mut tx = state.db.pool.begin().await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
if !payload.to_token.starts_with("0x") && payload.to_token.len() != 42 {
return Err(StatusCode::BAD_REQUEST);
}

if !payload.wallet_address.starts_with("0x") && payload.wallet_address.len() != 42 {
return Err(StatusCode::BAD_REQUEST);
}

let mut tx = state
.db
.pool
.begin()
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;

sqlx::query!(
r#"
Expand Down Expand Up @@ -63,9 +72,11 @@ pub async fn create_subscription(
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
}

tx.commit().await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
tx.commit()
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;

Ok(Json(CreateSubscriptionResponse {
wallet_address: payload.wallet_address,
}))
}
}
1 change: 1 addition & 0 deletions tests/api/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod health_check;
mod helpers;
mod unsubscription;
mod subscription;
245 changes: 245 additions & 0 deletions tests/api/subscription.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
use axum::{
body::Body,
http::{header::CONTENT_TYPE, Request, StatusCode},
};
use serde_json::json;
use sqlx::PgPool;

use crate::helpers::*;

#[tokio::test]
async fn test_subscribe_ok() {
let app = TestApp::new().await;

let payload = json!({
"wallet_address": "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
"to_token": "0x1234567890123456789012345678901234567890",
"from_token": [
"0xabcdef0123456789abcdef0123456789abcdef01",
"0x9876543210987654321098765432109876543210"
],
"percentage": [60, 40]
});

let req = Request::builder()
.method("POST")
.uri("/subscriptions")
.header(CONTENT_TYPE, "application/json")
.body(Body::from(serde_json::to_string(&payload).unwrap()))
.unwrap();

let resp = app.request(req).await;
assert_eq!(resp.status(), StatusCode::OK);
}

#[tokio::test]
async fn test_successful_subscription_creation() {
let app = TestApp::new().await;

sqlx::query!("DELETE FROM swap_subscription_from_token")
.execute(&app.db.pool)
.await
.unwrap();
sqlx::query!("DELETE FROM swap_subscription")
.execute(&app.db.pool)
.await
.unwrap();

let wallet_address = "0x742d35Cc6634C0532925a3b844Bc454e4438f44e";
let to_token = "0x1234567890123456789012345678901234567890";
let from_tokens = vec![
"0xabcdef0123456789abcdef0123456789abcdef01",
"0x9876543210987654321098765432109876543210",
];
let percentages = vec![60, 40];

let payload = json!({
"wallet_address": wallet_address,
"to_token": to_token,
"from_token": from_tokens,
"percentage": percentages
});

let req = Request::builder()
.method("POST")
.uri("/subscriptions")
.header(CONTENT_TYPE, "application/json")
.body(Body::from(serde_json::to_string(&payload).unwrap()))
.unwrap();

let resp = app.request(req).await;

assert_eq!(resp.status(), StatusCode::OK);

let subscription = sqlx::query!(
r#"
SELECT wallet_address, to_token, is_active
FROM swap_subscription
WHERE wallet_address = $1
"#,
wallet_address
)
.fetch_one(&app.db.pool)
.await
.unwrap();

assert_eq!(subscription.wallet_address, wallet_address);
assert_eq!(subscription.to_token, to_token);
assert!(subscription.is_active);

let from_token_records = sqlx::query!(
r#"
SELECT from_token, percentage
FROM swap_subscription_from_token
WHERE wallet_address = $1
"#,
wallet_address
)
.fetch_all(&app.db.pool)
.await
.unwrap();

assert_eq!(from_token_records.len(), 2);

assert_eq!(from_token_records[0].from_token, from_tokens[1]);
assert_eq!(from_token_records[0].percentage as i32, percentages[1]);

assert_eq!(from_token_records[1].from_token, from_tokens[0]);
assert_eq!(from_token_records[1].percentage as i32, percentages[0]);
}

#[tokio::test]
async fn test_invalid_subscription_percentage() {
let app = TestApp::new().await;

sqlx::query!("DELETE FROM swap_subscription_from_token")
.execute(&app.db.pool)
.await
.unwrap();
sqlx::query!("DELETE FROM swap_subscription")
.execute(&app.db.pool)
.await
.unwrap();

let payload = json!({
"wallet_address": "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
"to_token": "0x1234567890123456789012345678901234567890",
"from_token": [
"0xabcdef0123456789abcdef0123456789abcdef01",
"0x9876543210987654321098765432109876543210"
],
"percentage": [20, 40]
});

let req = Request::builder()
.method("POST")
.uri("/subscriptions")
.header(CONTENT_TYPE, "application/json")
.body(Body::from(serde_json::to_string(&payload).unwrap()))
.unwrap();

let resp = app.request(req).await;
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
}

#[tokio::test]
async fn test_invalid_percentage_length() {
let app = TestApp::new().await;

sqlx::query!("DELETE FROM swap_subscription_from_token")
.execute(&app.db.pool)
.await
.unwrap();
sqlx::query!("DELETE FROM swap_subscription")
.execute(&app.db.pool)
.await
.unwrap();

let payload = json!({
"wallet_address": "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
"to_token": "0x1234567890123456789012345678901234567890",
"from_token": [
"0xabcdef0123456789abcdef0123456789abcdef01",
"0x9876543210987654321098765432109876543210"
],
"percentage": [20]
});

let req = Request::builder()
.method("POST")
.uri("/subscriptions")
.header(CONTENT_TYPE, "application/json")
.body(Body::from(serde_json::to_string(&payload).unwrap()))
.unwrap();

let resp = app.request(req).await;
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
}

#[tokio::test]
async fn test_invalid_wallet_address() {
let app = TestApp::new().await;

sqlx::query!("DELETE FROM swap_subscription_from_token")
.execute(&app.db.pool)
.await
.unwrap();
sqlx::query!("DELETE FROM swap_subscription")
.execute(&app.db.pool)
.await
.unwrap();

let payload = json!({
"wallet_address": "invalid_wallet_address",
"to_token": "0x1234567890123456789012345678901234567890",
"from_token": [
"0xabcdef0123456789abcdef0123456789abcdef01",
"0x9876543210987654321098765432109876543210"
],
"percentage": [20, 80]
});

let req = Request::builder()
.method("POST")
.uri("/subscriptions")
.header(CONTENT_TYPE, "application/json")
.body(Body::from(serde_json::to_string(&payload).unwrap()))
.unwrap();

let resp = app.request(req).await;
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
}

#[tokio::test]
async fn test_invalid_to_token_address() {
let app = TestApp::new().await;

sqlx::query!("DELETE FROM swap_subscription_from_token")
.execute(&app.db.pool)
.await
.unwrap();
sqlx::query!("DELETE FROM swap_subscription")
.execute(&app.db.pool)
.await
.unwrap();

let payload = json!({
"wallet_address": "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
"to_token": "invalid_to_token",
"from_token": [
"0xabcdef0123456789abcdef0123456789abcdef01",
"0x9876543210987654321098765432109876543210"
],
"percentage": [20, 80]
});

let req = Request::builder()
.method("POST")
.uri("/subscriptions")
.header(CONTENT_TYPE, "application/json")
.body(Body::from(serde_json::to_string(&payload).unwrap()))
.unwrap();

let resp = app.request(req).await;
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
}

0 comments on commit 8f49e26

Please sign in to comment.