Commit 76744245 authored by MrMan's avatar MrMan

start writing user creation test

parent a7bcdf40
Pipeline #47795931 failed with stage
in 2 minutes and 36 seconds
......@@ -7,7 +7,9 @@ use std::process::{Command, Stdio};
use std::net::{TcpListener, Ipv4Addr};
use std::time::Duration;
use std::thread;
// use std::io::{Error as IOError};
use std::io::{Error as IOError};
use std::net::TcpStream;
use std::io::{BufRead, BufReader};
use self::rand::{thread_rng, Rng};
use self::rand::distributions::Alphanumeric;
......@@ -24,6 +26,9 @@ use self::rand::seq::sample_iter;
pub const POSTMASTER_AT_LOCALHOST: &'static str = "[email protected]";
pub const CR_LF_END: &'static str = "\r\n.\r\n";
pub const SMTP_B64_USERNAME_PROMPT: &'static str = "334 VXNlcm5hbWU6;";
pub const SMTP_B64_PASSWORD_PROMPT: &'static str = "334 UGFzc3dvcmQ6;";
// Get available port
// https://elliotekj.com/2017/07/25/find-available-tcp-port-rust/
const TMP_PORT_RANGE_START: u16 = 8000;
......@@ -117,6 +122,13 @@ impl DockerizedPostmgr {
}
}
/// Connect to the instance over TCP
pub fn connect_tcp(&self) -> Result<(TcpStream, BufReader<TcpStream>), IOError> {
let stream = TcpStream::connect(self.get_smtp_address())?;
let mut reader = BufReader::new(stream.try_clone()?);
Ok((stream, reader))
}
pub fn get_smtp_address(&self) -> String {
format!("{}:{}", self.host, self.smtp_port)
}
......@@ -126,6 +138,18 @@ impl DockerizedPostmgr {
// }
}
/// Send an EHLO & verify the response
pub fn read_ehlo(reader: &mut BufReader<TcpStream>) -> &mut BufReader<TcpStream> {
let mut line = String::new();
reader.read_line(&mut line).unwrap();
println!("EHLO response: [{}]", line.trim());
assert!(line.contains("220"), "EHLO response contained '220'");
assert!(line.contains("Postfix"), "EHLO response contained 'Postfix'");
reader
}
impl Drop for DockerizedPostmgr {
fn drop(&mut self) {
// Kill the docker process
......
......@@ -3,18 +3,14 @@ mod common;
use crate::common::DockerizedPostmgr;
use std::net::TcpStream;
use std::io::{Write, BufRead, BufReader};
use common::{POSTMASTER_AT_LOCALHOST, CR_LF_END};
use common::{
POSTMASTER_AT_LOCALHOST,
CR_LF_END,
SMTP_B64_USERNAME_PROMPT,
SMTP_B64_PASSWORD_PROMPT,
/// Send an EHLO & verify the response
fn read_ehlo(reader: &mut BufReader<TcpStream>) -> &mut BufReader<TcpStream> {
let mut line = String::new();
reader.read_line(&mut line).unwrap();
println!("EHLO response: [{}]", line.trim());
assert!(line.contains("220"), "EHLO response contained '220'");
assert!(line.contains("Postfix"), "EHLO response contained 'Postfix'");
reader
}
read_ehlo,
};
/// Send MAIL FROM and verify response
fn send_mail_from(
......@@ -33,7 +29,7 @@ fn send_mail_from(
(stream, reader)
}
/// Send MAIL FROM and verify response
/// Send RCPT TO and verify response
fn send_rcpt_to(
recipient: &str,
mut stream: TcpStream,
......@@ -50,7 +46,7 @@ fn send_rcpt_to(
(stream, reader)
}
/// Send MAIL FROM and verify response
/// Send a test message with a given subject/body
fn send_test_message(
subject: &str,
body: &str,
......@@ -78,15 +74,42 @@ fn send_test_message(
(stream, reader)
}
// /// Perform login with a given username and password
// fn login_with_username_password(
// username: &str,
// password: &str,
// mut stream: TcpStream,
// mut reader: BufReader<TcpStream>
// ) -> (TcpStream, BufReader<TcpStream>) {
// let mut line = String::new();
// // Send data start
// let cmd = format!("AUTH LOGIN\n");
// stream.write(cmd.as_bytes()).unwrap();
// reader.read_line(&mut line).unwrap();
// // Postfix sends 354 here to indicate how to end the data
// println!("DATA response: [{}]", line.trim());
// assert!(line.contains("354") && line.contains("End data with"), "DATA command succeeded");
// line.clear();
// // Send the test
// let cmd = format!("subject: {}\n{}\n{}", subject, body, CR_LF_END);
// stream.write(cmd.as_bytes()).unwrap();
// reader.read_line(&mut line).unwrap();
// println!("Message send response: [{}]", line.trim());
// assert!(line.contains("250") && line.contains("Ok:"), "Message successfully queued");
// (stream, reader)
// }
// #[test]
// #[ignore]
// /// Test basic SMTP connectivity
// pub fn test_smtp_25_connect() {
// pub fn test_connect() {
// // Start postmgr in a docker container
// let postmgr = DockerizedPostmgr::new();
// // Connect to the postmgr over port 25
// println!("connecting to test postmgr @ [{}]", postmgr.get_smtp_address());
// let mut stream = TcpStream::connect(postmgr.get_smtp_address()).unwrap();
// let mut reader = BufReader::new(stream.try_clone().unwrap());
......@@ -94,29 +117,54 @@ fn send_test_message(
// read_ehlo(&mut reader);
// }
// #[test]
// #[ignore]
// /// Test basic SMTP email sending
// pub fn test_email_send() {
// // Start postmgr in a docker container
// let postmgr = DockerizedPostmgr::new();
// // Connect to the postmgr over port 25
// let stream = TcpStream::connect(postmgr.get_smtp_address()).unwrap();
// let mut reader = BufReader::new(stream.try_clone().unwrap());
// // Ensure EHLO was received
// read_ehlo(&mut reader);
// // MAIL FROM
// let (stream, reader) = send_mail_from(POSTMASTER_AT_LOCALHOST, stream, reader);
// // RCPT TO
// let (stream, reader) = send_rcpt_to(POSTMASTER_AT_LOCALHOST, stream, reader);
// // send message contents
// send_test_message("test email", "this is a test", stream, reader);
// }
#[test]
#[ignore]
/// Test basic SMTP email sending
pub fn test_smtp_25_email_send() {
pub fn test_user_creation() {
/// Test user creation & interaction over basic SMTP relay
// Start postmgr in a docker container
let postmgr = DockerizedPostmgr::new();
// Connect to the postmgr over port 25
println!("connecting to test postmgr @ [{}]", postmgr.get_smtp_address());
let stream = TcpStream::connect(postmgr.get_smtp_address()).unwrap();
let mut reader = BufReader::new(stream.try_clone().unwrap());
// TODO: Create a new user through the API
// Connect to the postmgr over (unprotected) port 25
let (stream, mut reader) = postmgr.connect_tcp().unwrap();
// Ensure EHLO was received
read_ehlo(&mut reader);
// MAIL FROM
let (stream, reader) = send_mail_from(POSTMASTER_AT_LOCALHOST, stream, reader);
// RCPT TO
let (stream, reader) = send_rcpt_to(POSTMASTER_AT_LOCALHOST, stream, reader);
// send message contents
send_test_message("test email", "this is a test", stream, reader);
// // TODO: Login with username/pw
// let (stream, reader) = login_with_username_password(
// fixtures.TEST.username,
// fixtures.TEST.password,
// stream,
// reader,
// )
}
// TODO: test create user & login
// TODO: test sending email
// TODO: test sending email between local users
// TODO: test sending email between two local created
// TODO: test user delete (after deleting through the API a user should be unable to log in)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment