Confirm subscription endpoint
This commit is contained in:
69
tests/api/subscriptions_confirm.rs
Normal file
69
tests/api/subscriptions_confirm.rs
Normal file
@@ -0,0 +1,69 @@
|
||||
use crate::helpers::TestApp;
|
||||
use wiremock::{
|
||||
Mock, ResponseTemplate,
|
||||
matchers::{method, path},
|
||||
};
|
||||
|
||||
#[tokio::test]
|
||||
async fn confirmation_links_without_token_are_rejected_with_a_400() {
|
||||
let app = TestApp::spawn().await;
|
||||
|
||||
let response = reqwest::get(&format!("{}/subscriptions/confirm", &app.address))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(400, response.status().as_u16());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn the_link_returned_by_subscribe_returns_a_200_if_called() {
|
||||
let app = TestApp::spawn().await;
|
||||
|
||||
let body = "name=Alphonse&email=alphonse.paix%40outlook.com";
|
||||
|
||||
Mock::given(path("v1/email"))
|
||||
.and(method("POST"))
|
||||
.respond_with(ResponseTemplate::new(200))
|
||||
.expect(1)
|
||||
.mount(&app.email_server)
|
||||
.await;
|
||||
|
||||
app.post_subscriptions(body.into()).await;
|
||||
|
||||
let email_request = &app.email_server.received_requests().await.unwrap()[0];
|
||||
let confirmation_links = app.get_confirmation_links(email_request);
|
||||
let response = reqwest::get(confirmation_links.html).await.unwrap();
|
||||
assert_eq!(response.status().as_u16(), 200);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn clicking_on_the_confirmation_link_confirms_a_subscriber() {
|
||||
let app = TestApp::spawn().await;
|
||||
|
||||
let body = "name=Alphonse&email=alphonse.paix%40outlook.com";
|
||||
|
||||
Mock::given(path("v1/email"))
|
||||
.and(method("POST"))
|
||||
.respond_with(ResponseTemplate::new(200))
|
||||
.expect(1)
|
||||
.mount(&app.email_server)
|
||||
.await;
|
||||
|
||||
app.post_subscriptions(body.into()).await;
|
||||
|
||||
let email_request = &app.email_server.received_requests().await.unwrap()[0];
|
||||
let confirmation_links = app.get_confirmation_links(email_request);
|
||||
reqwest::get(confirmation_links.html)
|
||||
.await
|
||||
.unwrap()
|
||||
.error_for_status()
|
||||
.unwrap();
|
||||
|
||||
let saved = sqlx::query!("SELECT email, name, status FROM subscriptions")
|
||||
.fetch_one(&app.connection_pool)
|
||||
.await
|
||||
.expect("Failed to fetch saved subscription");
|
||||
|
||||
assert_eq!(saved.email, "alphonse.paix@outlook.com");
|
||||
assert_eq!(saved.name, "Alphonse");
|
||||
assert_eq!(saved.status, "confirmed");
|
||||
}
|
||||
Reference in New Issue
Block a user