release_checker/tests/request_test.rs

245 lines
7.4 KiB
Rust

use httpmock::prelude::*;
use serial_test::serial;
use std::cell::Cell;
use std::fs::File;
use test_context::test_context;
use application::configuration::ArgumentParser;
use application::Application;
use test_utilities::console::FakeConsole;
use test_utilities::database::FunctionalTestContext;
use test_utilities::notification::FakeNotification;
use test_utilities::{insert_current, insert_origin};
#[test_context(FunctionalTestContext)]
#[test]
#[serial]
fn should_make_request_gets_version_without_v_from_github_equal_versions(
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.3".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);
},
};
// 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("{\"tag_name\":\"1.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
});
let application = Application::new(
&context.settings,
&fake_console,
&fake_notification,
parser,
arguments,
);
application.run();
releases_mock.assert();
assert_eq!(0, times_notification_is_called.get());
}
#[test_context(FunctionalTestContext)]
#[test]
#[serial]
fn should_make_request_gets_version_without_v_from_github_current_is_smaller(
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);
},
};
// 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("{\"tag_name\":\"1.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
});
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_gets_version_with_v_from_github_current_is_smaller(
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);
},
};
// 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("{\"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);
});
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());
}