Added test console
ci/woodpecker/push/woodpecker Pipeline was successful Details

Added MVP code to send an empty email
Drinking some champagne to celebrate
This commit is contained in:
Martin Slot 2022-12-12 21:20:29 +01:00
parent c5b457accd
commit 793fe26a1d
5 changed files with 2637 additions and 8 deletions

View File

@ -3,7 +3,11 @@ use crate::request::models::{Current, GithubRelease};
use crate::request::notification::Notification;
use crate::request::versioning::{SemverVersionComparer, VersionComparer};
use crate::{Print, Settings};
use reqwest::get;
use reqwest::header::{ACCEPT, ACCEPT_ENCODING, ACCEPT_LANGUAGE};
use std::cmp::Ordering;
use std::io::Read;
use std::time::Duration;
pub fn init<'a>(
settings: &'a Settings,
@ -47,11 +51,23 @@ impl<'a> RequestHandler<'a> {
api_url, current.owner_name, current.repo_name
);
let release = reqwest::blocking::get(latest_release_api_url)
.unwrap()
.json::<GithubRelease>()
let client = reqwest::blocking::Client::builder()
.timeout(Duration::from_secs(10))
.user_agent("VersionChecker/1.0.0")
.build()
.unwrap();
let mut return_json = client
.get(&latest_release_api_url)
.header(ACCEPT, "application/vnd.github.v3+json")
// .header("X-Github-Api-Version", "2022-11-28")
// .header(ACCEPT_LANGUAGE, "en-US")
// .header(ACCEPT_ENCODING, "gzip, deflate, br")
.send()
.unwrap();
let release = return_json.json::<GithubRelease>().unwrap();
// Compare
// For now we only have one comparer: semver comparer, so it is hardcoded here. This is going to be changed when more is added
let comparer = SemverVersionComparer;

View File

@ -31,4 +31,4 @@ CREATE TABLE IF NOT EXISTS VcsTypes
);
INSERT INTO VcsTypes (vcs_type, api_root_url)
VALUES ('github', 'https://api.github.com')
VALUES ('github', 'https://api.github.com/')

View File

@ -1,3 +1,7 @@
use application::configuration::{ArgumentParser, Settings};
use application::console::Console;
use application::request::notification::EmailNotification;
use application::Application;
/*
I use this as a test bench instead of running the binary from a terminal.
TODO: i might change this to a test, that is muted somehow, but through configuration can be wired
@ -14,8 +18,57 @@ fn main() {
smtp_password: settings.email.smtp_password.clone(),
from: settings.email.from.clone(),
};
let args = std::env::args().collect();
let application = Application::new(&settings, &console, &notification, parser, args);
application.run();
let arguments_insert_origin = vec![
String::from("release_checker"),
String::from("register"),
String::from("origin"),
String::from("github"),
String::from("gitea"),
String::from("go-gitea"),
String::from("gitea"),
];
let application_insert_origin = Application::new(
&settings,
&console,
&notification,
parser,
arguments_insert_origin,
);
application_insert_origin.run();
let arguments_insert_current = vec![
String::from("release_checker"),
String::from("register"),
String::from("current"),
String::from("github"),
String::from("gitea"),
String::from("1.2.3"),
String::from(&settings.email.from),
];
let parser = ArgumentParser {};
let application_insert_current = Application::new(
&settings,
&console,
&notification,
parser,
arguments_insert_current,
);
application_insert_current.run();
let parser = ArgumentParser {};
let arguments_request = vec![String::from("release_checker"), String::from("request")];
let application_do_request = Application::new(
&settings,
&console,
&notification,
parser,
arguments_request,
);
application_do_request.run();
}

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,7 @@
use httpmock::prelude::*;
use serial_test::serial;
use std::cell::Cell;
use std::fs::File;
use test_context::test_context;
use application::configuration::ArgumentParser;
@ -167,7 +168,66 @@ fn should_make_request_gets_version_with_v_from_github_current_is_smaller(
.path("/repos/owner_name/repo_name/releases/latest");
then.status(200)
.header("content-type", "application/json")
.body("{\"tag_name\":\"v1.2.3\",\"name\":\"name\"}"); // TODO: this a really simple return that just returns what i want. It should contain the full json blob that /releases returns
.body("{\"tag_name\":\"v1.2.3\",\"name\":\"name\",\"assets_url\":\"url\"}");
// TODO: this a really simple return that just returns what i want. It should contain the full json blob that /releases returns
});
let application = Application::new(
&context.settings,
&fake_console,
&fake_notification,
parser,
arguments,
);
application.run();
releases_mock.assert();
assert_eq!(1, times_notification_is_called.get());
}
#[test_context(FunctionalTestContext)]
#[test]
#[serial]
fn should_make_request_on_full_github_release_json_return(context: &FunctionalTestContext) {
let parser = ArgumentParser {};
let arguments = vec![String::from("release_checker"), String::from("request")];
insert_origin(
String::from("name1"),
String::from("owner_name"),
String::from("repo_name"),
String::from("github"),
&context.settings.database.get_connection(),
);
insert_current(
String::from("name1"),
"1.2.2".to_string(),
"github".to_string(),
"email@domain.tld".to_string(),
&context.settings.database.get_connection(),
);
let times_notification_is_called = Cell::new(0);
let fake_console = FakeConsole { callback: || {} };
let fake_notification = FakeNotification {
callback: || {
let mut called = times_notification_is_called.get();
called += 1;
times_notification_is_called.set(called);
},
};
let response = std::fs::read_to_string("full_github_release.json").unwrap();
// 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/latest");
then.status(200)
.header("content-type", "application/json")
.body(&response);
// TODO: this a really simple return that just returns what i want. It should contain the full json blob that /releases returns
});
let application = Application::new(