-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
531 additions
and
37 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
[package] | ||
name = "chat_test" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dev-dependencies] | ||
anyhow = { workspace = true } | ||
axum = { workspace = true } | ||
chat-core = { workspace = true } | ||
chat-server = { workspace = true, features = ["test-util"] } | ||
notify-server = { workspace = true } | ||
reqwest = { version = "0.12.4", default-features = false, features = [ | ||
"rustls-tls", | ||
"json", | ||
"multipart", | ||
"stream", | ||
] } | ||
reqwest-eventsource = "0.6.0" | ||
serde = { workspace = true } | ||
serde_json = "1.0.116" | ||
tokio = { workspace = true } | ||
|
||
[dependencies] | ||
futures = "0.3.30" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
server: | ||
port: 6688 | ||
db_url: postgres://postgres:postgres@localhost:5432/chat | ||
base_dir: /tmp/chat_server | ||
auth: | ||
sk: | | ||
-----BEGIN PRIVATE KEY----- | ||
MC4CAQAwBQYDK2VwBCIEIDnxJGEJGoW+mNKHn4vRY1V6BQ3MglSQSuZ8featmyC4 | ||
-----END PRIVATE KEY----- | ||
pk: | | ||
-----BEGIN PUBLIC KEY----- | ||
MCowBQYDK2VwAyEAfM+lwNHj6TRJ3EGP38lIJcOo9Dlt2u2JzcwWMbu7jQY= | ||
-----END PUBLIC KEY----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
server: | ||
port: 6687 | ||
db_url: postgres://postgres:postgres@localhost:5432/chat | ||
auth: | ||
pk: | | ||
-----BEGIN PUBLIC KEY----- | ||
MCowBQYDK2VwAyEAfM+lwNHj6TRJ3EGP38lIJcOo9Dlt2u2JzcwWMbu7jQY= | ||
-----END PUBLIC KEY----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// empty |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
use anyhow::Result; | ||
use chat_core::{Chat, ChatType, Message}; | ||
use futures::StreamExt; | ||
use reqwest::{ | ||
multipart::{Form, Part}, | ||
StatusCode, | ||
}; | ||
use reqwest_eventsource::{Event, EventSource}; | ||
use serde::Deserialize; | ||
use serde_json::json; | ||
use std::{net::SocketAddr, time::Duration}; | ||
use tokio::{net::TcpListener, time::sleep}; | ||
|
||
/* | ||
test1: | ||
name: user 1 create chat | ||
steps: | ||
- signin | ||
email: [email protected] | ||
password: 123456 | ||
- create_chat | ||
name: test | ||
members: [1, 2] | ||
- create_message | ||
chat_id: 1 | ||
content: hello | ||
files: [Cargo.toml] | ||
*/ | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct AuthToken { | ||
token: String, | ||
} | ||
|
||
struct ChatServer { | ||
addr: SocketAddr, | ||
token: String, | ||
client: reqwest::Client, | ||
} | ||
|
||
struct NotifyServer; | ||
|
||
const WILD_ADDR: &str = "0.0.0.0:0"; | ||
|
||
#[tokio::test] | ||
async fn chat_server_should_work() -> Result<()> { | ||
let (tdb, state) = chat_server::AppState::new_for_test().await?; | ||
let chat_server = ChatServer::new(state).await?; | ||
let db_url = tdb.url(); | ||
NotifyServer::new(&db_url, &chat_server.token).await?; | ||
let chat = chat_server.create_chat().await?; | ||
let _msg = chat_server.create_message(chat.id as u64).await?; | ||
sleep(Duration::from_secs(1)).await; | ||
Ok(()) | ||
} | ||
|
||
impl NotifyServer { | ||
async fn new(db_url: &str, token: &str) -> Result<Self> { | ||
let mut config = notify_server::AppConfig::load()?; | ||
config.server.db_url = db_url.to_string(); | ||
let app = notify_server::get_router(config).await?; | ||
let listener = TcpListener::bind(WILD_ADDR).await?; | ||
let addr = listener.local_addr()?; | ||
|
||
tokio::spawn(async move { | ||
axum::serve(listener, app.into_make_service()) | ||
.await | ||
.unwrap(); | ||
}); | ||
|
||
let mut es = EventSource::get(format!("http://{}/events?access_token={}", addr, token)); | ||
|
||
tokio::spawn(async move { | ||
while let Some(event) = es.next().await { | ||
match event { | ||
Ok(Event::Open) => println!("Connection Open!"), | ||
Ok(Event::Message(message)) => match message.event.as_str() { | ||
"NewChat" => { | ||
let chat: Chat = serde_json::from_str(&message.data).unwrap(); | ||
assert_eq!(chat.name.as_ref().unwrap(), "test"); | ||
assert_eq!(chat.members, vec![1, 2]); | ||
assert_eq!(chat.r#type, ChatType::PrivateChannel); | ||
} | ||
|
||
"NewMessage" => { | ||
let msg: Message = serde_json::from_str(&message.data).unwrap(); | ||
assert_eq!(msg.content, "hello"); | ||
assert_eq!(msg.files.len(), 1); | ||
assert_eq!(msg.sender_id, 1); | ||
} | ||
_ => { | ||
panic!("unexpected event: {:?}", message); | ||
} | ||
}, | ||
Err(err) => { | ||
println!("Error: {}", err); | ||
es.close(); | ||
} | ||
} | ||
} | ||
}); | ||
|
||
Ok(Self) | ||
} | ||
} | ||
|
||
impl ChatServer { | ||
async fn new(state: chat_server::AppState) -> Result<Self> { | ||
let app = chat_server::get_router(state).await?; | ||
let listener = TcpListener::bind(WILD_ADDR).await?; | ||
let addr = listener.local_addr()?; | ||
|
||
tokio::spawn(async move { | ||
axum::serve(listener, app.into_make_service()) | ||
.await | ||
.unwrap(); | ||
}); | ||
|
||
let client = reqwest::Client::new(); | ||
|
||
let mut ret = Self { | ||
addr, | ||
client, | ||
token: "".to_string(), | ||
}; | ||
|
||
ret.token = ret.signin().await?; | ||
|
||
Ok(ret) | ||
} | ||
|
||
async fn signin(&self) -> Result<String> { | ||
let res = self | ||
.client | ||
.post(&format!("http://{}/api/signin", self.addr)) | ||
.header("Content-Type", "application/json") | ||
.body(r#"{"email": "[email protected]","password":"123456"}"#) | ||
.send() | ||
.await?; | ||
|
||
assert_eq!(res.status(), 200); | ||
let ret: AuthToken = res.json().await?; | ||
Ok(ret.token) | ||
} | ||
|
||
async fn create_chat(&self) -> Result<Chat> { | ||
let res = self | ||
.client | ||
.post(format!("http://{}/api/chats", self.addr)) | ||
.header("Authorization", format!("Bearer {}", self.token)) | ||
.header("Content-Type", "application/json") | ||
.body(r#"{"name": "test", "members": [1, 2], "public": false}"#); | ||
let res = res.send().await?; | ||
assert_eq!(res.status(), StatusCode::CREATED); | ||
let chat: Chat = res.json().await?; | ||
assert_eq!(chat.name.as_ref().unwrap(), "test"); | ||
assert_eq!(chat.members, vec![1, 2]); | ||
assert_eq!(chat.r#type, ChatType::PrivateChannel); | ||
|
||
Ok(chat) | ||
} | ||
|
||
async fn create_message(&self, chat_id: u64) -> Result<Message> { | ||
// upload file | ||
let data = include_bytes!("../Cargo.toml"); | ||
let files = Part::bytes(data) | ||
.file_name("Cargo.toml") | ||
.mime_str("text/plain")?; | ||
let form = Form::new().part("file", files); | ||
|
||
let res = self | ||
.client | ||
.post(&format!("http://{}/api/upload", self.addr)) | ||
.header("Authorization", format!("Bearer {}", self.token)) | ||
.multipart(form) | ||
.send() | ||
.await?; | ||
assert_eq!(res.status(), StatusCode::OK); | ||
let ret: Vec<String> = res.json().await?; | ||
|
||
let body = serde_json::to_string(&json!({ | ||
"content": "hello", | ||
"files": ret, | ||
}))?; | ||
let res = self | ||
.client | ||
.post(format!("http://{}/api/chats/{}", self.addr, chat_id)) | ||
.header("Authorization", format!("Bearer {}", self.token)) | ||
.header("Content-Type", "application/json") | ||
.body(body); | ||
let res = res.send().await?; | ||
assert_eq!(res.status(), StatusCode::CREATED); | ||
let message: Message = res.json().await?; | ||
assert_eq!(message.content, "hello"); | ||
assert_eq!(message.files, ret); | ||
assert_eq!(message.sender_id, 1); | ||
assert_eq!(message.chat_id, chat_id as i64); | ||
Ok(message) | ||
} | ||
} |
Oops, something went wrong.