Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(calendars): check availability correctly #125

Merged
merged 2 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 58 additions & 19 deletions spec/controllers/calendars_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,33 @@ describe Calendars do
status_code.should eq(200)
end

it "#availability should return list of available calendars" do
WebMock.stub(:post, "https://login.microsoftonline.com/bb89674a-238b-4b7d-91ec-6bebad83553a/oauth2/v2.0/token")
.to_return(body: File.read("./spec/fixtures/tokens/o365_token.json"))
WebMock.stub(:get, "https://graph.microsoft.com/v1.0/users/dev@acaprojects.com/calendars?")
.to_return(body: File.read("./spec/fixtures/calendars/o365/show.json"))
WebMock.stub(:post, "#{ENV["PLACE_URI"]}/auth/oauth/token")
.to_return(body: File.read("./spec/fixtures/tokens/placeos_token.json"))
WebMock.stub(:get, "#{ENV["PLACE_URI"]}/api/engine/v2/systems?limit=1000&offset=0&zone_id=zone-EzcsmWbvUG6")
.to_return(body: File.read("./spec/fixtures/placeos/systems.json"))
WebMock.stub(:post, "https://graph.microsoft.com/v1.0/users/dev@acaprojects.com/calendar/getSchedule")
.to_return(body: File.read("./spec/fixtures/events/o365/get_schedule.json"))
describe "#availability" do
it "should not return a calendar if it is busy at given time" do
CalendarsHelper.stub_cal_endpoints
time = Time.utc(2019, 3, 15, 10).to_unix
time2 = Time.utc(2019, 3, 15, 11).to_unix
route = "#{CALENDARS_BASE}?calendars=dev@acaprojects.com&period_start=#{time}&period_end=#{time2}&system_ids=sys-rJQQlR4Cn7,sys-rHQQlR4Cn7"
body = Context(Calendars, JSON::Any).response("GET", route, headers: Mock::Headers.office365_guest, &.availability)[1]
body.to_s.includes?("room1@example.com").should be_false
end

now = Time.local.to_unix
later = (Time.local + 1.hour).to_unix
route = "#{CALENDARS_BASE}?calendars=dev@acaprojects.com&period_start=#{now}&period_end=#{later}&zone_ids=zone-EzcsmWbvUG6"
body = Context(Calendars, JSON::Any).response("GET", route, headers: Mock::Headers.office365_guest, &.availability)[1].as_a
body.should eq(CalendarsHelper.calendar_list_output)
it "should not return a calendar if it is busy some of the given time" do
CalendarsHelper.stub_cal_endpoints
time = Time.utc(2020, 3, 15, 10).to_unix
time2 = Time.utc(2020, 3, 15, 12).to_unix
route = "#{CALENDARS_BASE}?calendars=dev@acaprojects.com&period_start=#{time}&period_end=#{time2}&system_ids=sys-rJQQlR4Cn7,sys-rHQQlR4Cn7"
body = Context(Calendars, JSON::Any).response("GET", route, headers: Mock::Headers.office365_guest, &.availability)[1]
body.to_s.includes?("room2@example.com").should be_false
end

it "should return list of available calendars" do
CalendarsHelper.stub_cal_endpoints
time = Time.utc(2021, 3, 15, 10).to_unix
time2 = Time.utc(2021, 3, 15, 11).to_unix
route = "#{CALENDARS_BASE}?calendars=dev@acaprojects.com&period_start=#{time}&period_end=#{time2}&system_ids=sys-rJQQlR4Cn7,sys-rHQQlR4Cn7"
body = Context(Calendars, JSON::Any).response("GET", route, headers: Mock::Headers.office365_guest, &.availability)[1]
body.should eq(CalendarsHelper.calendar_list_output)
end
end

it "#free_busy should return free busy data of calendars" do
Expand Down Expand Up @@ -56,13 +66,42 @@ module CalendarsHelper

