Skip to content

Commit

Permalink
feat: full flow test
Browse files Browse the repository at this point in the history
  • Loading branch information
EstebanBorai committed Dec 24, 2023
1 parent 49151c9 commit 5a2686c
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 11 deletions.
7 changes: 6 additions & 1 deletion crates/core/src/mail/templates/verification_code.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,12 @@
</tr>
<tr>
<td align="left" style="font-size:0px;padding:10px 25px;word-break:break-word;">
<div style="font-family:helvetica;font-size:20px;line-height:1;text-align:left;color:#F45E43;">This is your verification code: {{code}} please don't share it with anyone!</div>
<div style="font-family:helvetica;font-size:20px;line-height:1;text-align:left;color:#F45E43;">This is your verification code, please don't share it with anyone!</div>
</td>
</tr>
<tr>
<td align="left" style="font-size:0px;padding:10px 25px;word-break:break-word;">
<div style="font-family:helvetica;font-size:20px;line-height:1;text-align:left;color:#F45E43;" id="code">{{code}}</div>
</td>
</tr>
</tbody>
Expand Down
4 changes: 2 additions & 2 deletions crates/server/src/router/api/v1/account/verify_code_email.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub async fn handler(

match services.commune.account.verify_code(dto).await {
Ok(valid) => {
let mut response = Json(VerifyCodeResponse { valid }).into_response();
let mut response = Json(VerifyCodeEmailResponse { valid }).into_response();

*response.status_mut() = StatusCode::OK;
response
Expand Down Expand Up @@ -51,6 +51,6 @@ impl From<AccountVerifyCodeEmailPayload> for VerifyCodeDto {
}

#[derive(Deserialize, Serialize)]
pub struct VerifyCodeResponse {
pub struct VerifyCodeEmailResponse {
pub valid: bool,
}
3 changes: 3 additions & 0 deletions crates/test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ name = "test"
path = "src/lib.rs"

[dependencies]
fake = { version = "2.9.2", features = ["derive"] }
scraper = "0.18.1"

# Workspace Dependencies
anyhow = { workspace = true }
axum = { workspace = true, features = ["tokio"] }
dotenv = { workspace = true }
reqwest = { workspace = true, features = ["json", "stream", "multipart"] }
Expand Down
63 changes: 55 additions & 8 deletions crates/test/src/server/api/v1/account/create.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,69 @@
use fake::{
faker::internet::en::{FreeEmail, Password},
Fake,
};

use reqwest::StatusCode;
use scraper::Selector;
use uuid::Uuid;

use commune::util::secret::Secret;
use commune_server::router::api::v1::account::create::{
AccountRegisterPayload, AccountRegisterResponse,
use commune_server::router::api::v1::account::{
create::{AccountRegisterPayload, AccountRegisterResponse},
verify_code::{AccountVerifyCodePayload, VerifyCodeResponse},
verify_code_email::{AccountVerifyCodeEmailPayload, VerifyCodeEmailResponse},
};

use crate::tools::http::HttpClient;
use crate::tools::{http::HttpClient, maildev::MailDevClient};

#[tokio::test]
async fn register_account_with_success() {
let http_client = HttpClient::new().await;
let session = Uuid::new_v4();
let email: String = FreeEmail().fake();
let verify_code_pld = AccountVerifyCodePayload {
email: email.clone(),
session,
};
let verify_code_res = http_client
.post("/api/v1/account/verify/code")
.json(&verify_code_pld)
.send()
.await;
let verify_code = verify_code_res.json::<VerifyCodeResponse>().await;

assert!(verify_code.sent, "should return true for sent");

let maildev = MailDevClient::new();
let mail = maildev.latest().await.unwrap().unwrap();
let html = mail.html();
let code_sel = Selector::parse("#code").unwrap();
let mut code_el = html.select(&code_sel);
let code = code_el.next().unwrap().inner_html();
let verify_code_email_pld = AccountVerifyCodeEmailPayload {
email: email.clone(),
code: Secret::new(code.clone()),
session,
};

let verify_code_res = http_client
.post("/api/v1/account/verify/code/email")
.json(&verify_code_email_pld)
.send()
.await;
let verify_code_email = verify_code_res.json::<VerifyCodeEmailResponse>().await;

assert!(verify_code_email.valid, "should return true for valid");

let username: String = (10..12).fake();
let username = username.to_ascii_lowercase();
let password: String = Password(14..20).fake();
let request_payload = AccountRegisterPayload {
username: String::from("john_wick"),
password: String::from("P@ssW0Rd$"),
email: String::from("donttrythisathome@gmail.com"),
code: Secret::new("1234").to_string(),
session: Uuid::new_v4(),
username,
password,
email,
code,
session,
};
let response = http_client
.post("/api/v1/account")
Expand Down
104 changes: 104 additions & 0 deletions crates/test/src/tools/maildev.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
use anyhow::Result;
use reqwest::{Client, StatusCode};
use scraper::Html;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct Recipient {
pub address: String,
pub name: String,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")]
pub(crate) struct Header {
pub from: String,
pub to: String,
pub subject: String,
pub content_type: String,
pub content_transfer_encoding: String,
pub date: String,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct EnvelopeRecipient {
pub address: String,
pub args: bool,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct Envelope {
pub from: EnvelopeRecipient,
pub to: Vec<EnvelopeRecipient>,
pub host: String,
pub remote_address: String,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct Mail {
pub html: String,
pub headers: Header,
pub subject: String,
pub priority: String,
pub from: Vec<Recipient>,
pub to: Vec<Recipient>,
pub date: String,
pub id: String,
pub time: String,
pub read: bool,
pub envelope: Envelope,
pub source: String,
pub size: usize,
pub size_human: String,
pub attachments: Option<Vec<String>>,
pub calculated_bcc: Vec<String>,
}

impl Mail {
pub fn html(&self) -> Html {
Html::parse_fragment(&self.html)
}
}

pub(crate) struct MailDevClient {
pub client: Client,
}

impl MailDevClient {
pub(crate) fn new() -> Self {
let client = reqwest::Client::builder()
.redirect(reqwest::redirect::Policy::none())
.build()
.unwrap();

Self { client }
}

pub(crate) async fn latest(&self) -> Result<Option<Mail>> {
let response = self
.client
.get("http://localhost:1080/email")
.send()
.await?;

match response.status() {
StatusCode::OK => {
let response_body = response.json::<Vec<Mail>>().await?;

if response_body.is_empty() {
return Ok(None);
}

let mail = response_body.last().unwrap().clone().to_owned();

Ok(Some(mail))
}
StatusCode::NOT_FOUND => Ok(None),
_ => unreachable!(),
}
}
}
1 change: 1 addition & 0 deletions crates/test/src/tools/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod environment;
pub mod http;
pub mod maildev;

0 comments on commit 5a2686c

Please sign in to comment.