Tests for all time entry interactions

This commit is contained in:
Bodo Junglas 2020-02-05 21:05:38 +01:00
parent 3617d1ed00
commit 87ca12b744
No known key found for this signature in database
GPG Key ID: A7C4E6F450E47C3A
4 changed files with 176 additions and 471 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
/target
**/*.rs.bk
lib/target

6
Cargo.lock generated
View File

@ -2,9 +2,9 @@
# It is not intended for manual editing.
[[package]]
name = "acari-cli"
version = "0.1.3"
version = "0.1.4"
dependencies = [
"acari-lib 0.1.3",
"acari-lib 0.1.4",
"chrono 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)",
"clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
"dirs 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
@ -18,7 +18,7 @@ dependencies = [
[[package]]
name = "acari-lib"
version = "0.1.3"
version = "0.1.4"
dependencies = [
"chrono 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)",
"dirs 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -263,7 +263,49 @@ fn test_get_services() -> Result<(), Box<dyn std::error::Error>> {
}
#[test]
fn test_quest_entries() -> Result<(), Box<dyn std::error::Error>> {
fn test_query_entries() -> Result<(), Box<dyn std::error::Error>> {
let time_entry_json = json!({
"time_entry": {
"id": 36159117,
"minutes": 15,
"date_at": "2015-10-16",
"note": "Feedback einarbeiten",
"billable": true,
"locked": false,
"revenue": null,
"hourly_rate": 0,
"user_id": 211,
"user_name": "Fridolin Frei",
"project_id": 88309,
"project_name": "API v2",
"customer_id": 3213,
"customer_name": "König",
"service_id": 12984,
"service_name": "Entwurf",
"created_at": "2015-10-16T12:19:00+02:00",
"updated_at": "2015-10-16T12:39:00+02:00"
}
});
let expected = TimeEntry {
id: TimeEntryId(36159117),
minutes: Minutes(15),
date_at: NaiveDate::from_ymd(2015, 10, 16),
note: "Feedback einarbeiten".to_string(),
locked: false,
billable: true,
hourly_rate: 0,
user_id: UserId(211),
user_name: "Fridolin Frei".to_string(),
customer_id: CustomerId(3213),
customer_name: "König".to_string(),
service_id: ServiceId(12984),
service_name: "Entwurf".to_string(),
project_id: ProjectId(88309),
project_name: "API v2".to_string(),
created_at: Utc.ymd(2015, 10, 16).and_hms(10, 19, 00),
updated_at: Utc.ymd(2015, 10, 16).and_hms(10, 39, 00),
};
let pact = PactBuilder::new(CONSUMER, PROVIDER)
.interaction("query time entries", |i| {
i.given("User with API token");
@ -273,28 +315,15 @@ fn test_quest_entries() -> Result<(), Box<dyn std::error::Error>> {
.query_param("at", "2015-10-16")
.query_param("user", "current")
.header("X-MiteApiKey", term!("[0-9a-f]+", "12345678"));
i.response.ok().json_utf8().json_body(json!([{
"time_entry": {
"id": 36159117,
"minutes": 15,
"date_at": "2015-10-16",
"note": "Feedback einarbeiten",
"billable": true,
"locked": false,
"revenue": null,
"hourly_rate": 0,
"user_id": 211,
"user_name": "Fridolin Frei",
"project_id": 88309,
"project_name": "API v2",
"customer_id": 3213,
"customer_name": "König",
"service_id": 12984,
"service_name": "Entwurf",
"created_at": "2015-10-16T12:19:00+02:00",
"updated_at": "2015-10-16T12:39:00+02:00"
}
}]));
i.response.ok().json_utf8().json_body(json!([time_entry_json]));
})
.interaction("get time entry by id", |i| {
i.given("User with API token");
i.request
.get()
.path("/time_entries/36159117.json")
.header("X-MiteApiKey", term!("[0-9a-f]+", "12345678"));
i.response.ok().json_utf8().json_body(time_entry_json);
})
.build();
@ -306,12 +335,70 @@ fn test_quest_entries() -> Result<(), Box<dyn std::error::Error>> {
let entries = client.get_time_entries(DateSpan::Day(Day::Date(NaiveDate::from_ymd(2015, 10, 16))))?;
assert_eq!(entries.len(), 1);
assert_eq!(expected, entries[0]);
let entry = client.get_time_entry(TimeEntryId(36159117))?;
assert_eq!(expected, entry);
Ok(())
}
#[test]
fn test_create_entry() -> Result<(), Box<dyn std::error::Error>> {
let pact = PactBuilder::new(CONSUMER, PROVIDER)
.interaction("create time entry", |i| {
i.given("User with API token");
i.request
.post()
.path("/time_entries.json")
.json_body(json!({
"time_entry": {
"date_at": "2015-09-15",
"minutes": 185,
"project_id": 3456,
"service_id": 243
}
}))
.header("X-MiteApiKey", term!("[0-9a-f]+", "12345678"));
i.response.created().json_utf8().json_body(json!({
"time_entry": {
"id": 52324,
"minutes": 185,
"date_at": "2015-9-12",
"note": "",
"billable": true,
"locked": false,
"revenue": null,
"hourly_rate": 0,
"user_id": 211,
"user_name": "Fridolin Frei",
"customer_id": 3213,
"customer_name": "König",
"project_id": 3456,
"project_name": "Some project",
"service_id": 243,
"service_name": "Dokumentation",
"created_at": "2015-09-13T18:54:45+02:00",
"updated_at": "2015-09-13T18:54:45+02:00"
}
}));
})
.build();
let server = pact.start_mock_server();
let mut url = server.url().clone();
url.set_username("12345678").unwrap();
let client = StdClient::new_form_url(url);
let entry = client.create_time_entry(Day::Date(NaiveDate::from_ymd(2015, 9, 15)), ProjectId(3456), ServiceId(243), Minutes(185))?;
assert_eq!(
TimeEntry {
id: TimeEntryId(36159117),
minutes: Minutes(15),
date_at: NaiveDate::from_ymd(2015, 10, 16),
note: "Feedback einarbeiten".to_string(),
id: TimeEntryId(52324),
minutes: Minutes(185),
date_at: NaiveDate::from_ymd(2015, 9, 12),
note: "".to_string(),
locked: false,
billable: true,
hourly_rate: 0,
@ -319,15 +406,66 @@ fn test_quest_entries() -> Result<(), Box<dyn std::error::Error>> {
user_name: "Fridolin Frei".to_string(),
customer_id: CustomerId(3213),
customer_name: "König".to_string(),
service_id: ServiceId(12984),
service_name: "Entwurf".to_string(),
project_id: ProjectId(88309),
project_name: "API v2".to_string(),
created_at: Utc.ymd(2015, 10, 16).and_hms(10, 19, 00),
updated_at: Utc.ymd(2015, 10, 16).and_hms(10, 39, 00)
service_id: ServiceId(243),
service_name: "Dokumentation".to_string(),
project_id: ProjectId(3456),
project_name: "Some project".to_string(),
created_at: Utc.ymd(2015, 9, 13).and_hms(16, 54, 45),
updated_at: Utc.ymd(2015, 9, 13).and_hms(16, 54, 45),
},
entries[0]
entry
);
Ok(())
}
#[test]
fn test_delete_entry() -> Result<(), Box<dyn std::error::Error>> {
let pact = PactBuilder::new(CONSUMER, PROVIDER)
.interaction("delete time entry", |i| {
i.given("User with API token");
i.request
.delete()
.path("/time_entries/52324.json")
.header("X-MiteApiKey", term!("[0-9a-f]+", "12345678"));
i.response.ok();
})
.build();
let server = pact.start_mock_server();
let mut url = server.url().clone();
url.set_username("12345678").unwrap();
let client = StdClient::new_form_url(url);
client.delete_time_entry(TimeEntryId(52324))?;
Ok(())
}
#[test]
fn test_update_entry() -> Result<(), Box<dyn std::error::Error>> {
let pact = PactBuilder::new(CONSUMER, PROVIDER)
.interaction("update time entry", |i| {
i.given("User with API token");
i.request
.method("PATCH")
.path("/time_entries/52324.json")
.json_body(json!({
"time_entry": {
"minutes": 120
}
}))
.header("X-MiteApiKey", term!("[0-9a-f]+", "12345678"));
i.response.ok();
})
.build();
let server = pact.start_mock_server();
let mut url = server.url().clone();
url.set_username("12345678").unwrap();
let client = StdClient::new_form_url(url);
client.update_time_entry(TimeEntryId(52324), Minutes(120))?;
Ok(())
}

View File

@ -1,434 +0,0 @@
{
"consumer": {
"name": "acari-lib"
},
"interactions": [
{
"description": "get account",
"providerStates": [
{
"name": "User with API token"
}
],
"request": {
"headers": {
"X-MiteApiKey": "12345678"
},
"matchingRules": {
"header": {
"X-MiteApiKey": {
"combine": "AND",
"matchers": [
{
"match": "regex",
"regex": "[0-9a-f]+"
}
]
}
},
"path": {}
},
"method": "GET",
"path": "/account.json"
},
"response": {
"body": {
"account": {
"created_at": "2013-10-12T14:39:51+01:00",
"currency": "EUR",
"id": 1,
"name": "demo",
"title": "Demo GmbH",
"updated_at": "2015-05-02T13:21:09+01:00"
}
},
"headers": {
"Content-Type": "application/json; charset=utf-8"
},
"matchingRules": {
"body": {},
"header": {
"Content-Type": {
"combine": "AND",
"matchers": [
{
"match": "regex",
"regex": "^application/json; charset=(utf|UTF)-8$"
}
]
}
}
},
"status": 200
}
},
{
"description": "get customers",
"providerStates": [
{
"name": "User with API token"
}
],
"request": {
"headers": {
"X-MiteApiKey": "12345678"
},
"matchingRules": {
"header": {
"X-MiteApiKey": {
"combine": "AND",
"matchers": [
{
"match": "regex",
"regex": "[0-9a-f]+"
}
]
}
},
"path": {}
},
"method": "GET",
"path": "/customers.json"
},
"response": {
"body": [
{
"customer": {
"active_hourly_rate": "hourly_rates_per_service",
"archived": false,
"created_at": "2015-10-15T14:33:19+02:00",
"hourly_rate": null,
"hourly_rates_per_service": [
{
"hourly_rate": 4500,
"service_id": 742
},
{
"hourly_rate": 5500,
"service_id": 43212
}
],
"id": 83241,
"name": "Acme Inc.",
"note": "",
"updated_at": "2015-10-15T14:29:03+02:00"
}
}
],
"headers": {
"Content-Type": "application/json; charset=utf-8"
},
"matchingRules": {
"body": {},
"header": {
"Content-Type": {
"combine": "AND",
"matchers": [
{
"match": "regex",
"regex": "^application/json; charset=(utf|UTF)-8$"
}
]
}
}
},
"status": 200
}
},
{
"description": "get myself",
"providerStates": [
{
"name": "User with API token"
}
],
"request": {
"headers": {
"X-MiteApiKey": "12345678"
},
"matchingRules": {
"header": {
"X-MiteApiKey": {
"combine": "AND",
"matchers": [
{
"match": "regex",
"regex": "[0-9a-f]+"
}
]
}
},
"path": {}
},
"method": "GET",
"path": "/myself.json"
},
"response": {
"body": {
"user": {
"archived": false,
"created_at": "2013-06-23T23:00:58+02:00",
"email": "august.ausgedacht@demo.de",
"id": 3456,
"language": "de",
"name": "August Ausgedacht",
"note": "",
"role": "admin",
"updated_at": "2015-07-25T01:26:35+02:00"
}
},
"headers": {
"Content-Type": "application/json; charset=utf-8"
},
"matchingRules": {
"body": {},
"header": {
"Content-Type": {
"combine": "AND",
"matchers": [
{
"match": "regex",
"regex": "^application/json; charset=(utf|UTF)-8$"
}
]
}
}
},
"status": 200
}
},
{
"description": "get projects",
"providerStates": [
{
"name": "User with API token"
}
],
"request": {
"headers": {
"X-MiteApiKey": "12345678"
},
"matchingRules": {
"header": {
"X-MiteApiKey": {
"combine": "AND",
"matchers": [
{
"match": "regex",
"regex": "[0-9a-f]+"
}
]
}
},
"path": {}
},
"method": "GET",
"path": "/projects.json"
},
"response": {
"body": [
{
"project": {
"active_hourly_rate": "hourly_rate",
"archived": false,
"budget": 0,
"budget_type": "minutes",
"created_at": "2011-08-17T12:06:57+02:00",
"customer_id": 291,
"customer_name": "Yolk",
"hourly_rate": 6000,
"hourly_rates_per_service": [
{
"hourly_rate": 4500,
"service_id": 31272
},
{
"hourly_rate": 5500,
"service_id": 149228
}
],
"id": 643,
"name": "Open-Source",
"note": "valvat, memento et all.",
"updated_at": "2015-02-19T10:53:10+01:00"
}
}
],
"headers": {
"Content-Type": "application/json; charset=utf-8"
},
"matchingRules": {
"body": {},
"header": {
"Content-Type": {
"combine": "AND",
"matchers": [
{
"match": "regex",
"regex": "^application/json; charset=(utf|UTF)-8$"
}
]
}
}
},
"status": 200
}
},
{
"description": "get services",
"providerStates": [
{
"name": "User with API token"
}
],
"request": {
"headers": {
"X-MiteApiKey": "12345678"
},
"matchingRules": {
"header": {
"X-MiteApiKey": {
"combine": "AND",
"matchers": [
{
"match": "regex",
"regex": "[0-9a-f]+"
}
]
}
},
"path": {}
},
"method": "GET",
"path": "/services.json"
},
"response": {
"body": [
{
"service": {
"archived": false,
"billable": true,
"created_at": "2009-12-13T12:12:00+01:00",
"hourly_rate": 3300,
"id": 38672,
"name": "Website Konzeption",
"note": "",
"updated_at": "2015-12-13T07:20:04+01:00"
}
}
],
"headers": {
"Content-Type": "application/json; charset=utf-8"
},
"matchingRules": {
"body": {},
"header": {
"Content-Type": {
"combine": "AND",
"matchers": [
{
"match": "regex",
"regex": "^application/json; charset=(utf|UTF)-8$"
}
]
}
}
},
"status": 200
}
},
{
"description": "query time entries",
"providerStates": [
{
"name": "User with API token"
}
],
"request": {
"headers": {
"X-MiteApiKey": "12345678"
},
"matchingRules": {
"header": {
"X-MiteApiKey": {
"combine": "AND",
"matchers": [
{
"match": "regex",
"regex": "[0-9a-f]+"
}
]
}
},
"path": {},
"query": {}
},
"method": "GET",
"path": "/time_entries.json",
"query": {
"at": [
"2015-10-16"
],
"user": [
"current"
]
}
},
"response": {
"body": [
{
"time_entry": {
"billable": true,
"created_at": "2015-10-16T12:19:00+02:00",
"customer_id": 3213,
"customer_name": "König",
"date_at": "2015-10-16",
"hourly_rate": 0,
"id": 36159117,
"locked": false,
"minutes": 15,
"note": "Feedback einarbeiten",
"project_id": 88309,
"project_name": "API v2",
"revenue": null,
"service_id": 12984,
"service_name": "Entwurf",
"updated_at": "2015-10-16T12:39:00+02:00",
"user_id": 211,
"user_name": "Fridolin Frei"
}
}
],
"headers": {
"Content-Type": "application/json; charset=utf-8"
},
"matchingRules": {
"body": {},
"header": {
"Content-Type": {
"combine": "AND",
"matchers": [
{
"match": "regex",
"regex": "^application/json; charset=(utf|UTF)-8$"
}
]
}
}
},
"status": 200
}
}
],
"metadata": {
"pactRust": {
"version": "0.5.8"
},
"pactSpecification": {
"version": "3.0.0"
}
},
"provider": {
"name": "mite API"
}
}