def calendar_list_output
[{"id" => "dev@acaprojects.com"},
{"id" => "room2@example.com", "system" => {"created_at" => 1562041127, "updated_at" => 1562041137, "id" => "sys_id", "name" => "Room 2", "zones" => ["zone-rGhCRp_aUD"], "modules" => ["mod-rJRJOM27Kb", "mod-rJRLE4_PQ7", "mod-rJRLwe72Mo"], "email" => "room2@example.com", "capacity" => 10, "features" => [] of String, "bookable" => true, "installed_ui_devices" => 0, "version" => 4}},
{"id" => "room3@example.com", "system" => {"created_at" => 1562041145, "updated_at" => 1562041155, "id" => "sys-rJQVPIR9Uf", "name" => "Room 3", "zones" => ["zone-rGhCRp_aUD"], "modules" => ["mod-rJRNrLDPNz", "mod-rJRQ~JwE7U", "mod-rJRV1qokbH"], "email" => "room3@example.com", "capacity" => 4, "features" => [] of String, "bookable" => true, "installed_ui_devices" => 0, "version" => 4}},
{"id" => "room4@example.com", "system" => {"created_at" => 1562041145, "updated_at" => 1562041155, "id" => "sys-AAJQVPIR9Uf", "name" => "Room 4", "zones" => ["zone-rGhCRp_aUD"], "modules" => ["mod-rJRNrLDPNz", "mod-rJRQ~JwE7U", "mod-rJRV1qokbH"], "email" => "room4@example.com", "capacity" => 20, "features" => [] of String, "bookable" => true, "installed_ui_devices" => 0, "version" => 4}}]
{"id" => "room1@example.com", "system" => {"created_at" => 1562041110, "updated_at" => 1562041120, "id" => "sys-rJQQlR4Cn7", "name" => "Room 1", "zones" => ["zone-rGhCRp_aUD"], "modules" => ["mod-rJRCVYKVuB", "mod-rJRGK21pya", "mod-rJRHYsZExU"], "email" => "room1@example.com", "capacity" => 10, "features" => [] of String, "bookable" => true, "installed_ui_devices" => 0, "version" => 5}},
{"id" => "room2@example.com", "system" => {"created_at" => 1562041110, "updated_at" => 1562041120, "id" => "sys-rHQQlR4Cn7", "name" => "Room 2", "zones" => ["zone-rGfCRp_aUD"], "modules" => ["mod-rJRCVFDVuB", "mod-rJRWE21pya", "mod-rJBGYsZExU"], "email" => "room2@example.com", "capacity" => 10, "features" => [] of String, "bookable" => true, "installed_ui_devices" => 0, "version" => 5}}]
end

def free_busy_output
[{"id" => "jon@example.com", "availability" => [{"status" => "busy", "starts_at" => 1552676400, "ends_at" => 1552683600, "timezone" => "America/Los_Angeles"}]},
{"id" => "room1@example.com", "system" => {"created_at" => 1562041110, "updated_at" => 1562041120, "id" => "sys-rJQQlR4Cn7", "name" => "Room 1", "zones" => ["zone-rGhCRp_aUD"], "modules" => ["mod-rJRCVYKVuB", "mod-rJRGK21pya", "mod-rJRHYsZExU"], "email" => "room1@example.com", "capacity" => 10, "features" => [] of String, "bookable" => true, "installed_ui_devices" => 0, "version" => 5}, "availability" => [{"status" => "busy", "starts_at" => 1552663800, "ends_at" => 1552667400, "timezone" => "America/Los_Angeles"}, {"status" => "busy", "starts_at" => 1552676400, "ends_at" => 1552683600, "timezone" => "America/Los_Angeles"}, {"status" => "busy", "starts_at" => 1552676400, "ends_at" => 1552680000, "timezone" => "America/Los_Angeles"}, {"status" => "busy", "starts_at" => 1552680000, "ends_at" => 1552683600, "timezone" => "America/Los_Angeles"}, {"status" => "busy", "starts_at" => 1552690800, "ends_at" => 1552694400, "timezone" => "America/Los_Angeles"}]}]
end

def stub_cal_endpoints
WebMock.stub(:post, "https://login.microsoftonline.com/bb89674a-238b-4b7d-91ec-6bebad83553a/oauth2/v2.0/token")
.to_return(body: File.read("./spec/fixtures/tokens/o365_token.json"))
WebMock.stub(:get, "https://graph.microsoft.com/v1.0/users/dev@acaprojects.com/calendars?")
.to_return(body: File.read("./spec/fixtures/calendars/o365/show.json"))
WebMock.stub(:post, "#{ENV["PLACE_URI"]}/auth/oauth/token")
.to_return(body: File.read("./spec/fixtures/tokens/placeos_token.json"))
WebMock.stub(:get, "#{ENV["PLACE_URI"]}/api/engine/v2/systems?limit=1000&offset=0&zone_id=zone-EzcsmWbvUG6")
.to_return(body: File.read("./spec/fixtures/placeos/systems.json"))
WebMock.stub(:post, "https://graph.microsoft.com/v1.0/users/dev@acaprojects.com/calendar/getSchedule")
.to_return(body: File.read("./spec/fixtures/events/o365/get_schedule_avail.json"))

