Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: account login test
Browse files Browse the repository at this point in the history
EstebanBorai committed Dec 24, 2023
1 parent 5a2686c commit fa1d61e
Showing 1 changed file with 69 additions and 21 deletions.
90 changes: 69 additions & 21 deletions crates/test/src/server/api/v1/account/login.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,82 @@
use commune::util::secret::Secret;
use commune_server::router::api::v1::account::create::AccountRegisterPayload;
use fake::faker::internet::en::{FreeEmail, Password};
use fake::Fake;
use reqwest::StatusCode;
use scraper::Selector;
use uuid::Uuid;

use commune::account::service::CreateAccountDto;
use commune::util::secret::Secret;
use commune_server::router::api::v1::account::login::{AccountLoginPayload, AccountLoginResponse};
use commune_server::router::api::v1::account::verify_code::{
AccountVerifyCodePayload, VerifyCodeResponse,
};
use commune_server::router::api::v1::account::verify_code_email::{
AccountVerifyCodeEmailPayload, VerifyCodeEmailResponse,
};

use crate::tools::environment::Environment;
use crate::tools::http::HttpClient;
use crate::tools::maildev::MailDevClient;

#[tokio::test]
async fn logs_into_account() {
let environment = Environment::new().await;

let username = "lucy".to_string();
let password = "P@ssW0Rd$".to_string();

environment
.commune
.account
.register(CreateAccountDto {
username: username.clone(),
password: password.clone().into(),
email: "lucyinthesky@gmail.com".to_string(),
code: Secret::new("1234"),
session: Uuid::new_v4(),
})
.await
.unwrap();

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: username.clone(),
password: password.clone(),
email,
code,
session,
};
let response = http_client
.post("/api/v1/account")
.json(&request_payload)
.send()
.await;

assert_eq!(
response.status(),
StatusCode::CREATED,
"should return 201 for successful registration"
);

let response = http_client
.post("/api/v1/account/login")

0 comments on commit fa1d61e

Please sign in to comment.