Added initial mockserver that doesnt work
ci/woodpecker/push/woodpecker Pipeline failed Details

This commit is contained in:
Martin Slot 2022-11-14 16:18:24 +01:00
parent de1478daf9
commit cdd0070d94
6 changed files with 49 additions and 51 deletions

View File

@ -142,10 +142,11 @@ pub struct Database {
#[derive(Debug, Deserialize)]
pub struct Settings {
pub database: Database,
pub github_api_override: Option<String>, // This is a simple short termed fix that is likly to be deprecated when we get another provider. This has been made so i can find a way to inject the httpmock server url into application
}
impl Settings {
pub fn new() -> Result<Self, ConfigError> {
pub fn new(github_api_override: Option<&str>) -> Result<Self, ConfigError> {
let run_mode = env::var("RUN_MODE").unwrap_or_else(|_| "development".into());
let s = Config::builder()
@ -155,7 +156,15 @@ impl Settings {
.add_source(Environment::with_prefix("version_checker"))
.build()?;
s.try_deserialize()
let mut settings: Settings = s.try_deserialize().unwrap();
// TODO: there must be a easier way of expressing this in rust :D
match github_api_override {
Some(o) => settings.github_api_override = Some(o.to_string()),
None => settings.github_api_override = None,
};
Ok(settings)
}
}

View File

@ -1,5 +1,3 @@
use serde::{Deserialize, Serialize};
use crate::configuration::VcsType;
use crate::request::models::{Current, GithubRelease};
use crate::{Print, Settings};
@ -28,9 +26,15 @@ impl<'a> RequestHandler<'a> {
let currents = self.request_gateway.get_currents();
for current in currents.iter() {
//TODO: This needs to be rethought a bit
let api_url = match &self.settings.github_api_override {
Some(api) => api,
None => &current.api_root_url,
};
let releases_api_url = format!(
"{}/repos/{}/{}/releases", // api_root_url contains protocol - this is very much hard coded to Github right now, so i wont make any fancy factory or something. When there is added another provider i will think on how to do this the correct way
current.api_root_url, current.owner_name, current.repo_name
api_url, current.owner_name, current.repo_name
);
let release = reqwest::blocking::get(releases_api_url)

View File

@ -6,7 +6,9 @@ publish = false
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
# this is only for test development, so it will always just be referenced as dev-dependencies
[dependencies]
rusqlite = { version = "0.27.0"}
test-context = { version = "0.1.4"}
application = { path = "../application" }
httpmock = "0.6"

View File

@ -1,6 +1,7 @@
use rusqlite::Connection;
use std::error::Error;
use rusqlite::Connection;
use application::configuration::VcsType;
use application::register_current::models::Current;
use application::register_origin::models::Origin;
@ -86,6 +87,7 @@ pub fn insert_origin(
pub mod database {
use std::fs;
use httpmock::MockServer;
use test_context::TestContext;
use application::configuration::Settings;
@ -93,13 +95,18 @@ pub mod database {
pub struct FunctionalTestContext {
pub settings: Settings,
pub mock_server: MockServer,
}
impl TestContext for FunctionalTestContext {
fn setup() -> Self {
let settings = Settings::new().unwrap();
let mock_server = MockServer::start(); // TODO: this could be overkill to start a server on every test but for now this is the easiests
let settings = Settings::new(Some(mock_server.url("/").as_str())).unwrap();
migrate(&settings);
FunctionalTestContext { settings }
FunctionalTestContext {
settings,
mock_server,
}
}
fn teardown(self) {
// we dont care about if this went well or not, because there is not much we can do about it

View File

@ -4,13 +4,12 @@ version = "0.1.0"
edition = "2021"
publish = false
[dependencies]
test-context = { version = "0.1.4"}
[dev-dependencies]
application = { path = "../application" }
test_utilities = { path = "../test_utilities"}
test_utilities = { path = "../test_utilities" }
serial_test = "*"
httpmock = "0.6"
test-context = { version = "0.1.4" }
[[test]]
name = "settings_test"

View File

@ -1,8 +1,9 @@
use httpmock::prelude::*;
use serial_test::serial;
use test_context::test_context;
use application::configuration::ArgumentParser;
use application::Application;
use serial_test::serial;
use std::cell::Cell;
use test_context::test_context;
use test_utilities::console::FakeConsole;
use test_utilities::database::FunctionalTestContext;
use test_utilities::{insert_current, insert_origin};
@ -29,44 +30,20 @@ fn should_make_request(context: &FunctionalTestContext) {
&context.settings.database.get_connection(),
);
insert_origin(
String::from("name2"),
String::from("owner_name"),
String::from("repo_name"),
String::from("github"),
&context.settings.database.get_connection(),
);
let fake_console = FakeConsole { callback: || {} };
insert_current(
String::from("name2"),
"1".to_string(),
"github".to_string(),
&context.settings.database.get_connection(),
);
// Create a mock on the server.
let releases_mock = context.mock_server.mock(|when, then| {
when.method(GET)
.path("/repos/owner_name/repo_name/releases");
then.status(200)
.header("content-type", "application/json")
.body("{}");
});
insert_origin(
String::from("name3"),
String::from("owner_name"),
String::from("repo_name"),
String::from("github"),
&context.settings.database.get_connection(),
);
insert_current(
String::from("name3"),
"1".to_string(),
"github".to_string(),
&context.settings.database.get_connection(),
);
let times_print_is_called = Cell::new(0);
let fake_console = FakeConsole {
callback: || {
let mut called = times_print_is_called.get();
called += 1;
times_print_is_called.set(called);
},
};
let application = Application::new(&context.settings, &fake_console, parser, arguments);
application.run();
releases_mock.assert();
}