WebMock.stub(:get, ENV["PLACE_URI"].to_s + "/api/engine/v2/systems/sys-rJQQlR4Cn7")
.to_return(body: File.read("./spec/fixtures/placeos/systemJ.json"))

WebMock.stub(:get, ENV["PLACE_URI"].to_s + "/api/engine/v2/systems/sys-rHQQlR4Cn7")
.to_return(body: File.read("./spec/fixtures/placeos/systemH.json"))

WebMock.stub(:post, "https://graph.microsoft.com/v1.0/users/dev@acaprojects.onmicrosoft.com/calendar/events")
.to_return(body: File.read("./spec/fixtures/events/o365/create.json"))
WebMock.stub(:post, "#{ENV["PLACE_URI"]}/api/engine/v2/signal?channel=staff/event/changed")
.to_return(body: "")
WebMock.stub(:post, "#{ENV["PLACE_URI"]}/api/engine/v2/signal?channel=staff/guest/attending")
.to_return(body: "")
WebMock.stub(:get, "https://graph.microsoft.com/v1.0/users/dev@acaprojects.com/calendar/events/AAMkADE3YmQxMGQ2LTRmZDgtNDljYy1hNDg1LWM0NzFmMGI0ZTQ3YgBGAAAAAADFYQb3DJ_xSJHh14kbXHWhBwB08dwEuoS_QYSBDzuv558sAAAAAAENAAB08dwEuoS_QYSBDzuv558sAACGVOwUAAA=")
.to_return(body: File.read("./spec/fixtures/events/o365/create.json"))
WebMock.stub(:patch, "https://graph.microsoft.com/v1.0/users/dev@acaprojects.onmicrosoft.com/calendar/events/")
.to_return(body: File.read("./spec/fixtures/events/o365/update.json"))
end
end
73 changes: 73 additions & 0 deletions spec/fixtures/events/o365/get_schedule_avail.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#Collection(microsoft.graph.scheduleInformation)",
"value": [
{
"scheduleId": "room1@example.com",
"availabilityView": "000220000",
"scheduleItems": [
{
"isPrivate": false,
"status": "busy",
"subject": "Let's go for lunch",
"location": "Harry's Bar",
"start": {
"dateTime": "2019-03-15T10:00:00.0000000",
"timeZone": "Coordinated Universal Time"
},
"end": {
"dateTime": "2019-03-15T10:30:00.0000000",
"timeZone": "Coordinated Universal Time"
}
}
],
"workingHours": {
"daysOfWeek": [
"monday",
"tuesday",
"wednesday",
"thursday",
"friday"
],
"startTime": "08:00:00.0000000",
"endTime": "17:00:00.0000000",
"timeZone": {
"name": "Coordinated Universal Time"
}
}
},
{
"scheduleId": "room2@example.com",
"availabilityView": "000220000",
"scheduleItems": [
{
"isPrivate": false,
"status": "busy",
"subject": "Meeting",
"location": "Orange Room",
"start": {
"dateTime": "2020-03-15T11:00:00.0000000",
"timeZone": "Coordinated Universal Time"
},
"end": {
"dateTime": "2020-03-15T12:30:00.0000000",
"timeZone": "Coordinated Universal Time"
}
}
],
"workingHours": {
"daysOfWeek": [
"monday",
"tuesday",
"wednesday",
"thursday",
"friday"
],
"startTime": "08:00:00.0000000",
"endTime": "17:00:00.0000000",
"timeZone": {
"name": "Coordinated Universal Time"
}
}
}
]
}
22 changes: 22 additions & 0 deletions spec/fixtures/placeos/systemH.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "Room 2",
"description": null,
"email": "room2@example.com",
"capacity": 10,
"features": [],
"bookable": true,
"installed_ui_devices": 0,
"zones": [
"zone-rGfCRp_aUD"
],
"modules": [
"mod-rJRCVFDVuB",
"mod-rJRWE21pya",
"mod-rJBGYsZExU"
],
"created_at": 1562041110,
"updated_at": 1562041120,
"support_url": null,
"version": 5,
"id": "sys-rHQQlR4Cn7"
}
22 changes: 22 additions & 0 deletions spec/fixtures/placeos/systemJ.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "Room 1",
"description": null,
"email": "room1@example.com",
"capacity": 10,
"features": [],
"bookable": true,
"installed_ui_devices": 0,
"zones": [
"zone-rGhCRp_aUD"
],
"modules": [
"mod-rJRCVYKVuB",
"mod-rJRGK21pya",
"mod-rJRHYsZExU"
],
"created_at": 1562041110,
"updated_at": 1562041120,
"support_url": null,
"version": 5,
"id": "sys-rJQQlR4Cn7"
}
180 changes: 90 additions & 90 deletions spec/fixtures/placeos/systems.json
Original file line number Diff line number Diff line change
@@ -1,92 +1,92 @@
[
{
"name": "Room 1",
"description": null,
"email": "room1@example.com",
"capacity": 10,
"features": [],
"bookable": true,
"installed_ui_devices": 0,
"zones": [
"zone-rGhCRp_aUD"
],
"modules": [
"mod-rJRCVYKVuB",
"mod-rJRGK21pya",
"mod-rJRHYsZExU"
],
"created_at": 1562041110,
"updated_at": 1562041120,
"support_url": null,
"version": 5,
"id": "sys-rJQQlR4Cn7"
},
{
"name": "Room 2",
"description": null,
"email": "room2@example.com",
"capacity": 10,
"features": [],
"bookable": true,
"installed_ui_devices": 0,
"zones": [
"zone-rGhCRp_aUD"
],
"modules": [
"mod-rJRJOM27Kb",
"mod-rJRLE4_PQ7",
"mod-rJRLwe72Mo"
],
"created_at": 1562041127,
"updated_at": 1562041137,
"support_url": null,
"version": 4,
"id": "sys_id"
},
{
"edge_id": "edge-QC03B3OM",
"name": "Room 3",
"description": null,
"email": "room3@example.com",
"capacity": 4,
"features": [],
"bookable": true,
"installed_ui_devices": 0,
"zones": [
"zone-rGhCRp_aUD"
],
"modules": [
"mod-rJRNrLDPNz",
"mod-rJRQ~JwE7U",
"mod-rJRV1qokbH"
],
"created_at": 1562041145,
"updated_at": 1562041155,
"support_url": null,
"version": 4,
"id": "sys-rJQVPIR9Uf"
},
{
"edge_id": "edge-QC03B3OM",
"name": "Room 4",
"description": null,
"email": "room4@example.com",
"capacity": 20,
"features": [],
"bookable": true,
"installed_ui_devices": 0,
"zones": [
"zone-rGhCRp_aUD"
],
"modules": [
"mod-rJRNrLDPNz",
"mod-rJRQ~JwE7U",
"mod-rJRV1qokbH"
],
"created_at": 1562041145,
"updated_at": 1562041155,
"support_url": null,
"version": 4,
"id": "sys-AAJQVPIR9Uf"
}
{
"name": "Room 1",
"description": null,
"email": "room1@example.com",
"capacity": 10,
"features": [],
"bookable": true,
"installed_ui_devices": 0,
"zones": [
"zone-rGhCRp_aUD"
],
"modules": [
"mod-rJRCVYKVuB",
"mod-rJRGK21pya",
"mod-rJRHYsZExU"
],
"created_at": 1562041110,
"updated_at": 1562041120,
"support_url": null,
"version": 5,
"id": "sys-rJQQlR4Cn7"
},
{
"name": "Room 2",
"description": null,
"email": "room2@example.com",
"capacity": 10,
"features": [],
"bookable": true,
"installed_ui_devices": 0,
"zones": [
"zone-rGhCRp_aUD"
],
"modules": [
"mod-rJRJOM27Kb",
"mod-rJRLE4_PQ7",
"mod-rJRLwe72Mo"
],
"created_at": 1562041127,
"updated_at": 1562041137,
"support_url": null,
"version": 4,
"id": "sys_id"
},
{
"edge_id": "edge-QC03B3OM",
"name": "Room 3",
"description": null,
"email": "room3@example.com",
"capacity": 4,
"features": [],
"bookable": true,
"installed_ui_devices": 0,
"zones": [
"zone-rGhCRp_aUD"
],
"modules": [
"mod-rJRNrLDPNz",
"mod-rJRQ~JwE7U",
"mod-rJRV1qokbH"
],
"created_at": 1562041145,
"updated_at": 1562041155,
"support_url": null,
"version": 4,
"id": "sys-rJQVPIR9Uf"
},
{
"edge_id": "edge-QC03B3OM",
"name": "Room 4",
"description": null,
"email": "room4@example.com",
"capacity": 20,
"features": [],
"bookable": true,
"installed_ui_devices": 0,
"zones": [
"zone-rGhCRp_aUD"
],
"modules": [
"mod-rJRNrLDPNz",
"mod-rJRQ~JwE7U",
"mod-rJRV1qokbH"
],
"created_at": 1562041145,
"updated_at": 1562041155,
"support_url": null,
"version": 4,
"id": "sys-AAJQVPIR9Uf"
}
]
Loading