Skip to content

Commit

Permalink
feat: add integration test (#1)
Browse files Browse the repository at this point in the history
* refactor requests

* better error handling

* test it

* add rust workflow
  • Loading branch information
malteo authored Jun 7, 2024
1 parent d78fc28 commit 8589192
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 16 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Rust

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

env:
CARGO_TERM_COLOR: always

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
116 changes: 116 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ env_logger = "0.11"
log = "0.4"
serde = { version = "1.0", features = ["derive"] }
ureq = { version = "2.9", features = ["json"] }

[dev-dependencies]
mockito = "1.4.0"
25 changes: 13 additions & 12 deletions src/cloudesire_client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use serde::{Deserialize, Serialize};
use std::env;
use ureq::Request;

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "UPPERCASE")]
Expand All @@ -19,16 +20,11 @@ pub struct Subscription {
}

pub fn get_subscription(id: u32) -> Subscription {
let base_url = env::var("CMW_BASE_URL").unwrap_or("http://localhost:8081".to_string());
let auth_token = env::var("CMW_AUTH_TOKEN").unwrap_or("test-token".to_string());

let url = base_url + "/subscription/" + &id.to_string();
ureq::get(&url)
.set("CMW-Auth-Token", &auth_token)
build_request("GET", "subscription", id)
.call()
.unwrap()
.expect("call should succeed")
.into_json()
.unwrap()
.expect("response should be deserialized")
}

pub fn update_status(subscription_id: u32, status: DeploymentStatus) {
Expand All @@ -40,12 +36,17 @@ pub fn update_status(subscription_id: u32, status: DeploymentStatus) {
return;
}

build_request("PATCH", "subscription", subscription_id)
.send_json(ureq::json!({"deploymentStatus": status}))
.expect("call should succeed");
}

fn build_request(method: &str, path: &str, id: u32) -> Request {
let base_url = env::var("CMW_BASE_URL").unwrap_or("http://localhost:8081".to_string());
let auth_token = env::var("CMW_AUTH_TOKEN").unwrap_or("test-token".to_string());

let url = base_url + "/subscription/" + &subscription_id.to_string();
ureq::patch(&url)
let url = base_url + "/" + path + "/" + &id.to_string();
ureq::agent()
.request(method, &url)
.set("CMW-Auth-Token", &auth_token)
.send_json(ureq::json!({"deploymentStatus": status}))
.unwrap();
}
53 changes: 49 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use actix_web::{post, web, App, HttpResponse, HttpServer, Responder};
use cloudesire_client::{DeploymentStatus, Subscription};
use log::{debug, info};
use serde::Deserialize;
use serde::{Deserialize, Serialize};

pub mod cloudesire_client;

#[derive(Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "UPPERCASE")]
enum Lifecycle {
Created,
Modified,
Deleted,
}

#[derive(Deserialize)]
#[derive(Serialize, Deserialize)]
struct EventNotification {
entity: String,
id: u32,
Expand Down Expand Up @@ -53,7 +53,7 @@ fn subscription_deploy(subscription: Subscription) {
}
DeploymentStatus::Stopped => info!("Temporarily suspend the subscription"),
DeploymentStatus::Deployed => info!("Check if tenant is OK"),
_ => debug!("Unimplemented"),
_ => todo!(),
}
}

Expand All @@ -73,3 +73,48 @@ async fn main() -> std::io::Result<()> {
.run()
.await
}

#[cfg(test)]
mod tests {
use actix_web::{test, App};
use std::env;

use super::*;

#[actix_web::test]
async fn test_event_post() {
let mut server = mockito::Server::new_async().await;

let url = server.url();
env::set_var("CMW_BASE_URL", url);
env::remove_var("CMW_READ_ONLY");

server
.mock("GET", "/subscription/1")
.with_body(r#"{"id": 1, "deploymentStatus": "PENDING", "paid": true}"#)
.create_async()
.await;

let mock = server
.mock("PATCH", "/subscription/1")
.match_body(mockito::Matcher::JsonString(
r#"{"deploymentStatus": "DEPLOYED"}"#.to_string(),
))
.create_async()
.await;

let app = test::init_service(App::new().service(event)).await;
let req = test::TestRequest::post()
.uri("/event")
.set_json(EventNotification {
entity: "Subscription".to_string(),
id: 1,
lifecycle: Lifecycle::Created,
})
.to_request();
let res = test::call_service(&app, req).await;

mock.assert_async().await;
assert!(res.status().is_success());
}
}

0 comments on commit 8589192

Please sign in to comment.