Skip to content

Commit

Permalink
More tests (#822)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgershman authored Jul 23, 2023
1 parent 93d0bee commit 3ffbcb3
Show file tree
Hide file tree
Showing 11 changed files with 260 additions and 82 deletions.
2 changes: 0 additions & 2 deletions app/Constants/DataType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

class DataType
{
const YAP_CONFIG = "_YAP_CONFIG_";
const YAP_CONFIG_V2 = "_YAP_CONFIG_V2_";
const YAP_DATA = "_YAP_DATA_";
const YAP_CALL_HANDLING_V2 = "_YAP_CALL_HANDLING_V2_";
const YAP_VOLUNTEERS_V2 = "_YAP_VOLUNTEERS_V2_";
const YAP_GROUPS_V2 = "_YAP_GROUPS_V2_";
Expand Down
8 changes: 3 additions & 5 deletions app/Http/Controllers/CallFlowController.php
Original file line number Diff line number Diff line change
Expand Up @@ -787,15 +787,14 @@ public function helplineOutdialResponse(Request $request)
return response($twiml)->header("Content-Type", "text/xml");
}

public function helplineSms(Request $request)
public function helplineSms(Request $request, $latitude, $longitude)
{
try {
if ($request->has("OriginalCallerId")) {
$original_caller_id = $request->query("OriginalCallerId");
}

$service_body = $this->meetingResults
->getServiceBodyCoverage($request->query("Latitude"), $request->query("Longitude"));
$service_body = $this->meetingResults->getServiceBodyCoverage($latitude, $longitude);
$serviceBodyCallHandling = $this->config->getCallHandling($service_body->id);
$tracker = $request->has("tracker") ? 0 : intval($request->query("tracker"));

Expand Down Expand Up @@ -926,8 +925,7 @@ public function smsGateway(Request $request)
$sms_helpline_keyword = $this->settings->get("sms_helpline_keyword");
if (str_contains(strtoupper($address), strtoupper($sms_helpline_keyword))) {
if (strlen(trim(str_replace(strtoupper($sms_helpline_keyword), "", strtoupper($address)))) > 0) {
$twiml->redirect("helpline-sms.php?OriginalCallerId=" . $request->get("From") . "&To=" . $request->get("To") . "&Latitude=" . $coordinates->latitude . "&Longitude=" . $coordinates->longitude)
->setMethod("GET");
return self::helplineSms($request, $coordinates->latitude, $coordinates->longitude);
} else {
$message = $this->settings->word('please_send_a_message_formatting_as') . " " . $sms_helpline_keyword . ", " . $this->settings->word('followed_by_your_location') . " " . $this->settings->word('for') . " " . $this->settings->word('someone_to_talk_to');
$this->twilio->client()->messages->create($request->get("From"), array("from" => $request->get("To"), "body" => $message));
Expand Down
9 changes: 7 additions & 2 deletions app/Services/MeetingResultsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,12 @@ public function meetingSearch($meeting_results, $latitude, $longitude, $day)

public function getServiceBodyCoverage($latitude, $longitude)
{
$search_results = $this->rootServer->helplineSearch($latitude, $longitude);
$search_results = $this->rootServer->helplineSearch(
$latitude,
$longitude,
$this->settings->get('helpline_search_radius'),
$this->settings->get('call_routing_filter')
);
$service_bodies = $this->rootServer->getServiceBodiesForRouting($latitude, $longitude);
$already_checked = [];

Expand All @@ -136,7 +141,7 @@ public function getServiceBodyCoverage($latitude, $longitude)
for ($i = 0; $i < count($service_bodies); $i++) {
if ($service_bodies[$i]->id == $service_body_id) {
if ((isset($service_bodies[$i]->helpline) && strlen($service_bodies[$i]->helpline) > 0)
|| $this->config->getServiceBodyCallHandling($service_bodies[$i]->id)->volunteer_routing_enabled) {
|| $this->config->getServiceBodyCallHandlingData($service_bodies[$i]->id)->volunteer_routing_enabled) {
return $service_bodies[$i];
} else {
$already_checked[] = $service_bodies[$i]->id;
Expand Down
7 changes: 3 additions & 4 deletions app/Services/RootServerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace App\Services;

use App\Constants\AuthMechanism;
use App\Constants\CacheType;
use App\Utility\Sort;
use CurlException;
use Exception;
Expand Down Expand Up @@ -229,15 +228,15 @@ private function getIdsFormats($types): array
return $finalFormats;
}

public function helplineSearch($latitude, $longitude)
public function helplineSearch($latitude, $longitude, $helplineSearchRadius, $callRoutingFilter)
{
$search_url = sprintf(
"%s/client_interface/json/?switcher=GetSearchResults&data_field_key=longitude,latitude,service_body_bigint&sort_results_by_distance=1&lat_val=%s&long_val=%s&geo_width=%s%s",
$this->getHelplineRoutingBMLTServer($latitude, $longitude),
$latitude,
$longitude,
$this->settings->get('helpline_search_radius'),
$this->settings->get('call_routing_filter')
$helplineSearchRadius,
$callRoutingFilter,
);

return json_decode($this->http->get($search_url, 60));
Expand Down
2 changes: 1 addition & 1 deletion app/Services/UpgradeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function getStatus()
if (count($alerts) > 0) {
$misconfiguredPhoneNumbers = [];
foreach ($alerts as $alert) {
array_push($misconfiguredPhoneNumbers, $alert['payload']);
$misconfiguredPhoneNumbers[] = $alert['payload'];
}

$warnings = sprintf("%s is/are phone numbers that are missing Twilio Call Status Changes Callback status.php webhook. This will not allow call reporting to work correctly. For more information review the documentation page https://github.com/bmlt-enabled/yap/wiki/Call-Detail-Records.", implode(",", $misconfiguredPhoneNumbers));
Expand Down
2 changes: 0 additions & 2 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@
->where('ext', $ext);
Route::get("/province-voice-input{ext}", 'App\Http\Controllers\CallFlowController@provinceVoiceInput')
->where('ext', $ext);
Route::get("/helpline-sms{ext}", 'App\Http\Controllers\CallFlowController@helplineSms')
->where('ext', $ext);
Route::get("/helpline-answer-response{ext}", 'App\Http\Controllers\CallFlowController@helplineAnswerResponse')
->where('ext', $ext);
Route::get("/helpline-outdial-response{ext}", 'App\Http\Controllers\CallFlowController@helplineOutdialResponse')
Expand Down
148 changes: 138 additions & 10 deletions tests/Feature/HelplineSMSTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php

use App\Constants\VolunteerGender;
use App\Constants\VolunteerResponderOption;
use App\Repositories\ConfigRepository;
use App\Repositories\ReportsRepository;
use App\Services\RootServerService;
use App\Services\SettingsService;
use App\Services\TwilioService;
use App\Constants\DataType;
use Tests\FakeTwilioHttpClient;
use App\Services\SettingsService;
use Tests\MiddlewareTests;
use Tests\RootServerMocks;

beforeAll(function () {
Expand All @@ -17,37 +19,163 @@
$_REQUEST = null;
$_SESSION = null;

$this->settings = new SettingsService();
app()->instance(SettingsService::class, $this->settings);

$this->midddleware = new MiddlewareTests();
$this->utility = setupTwilioService();
//$this->rootServerMocks = new RootServerMocks();
$this->rootServerMocks = new RootServerMocks();
$this->id = "200";
$this->serviceBodyId = "44";
$this->parentServiceBodyId = "43";
$this->data = "{\"data\":{}}";
$this->configRepository = $this->midddleware->getAllDbData(
$this->id,
$this->serviceBodyId,
$this->parentServiceBodyId,
$this->data
);
});

test('initial sms gateway default', function () {
// app()->instance(RootServerService::class, $this->rootServerMocks->getService());
test('initial sms gateway talk option using a different keyword', function () {
$reportsRepository = Mockery::mock(ReportsRepository::class);
$reportsRepository->shouldReceive("insertCallRecord")->withAnyArgs();
app()->instance(ReportsRepository::class, $reportsRepository);
$_SESSION['override_sms_helpline_keyword'] = 'dude';
$_REQUEST['stub_google_maps_endpoint'] = true;
$this->callerIdInfo['Body'] = 'dude 27592';
$response = $this->call(
'GET',
'/sms-gateway.php',
$this->callerIdInfo
);
$response
->assertStatus(200)
->assertHeader("Content-Type", "text/xml; charset=UTF-8");
});

test('initial sms helpline gateway default when there is no volunteer', function () {
$reportsRepository = Mockery::mock(ReportsRepository::class);
$reportsRepository->shouldReceive("insertCallRecord")->withAnyArgs();
$reportsRepository->shouldReceive("insertCallEventRecord")->withAnyArgs();
app()->instance(ReportsRepository::class, $reportsRepository);
$results[] = (object)["service_body_bigint"=>"44"];
$this->rootServerMocks->getService()
->shouldReceive("helplineSearch")
->withAnyArgs()->andReturn($results);
$this->rootServerMocks->getService()
->shouldReceive("isBMLTServerOwned")
->withNoArgs()->andReturn(true);
app()->instance(RootServerService::class, $this->rootServerMocks->getService());
// mocking TwilioRestClient->messages->create()
$messageListMock = mock('\Twilio\Rest\Api\V2010\Account\MessageList');
$messageListMock->shouldReceive('create')
->with(is_string(""), is_array([]))->times(1);
->withArgs(['+19735551212', [
"body" => 'could not find a volunteer for the location, please retry your request.',
"from" => '+12125551212']])->times(1);
$this->utility->client->messages = $messageListMock;

$repository = Mockery::mock(ConfigRepository::class);
$repository->shouldReceive("getDbData")->with(
'99',
'44',
DataType::YAP_CALL_HANDLING_V2
)->andReturn([(object)[
"service_body_id" => "99",
"service_body_id" => "44",
"id" => "200",
"parent_id" => "43",
"data" => "[{\"volunteer_routing\":\"volunteers_and_sms\",\"volunteers_redirect_id\":\"\",\"forced_caller_id\":\"\",\"call_timeout\":\"\",\"gender_routing\":\"0\",\"call_strategy\":\"1\",\"volunteer_sms_notification\":\"send_sms\",\"sms_strategy\":\"2\",\"primary_contact\":\"\",\"primary_contact_email\":\"\",\"moh\":\"\",\"override_en_US_greeting\":\"\",\"override_en_US_voicemail_greeting\":\"\"}]"
]])->once();
app()->instance(ConfigRepository::class, $repository);

$response = $this->call('GET', '/helpline-sms.php', [
$response = $this->call('GET', '/sms-gateway.php', [
"OriginalCallerId" => '+19735551212',
"To" => '+12125551212',
"Body" => "talk blah"
]);
$response
->assertStatus(200)
->assertHeader("Content-Type", "text/xml; charset=UTF-8")
->assertSeeInOrder([
], false);
});

test('initial sms helpline gateway with a volunteer', function () {
$reportsRepository = Mockery::mock(ReportsRepository::class);
$reportsRepository->shouldReceive("insertCallRecord")->withAnyArgs();
app()->instance(ReportsRepository::class, $reportsRepository);
$results[] = (object)["service_body_bigint"=>"44"];
$this->rootServerMocks->getService()
->shouldReceive("helplineSearch")
->withAnyArgs()->andReturn($results);
$this->rootServerMocks->getService()
->shouldReceive("isBMLTServerOwned")
->withNoArgs()->andReturn(true);
app()->instance(RootServerService::class, $this->rootServerMocks->getService());
// mocking TwilioRestClient->messages->create()
$messageListMock = mock('\Twilio\Rest\Api\V2010\Account\MessageList');
$this->utility->client->messages = $messageListMock;

$this->configRepository->shouldReceive("getDbData")->with(
$this->serviceBodyId,
DataType::YAP_CALL_HANDLING_V2
)->andReturn([(object)[
"service_body_id" => $this->serviceBodyId,
"id" => "200",
"parent_id" => $this->parentServiceBodyId,
"data" => "[{\"volunteer_routing\":\"volunteers_and_sms\",\"volunteers_redirect_id\":\"\",\"forced_caller_id\":\"\",\"call_timeout\":\"\",\"gender_routing\":\"0\",\"call_strategy\":\"1\",\"volunteer_sms_notification\":\"send_sms\",\"sms_strategy\":\"2\",\"primary_contact\":\"+15551112222\",\"primary_contact_email\":\"\",\"moh\":\"\",\"override_en_US_greeting\":\"\",\"override_en_US_voicemail_greeting\":\"\"}]"
]])->once();

$volunteer_name = "Corey";
$volunteer_gender = VolunteerGender::UNSPECIFIED;
$volunteer_responder = VolunteerResponderOption::UNSPECIFIED;
$volunteer_languages = ["en-US"];
$shiftDay = 2;
$shiftTz = "America/New_York";
$shiftStart = "12:00 AM";
$shiftEnd = "11:59 PM";
$volunteer = [[
"volunteer_name"=>$volunteer_name,
"volunteer_phone_number"=>"(555) 111-2222",
"volunteer_gender"=>$volunteer_gender,
"volunteer_responder"=>$volunteer_responder,
"volunteer_notes"=>"",
"volunteer_enabled"=>true,
"volunteer_shift_schedule"=>base64_encode(json_encode([[
"day"=>$shiftDay,
"tz"=>$shiftTz,
"start_time"=>$shiftStart,
"end_time"=>$shiftEnd,
]]))
]];
$this->configRepository->shouldReceive("getDbData")->with(44, DataType::YAP_VOLUNTEERS_V2)
->andReturn([(object)[
"service_body_id" => $this->serviceBodyId,
"id" => "200",
"parent_id" => $this->parentServiceBodyId,
"data" => json_encode($volunteer)
]])->once();

app()->instance(ConfigRepository::class, $this->configRepository);

$messageListMock = mock('\Twilio\Rest\Api\V2010\Account\MessageList');
$messageListMock->shouldReceive('create')
->withArgs(['+19735551212', [
"body" => 'Thank you and your request has been received. A volunteer should be responding to you shortly.',
"from" => '+12125551212']])->once();
$messageListMock->shouldReceive('create')
->withArgs(['+15551112222', [
"body" => 'Helpline: someone is requesting SMS help from +19735551212 please text or call them back.',
"from" => '+12125551212']])->once();
$this->utility->client->messages = $messageListMock;

$response = $this->call('GET', '/sms-gateway.php', [
"OriginalCallerId" => '+19735551212',
"To" => '+12125551212',
"Body"=>"talk Benson, NC"
]);
$response
->assertStatus(200)
->assertHeader("Content-Type", "text/xml; charset=UTF-8")
->assertSeeInOrder([
], false);
});
82 changes: 62 additions & 20 deletions tests/Feature/InitialIndexTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
<?php

use App\Constants\AlertId;
use App\Constants\DataType;
use App\Repositories\ConfigRepository;
use App\Repositories\ReportsRepository;
use App\Services\RootServerService;
use App\Services\SettingsService;
use App\Services\TwilioService;
use Tests\FakeTwilioHttpClient;
use Tests\MiddlewareTests;
use Tests\RootServerMocks;

beforeAll(function () {
putenv("ENVIRONMENT=test");
Expand All @@ -16,7 +21,8 @@
$_SESSION = null;

$this->fakeCallSid = "abcdefghij";
$this->middleware = new \Tests\MiddlewareTests();
$this->middleware = new MiddlewareTests();
$this->rootServerMocks = new RootServerMocks();
$this->reportsRepository = $this->middleware->insertSession($this->fakeCallSid);

$fakeHttpClient = new FakeTwilioHttpClient();
Expand Down Expand Up @@ -310,22 +316,58 @@
], false);
});

// TODO: load up some kind of service body configuration
//test('initial callin with service body override', function () {
// $response = $this->call("GET", '/', [
// "override_service_body_id"=>2
// ]);
//
// $response
// ->assertStatus(200)
// ->assertHeader("Content-Type", "text/xml; charset=UTF-8")
// ->assertSeeInOrder([
/* '<?xml version="1.0" encoding="UTF-8"?>',*/
// '<Response>',
// '<Gather language="en-US" input="dtmf" numDigits="1" timeout="10" speechTimeout="auto" action="input-method.php" method="GET">',
// '<Pause length="2"/>',
// '<Play>https://crossroadsarea.org/wp-content/uploads/9/2018/08/crossroads_v4_greeting.mp3</Play>',
// '</Gather>',
// '</Response>',
// ], false);
//});
test('initial callin with service body override', function () {
app()->instance(RootServerService::class, $this->rootServerMocks->getService());
$repository = Mockery::mock(ConfigRepository::class);
$repository->shouldReceive("getDbData")->with(
'44',
DataType::YAP_CALL_HANDLING_V2
)->andReturn([(object)[
"service_body_id" => "44",
"id" => "200",
"parent_id" => "43",
"data" => "[{\"volunteer_routing\":\"volunteers_and_sms\",\"volunteers_redirect_id\":\"\",\"forced_caller_id\":\"\",\"call_timeout\":\"\",\"gender_routing\":\"0\",\"call_strategy\":\"1\",\"volunteer_sms_notification\":\"send_sms\",\"sms_strategy\":\"2\",\"primary_contact\":\"\",\"primary_contact_email\":\"\",\"moh\":\"\",\"override_en_US_greeting\":\"https://fake.mp3\",\"override_en_US_voicemail_greeting\":\"\"}]"
]])->once();
$repository->shouldReceive("getAllDbData")->with(
DataType::YAP_CONFIG_V2
)->andReturn([(object)[
"service_body_id" => "44",
"id" => "200",
"parent_id" => "43",
"data" => "[]"
]])->times(2);
app()->instance(ConfigRepository::class, $repository);

$response = $this->call("GET", '/', [
"override_service_body_id"=>44
]);

$response
->assertStatus(200)
->assertHeader("Content-Type", "text/xml; charset=UTF-8")
->assertSeeInOrder([
'<?xml version="1.0" encoding="UTF-8"?>',
'<Response>',
'<Gather language="en-US" input="dtmf" numDigits="1" timeout="10" speechTimeout="auto" action="input-method.php" method="GET">',
'<Pause length="2"/>',
'<Play>https://fake.mp3</Play>',
'</Gather>',
'</Response>',
], false);
});

test('initial call-in extension dial', function () {
$_SESSION['override_extension_dial'] = true;
$response = $this->get('/');
$response
->assertStatus(200)
->assertHeader("Content-Type", "text/xml; charset=UTF-8")
->assertSeeInOrder([
'<?xml version="1.0" encoding="UTF-8"?>',
'<Response>',
'<Gather language="en-US" input="dtmf" finishOnKey="#" timeout="10" action="service-body-ext-response.php" method="GET">',
'<Say>Enter the service body ID, followed by the pound sign.</Say>',
'</Gather>',
'</Response>'
], false);
});
Loading

0 comments on commit 3ffbcb3

Please sign in to comment.