Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test de bout en bout avec un serveur Python/flask #35

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
**/*.rs.bk
.idea
/tests/fixtures/server-static/node_modules
/tests/fixtures/server-static/uploads/*
/tests/fixtures/server-static/uploads/*
/tests/fixtures/server-python/.env
/tests/fixtures/server-python/__pycache__
75 changes: 71 additions & 4 deletions tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ fn the_app_crashes_with_an_invalid_password() {
use std::{thread, time};

#[test]
fn end_to_end_upload_and_download() {
fn end_to_end_upload_and_download_node() {
/*
This test:
- spawns a node server that stores uploaded files in tests/fixtures/server-static/uploads/
Expand All @@ -177,7 +177,7 @@ fn end_to_end_upload_and_download() {
.expect(&format!("Unable to remove {} !", uploaded_path.to_owned()));
}

let mut proxy_server = launch_proxy();
let mut proxy_server = launch_proxy(3000);
let mut node_server = launch_node();

thread::sleep(time::Duration::from_millis(1000));
Expand Down Expand Up @@ -212,12 +212,68 @@ fn end_to_end_upload_and_download() {
temp.close().unwrap();
}

fn launch_proxy() -> Child {
#[test]
fn end_to_end_upload_and_download_python() {
/*
This test:
- spawns a python server that stores uploaded files in tests/fixtures/server-python/uploads/
- spawns a ds proxy that uses the python proxy as a storage backend
- uploads a file using curl via the DS proxy
- checks that said file is encrypted
- decrypt the uploaded file by the decrypted command and check the result
- downloads the uploaded file via the proxy, and checks that its content matches the initial content
*/
let original_path = "tests/fixtures/computer.svg";
let original_bytes = std::fs::read(original_path).unwrap();
let uploaded_path = "tests/fixtures/server-python/uploads/victory";

let temp = assert_fs::TempDir::new().unwrap();
let decrypted_file = temp.child("computer.dec.svg");
let decrypted_path = decrypted_file.path();

if Path::new(uploaded_path).exists() {
std::fs::remove_file(uploaded_path)
.expect(&format!("Unable to remove {} !", uploaded_path.to_owned()));
}

let mut proxy_server = launch_proxy(5000);
let mut python_server = launch_python();

thread::sleep(time::Duration::from_millis(1000));

let curl_upload = curl_put(original_path, "127.0.0.1:5000/victory");
if !curl_upload.status.success() {
panic!("unable to upload file !");
}

// let uploaded_bytes = std::fs::read(uploaded_path).expect("uploaded should exist !");
// assert_eq!(&uploaded_bytes[0..PREFIX_SIZE], PREFIX);

// decrypt(uploaded_path, decrypted_path);
// let decrypted_bytes = std::fs::read(decrypted_path).unwrap();
// assert_eq!(original_bytes, decrypted_bytes);

// let curl_download = curl_get("localhost:5000/victory");
// assert_eq!(curl_download.stdout, original_bytes);

// let curl_socket_download = curl_socket_get("localhost:5000/victory");
// assert_eq!(curl_socket_download.stdout, original_bytes);

proxy_server
.kill()
.expect("killing the proxy server should succeed !");
python_server
.kill()
.expect("killing node's upload server should succeed !");
temp.close().unwrap();
}

fn launch_proxy(upstream_port: i32) -> Child {
Command::cargo_bin("ds_proxy")
.unwrap()
.arg("proxy")
.arg("--address=localhost:4444")
.arg("--upstream-url=http://localhost:3000")
.arg(format!("--upstream-url=http://localhost:{}", upstream_port))
.arg(HASH_FILE_ARG)
.env("DS_PASSWORD", PASSWORD)
.env("DS_SALT", SALT)
Expand All @@ -226,6 +282,17 @@ fn launch_proxy() -> Child {
.expect("failed to execute ds_proxy")
}

fn launch_python() -> Child {
Command::new("python")
.arg("-m")
.arg("flask")
.arg("run")
.current_dir("tests/fixtures/server-python")
.env("FLASK_APP", "main.py")
.spawn()
.expect("failed to execute python")
}

fn launch_node() -> Child {
Command::new("node")
.arg("tests/fixtures/server-static/server.js")
Expand Down
18 changes: 18 additions & 0 deletions tests/fixtures/server-python/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import urllib.request
from flask import Flask, request, send_from_directory
app = Flask(__name__)

@app.route("/<path:filename>")
def serve_uploaded_file(filename):
return send_from_directory("uploads", filename)

@app.route('/<path:filename>', methods=['PUT'])
def upload_file(filename):
file = open("uploads/{}".format(filename), 'wb')
file.write(request.data)
file.close()

return 'OK!', 200, {'ContentType':'text/plain'}

if __name__ == "__main__":
app.run()
7 changes: 7 additions & 0 deletions tests/fixtures/server-python/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
click==7.1.2
Flask==1.1.2
itsdangerous==1.1.0
Jinja2==2.11.2
MarkupSafe==1.1.1
pkg-resources==0.0.0
Werkzeug==1.0.1
1 change: 1 addition & 0 deletions tests/fixtures/server-python/uploads/.keep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.keep